MediaWiki  REL1_22
rebuildtextindex.php
Go to the documentation of this file.
00001 <?php
00028 require_once __DIR__ . '/Maintenance.php';
00029 
00035 class RebuildTextIndex extends Maintenance {
00036     const RTI_CHUNK_SIZE = 500;
00037 
00041     private $db;
00042 
00043     public function __construct() {
00044         parent::__construct();
00045         $this->mDescription = "Rebuild search index table from scratch";
00046     }
00047 
00048     public function getDbType() {
00049         return Maintenance::DB_ADMIN;
00050     }
00051 
00052     public function execute() {
00053         global $wgTitle;
00054 
00055         // Shouldn't be needed for Postgres
00056         $this->db = wfGetDB( DB_MASTER );
00057         if ( $this->db->getType() == 'postgres' ) {
00058             $this->error( "This script is not needed when using Postgres.\n", true );
00059         }
00060 
00061         $this->db = wfGetDB( DB_MASTER );
00062         if ( $this->db->getType() == 'sqlite' ) {
00063             if ( !DatabaseSqlite::getFulltextSearchModule() ) {
00064                 $this->error( "Your version of SQLite module for PHP doesn't support full-text search (FTS3).\n", true );
00065             }
00066             if ( !$this->db->checkForEnabledSearch() ) {
00067                 $this->error( "Your database schema is not configured for full-text search support. Run update.php.\n", true );
00068             }
00069         }
00070 
00071         $wgTitle = Title::newFromText( "Rebuild text index script" );
00072 
00073         if ( $this->db->getType() == 'mysql' ) {
00074             $this->dropMysqlTextIndex();
00075             $this->populateSearchIndex();
00076             $this->createMysqlTextIndex();
00077         } else {
00078             $this->clearSearchIndex();
00079             $this->populateSearchIndex();
00080         }
00081 
00082         $this->output( "Done.\n" );
00083     }
00084 
00088     protected function populateSearchIndex() {
00089         $res = $this->db->select( 'page', 'MAX(page_id) AS count' );
00090         $s = $this->db->fetchObject( $res );
00091         $count = $s->count;
00092         $this->output( "Rebuilding index fields for {$count} pages...\n" );
00093         $n = 0;
00094 
00095         $fields = array_merge(
00096             Revision::selectPageFields(),
00097             Revision::selectFields(),
00098             Revision::selectTextFields()
00099         );
00100 
00101         while ( $n < $count ) {
00102             if ( $n ) {
00103                 $this->output( $n . "\n" );
00104             }
00105             $end = $n + self::RTI_CHUNK_SIZE - 1;
00106 
00107             $res = $this->db->select( array( 'page', 'revision', 'text' ), $fields,
00108                 array( "page_id BETWEEN $n AND $end", 'page_latest = rev_id', 'rev_text_id = old_id' ),
00109                 __METHOD__
00110             );
00111 
00112             foreach ( $res as $s ) {
00113                 try {
00114                     $title = Title::makeTitle( $s->page_namespace, $s->page_title );
00115 
00116                     $rev = new Revision( $s );
00117                     $content = $rev->getContent();
00118 
00119                     $u = new SearchUpdate( $s->page_id, $title, $content );
00120                     $u->doUpdate();
00121                 } catch ( MWContentSerializationException $ex ) {
00122                     $this->output( "Failed to deserialize content of revision {$s->rev_id} of page "
00123                         . "`" . $title->getPrefixedDBkey() . "`!\n" );
00124                 }
00125             }
00126             $n += self::RTI_CHUNK_SIZE;
00127         }
00128     }
00129 
00133     private function dropMysqlTextIndex() {
00134         $searchindex = $this->db->tableName( 'searchindex' );
00135         if ( $this->db->indexExists( 'searchindex', 'si_title', __METHOD__ ) ) {
00136             $this->output( "Dropping index...\n" );
00137             $sql = "ALTER TABLE $searchindex DROP INDEX si_title, DROP INDEX si_text";
00138             $this->db->query( $sql, __METHOD__ );
00139         }
00140     }
00141 
00145     private function createMysqlTextIndex() {
00146         $searchindex = $this->db->tableName( 'searchindex' );
00147         $this->output( "\nRebuild the index...\n" );
00148         $sql = "ALTER TABLE $searchindex ADD FULLTEXT si_title (si_title), " .
00149             "ADD FULLTEXT si_text (si_text)";
00150         $this->db->query( $sql, __METHOD__ );
00151     }
00152 
00156     private function clearSearchIndex() {
00157         $this->output( 'Clearing searchindex table...' );
00158         $this->db->delete( 'searchindex', '*', __METHOD__ );
00159         $this->output( "Done\n" );
00160     }
00161 }
00162 
00163 $maintClass = "RebuildTextIndex";
00164 require_once RUN_MAINTENANCE_IF_MAIN;