MediaWiki  REL1_24
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         // Shouldn't be needed for Postgres
00054         $this->db = wfGetDB( DB_MASTER );
00055         if ( $this->db->getType() == 'postgres' ) {
00056             $this->error( "This script is not needed when using Postgres.\n", true );
00057         }
00058 
00059         $this->db = wfGetDB( DB_MASTER );
00060         if ( $this->db->getType() == 'sqlite' ) {
00061             if ( !DatabaseSqlite::getFulltextSearchModule() ) {
00062                 $this->error( "Your version of SQLite module for PHP doesn't "
00063                     . "support full-text search (FTS3).\n", true );
00064             }
00065             if ( !$this->db->checkForEnabledSearch() ) {
00066                 $this->error( "Your database schema is not configured for "
00067                     . "full-text search support. Run update.php.\n", true );
00068             }
00069         }
00070 
00071         if ( $this->db->getType() == 'mysql' ) {
00072             $this->dropMysqlTextIndex();
00073             $this->populateSearchIndex();
00074             $this->createMysqlTextIndex();
00075         } else {
00076             $this->clearSearchIndex();
00077             $this->populateSearchIndex();
00078         }
00079 
00080         $this->output( "Done.\n" );
00081     }
00082 
00086     protected function populateSearchIndex() {
00087         $res = $this->db->select( 'page', 'MAX(page_id) AS count' );
00088         $s = $this->db->fetchObject( $res );
00089         $count = $s->count;
00090         $this->output( "Rebuilding index fields for {$count} pages...\n" );
00091         $n = 0;
00092 
00093         $fields = array_merge(
00094             Revision::selectPageFields(),
00095             Revision::selectFields(),
00096             Revision::selectTextFields()
00097         );
00098 
00099         while ( $n < $count ) {
00100             if ( $n ) {
00101                 $this->output( $n . "\n" );
00102             }
00103             $end = $n + self::RTI_CHUNK_SIZE - 1;
00104 
00105             $res = $this->db->select( array( 'page', 'revision', 'text' ), $fields,
00106                 array( "page_id BETWEEN $n AND $end", 'page_latest = rev_id', 'rev_text_id = old_id' ),
00107                 __METHOD__
00108             );
00109 
00110             foreach ( $res as $s ) {
00111                 try {
00112                     $title = Title::makeTitle( $s->page_namespace, $s->page_title );
00113 
00114                     $rev = new Revision( $s );
00115                     $content = $rev->getContent();
00116 
00117                     $u = new SearchUpdate( $s->page_id, $title, $content );
00118                     $u->doUpdate();
00119                 } catch ( MWContentSerializationException $ex ) {
00120                     $this->output( "Failed to deserialize content of revision {$s->rev_id} of page "
00121                         . "`" . $title->getPrefixedDBkey() . "`!\n" );
00122                 }
00123             }
00124             $n += self::RTI_CHUNK_SIZE;
00125         }
00126     }
00127 
00131     private function dropMysqlTextIndex() {
00132         $searchindex = $this->db->tableName( 'searchindex' );
00133         if ( $this->db->indexExists( 'searchindex', 'si_title', __METHOD__ ) ) {
00134             $this->output( "Dropping index...\n" );
00135             $sql = "ALTER TABLE $searchindex DROP INDEX si_title, DROP INDEX si_text";
00136             $this->db->query( $sql, __METHOD__ );
00137         }
00138     }
00139 
00143     private function createMysqlTextIndex() {
00144         $searchindex = $this->db->tableName( 'searchindex' );
00145         $this->output( "\nRebuild the index...\n" );
00146         $sql = "ALTER TABLE $searchindex ADD FULLTEXT si_title (si_title), " .
00147             "ADD FULLTEXT si_text (si_text)";
00148         $this->db->query( $sql, __METHOD__ );
00149     }
00150 
00154     private function clearSearchIndex() {
00155         $this->output( 'Clearing searchindex table...' );
00156         $this->db->delete( 'searchindex', '*', __METHOD__ );
00157         $this->output( "Done\n" );
00158     }
00159 }
00160 
00161 $maintClass = "RebuildTextIndex";
00162 require_once RUN_MAINTENANCE_IF_MAIN;