MediaWiki  REL1_20
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                 while ( $n < $count ) {
00096                         if ( $n ) {
00097                                 $this->output( $n . "\n" );
00098                         }
00099                         $end = $n + self::RTI_CHUNK_SIZE - 1;
00100 
00101                         $res = $this->db->select( array( 'page', 'revision', 'text' ),
00102                                 array( 'page_id', 'page_namespace', 'page_title', 'old_flags', 'old_text' ),
00103                                 array( "page_id BETWEEN $n AND $end", 'page_latest = rev_id', 'rev_text_id = old_id' ),
00104                                 __METHOD__
00105                                 );
00106 
00107                         foreach ( $res as $s ) {
00108                                 $revtext = Revision::getRevisionText( $s );
00109                                 $u = new SearchUpdate( $s->page_id, $s->page_title, $revtext );
00110                                 $u->doUpdate();
00111                         }
00112                         $n += self::RTI_CHUNK_SIZE;
00113                 }
00114         }
00115 
00119         private function dropMysqlTextIndex() {
00120                 $searchindex = $this->db->tableName( 'searchindex' );
00121                 if ( $this->db->indexExists( 'searchindex', 'si_title', __METHOD__ ) ) {
00122                         $this->output( "Dropping index...\n" );
00123                         $sql = "ALTER TABLE $searchindex DROP INDEX si_title, DROP INDEX si_text";
00124                         $this->db->query( $sql, __METHOD__ );
00125                 }
00126         }
00127 
00131         private function createMysqlTextIndex() {
00132                 $searchindex = $this->db->tableName( 'searchindex' );
00133                 $this->output( "\nRebuild the index...\n" );
00134                 $sql = "ALTER TABLE $searchindex ADD FULLTEXT si_title (si_title), " .
00135                   "ADD FULLTEXT si_text (si_text)";
00136                 $this->db->query( $sql, __METHOD__ );
00137         }
00138 
00142         private function clearSearchIndex() {
00143                 $this->output( 'Clearing searchindex table...' );
00144                 $this->db->delete( 'searchindex', '*', __METHOD__ );
00145                 $this->output( "Done\n" );
00146         }
00147 }
00148 
00149 $maintClass = "RebuildTextIndex";
00150 require_once( RUN_MAINTENANCE_IF_MAIN );