Merge "Make the global objects documentation consistent in Setup.php"
[lhc/web/wiklou.git] / includes / deferred / SearchUpdate.php
index 7ca4158..20f348a 100644 (file)
@@ -35,7 +35,7 @@ class SearchUpdate implements DeferrableUpdate {
        /** @var Title Title we're updating */
        private $title;
 
-       /** @var Content|false Content of the page (not text) */
+       /** @var Content|bool Content of the page (not text) */
        private $content;
 
        /**
@@ -81,10 +81,10 @@ class SearchUpdate implements DeferrableUpdate {
                wfProfileIn( __METHOD__ );
 
                $page = WikiPage::newFromId( $this->id, WikiPage::READ_LATEST );
-               $indexTitle = Title::indexTitle( $this->title->getNamespace(), $this->title->getText() );
 
                foreach ( SearchEngine::getSearchTypes() as $type ) {
                        $search = SearchEngine::create( $type );
+                       $indexTitle = $this->indexTitle( $search );
                        if ( !$search->supports( 'search-update' ) ) {
                                continue;
                        }
@@ -174,4 +174,33 @@ class SearchUpdate implements DeferrableUpdate {
 
                return $text;
        }
+
+       /**
+        * Get a string representation of a title suitable for
+        * including in a search index
+        *
+        * @return string A stripped-down title string ready for the search index
+        */
+       private function indexTitle( SearchEngine $search ) {
+               global $wgContLang;
+
+               $ns = $this->title->getNamespace();
+               $title = $this->title->getText();
+
+               $lc = $search->legalSearchChars() . '&#;';
+               $t = $wgContLang->normalizeForSearch( $title );
+               $t = preg_replace( "/[^{$lc}]+/", ' ', $t );
+               $t = $wgContLang->lc( $t );
+
+               # Handle 's, s'
+               $t = preg_replace( "/([{$lc}]+)'s( |$)/", "\\1 \\1's ", $t );
+               $t = preg_replace( "/([{$lc}]+)s'( |$)/", "\\1s ", $t );
+
+               $t = preg_replace( "/\\s+/", ' ', $t );
+
+               if ( $ns == NS_FILE ) {
+                       $t = preg_replace( "/ (png|gif|jpg|jpeg|ogg)$/", "", $t );
+               }
+               return trim( $t );
+       }
 }