00001 <?php
00027 require_once( dirname(__FILE__) . '/Maintenance.php' );
00028
00029 class RebuildTextIndex extends Maintenance {
00030 const RTI_CHUNK_SIZE = 500;
00031 private $db;
00032
00033 public function __construct() {
00034 parent::__construct();
00035 $this->mDescription = "Rebuild search index table from scratch";
00036 }
00037
00038 public function getDbType() {
00039 return Maintenance::DB_ADMIN;
00040 }
00041
00042 public function execute() {
00043 global $wgTitle, $wgDBtype;
00044
00045
00046 if ( $wgDBtype == 'postgres' ) {
00047 $this->error( "This script is not needed when using Postgres.\n", true );
00048 }
00049
00050 $this->db = wfGetDB( DB_MASTER );
00051 $wgTitle = Title::newFromText( "Rebuild text index script" );
00052
00053 if ( $wgDBtype == 'mysql' ) {
00054 $this->dropMysqlTextIndex();
00055 $this->populateSearchIndex();
00056 $this->createMysqlTextIndex();
00057 } else {
00058 $this->clearSearchIndex();
00059 $this->populateSearchIndex();
00060 }
00061
00062 $this->output( "Done.\n" );
00063 }
00064
00068 protected function populateSearchIndex() {
00069 $res = $this->db->select( 'page', 'MAX(page_id) AS count' );
00070 $s = $this->db->fetchObject($res);
00071 $count = $s->count;
00072 $this->output( "Rebuilding index fields for {$count} pages...\n" );
00073 $n = 0;
00074
00075 while ( $n < $count ) {
00076 $this->output( $n . "\n" );
00077 $end = $n + self::RTI_CHUNK_SIZE - 1;
00078
00079 $res = $this->db->select( array( 'page', 'revision', 'text' ),
00080 array( 'page_id', 'page_namespace', 'page_title', 'old_flags', 'old_text' ),
00081 array( "page_id BETWEEN $n AND $end", 'page_latest = rev_id', 'rev_text_id = old_id' ),
00082 __METHOD__
00083 );
00084
00085 foreach( $res as $s ) {
00086 $revtext = Revision::getRevisionText( $s );
00087 $u = new SearchUpdate( $s->page_id, $s->page_title, $revtext );
00088 $u->doUpdate();
00089 }
00090 $this->db->freeResult( $res );
00091 $n += self::RTI_CHUNK_SIZE;
00092 }
00093 }
00094
00098 private function dropMysqlTextIndex() {
00099 $searchindex = $this->db->tableName( 'searchindex' );
00100 if ( $this->db->indexExists( 'searchindex', 'si_title' ) ) {
00101 $this->output( "Dropping index...\n" );
00102 $sql = "ALTER TABLE $searchindex DROP INDEX si_title, DROP INDEX si_text";
00103 $this->db->query($sql, __METHOD__ );
00104 }
00105 }
00106
00110 private function createMysqlTextIndex() {
00111 $searchindex = $this->db->tableName( 'searchindex' );
00112 $this->output( "\nRebuild the index...\n" );
00113 $sql = "ALTER TABLE $searchindex ADD FULLTEXT si_title (si_title), " .
00114 "ADD FULLTEXT si_text (si_text)";
00115 $this->db->query( $sql, __METHOD__ );
00116 }
00117
00121 private function clearSearchIndex() {
00122 $this->output( 'Clearing searchindex table...' );
00123 $this->db->delete( 'searchindex', '*', __METHOD__ );
00124 $this->output( "Done\n" );
00125 }
00126 }
00127
00128 $maintClass = "RebuildTextIndex";
00129 require_once( DO_MAINTENANCE );