Fix exception when searching for stuff beginning with "Media:"
[lhc/web/wiklou.git] / includes / search / SearchEngine.php
index 27a321a..fa7b872 100644 (file)
@@ -163,7 +163,7 @@ class SearchEngine {
 
        /**
         * Really find the title match.
-        * @return null|\Title
+        * @return null|Title
         */
        private static function getNearMatchInternal( $searchterm ) {
                global $wgContLang, $wgEnableSearchContributorsByIP;
@@ -187,6 +187,11 @@ class SearchEngine {
                                return null;
                        }
 
+                       # Try files if searching in the Media: namespace
+                       if ( $title->getNamespace() == NS_MEDIA ) {
+                               $title = Title::makeTitle( NS_FILE, $title->getText() );
+                       }
+
                        if ( $title->isSpecialPage() || $title->isExternal() || $title->exists() ) {
                                return $title;
                        }
@@ -791,11 +796,14 @@ class SearchResult {
         */
        protected function initText() {
                if ( !isset( $this->mText ) ) {
-                       if ( $this->mRevision != null )
-                               $this->mText = $this->mRevision->getText();
-                       else // TODO: can we fetch raw wikitext for commons images?
+                       if ( $this->mRevision != null ) {
+                               //TODO: if we could plug in some code that knows about special content models *and* about
+                               //      special features of the search engine, the search could benefit.
+                               $content = $this->mRevision->getContent();
+                               $this->mText = $content ? $content->getTextForSearchIndex() : '';
+                       } else { // TODO: can we fetch raw wikitext for commons images?
                                $this->mText = '';
-
+                       }
                }
        }
 
@@ -806,6 +814,8 @@ class SearchResult {
        function getTextSnippet( $terms ) {
                global $wgUser, $wgAdvancedSearchHighlighting;
                $this->initText();
+
+               // TODO: make highliter take a content object. Make ContentHandler a factory for SearchHighliter.
                list( $contextlines, $contextchars ) = SearchEngine::userHighlightPrefs( $wgUser );
                $h = new SearchHighlighter();
                if ( $wgAdvancedSearchHighlighting )