Merge "Make DBAccessBase use DBConnRef, rename $wiki, and hide getLoadBalancer()"
[lhc/web/wiklou.git] / includes / search / RevisionSearchResultTrait.php
1 <?php
2
3 use MediaWiki\MediaWikiServices;
4
5 /**
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
11 */
12 trait RevisionSearchResultTrait {
13 /**
14 * @var Revision
15 */
16 protected $mRevision = null;
17
18 /**
19 * @var File
20 */
21 protected $mImage = null;
22
23 /**
24 * @var Title
25 */
26 protected $mTitle;
27
28 /**
29 * @var string
30 */
31 protected $mText;
32
33 /**
34 * Initialize from a Title and if possible initializes a corresponding
35 * Revision and File.
36 *
37 * @param Title $title
38 */
39 protected function initFromTitle( $title ) {
40 $this->mTitle = $title;
41 $services = MediaWikiServices::getInstance();
42 if ( !is_null( $this->mTitle ) ) {
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
53 /**
54 * Check if this is result points to an invalid title
55 *
56 * @return bool
57 */
58 public function isBrokenTitle() {
59 return is_null( $this->mTitle );
60 }
61
62 /**
63 * Check if target page is missing, happens when index is out of date
64 *
65 * @return bool
66 */
67 public function isMissingRevision() {
68 return !$this->mRevision && !$this->mImage;
69 }
70
71 /**
72 * @return Title
73 */
74 public function getTitle() {
75 return $this->mTitle;
76 }
77
78 /**
79 * Get the file for this page, if one exists
80 * @return File|null
81 */
82 public function getFile() {
83 return $this->mImage;
84 }
85
86 /**
87 * Lazy initialization of article text from DB
88 */
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
100 /**
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
103 */
104 public function getTextSnippet( $terms = [] ) {
105 return '';
106 }
107
108 /**
109 * @return string Highlighted title, '' if not supported
110 */
111 public function getTitleSnippet() {
112 return '';
113 }
114
115 /**
116 * @return string Highlighted redirect name (redirect to this page), '' if none or not supported
117 */
118 public function getRedirectSnippet() {
119 return '';
120 }
121
122 /**
123 * @return Title|null Title object for the redirect to this page, null if none or not supported
124 */
125 public function getRedirectTitle() {
126 return null;
127 }
128
129 /**
130 * @return string Highlighted relevant section name, null if none or not supported
131 */
132 public function getSectionSnippet() {
133 return '';
134 }
135
136 /**
137 * @return Title|null Title object (pagename+fragment) for the section,
138 * null if none or not supported
139 */
140 public function getSectionTitle() {
141 return null;
142 }
143
144 /**
145 * @return string Highlighted relevant category name or '' if none or not supported
146 */
147 public function getCategorySnippet() {
148 return '';
149 }
150
151 /**
152 * @return string Timestamp
153 */
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
163 /**
164 * @return int Number of words
165 */
166 public function getWordCount() {
167 $this->initText();
168 return str_word_count( $this->mText );
169 }
170
171 /**
172 * @return int Size in bytes
173 */
174 public function getByteSize() {
175 $this->initText();
176 return strlen( $this->mText );
177 }
178
179 /**
180 * @return string Interwiki prefix of the title (return iw even if title is broken)
181 */
182 public function getInterwikiPrefix() {
183 return '';
184 }
185
186 /**
187 * @return string Interwiki namespace of the title (since we likely can't resolve it locally)
188 */
189 public function getInterwikiNamespaceText() {
190 return '';
191 }
192
193 /**
194 * Did this match file contents (eg: PDF/DJVU)?
195 * @return bool
196 */
197 public function isFileMatch() {
198 return false;
199 }
200 }