Made TempFSFile try to purge files on fatals too
[mediawiki.git] / includes / search / SearchResult.php
blob153590a5a9eab08159b8fb4c8df79eb284b7acdf
1 <?php
2 /**
3 * Search engine result
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
20 * @file
21 * @ingroup Search
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
30 class SearchResult {
32 /**
33 * @var Revision
35 protected $mRevision = null;
37 /**
38 * @var File
40 protected $mImage = null;
42 /**
43 * @var Title
45 protected $mTitle;
47 /**
48 * @var String
50 protected $mText;
52 /**
53 * Return a new SearchResult and initializes it with a title.
55 * @param $title Title
56 * @return SearchResult
58 public static function newFromTitle( $title ) {
59 $result = new self();
60 $result->initFromTitle( $title );
61 return $result;
64 /**
65 * Return a new SearchResult and initializes it with a row.
67 * @param $row object
68 * @return SearchResult
70 public static function newFromRow( $row ) {
71 $result = new self();
72 $result->initFromRow( $row );
73 return $result;
76 public function __construct( $row = null ) {
77 if ( !is_null( $row ) ) {
78 // Backwards compatibility with pre-1.17 callers
79 $this->initFromRow( $row );
83 /**
84 * Initialize from a database row. Makes a Title and passes that to
85 * initFromTitle.
87 * @param $row object
89 protected function initFromRow( $row ) {
90 $this->initFromTitle( Title::makeTitle( $row->page_namespace, $row->page_title ) );
93 /**
94 * Initialize from a Title and if possible initializes a corresponding
95 * Revision and File.
97 * @param $title Title
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 );
113 * Check if this is result points to an invalid title
115 * @return Boolean
117 function isBrokenTitle() {
118 return is_null( $this->mTitle );
122 * Check if target page is missing, happens when index is out of date
124 * @return Boolean
126 function isMissingRevision() {
127 return !$this->mRevision && !$this->mImage;
131 * @return Title
133 function getTitle() {
134 return $this->mTitle;
138 * Get the file for this page, if one exists
139 * @return File|null
141 function getFile() {
142 return $this->mImage;
146 * @return float|null if not supported
148 function getScore() {
149 return null;
153 * Lazy initialization of article text from DB
155 protected function initText() {
156 if ( !isset( $this->mText ) ) {
157 if ( $this->mRevision != null ) {
158 $this->mText = SearchEngine::create()
159 ->getTextFromContent( $this->mTitle, $this->mRevision->getContent() );
160 } else { // TODO: can we fetch raw wikitext for commons images?
161 $this->mText = '';
167 * @param array $terms terms to highlight
168 * @return String: highlighted text snippet, null (and not '') if not supported
170 function getTextSnippet( $terms ) {
171 global $wgAdvancedSearchHighlighting;
172 $this->initText();
174 // TODO: make highliter take a content object. Make ContentHandler a factory for SearchHighliter.
175 list( $contextlines, $contextchars ) = SearchEngine::userHighlightPrefs();
176 $h = new SearchHighlighter();
177 if ( $wgAdvancedSearchHighlighting ) {
178 return $h->highlightText( $this->mText, $terms, $contextlines, $contextchars );
179 } else {
180 return $h->highlightSimple( $this->mText, $terms, $contextlines, $contextchars );
185 * @return String: highlighted title, '' if not supported
187 function getTitleSnippet() {
188 return '';
192 * @return String: highlighted redirect name (redirect to this page), '' if none or not supported
194 function getRedirectSnippet() {
195 return '';
199 * @return Title object for the redirect to this page, null if none or not supported
201 function getRedirectTitle() {
202 return null;
206 * @return string highlighted relevant section name, null if none or not supported
208 function getSectionSnippet() {
209 return '';
213 * @return Title object (pagename+fragment) for the section, null if none or not supported
215 function getSectionTitle() {
216 return null;
220 * @return String: timestamp
222 function getTimestamp() {
223 if ( $this->mRevision ) {
224 return $this->mRevision->getTimestamp();
225 } elseif ( $this->mImage ) {
226 return $this->mImage->getTimestamp();
228 return '';
232 * @return Integer: number of words
234 function getWordCount() {
235 $this->initText();
236 return str_word_count( $this->mText );
240 * @return Integer: size in bytes
242 function getByteSize() {
243 $this->initText();
244 return strlen( $this->mText );
248 * @return Boolean if hit has related articles
250 function hasRelated() {
251 return false;
255 * @return String: interwiki prefix of the title (return iw even if title is broken)
257 function getInterwikiPrefix() {
258 return '';
262 * @return string interwiki namespace of the title (since we likely can't resolve it locally)
264 function getInterwikiNamespaceText() {
265 return '';
269 * Did this match file contents (eg: PDF/DJVU)?
271 function isFileMatch() {
272 return false;