doMaintenance.php -> DO_MAINTENANCE
[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( dirname(__FILE__) . '/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 protected function getDbType() {
40 return Maintenance::DB_ADMIN;
41 }
42
43 public function execute() {
44 global $wgTitle;
45
46 // Only do this for MySQL
47 $database = wfGetDB( DB_MASTER );
48 if( !$database instanceof DatabaseMysql ) {
49 $this->error( "This script is only for MySQL.", true );
50 }
51
52 $wgTitle = Title::newFromText( "Rebuild text index script" );
53
54 $this->dropTextIndex( $database );
55 $this->doRebuildTextIndex( $database );
56 $this->createTextIndex( $database );
57
58 $this->output( "Done.\n" );
59 }
60
61 private function dropTextIndex( &$database ) {
62 $searchindex = $database->tableName( 'searchindex' );
63 if ( $database->indexExists( "searchindex", "si_title" ) ) {
64 $this->output( "Dropping index...\n" );
65 $sql = "ALTER TABLE $searchindex DROP INDEX si_title, DROP INDEX si_text";
66 $database->query($sql, "dropTextIndex" );
67 }
68 }
69
70 private function createTextIndex( &$database ) {
71 $searchindex = $database->tableName( 'searchindex' );
72 $this->output( "\nRebuild the index...\n" );
73 $sql = "ALTER TABLE $searchindex ADD FULLTEXT si_title (si_title), " .
74 "ADD FULLTEXT si_text (si_text)";
75 $database->query($sql, "createTextIndex" );
76 }
77
78 private function doRebuildTextIndex( &$database ) {
79 list ($page, $revision, $text, $searchindex) = $database->tableNamesN( 'page', 'revision', 'text', 'searchindex' );
80
81 $sql = "SELECT MAX(page_id) AS count FROM $page";
82 $res = $database->query($sql, "rebuildTextIndex" );
83 $s = $database->fetchObject($res);
84 $count = $s->count;
85 $this->output( "Rebuilding index fields for {$count} pages...\n" );
86 $n = 0;
87
88 while ( $n < $count ) {
89 $this->output( $n . "\n" );
90 $end = $n + self::RTI_CHUNK_SIZE - 1;
91 $sql = "SELECT page_id, page_namespace, page_title, old_flags, old_text
92 FROM $page, $revision, $text
93 WHERE page_id BETWEEN $n AND $end
94 AND page_latest=rev_id
95 AND rev_text_id=old_id";
96 $res = $database->query($sql, "rebuildTextIndex" );
97
98 foreach( $res as $s ) {
99 $revtext = Revision::getRevisionText( $s );
100 $u = new SearchUpdate( $s->page_id, $s->page_title, $revtext );
101 $u->doUpdate();
102 }
103 $database->freeResult( $res );
104 $n += self::RTI_CHUNK_SIZE;
105 }
106 }
107 }
108
109 $maintClass = "RebuildTextIndex";
110 require_once( DO_MAINTENANCE );