Revert "merged master"
[lhc/web/wiklou.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 * @file
24 * @ingroup Maintenance
25 * @todo document
26 */
27
28 require_once( dirname( __FILE__ ) . '/Maintenance.php' );
29
30 /**
31 * Maintenance script that rebuilds search index table from scratch.
32 *
33 * @ingroup Maintenance
34 */
35 class RebuildTextIndex extends Maintenance {
36 const RTI_CHUNK_SIZE = 500;
37
38 /**
39 * @var DatabaseBase
40 */
41 private $db;
42
43 public function __construct() {
44 parent::__construct();
45 $this->mDescription = "Rebuild search index table from scratch";
46 }
47
48 public function getDbType() {
49 return Maintenance::DB_ADMIN;
50 }
51
52 public function execute() {
53 global $wgTitle;
54
55 // Shouldn't be needed for Postgres
56 $this->db = wfGetDB( DB_MASTER );
57 if ( $this->db->getType() == 'postgres' ) {
58 $this->error( "This script is not needed when using Postgres.\n", true );
59 }
60
61 $this->db = wfGetDB( DB_MASTER );
62 if ( $this->db->getType() == 'sqlite' ) {
63 if ( !DatabaseSqlite::getFulltextSearchModule() ) {
64 $this->error( "Your version of SQLite module for PHP doesn't support full-text search (FTS3).\n", true );
65 }
66 if ( !$this->db->checkForEnabledSearch() ) {
67 $this->error( "Your database schema is not configured for full-text search support. Run update.php.\n", true );
68 }
69 }
70
71 $wgTitle = Title::newFromText( "Rebuild text index script" );
72
73 if ( $this->db->getType() == 'mysql' ) {
74 $this->dropMysqlTextIndex();
75 $this->populateSearchIndex();
76 $this->createMysqlTextIndex();
77 } else {
78 $this->clearSearchIndex();
79 $this->populateSearchIndex();
80 }
81
82 $this->output( "Done.\n" );
83 }
84
85 /**
86 * Populates the search index with content from all pages
87 */
88 protected function populateSearchIndex() {
89 $res = $this->db->select( 'page', 'MAX(page_id) AS count' );
90 $s = $this->db->fetchObject( $res );
91 $count = $s->count;
92 $this->output( "Rebuilding index fields for {$count} pages...\n" );
93 $n = 0;
94
95 while ( $n < $count ) {
96 if ( $n ) {
97 $this->output( $n . "\n" );
98 }
99 $end = $n + self::RTI_CHUNK_SIZE - 1;
100
101 $res = $this->db->select( array( 'page', 'revision', 'text' ),
102 array( 'page_id', 'page_namespace', 'page_title', 'old_flags', 'old_text' ),
103 array( "page_id BETWEEN $n AND $end", 'page_latest = rev_id', 'rev_text_id = old_id' ),
104 __METHOD__
105 );
106
107 foreach ( $res as $s ) {
108 $revtext = Revision::getRevisionText( $s );
109 $u = new SearchUpdate( $s->page_id, $s->page_title, $revtext );
110 $u->doUpdate();
111 }
112 $n += self::RTI_CHUNK_SIZE;
113 }
114 }
115
116 /**
117 * (MySQL only) Drops fulltext index before populating the table.
118 */
119 private function dropMysqlTextIndex() {
120 $searchindex = $this->db->tableName( 'searchindex' );
121 if ( $this->db->indexExists( 'searchindex', 'si_title', __METHOD__ ) ) {
122 $this->output( "Dropping index...\n" );
123 $sql = "ALTER TABLE $searchindex DROP INDEX si_title, DROP INDEX si_text";
124 $this->db->query( $sql, __METHOD__ );
125 }
126 }
127
128 /**
129 * (MySQL only) Adds back fulltext index after populating the table.
130 */
131 private function createMysqlTextIndex() {
132 $searchindex = $this->db->tableName( 'searchindex' );
133 $this->output( "\nRebuild the index...\n" );
134 $sql = "ALTER TABLE $searchindex ADD FULLTEXT si_title (si_title), " .
135 "ADD FULLTEXT si_text (si_text)";
136 $this->db->query( $sql, __METHOD__ );
137 }
138
139 /**
140 * Deletes everything from search index.
141 */
142 private function clearSearchIndex() {
143 $this->output( 'Clearing searchindex table...' );
144 $this->db->delete( 'searchindex', '*', __METHOD__ );
145 $this->output( "Done\n" );
146 }
147 }
148
149 $maintClass = "RebuildTextIndex";
150 require_once( RUN_MAINTENANCE_IF_MAIN );