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