MediaWiki  REL1_21
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                                         $text = $content->getTextForSearchIndex();
00119 
00120                                         $u = new SearchUpdate( $s->page_id, $title, $text );
00121                                         $u->doUpdate();
00122                                 } catch ( MWContentSerializationException $ex ) {
00123                                         $this->output( "Failed to deserialize content of revision {$s->rev_id} of page "
00124                                                 . "`" . $title->getPrefixedDBkey() . "`!\n" );
00125                                 }
00126                         }
00127                         $n += self::RTI_CHUNK_SIZE;
00128                 }
00129         }
00130 
00134         private function dropMysqlTextIndex() {
00135                 $searchindex = $this->db->tableName( 'searchindex' );
00136                 if ( $this->db->indexExists( 'searchindex', 'si_title', __METHOD__ ) ) {
00137                         $this->output( "Dropping index...\n" );
00138                         $sql = "ALTER TABLE $searchindex DROP INDEX si_title, DROP INDEX si_text";
00139                         $this->db->query( $sql, __METHOD__ );
00140                 }
00141         }
00142 
00146         private function createMysqlTextIndex() {
00147                 $searchindex = $this->db->tableName( 'searchindex' );
00148                 $this->output( "\nRebuild the index...\n" );
00149                 $sql = "ALTER TABLE $searchindex ADD FULLTEXT si_title (si_title), " .
00150                   "ADD FULLTEXT si_text (si_text)";
00151                 $this->db->query( $sql, __METHOD__ );
00152         }
00153 
00157         private function clearSearchIndex() {
00158                 $this->output( 'Clearing searchindex table...' );
00159                 $this->db->delete( 'searchindex', '*', __METHOD__ );
00160                 $this->output( "Done\n" );
00161         }
00162 }
00163 
00164 $maintClass = "RebuildTextIndex";
00165 require_once( RUN_MAINTENANCE_IF_MAIN );