]> scripts.mit.edu Git - autoinstalls/mediawiki.git/blob - maintenance/rebuildtextindex.php
MediaWiki 1.17.0
[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                 if ( $this->db->getType() == 'sqlite' ) {
52                         if ( !DatabaseSqlite::getFulltextSearchModule() ) {
53                                 $this->error( "Your version of SQLite module for PHP doesn't support full-text search (FTS3).\n", true );
54                         }
55                         if ( !$this->db->checkForEnabledSearch() ) {
56                                 $this->error( "Your database schema is not configured for full-text search support. Run update.php.\n", true );
57                         }
58                 }
59
60                 $wgTitle = Title::newFromText( "Rebuild text index script" );
61
62                 if ( $this->db->getType() == 'mysql' ) {
63                         $this->dropMysqlTextIndex();
64                         $this->populateSearchIndex();
65                         $this->createMysqlTextIndex();
66                 } else {
67                         $this->clearSearchIndex();
68                         $this->populateSearchIndex();
69                 }
70
71                 $this->output( "Done.\n" );
72         }
73
74         /**
75          * Populates the search index with content from all pages
76          */
77         protected function populateSearchIndex() {
78                 $res = $this->db->select( 'page', 'MAX(page_id) AS count' );
79                 $s = $this->db->fetchObject( $res );
80                 $count = $s->count;
81                 $this->output( "Rebuilding index fields for {$count} pages...\n" );
82                 $n = 0;
83
84                 while ( $n < $count ) {
85                         if ( $n ) {
86                                 $this->output( $n . "\n" );
87                         }
88                         $end = $n + self::RTI_CHUNK_SIZE - 1;
89
90                         $res = $this->db->select( array( 'page', 'revision', 'text' ),
91                                 array( 'page_id', 'page_namespace', 'page_title', 'old_flags', 'old_text' ),
92                                 array( "page_id BETWEEN $n AND $end", 'page_latest = rev_id', 'rev_text_id = old_id' ),
93                                 __METHOD__
94                                 );
95
96                         foreach ( $res as $s ) {
97                                 $revtext = Revision::getRevisionText( $s );
98                                 $u = new SearchUpdate( $s->page_id, $s->page_title, $revtext );
99                                 $u->doUpdate();
100                         }
101                         $n += self::RTI_CHUNK_SIZE;
102                 }
103         }
104
105         /**
106          * (MySQL only) Drops fulltext index before populating the table.
107          */
108         private function dropMysqlTextIndex() {
109                 $searchindex = $this->db->tableName( 'searchindex' );
110                 if ( $this->db->indexExists( 'searchindex', 'si_title' ) ) {
111                         $this->output( "Dropping index...\n" );
112                         $sql = "ALTER TABLE $searchindex DROP INDEX si_title, DROP INDEX si_text";
113                         $this->db->query( $sql, __METHOD__ );
114                 }
115         }
116
117         /**
118          * (MySQL only) Adds back fulltext index after populating the table.
119          */
120         private function createMysqlTextIndex() {
121                 $searchindex = $this->db->tableName( 'searchindex' );
122                 $this->output( "\nRebuild the index...\n" );
123                 $sql = "ALTER TABLE $searchindex ADD FULLTEXT si_title (si_title), " .
124                   "ADD FULLTEXT si_text (si_text)";
125                 $this->db->query( $sql, __METHOD__ );
126         }
127
128         /**
129          * Deletes everything from search index.
130          */
131         private function clearSearchIndex() {
132                 $this->output( 'Clearing searchindex table...' );
133                 $this->db->delete( 'searchindex', '*', __METHOD__ );
134                 $this->output( "Done\n" );
135         }
136 }
137
138 $maintClass = "RebuildTextIndex";
139 require_once( RUN_MAINTENANCE_IF_MAIN );