3 use MediaWiki\MediaWikiServices
;
6 * Transitional trait used to share the methods between SearchResult and RevisionSearchResult.
7 * All the content of this trait can be moved to RevisionSearchResult once SearchResult is finally
8 * refactored into an abstract class.
9 * NOTE: This trait MUST NOT be used by something else than SearchResult and RevisionSearchResult.
10 * It will be removed without deprecation period once SearchResult
12 trait RevisionSearchResultTrait
{
16 protected $mRevision = null;
21 protected $mImage = null;
34 * Initialize from a Title and if possible initializes a corresponding
39 protected function initFromTitle( $title ) {
40 $this->mTitle
= $title;
41 $services = MediaWikiServices
::getInstance();
42 if ( !is_null( $this->mTitle
) ) {
44 Hooks
::run( 'SearchResultInitFromTitle', [ $title, &$id ] );
45 $this->mRevision
= Revision
::newFromTitle(
46 $this->mTitle
, $id, Revision
::READ_NORMAL
);
47 if ( $this->mTitle
->getNamespace() === NS_FILE
) {
48 $this->mImage
= $services->getRepoGroup()->findFile( $this->mTitle
);
54 * Check if this is result points to an invalid title
58 public function isBrokenTitle() {
59 return is_null( $this->mTitle
);
63 * Check if target page is missing, happens when index is out of date
67 public function isMissingRevision() {
68 return !$this->mRevision
&& !$this->mImage
;
74 public function getTitle() {
79 * Get the file for this page, if one exists
82 public function getFile() {
87 * Lazy initialization of article text from DB
89 protected function initText() {
90 if ( !isset( $this->mText
) ) {
91 if ( $this->mRevision
!= null ) {
92 $content = $this->mRevision
->getContent();
93 $this->mText
= $content !== null ?
$content->getTextForSearchIndex() : '';
94 } else { // TODO: can we fetch raw wikitext for commons images?
101 * @param string[] $terms Terms to highlight (this parameter is deprecated and ignored)
102 * @return string Highlighted text snippet, null (and not '') if not supported
104 public function getTextSnippet( $terms = [] ) {
109 * @return string Highlighted title, '' if not supported
111 public function getTitleSnippet() {
116 * @return string Highlighted redirect name (redirect to this page), '' if none or not supported
118 public function getRedirectSnippet() {
123 * @return Title|null Title object for the redirect to this page, null if none or not supported
125 public function getRedirectTitle() {
130 * @return string Highlighted relevant section name, null if none or not supported
132 public function getSectionSnippet() {
137 * @return Title|null Title object (pagename+fragment) for the section,
138 * null if none or not supported
140 public function getSectionTitle() {
145 * @return string Highlighted relevant category name or '' if none or not supported
147 public function getCategorySnippet() {
152 * @return string Timestamp
154 public function getTimestamp() {
155 if ( $this->mRevision
) {
156 return $this->mRevision
->getTimestamp();
157 } elseif ( $this->mImage
) {
158 return $this->mImage
->getTimestamp();
164 * @return int Number of words
166 public function getWordCount() {
168 return str_word_count( $this->mText
);
172 * @return int Size in bytes
174 public function getByteSize() {
176 return strlen( $this->mText
);
180 * @return string Interwiki prefix of the title (return iw even if title is broken)
182 public function getInterwikiPrefix() {
187 * @return string Interwiki namespace of the title (since we likely can't resolve it locally)
189 public function getInterwikiNamespaceText() {
194 * Did this match file contents (eg: PDF/DJVU)?
197 public function isFileMatch() {