Expose $wgMaxArticleSize in siteinfo query api
[mediawiki.git] / includes / search / SearchResult.php
blob21effbbc988f9e675ed93d861599e8120d4776a0
1 <?php
2 /**
3 * Search engine result
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
20 * @file
21 * @ingroup Search
24 use MediaWiki\MediaWikiServices;
26 /**
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.
30 * @ingroup Search
32 class SearchResult {
34 /**
35 * @var Revision
37 protected $mRevision = null;
39 /**
40 * @var File
42 protected $mImage = null;
44 /**
45 * @var Title
47 protected $mTitle;
49 /**
50 * @var string
52 protected $mText;
54 /**
55 * @var SearchEngine
57 protected $searchEngine;
59 /**
60 * Return a new SearchResult and initializes it with a title.
62 * @param Title $title
63 * @return SearchResult
65 public static function newFromTitle( $title ) {
66 $result = new static();
67 $result->initFromTitle( $title );
68 return $result;
71 /**
72 * Initialize from a Title and if possible initializes a corresponding
73 * Revision and File.
75 * @param Title $title
77 protected function initFromTitle( $title ) {
78 $this->mTitle = $title;
79 if ( !is_null( $this->mTitle ) ) {
80 $id = false;
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();
91 /**
92 * Check if this is result points to an invalid title
94 * @return bool
96 function isBrokenTitle() {
97 return is_null( $this->mTitle );
101 * Check if target page is missing, happens when index is out of date
103 * @return bool
105 function isMissingRevision() {
106 return !$this->mRevision && !$this->mImage;
110 * @return Title
112 function getTitle() {
113 return $this->mTitle;
117 * Get the file for this page, if one exists
118 * @return File|null
120 function getFile() {
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?
133 $this->mText = '';
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;
144 $this->initText();
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 );
153 } else {
154 return $h->highlightSimple( $this->mText, $terms, $contextlines, $contextchars );
156 } else {
157 return $h->highlightNone( $this->mText, $contextlines, $contextchars );
162 * @return string Highlighted title, '' if not supported
164 function getTitleSnippet() {
165 return '';
169 * @return string Highlighted redirect name (redirect to this page), '' if none or not supported
171 function getRedirectSnippet() {
172 return '';
176 * @return Title|null Title object for the redirect to this page, null if none or not supported
178 function getRedirectTitle() {
179 return null;
183 * @return string Highlighted relevant section name, null if none or not supported
185 function getSectionSnippet() {
186 return '';
190 * @return Title|null Title object (pagename+fragment) for the section,
191 * null if none or not supported
193 function getSectionTitle() {
194 return null;
198 * @return string Highlighted relevant category name or '' if none or not supported
200 public function getCategorySnippet() {
201 return '';
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();
213 return '';
217 * @return int Number of words
219 function getWordCount() {
220 $this->initText();
221 return str_word_count( $this->mText );
225 * @return int Size in bytes
227 function getByteSize() {
228 $this->initText();
229 return strlen( $this->mText );
233 * @return string Interwiki prefix of the title (return iw even if title is broken)
235 function getInterwikiPrefix() {
236 return '';
240 * @return string Interwiki namespace of the title (since we likely can't resolve it locally)
242 function getInterwikiNamespaceText() {
243 return '';
247 * Did this match file contents (eg: PDF/DJVU)?
248 * @return bool
250 function isFileMatch() {
251 return false;