rebuildtextindex.php 4.7 KB

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