Merge "Fix IE9's lack of support for console.warn.apply"
[lhc/web/wiklou.git] / includes / deferred / SearchUpdate.php
index 82a413e..9ae9034 100644 (file)
  * @ingroup Search
  */
 class SearchUpdate implements DeferrableUpdate {
-       /**
-        * Page id being updated
-        * @var int
-        */
+       /** @var int Page id being updated */
        private $id = 0;
 
-       /**
-        * Title we're updating
-        * @var Title
-        */
+       /** @var Title Title we're updating */
        private $title;
 
-       /**
-        * Content of the page (not text)
-        * @var Content|false
-        */
+       /** @var Content|bool Content of the page (not text) */
        private $content;
 
        /**
@@ -52,7 +43,7 @@ class SearchUpdate implements DeferrableUpdate {
         *
         * @param int $id Page id to update
         * @param Title|string $title Title of page to update
-        * @param Content|string|false $c Content of the page to update.
+        * @param Content|string|bool $c Content of the page to update. Default: false.
         *  If a Content object, text will be gotten from it. String is for back-compat.
         *  Passing false tells the backend to just update the title, not the content
         */
@@ -90,7 +81,7 @@ class SearchUpdate implements DeferrableUpdate {
                wfProfileIn( __METHOD__ );
 
                $page = WikiPage::newFromId( $this->id, WikiPage::READ_LATEST );
-               $indexTitle = Title::indexTitle( $this->title->getNamespace(), $this->title->getText() );
+               $indexTitle = $this->indexTitle();
 
                foreach ( SearchEngine::getSearchTypes() as $type ) {
                        $search = SearchEngine::create( $type );
@@ -180,6 +171,36 @@ class SearchUpdate implements DeferrableUpdate {
                # Strip wiki '' and '''
                $text = preg_replace( "/''[']*/", " ", $text );
                wfProfileOut( __METHOD__ . '-regexps' );
+
                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() {
+               global $wgContLang;
+
+               $ns = $this->title->getNamespace();
+               $title = $this->title->getText();
+
+               $lc = SearchEngine::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 );
+       }
 }