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
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.
35 protected $mRevision = null;
40 protected $mImage = null;
53 * Return a new SearchResult and initializes it with a title.
56 * @return SearchResult
58 public static function newFromTitle( $title ) {
60 $result->initFromTitle( $title );
65 * Return a new SearchResult and initializes it with a row.
68 * @return SearchResult
70 public static function newFromRow( $row ) {
72 $result->initFromRow( $row );
76 public function __construct( $row = null ) {
77 if ( !is_null( $row ) ) {
78 // Backwards compatibility with pre-1.17 callers
79 $this->initFromRow( $row );
84 * Initialize from a database row. Makes a Title and passes that to
89 protected function initFromRow( $row ) {
90 $this->initFromTitle( Title
::makeTitle( $row->page_namespace
, $row->page_title
) );
94 * Initialize from a Title and if possible initializes a corresponding
99 protected function initFromTitle( $title ) {
100 $this->mTitle
= $title;
101 if ( !is_null( $this->mTitle
) ) {
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
117 function isBrokenTitle() {
118 return is_null( $this->mTitle
);
122 * Check if target page is missing, happens when index is out of date
126 function isMissingRevision() {
127 return !$this->mRevision
&& !$this->mImage
;
133 function getTitle() {
134 return $this->mTitle
;
138 * Get the file for this page, if one exists
142 return $this->mImage
;
146 * @return float|null If not supported
148 function getScore() {
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?
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;
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 );
180 return $h->highlightSimple( $this->mText
, $terms, $contextlines, $contextchars );
185 * @return string Highlighted title, '' if not supported
187 function getTitleSnippet() {
192 * @return string Highlighted redirect name (redirect to this page), '' if none or not supported
194 function getRedirectSnippet() {
199 * @return Title Title object for the redirect to this page, null if none or not supported
201 function getRedirectTitle() {
206 * @return string Highlighted relevant section name, null if none or not supported
208 function getSectionSnippet() {
213 * @return Title Title object (pagename+fragment) for the section, null if none or not supported
215 function getSectionTitle() {
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();
232 * @return int Number of words
234 function getWordCount() {
236 return str_word_count( $this->mText
);
240 * @return int Size in bytes
242 function getByteSize() {
244 return strlen( $this->mText
);
248 * @return bool If hit has related articles
250 function hasRelated() {
255 * @return string Interwiki prefix of the title (return iw even if title is broken)
257 function getInterwikiPrefix() {
262 * @return string Interwiki namespace of the title (since we likely can't resolve it locally)
264 function getInterwikiNamespaceText() {
269 * Did this match file contents (eg: PDF/DJVU)?
272 function isFileMatch() {