MediaWiki
REL1_24
|
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 protected function initFromTitle( $title ) { 00071 $this->mTitle = $title; 00072 if ( !is_null( $this->mTitle ) ) { 00073 $id = false; 00074 wfRunHooks( 'SearchResultInitFromTitle', array( $title, &$id ) ); 00075 $this->mRevision = Revision::newFromTitle( 00076 $this->mTitle, $id, Revision::READ_NORMAL ); 00077 if ( $this->mTitle->getNamespace() === NS_FILE ) { 00078 $this->mImage = wfFindFile( $this->mTitle ); 00079 } 00080 } 00081 } 00082 00088 function isBrokenTitle() { 00089 return is_null( $this->mTitle ); 00090 } 00091 00097 function isMissingRevision() { 00098 return !$this->mRevision && !$this->mImage; 00099 } 00100 00104 function getTitle() { 00105 return $this->mTitle; 00106 } 00107 00112 function getFile() { 00113 return $this->mImage; 00114 } 00115 00119 protected function initText() { 00120 if ( !isset( $this->mText ) ) { 00121 if ( $this->mRevision != null ) { 00122 $this->mText = SearchEngine::create() 00123 ->getTextFromContent( $this->mTitle, $this->mRevision->getContent() ); 00124 } else { // TODO: can we fetch raw wikitext for commons images? 00125 $this->mText = ''; 00126 } 00127 } 00128 } 00129 00134 function getTextSnippet( $terms ) { 00135 global $wgAdvancedSearchHighlighting; 00136 $this->initText(); 00137 00138 // TODO: make highliter take a content object. Make ContentHandler a factory for SearchHighliter. 00139 list( $contextlines, $contextchars ) = SearchEngine::userHighlightPrefs(); 00140 00141 $h = new SearchHighlighter(); 00142 if ( count( $terms ) > 0 ) { 00143 if ( $wgAdvancedSearchHighlighting ) { 00144 return $h->highlightText( $this->mText, $terms, $contextlines, $contextchars ); 00145 } else { 00146 return $h->highlightSimple( $this->mText, $terms, $contextlines, $contextchars ); 00147 } 00148 } else { 00149 return $h->highlightNone( $this->mText, $contextlines, $contextchars ); 00150 } 00151 } 00152 00156 function getTitleSnippet() { 00157 return ''; 00158 } 00159 00163 function getRedirectSnippet() { 00164 return ''; 00165 } 00166 00170 function getRedirectTitle() { 00171 return null; 00172 } 00173 00177 function getSectionSnippet() { 00178 return ''; 00179 } 00180 00184 function getSectionTitle() { 00185 return null; 00186 } 00187 00191 function getTimestamp() { 00192 if ( $this->mRevision ) { 00193 return $this->mRevision->getTimestamp(); 00194 } elseif ( $this->mImage ) { 00195 return $this->mImage->getTimestamp(); 00196 } 00197 return ''; 00198 } 00199 00203 function getWordCount() { 00204 $this->initText(); 00205 return str_word_count( $this->mText ); 00206 } 00207 00211 function getByteSize() { 00212 $this->initText(); 00213 return strlen( $this->mText ); 00214 } 00215 00219 function getInterwikiPrefix() { 00220 return ''; 00221 } 00222 00226 function getInterwikiNamespaceText() { 00227 return ''; 00228 } 00229 00234 function isFileMatch() { 00235 return false; 00236 } 00237 }