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.
33 use SearchResultTrait
;
38 protected $mRevision = null;
43 protected $mImage = null;
56 * Return a new SearchResult and initializes it with a title.
59 * @param ISearchResultSet|null $parentSet
60 * @return SearchResult
62 public static function newFromTitle( $title, ISearchResultSet
$parentSet = null ) {
63 $result = new static();
64 $result->initFromTitle( $title );
66 $parentSet->augmentResult( $result );
72 * Initialize from a Title and if possible initializes a corresponding
77 protected function initFromTitle( $title ) {
78 $this->mTitle
= $title;
79 $services = MediaWikiServices
::getInstance();
80 if ( !is_null( $this->mTitle
) ) {
82 Hooks
::run( 'SearchResultInitFromTitle', [ $title, &$id ] );
83 $this->mRevision
= Revision
::newFromTitle(
84 $this->mTitle
, $id, Revision
::READ_NORMAL
);
85 if ( $this->mTitle
->getNamespace() === NS_FILE
) {
86 $this->mImage
= $services->getRepoGroup()->findFile( $this->mTitle
);
92 * Check if this is result points to an invalid title
96 public function isBrokenTitle() {
97 return is_null( $this->mTitle
);
101 * Check if target page is missing, happens when index is out of date
105 public function isMissingRevision() {
106 return !$this->mRevision
&& !$this->mImage
;
112 public function getTitle() {
113 return $this->mTitle
;
117 * Get the file for this page, if one exists
120 public 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 $content = $this->mRevision
->getContent();
131 $this->mText
= $content !== null ?
$content->getTextForSearchIndex() : '';
132 } else { // TODO: can we fetch raw wikitext for commons images?
139 * @param string[] $terms Terms to highlight (this parameter is deprecated and ignored)
140 * @return string Highlighted text snippet, null (and not '') if not supported
142 public function getTextSnippet( $terms = [] ) {
147 * @return string Highlighted title, '' if not supported
149 public function getTitleSnippet() {
154 * @return string Highlighted redirect name (redirect to this page), '' if none or not supported
156 public function getRedirectSnippet() {
161 * @return Title|null Title object for the redirect to this page, null if none or not supported
163 public function getRedirectTitle() {
168 * @return string Highlighted relevant section name, null if none or not supported
170 public function getSectionSnippet() {
175 * @return Title|null Title object (pagename+fragment) for the section,
176 * null if none or not supported
178 public function getSectionTitle() {
183 * @return string Highlighted relevant category name or '' if none or not supported
185 public function getCategorySnippet() {
190 * @return string Timestamp
192 public function getTimestamp() {
193 if ( $this->mRevision
) {
194 return $this->mRevision
->getTimestamp();
195 } elseif ( $this->mImage
) {
196 return $this->mImage
->getTimestamp();
202 * @return int Number of words
204 public function getWordCount() {
206 return str_word_count( $this->mText
);
210 * @return int Size in bytes
212 public function getByteSize() {
214 return strlen( $this->mText
);
218 * @return string Interwiki prefix of the title (return iw even if title is broken)
220 public function getInterwikiPrefix() {
225 * @return string Interwiki namespace of the title (since we likely can't resolve it locally)
227 public function getInterwikiNamespaceText() {
232 * Did this match file contents (eg: PDF/DJVU)?
235 public function isFileMatch() {