Merge maintenance-work branch (now with less errors!):
[lhc/web/wiklou.git] / maintenance / rebuildtextindex.php
1 <?php
2 /**
3 * Rebuild search index table from scratch. This takes several
4 * hours, depending on the database size and server configuration.
5 *
6 * This is only for MySQL (see bug 9905).
7 * Postgres is trigger-based and should never need rebuilding.
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 * http://www.gnu.org/copyleft/gpl.html
23 *
24 * @ingroup Maintenance
25 * @todo document
26 */
27
28 require_once( "Maintenance.php" );
29
30 class RebuildTextIndex extends Maintenance {
31
32 const RTI_CHUNK_SIZE = 500;
33
34 public function __construct() {
35 parent::__construct();
36 $this->mDescription = "Rebuild search index table from scratch";
37 }
38
39 public function execute() {
40 global $wgTitle;
41
42 // Only do this for MySQL
43 $database = wfGetDB( DB_MASTER );
44 if( !$database instanceof DatabaseMysql ) {
45 $this->error( "This script is only for MySQL.\n", true );
46 }
47
48 $wgTitle = Title::newFromText( "Rebuild text index script" );
49
50 $this->dropTextIndex( $database );
51 $this->doRebuildTextIndex( $database );
52 $this->createTextIndex( $database );
53
54 $this->output( "Done.\n" );
55 }
56
57 private function dropTextIndex( &$database ) {
58 $searchindex = $database->tableName( 'searchindex' );
59 if ( $database->indexExists( "searchindex", "si_title" ) ) {
60 $this->output( "Dropping index...\n" );
61 $sql = "ALTER TABLE $searchindex DROP INDEX si_title, DROP INDEX si_text";
62 $database->query($sql, "dropTextIndex" );
63 }
64 }
65
66 private function createTextIndex( &$database ) {
67 $searchindex = $database->tableName( 'searchindex' );
68 $this->output( "\nRebuild the index...\n" );
69 $sql = "ALTER TABLE $searchindex ADD FULLTEXT si_title (si_title), " .
70 "ADD FULLTEXT si_text (si_text)";
71 $database->query($sql, "createTextIndex" );
72 }
73
74 private function doRebuildTextIndex( &$database ) {
75 list ($page, $revision, $text, $searchindex) = $database->tableNamesN( 'page', 'revision', 'text', 'searchindex' );
76
77 $sql = "SELECT MAX(page_id) AS count FROM $page";
78 $res = $database->query($sql, "rebuildTextIndex" );
79 $s = $database->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 $this->output( $n . "\n" );
86 $end = $n + self::RTI_CHUNK_SIZE - 1;
87 $sql = "SELECT page_id, page_namespace, page_title, old_flags, old_text
88 FROM $page, $revision, $text
89 WHERE page_id BETWEEN $n AND $end
90 AND page_latest=rev_id
91 AND rev_text_id=old_id";
92 $res = $database->query($sql, "rebuildTextIndex" );
93
94 while( $s = $database->fetchObject($res) ) {
95 $revtext = Revision::getRevisionText( $s );
96 $u = new SearchUpdate( $s->page_id, $s->page_title, $revtext );
97 $u->doUpdate();
98 }
99 $database->freeResult( $res );
100 $n += self::RTI_CHUNK_SIZE;
101 }
102 }
103 }
104
105 $maintClass = "RebuildTextIndex";
106 require_once( DO_MAINTENANCE );