5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
24 use MediaWiki\MediaWikiServices
;
27 * @todo FIXME: This class is horribly factored. It would probably be better to
28 * have a useful base class to which you pass some standard information, then
29 * let the fancy self-highlighters extend that.
37 protected $mRevision = null;
42 protected $mImage = null;
57 protected $searchEngine;
60 * Return a new SearchResult and initializes it with a title.
63 * @return SearchResult
65 public static function newFromTitle( $title ) {
66 $result = new static();
67 $result->initFromTitle( $title );
72 * Initialize from a Title and if possible initializes a corresponding
77 protected function initFromTitle( $title ) {
78 $this->mTitle
= $title;
79 if ( !is_null( $this->mTitle
) ) {
81 Hooks
::run( 'SearchResultInitFromTitle', [ $title, &$id ] );
82 $this->mRevision
= Revision
::newFromTitle(
83 $this->mTitle
, $id, Revision
::READ_NORMAL
);
84 if ( $this->mTitle
->getNamespace() === NS_FILE
) {
85 $this->mImage
= wfFindFile( $this->mTitle
);
88 $this->searchEngine
= MediaWikiServices
::getInstance()->newSearchEngine();
92 * Check if this is result points to an invalid title
96 function isBrokenTitle() {
97 return is_null( $this->mTitle
);
101 * Check if target page is missing, happens when index is out of date
105 function isMissingRevision() {
106 return !$this->mRevision
&& !$this->mImage
;
112 function getTitle() {
113 return $this->mTitle
;
117 * Get the file for this page, if one exists
121 return $this->mImage
;
125 * Lazy initialization of article text from DB
127 protected function initText() {
128 if ( !isset( $this->mText
) ) {
129 if ( $this->mRevision
!= null ) {
130 $this->mText
= $this->searchEngine
->getTextFromContent(
131 $this->mTitle
, $this->mRevision
->getContent() );
132 } else { // TODO: can we fetch raw wikitext for commons images?
139 * @param array $terms Terms to highlight
140 * @return string Highlighted text snippet, null (and not '') if not supported
142 function getTextSnippet( $terms ) {
143 global $wgAdvancedSearchHighlighting;
146 // TODO: make highliter take a content object. Make ContentHandler a factory for SearchHighliter.
147 list( $contextlines, $contextchars ) = $this->searchEngine
->userHighlightPrefs();
149 $h = new SearchHighlighter();
150 if ( count( $terms ) > 0 ) {
151 if ( $wgAdvancedSearchHighlighting ) {
152 return $h->highlightText( $this->mText
, $terms, $contextlines, $contextchars );
154 return $h->highlightSimple( $this->mText
, $terms, $contextlines, $contextchars );
157 return $h->highlightNone( $this->mText
, $contextlines, $contextchars );
162 * @return string Highlighted title, '' if not supported
164 function getTitleSnippet() {
169 * @return string Highlighted redirect name (redirect to this page), '' if none or not supported
171 function getRedirectSnippet() {
176 * @return Title|null Title object for the redirect to this page, null if none or not supported
178 function getRedirectTitle() {
183 * @return string Highlighted relevant section name, null if none or not supported
185 function getSectionSnippet() {
190 * @return Title|null Title object (pagename+fragment) for the section,
191 * null if none or not supported
193 function getSectionTitle() {
198 * @return string Highlighted relevant category name or '' if none or not supported
200 public function getCategorySnippet() {
205 * @return string Timestamp
207 function getTimestamp() {
208 if ( $this->mRevision
) {
209 return $this->mRevision
->getTimestamp();
210 } elseif ( $this->mImage
) {
211 return $this->mImage
->getTimestamp();
217 * @return int Number of words
219 function getWordCount() {
221 return str_word_count( $this->mText
);
225 * @return int Size in bytes
227 function getByteSize() {
229 return strlen( $this->mText
);
233 * @return string Interwiki prefix of the title (return iw even if title is broken)
235 function getInterwikiPrefix() {
240 * @return string Interwiki namespace of the title (since we likely can't resolve it locally)
242 function getInterwikiNamespaceText() {
247 * Did this match file contents (eg: PDF/DJVU)?
250 function isFileMatch() {