MediaWiki  master
RevisionSearchResultTrait.php
Go to the documentation of this file.
1 <?php
2 
4 
16  protected $mRevision = null;
17 
21  protected $mImage = null;
22 
26  protected $mTitle;
27 
31  protected $mText;
32 
39  protected function initFromTitle( $title ) {
40  $this->mTitle = $title;
41  $services = MediaWikiServices::getInstance();
42  if ( $this->mTitle !== null ) {
43  $id = false;
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 );
49  }
50  }
51  }
52 
58  public function isBrokenTitle() {
59  return $this->mTitle === null;
60  }
61 
67  public function isMissingRevision() {
68  return !$this->mRevision && !$this->mImage;
69  }
70 
74  public function getTitle() {
75  return $this->mTitle;
76  }
77 
82  public function getFile() {
83  return $this->mImage;
84  }
85 
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?
95  $this->mText = '';
96  }
97  }
98  }
99 
104  public function getTextSnippet( $terms = [] ) {
105  return '';
106  }
107 
111  public function getTitleSnippet() {
112  return '';
113  }
114 
118  public function getRedirectSnippet() {
119  return '';
120  }
121 
125  public function getRedirectTitle() {
126  return null;
127  }
128 
132  public function getSectionSnippet() {
133  return '';
134  }
135 
140  public function getSectionTitle() {
141  return null;
142  }
143 
147  public function getCategorySnippet() {
148  return '';
149  }
150 
154  public function getTimestamp() {
155  if ( $this->mRevision ) {
156  return $this->mRevision->getTimestamp();
157  } elseif ( $this->mImage ) {
158  return $this->mImage->getTimestamp();
159  }
160  return '';
161  }
162 
166  public function getWordCount() {
167  $this->initText();
168  return str_word_count( $this->mText );
169  }
170 
174  public function getByteSize() {
175  $this->initText();
176  return strlen( $this->mText );
177  }
178 
182  public function getInterwikiPrefix() {
183  return '';
184  }
185 
189  public function getInterwikiNamespaceText() {
190  return '';
191  }
192 
197  public function isFileMatch() {
198  return false;
199  }
200 }
getRedirectTitle
getRedirectTitle()
Definition: RevisionSearchResultTrait.php:125
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:130
getSectionSnippet
getSectionSnippet()
Definition: RevisionSearchResultTrait.php:132
getByteSize
getByteSize()
Definition: RevisionSearchResultTrait.php:174
NS_FILE
const NS_FILE
Definition: Defines.php:66
Revision
Definition: Revision.php:40
Revision\newFromTitle
static newFromTitle(LinkTarget $linkTarget, $id=0, $flags=0)
Load either the current, or a specified, revision that's attached to a given link target.
Definition: Revision.php:138
isFileMatch
isFileMatch()
Did this match file contents (eg: PDF/DJVU)?
Definition: RevisionSearchResultTrait.php:197
File
Implements some public methods and some protected utility functions which are required by multiple ch...
Definition: File.php:61
$mTitle
Title null $mTitle
Definition: RevisionSearchResultTrait.php:26
getSectionTitle
getSectionTitle()
Definition: RevisionSearchResultTrait.php:140
getWordCount
getWordCount()
Definition: RevisionSearchResultTrait.php:166
$title
$title
Definition: testCompression.php:36
$mImage
File $mImage
Definition: RevisionSearchResultTrait.php:21
$content
$content
Definition: router.php:78
isMissingRevision
isMissingRevision()
Check if target page is missing, happens when index is out of date.
Definition: RevisionSearchResultTrait.php:67
$mText
string $mText
Definition: RevisionSearchResultTrait.php:31
initFromTitle
initFromTitle( $title)
Initialize from a Title and if possible initializes a corresponding Revision and File.
Definition: RevisionSearchResultTrait.php:39
getInterwikiNamespaceText
getInterwikiNamespaceText()
Definition: RevisionSearchResultTrait.php:189
Title
Represents a title within MediaWiki.
Definition: Title.php:42
getCategorySnippet
getCategorySnippet()
Definition: RevisionSearchResultTrait.php:147
initText
initText()
Lazy initialization of article text from DB.
Definition: RevisionSearchResultTrait.php:89
getTitle
getTitle()
Definition: RevisionSearchResultTrait.php:74
getTimestamp
getTimestamp()
Definition: RevisionSearchResultTrait.php:154
getTextSnippet
getTextSnippet( $terms=[])
Definition: RevisionSearchResultTrait.php:104
getInterwikiPrefix
getInterwikiPrefix()
Definition: RevisionSearchResultTrait.php:182
getTitleSnippet
getTitleSnippet()
Definition: RevisionSearchResultTrait.php:111
RevisionSearchResultTrait
trait RevisionSearchResultTrait
Transitional trait used to share the methods between SearchResult and RevisionSearchResult.
Definition: RevisionSearchResultTrait.php:12
Hooks\run
static run( $event, array $args=[], $deprecatedVersion=null)
Call hook functions defined in Hooks::register and $wgHooks.
Definition: Hooks.php:200
isBrokenTitle
isBrokenTitle()
Check if this is result points to an invalid title.
Definition: RevisionSearchResultTrait.php:58
getFile
getFile()
Get the file for this page, if one exists.
Definition: RevisionSearchResultTrait.php:82
getRedirectSnippet
getRedirectSnippet()
Definition: RevisionSearchResultTrait.php:118