Fixing a variable name, and a PHP warning.
[mediawiki.git] / maintenance / rebuildtextindex.inc
blobef65eeda6ce9f59425f964833fad4f418e207874
1 <?php
2 require_once 'counter.php';
3 /**
4  * Rebuild the fulltext search indexes. This may take a while
5  * depending on the database size and server configuration.
6  *
7  * Rebuilding is faster if you drop the index and recreate it,
8  * but that will prevent searches from working while it runs.
9  *
10  * @todo document
11  * @addtogroup Maintenance
12  */
14 /** */
15 define( "RTI_CHUNK_SIZE", 500 );
17 function dropTextIndex( &$database )
19         $searchindex = $database->tableName( 'searchindex' );
20         if ( $database->indexExists( "searchindex", "si_title" ) ) {
21                 echo "Dropping index...\n";
22                 $sql = "ALTER TABLE $searchindex DROP INDEX si_title, DROP INDEX si_text";
23                 $database->query($sql, "dropTextIndex" );
24         }
27 function createTextIndex( &$database )
29         $searchindex = $database->tableName( 'searchindex' );
30         echo "\nRebuild the index...\n";
31         $sql = "ALTER TABLE $searchindex ADD FULLTEXT si_title (si_title), " .
32           "ADD FULLTEXT si_text (si_text)";
33         $database->query($sql, "createTextIndex" );
36 function rebuildTextIndex( &$database )
38         list ($page, $revision, $text, $searchindex) = $database->tableNamesN( 'page', 'revision', 'text', 'searchindex' );
40         $sql = "SELECT MAX(page_id) AS count FROM $page";
41         $res = $database->query($sql, "rebuildTextIndex" );
42         $s = $database->fetchObject($res);
43         $count = $s->count;
44         echo "Rebuilding index fields for {$count} pages...\n";
45         $n = 0;
47         while ( $n < $count ) {
48                 print_c( $n - 1, $n);
49                 $end = $n + RTI_CHUNK_SIZE - 1;
50                 $sql = "SELECT page_id, page_namespace, page_title, old_flags, old_text
51                           FROM $page, $revision, $text
52                          WHERE page_id BETWEEN $n AND $end
53                            AND page_latest=rev_id
54                            AND rev_text_id=old_id";
55                 $res = $database->query($sql, "rebuildTextIndex" );
57                 while( $s = $database->fetchObject($res) ) {
58                         $revtext = Revision::getRevisionText( $s );
59                         $u = new SearchUpdate( $s->page_id, $s->page_title, $revtext );
60                         $u->doUpdate();
61                 }
62                 $database->freeResult( $res );
63                 $n += RTI_CHUNK_SIZE;
64         }