]> scripts.mit.edu Git - autoinstalls/mediawiki.git/blob - maintenance/rebuildtextindex.php
MediaWiki 1.16.0-scripts
[autoinstalls/mediawiki.git] / maintenance / rebuildtextindex.php
1 <?php
2 /**
3  * Rebuild search index table from scratch.  This may take several
4  * hours, depending on the database size and server configuration.
5  *
6  * Postgres is trigger-based and should never need rebuilding.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License along
19  * with this program; if not, write to the Free Software Foundation, Inc.,
20  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21  * http://www.gnu.org/copyleft/gpl.html
22  *
23  * @ingroup Maintenance
24  * @todo document
25  */
26
27 require_once( dirname(__FILE__) . '/Maintenance.php' );
28
29 class RebuildTextIndex extends Maintenance {
30         const RTI_CHUNK_SIZE = 500;
31         private $db;
32
33         public function __construct() {
34                 parent::__construct();
35                 $this->mDescription = "Rebuild search index table from scratch";
36         }
37
38         public function getDbType() {
39                 return Maintenance::DB_ADMIN;
40         }
41
42         public function execute() {
43                 global $wgTitle, $wgDBtype;
44
45                 // Shouldn't be needed for Postgres
46                 if ( $wgDBtype == 'postgres' ) {
47                         $this->error( "This script is not needed when using Postgres.\n", true );
48                 }
49         
50                 $this->db = wfGetDB( DB_MASTER );
51                 $wgTitle = Title::newFromText( "Rebuild text index script" );
52         
53                 if ( $wgDBtype == 'mysql' ) {
54                         $this->dropMysqlTextIndex();
55                         $this->populateSearchIndex();
56                         $this->createMysqlTextIndex();
57                 } else {
58                         $this->clearSearchIndex();
59                         $this->populateSearchIndex();
60                 }
61         
62                 $this->output( "Done.\n" );
63         }
64
65         /**
66          * Populates the search index with content from all pages
67          */
68         protected function populateSearchIndex() {
69                 $res = $this->db->select( 'page', 'MAX(page_id) AS count' );
70                 $s = $this->db->fetchObject($res);
71                 $count = $s->count;
72                 $this->output( "Rebuilding index fields for {$count} pages...\n" );
73                 $n = 0;
74         
75                 while ( $n < $count ) {
76                         $this->output( $n . "\n" );
77                         $end = $n + self::RTI_CHUNK_SIZE - 1;
78
79                         $res = $this->db->select( array( 'page', 'revision', 'text' ), 
80                                 array( 'page_id', 'page_namespace', 'page_title', 'old_flags', 'old_text' ),
81                                 array( "page_id BETWEEN $n AND $end", 'page_latest = rev_id', 'rev_text_id = old_id' ),
82                                 __METHOD__
83                                 );
84         
85                         foreach( $res as $s ) {
86                                 $revtext = Revision::getRevisionText( $s );
87                                 $u = new SearchUpdate( $s->page_id, $s->page_title, $revtext );
88                                 $u->doUpdate();
89                         }
90                         $this->db->freeResult( $res );
91                         $n += self::RTI_CHUNK_SIZE;
92                 }
93         }
94
95         /**
96          * (MySQL only) Drops fulltext index before populating the table.
97          */
98         private function dropMysqlTextIndex() {
99                 $searchindex = $this->db->tableName( 'searchindex' );
100                 if ( $this->db->indexExists( 'searchindex', 'si_title' ) ) {
101                         $this->output( "Dropping index...\n" );
102                         $sql = "ALTER TABLE $searchindex DROP INDEX si_title, DROP INDEX si_text";
103                         $this->db->query($sql, __METHOD__ );
104                 }
105         }
106
107         /**
108          * (MySQL only) Adds back fulltext index after populating the table.
109          */
110         private function createMysqlTextIndex() {
111                 $searchindex = $this->db->tableName( 'searchindex' );
112                 $this->output( "\nRebuild the index...\n" );
113                 $sql = "ALTER TABLE $searchindex ADD FULLTEXT si_title (si_title), " .
114                   "ADD FULLTEXT si_text (si_text)";
115                 $this->db->query( $sql, __METHOD__ );
116         }
117
118         /**
119          * Deletes everything from search index.
120          */
121         private function clearSearchIndex() {
122                 $this->output( 'Clearing searchindex table...' );
123                 $this->db->delete( 'searchindex', '*', __METHOD__ );
124                 $this->output( "Done\n" );
125         }
126 }
127
128 $maintClass = "RebuildTextIndex";
129 require_once( DO_MAINTENANCE );