MediaWiki  REL1_23
SearchResult.php
Go to the documentation of this file.
00001 <?php
00030 class SearchResult {
00031 
00035     protected $mRevision = null;
00036 
00040     protected $mImage = null;
00041 
00045     protected $mTitle;
00046 
00050     protected $mText;
00051 
00058     public static function newFromTitle( $title ) {
00059         $result = new self();
00060         $result->initFromTitle( $title );
00061         return $result;
00062     }
00063 
00070     public static function newFromRow( $row ) {
00071         $result = new self();
00072         $result->initFromRow( $row );
00073         return $result;
00074     }
00075 
00076     public function __construct( $row = null ) {
00077         if ( !is_null( $row ) ) {
00078             // Backwards compatibility with pre-1.17 callers
00079             $this->initFromRow( $row );
00080         }
00081     }
00082 
00089     protected function initFromRow( $row ) {
00090         $this->initFromTitle( Title::makeTitle( $row->page_namespace, $row->page_title ) );
00091     }
00092 
00099     protected function initFromTitle( $title ) {
00100         $this->mTitle = $title;
00101         if ( !is_null( $this->mTitle ) ) {
00102             $id = false;
00103             wfRunHooks( 'SearchResultInitFromTitle', array( $title, &$id ) );
00104             $this->mRevision = Revision::newFromTitle(
00105                 $this->mTitle, $id, Revision::READ_NORMAL );
00106             if ( $this->mTitle->getNamespace() === NS_FILE ) {
00107                 $this->mImage = wfFindFile( $this->mTitle );
00108             }
00109         }
00110     }
00111 
00117     function isBrokenTitle() {
00118         return is_null( $this->mTitle );
00119     }
00120 
00126     function isMissingRevision() {
00127         return !$this->mRevision && !$this->mImage;
00128     }
00129 
00133     function getTitle() {
00134         return $this->mTitle;
00135     }
00136 
00141     function getFile() {
00142         return $this->mImage;
00143     }
00144 
00148     function getScore() {
00149         return null;
00150     }
00151 
00155     protected function initText() {
00156         if ( !isset( $this->mText ) ) {
00157             if ( $this->mRevision != null ) {
00158                 $this->mText = SearchEngine::create()
00159                     ->getTextFromContent( $this->mTitle, $this->mRevision->getContent() );
00160             } else { // TODO: can we fetch raw wikitext for commons images?
00161                 $this->mText = '';
00162             }
00163         }
00164     }
00165 
00170     function getTextSnippet( $terms ) {
00171         global $wgAdvancedSearchHighlighting;
00172         $this->initText();
00173 
00174         // TODO: make highliter take a content object. Make ContentHandler a factory for SearchHighliter.
00175         list( $contextlines, $contextchars ) = SearchEngine::userHighlightPrefs();
00176         $h = new SearchHighlighter();
00177         if ( $wgAdvancedSearchHighlighting ) {
00178             return $h->highlightText( $this->mText, $terms, $contextlines, $contextchars );
00179         } else {
00180             return $h->highlightSimple( $this->mText, $terms, $contextlines, $contextchars );
00181         }
00182     }
00183 
00187     function getTitleSnippet() {
00188         return '';
00189     }
00190 
00194     function getRedirectSnippet() {
00195         return '';
00196     }
00197 
00201     function getRedirectTitle() {
00202         return null;
00203     }
00204 
00208     function getSectionSnippet() {
00209         return '';
00210     }
00211 
00215     function getSectionTitle() {
00216         return null;
00217     }
00218 
00222     function getTimestamp() {
00223         if ( $this->mRevision ) {
00224             return $this->mRevision->getTimestamp();
00225         } elseif ( $this->mImage ) {
00226             return $this->mImage->getTimestamp();
00227         }
00228         return '';
00229     }
00230 
00234     function getWordCount() {
00235         $this->initText();
00236         return str_word_count( $this->mText );
00237     }
00238 
00242     function getByteSize() {
00243         $this->initText();
00244         return strlen( $this->mText );
00245     }
00246 
00250     function hasRelated() {
00251         return false;
00252     }
00253 
00257     function getInterwikiPrefix() {
00258         return '';
00259     }
00260 
00264     function getInterwikiNamespaceText() {
00265         return '';
00266     }
00267 
00271     function isFileMatch() {
00272         return false;
00273     }
00274 }