Merge "Removed some b/c code from file backend"
[lhc/web/wiklou.git] / includes / search / SearchResult.php
1 <?php
2 /**
3 * Search engine result
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 * @ingroup Search
22 */
23
24 /**
25 * @todo FIXME: This class is horribly factored. It would probably be better to
26 * have a useful base class to which you pass some standard information, then
27 * let the fancy self-highlighters extend that.
28 * @ingroup Search
29 */
30 class SearchResult {
31
32 /**
33 * @var Revision
34 */
35 protected $mRevision = null;
36
37 /**
38 * @var File
39 */
40 protected $mImage = null;
41
42 /**
43 * @var Title
44 */
45 protected $mTitle;
46
47 /**
48 * @var string
49 */
50 protected $mText;
51
52 /**
53 * Return a new SearchResult and initializes it with a title.
54 *
55 * @param Title $title
56 * @return SearchResult
57 */
58 public static function newFromTitle( $title ) {
59 $result = new self();
60 $result->initFromTitle( $title );
61 return $result;
62 }
63
64 /**
65 * Return a new SearchResult and initializes it with a row.
66 *
67 * @param object $row
68 * @return SearchResult
69 */
70 public static function newFromRow( $row ) {
71 $result = new self();
72 $result->initFromRow( $row );
73 return $result;
74 }
75
76 public function __construct( $row = null ) {
77 if ( !is_null( $row ) ) {
78 // Backwards compatibility with pre-1.17 callers
79 $this->initFromRow( $row );
80 }
81 }
82
83 /**
84 * Initialize from a database row. Makes a Title and passes that to
85 * initFromTitle.
86 *
87 * @param object $row
88 */
89 protected function initFromRow( $row ) {
90 $this->initFromTitle( Title::makeTitle( $row->page_namespace, $row->page_title ) );
91 }
92
93 /**
94 * Initialize from a Title and if possible initializes a corresponding
95 * Revision and File.
96 *
97 * @param Title $title
98 */
99 protected function initFromTitle( $title ) {
100 $this->mTitle = $title;
101 if ( !is_null( $this->mTitle ) ) {
102 $id = false;
103 wfRunHooks( 'SearchResultInitFromTitle', array( $title, &$id ) );
104 $this->mRevision = Revision::newFromTitle(
105 $this->mTitle, $id, Revision::READ_NORMAL );
106 if ( $this->mTitle->getNamespace() === NS_FILE ) {
107 $this->mImage = wfFindFile( $this->mTitle );
108 }
109 }
110 }
111
112 /**
113 * Check if this is result points to an invalid title
114 *
115 * @return bool
116 */
117 function isBrokenTitle() {
118 return is_null( $this->mTitle );
119 }
120
121 /**
122 * Check if target page is missing, happens when index is out of date
123 *
124 * @return bool
125 */
126 function isMissingRevision() {
127 return !$this->mRevision && !$this->mImage;
128 }
129
130 /**
131 * @return Title
132 */
133 function getTitle() {
134 return $this->mTitle;
135 }
136
137 /**
138 * Get the file for this page, if one exists
139 * @return File|null
140 */
141 function getFile() {
142 return $this->mImage;
143 }
144
145 /**
146 * Lazy initialization of article text from DB
147 */
148 protected function initText() {
149 if ( !isset( $this->mText ) ) {
150 if ( $this->mRevision != null ) {
151 $this->mText = SearchEngine::create()
152 ->getTextFromContent( $this->mTitle, $this->mRevision->getContent() );
153 } else { // TODO: can we fetch raw wikitext for commons images?
154 $this->mText = '';
155 }
156 }
157 }
158
159 /**
160 * @param array $terms Terms to highlight
161 * @return string Highlighted text snippet, null (and not '') if not supported
162 */
163 function getTextSnippet( $terms ) {
164 global $wgAdvancedSearchHighlighting;
165 $this->initText();
166
167 // TODO: make highliter take a content object. Make ContentHandler a factory for SearchHighliter.
168 list( $contextlines, $contextchars ) = SearchEngine::userHighlightPrefs();
169
170 $h = new SearchHighlighter();
171 if ( count( $terms ) > 0 ) {
172 if ( $wgAdvancedSearchHighlighting ) {
173 return $h->highlightText( $this->mText, $terms, $contextlines, $contextchars );
174 } else {
175 return $h->highlightSimple( $this->mText, $terms, $contextlines, $contextchars );
176 }
177 } else {
178 return $h->highlightNone( $this->mText, $contextlines, $contextchars );
179 }
180 }
181
182 /**
183 * @return string Highlighted title, '' if not supported
184 */
185 function getTitleSnippet() {
186 return '';
187 }
188
189 /**
190 * @return string Highlighted redirect name (redirect to this page), '' if none or not supported
191 */
192 function getRedirectSnippet() {
193 return '';
194 }
195
196 /**
197 * @return Title Title object for the redirect to this page, null if none or not supported
198 */
199 function getRedirectTitle() {
200 return null;
201 }
202
203 /**
204 * @return string Highlighted relevant section name, null if none or not supported
205 */
206 function getSectionSnippet() {
207 return '';
208 }
209
210 /**
211 * @return Title Title object (pagename+fragment) for the section, null if none or not supported
212 */
213 function getSectionTitle() {
214 return null;
215 }
216
217 /**
218 * @return string Timestamp
219 */
220 function getTimestamp() {
221 if ( $this->mRevision ) {
222 return $this->mRevision->getTimestamp();
223 } elseif ( $this->mImage ) {
224 return $this->mImage->getTimestamp();
225 }
226 return '';
227 }
228
229 /**
230 * @return int Number of words
231 */
232 function getWordCount() {
233 $this->initText();
234 return str_word_count( $this->mText );
235 }
236
237 /**
238 * @return int Size in bytes
239 */
240 function getByteSize() {
241 $this->initText();
242 return strlen( $this->mText );
243 }
244
245 /**
246 * @return string Interwiki prefix of the title (return iw even if title is broken)
247 */
248 function getInterwikiPrefix() {
249 return '';
250 }
251
252 /**
253 * @return string Interwiki namespace of the title (since we likely can't resolve it locally)
254 */
255 function getInterwikiNamespaceText() {
256 return '';
257 }
258
259 /**
260 * Did this match file contents (eg: PDF/DJVU)?
261 * @return bool
262 */
263 function isFileMatch() {
264 return false;
265 }
266 }