MediaWiki  REL1_19
rebuildtextindex.php
Go to the documentation of this file.
00001 <?php
00027 require_once( dirname( __FILE__ ) . '/Maintenance.php' );
00028 
00029 class RebuildTextIndex extends Maintenance {
00030         const RTI_CHUNK_SIZE = 500;
00031 
00035         private $db;
00036 
00037         public function __construct() {
00038                 parent::__construct();
00039                 $this->mDescription = "Rebuild search index table from scratch";
00040         }
00041 
00042         public function getDbType() {
00043                 return Maintenance::DB_ADMIN;
00044         }
00045 
00046         public function execute() {
00047                 global $wgTitle;
00048 
00049                 // Shouldn't be needed for Postgres
00050                 $this->db = wfGetDB( DB_MASTER );
00051                 if ( $this->db->getType() == 'postgres' ) {
00052                         $this->error( "This script is not needed when using Postgres.\n", true );
00053                 }
00054 
00055                 $this->db = wfGetDB( DB_MASTER );
00056                 if ( $this->db->getType() == 'sqlite' ) {
00057                         if ( !DatabaseSqlite::getFulltextSearchModule() ) {
00058                                 $this->error( "Your version of SQLite module for PHP doesn't support full-text search (FTS3).\n", true );
00059                         }
00060                         if ( !$this->db->checkForEnabledSearch() ) {
00061                                 $this->error( "Your database schema is not configured for full-text search support. Run update.php.\n", true );
00062                         }
00063                 }
00064 
00065                 $wgTitle = Title::newFromText( "Rebuild text index script" );
00066 
00067                 if ( $this->db->getType() == 'mysql' ) {
00068                         $this->dropMysqlTextIndex();
00069                         $this->populateSearchIndex();
00070                         $this->createMysqlTextIndex();
00071                 } else {
00072                         $this->clearSearchIndex();
00073                         $this->populateSearchIndex();
00074                 }
00075 
00076                 $this->output( "Done.\n" );
00077         }
00078 
00082         protected function populateSearchIndex() {
00083                 $res = $this->db->select( 'page', 'MAX(page_id) AS count' );
00084                 $s = $this->db->fetchObject( $res );
00085                 $count = $s->count;
00086                 $this->output( "Rebuilding index fields for {$count} pages...\n" );
00087                 $n = 0;
00088 
00089                 while ( $n < $count ) {
00090                         if ( $n ) {
00091                                 $this->output( $n . "\n" );
00092                         }
00093                         $end = $n + self::RTI_CHUNK_SIZE - 1;
00094 
00095                         $res = $this->db->select( array( 'page', 'revision', 'text' ),
00096                                 array( 'page_id', 'page_namespace', 'page_title', 'old_flags', 'old_text' ),
00097                                 array( "page_id BETWEEN $n AND $end", 'page_latest = rev_id', 'rev_text_id = old_id' ),
00098                                 __METHOD__
00099                                 );
00100 
00101                         foreach ( $res as $s ) {
00102                                 $revtext = Revision::getRevisionText( $s );
00103                                 $u = new SearchUpdate( $s->page_id, $s->page_title, $revtext );
00104                                 $u->doUpdate();
00105                         }
00106                         $n += self::RTI_CHUNK_SIZE;
00107                 }
00108         }
00109 
00113         private function dropMysqlTextIndex() {
00114                 $searchindex = $this->db->tableName( 'searchindex' );
00115                 if ( $this->db->indexExists( 'searchindex', 'si_title', __METHOD__ ) ) {
00116                         $this->output( "Dropping index...\n" );
00117                         $sql = "ALTER TABLE $searchindex DROP INDEX si_title, DROP INDEX si_text";
00118                         $this->db->query( $sql, __METHOD__ );
00119                 }
00120         }
00121 
00125         private function createMysqlTextIndex() {
00126                 $searchindex = $this->db->tableName( 'searchindex' );
00127                 $this->output( "\nRebuild the index...\n" );
00128                 $sql = "ALTER TABLE $searchindex ADD FULLTEXT si_title (si_title), " .
00129                   "ADD FULLTEXT si_text (si_text)";
00130                 $this->db->query( $sql, __METHOD__ );
00131         }
00132 
00136         private function clearSearchIndex() {
00137                 $this->output( 'Clearing searchindex table...' );
00138                 $this->db->delete( 'searchindex', '*', __METHOD__ );
00139                 $this->output( "Done\n" );
00140         }
00141 }
00142 
00143 $maintClass = "RebuildTextIndex";
00144 require_once( RUN_MAINTENANCE_IF_MAIN );