| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
require_once __DIR__ . '/Maintenance.php'; |
| 30 |
|
| 31 |
|
| 32 |
use MediaWiki\Deferred\SearchUpdate; |
| 33 |
use MediaWiki\Revision\SlotRecord; |
| 34 |
use MediaWiki\Title\Title; |
| 35 |
use Wikimedia\Rdbms\DatabaseSqlite; |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
class RebuildTextIndex extends Maintenance { |
| 43 |
private const RTI_CHUNK_SIZE = 500; |
| 44 |
|
| 45 |
public function __construct() { |
| 46 |
parent::__construct(); |
| 47 |
$this->addDescription( 'Rebuild search index table from scratch' ); |
| 48 |
} |
| 49 |
|
| 50 |
public function getDbType() { |
| 51 |
return Maintenance::DB_ADMIN; |
| 52 |
} |
| 53 |
|
| 54 |
public function execute() { |
| 55 |
|
| 56 |
$dbw = $this->getPrimaryDB(); |
| 57 |
if ( $dbw->getType() == 'postgres' ) { |
| 58 |
$this->fatalError( "This script is not needed when using Postgres.\n" ); |
| 59 |
} |
| 60 |
|
| 61 |
if ( $dbw->getType() == 'sqlite' ) { |
| 62 |
if ( !DatabaseSqlite::getFulltextSearchModule() ) { |
| 63 |
$this->fatalError( "Your version of SQLite module for PHP doesn't " |
| 64 |
. "support full-text search (FTS3).\n" ); |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
if ( $dbw->getType() == 'mysql' ) { |
| 69 |
$this->dropMysqlTextIndex(); |
| 70 |
$this->clearSearchIndex(); |
| 71 |
$this->populateSearchIndex(); |
| 72 |
$this->createMysqlTextIndex(); |
| 73 |
} else { |
| 74 |
$this->clearSearchIndex(); |
| 75 |
$this->populateSearchIndex(); |
| 76 |
} |
| 77 |
|
| 78 |
$this->output( "Done.\n" ); |
| 79 |
} |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
protected function populateSearchIndex() { |
| 85 |
$dbw = $this->getPrimaryDB(); |
| 86 |
$res = $dbw->newSelectQueryBuilder() |
| 87 |
->select( [ 'count' => 'MAX(page_id)' ] ) |
| 88 |
->from( 'page' ) |
| 89 |
->caller( __METHOD__ )->fetchResultSet(); |
| 90 |
$s = $res->fetchObject(); |
| 91 |
$count = $s->count; |
| 92 |
$this->output( "Rebuilding index fields for {$count} pages...\n" ); |
| 93 |
$n = 0; |
| 94 |
|
| 95 |
$revStore = $this->getServiceContainer()->getRevisionStore(); |
| 96 |
$queryBuilderTemplate = $revStore->newSelectQueryBuilder( $dbw ) |
| 97 |
->joinPage() |
| 98 |
->joinComment(); |
| 99 |
|
| 100 |
while ( $n < $count ) { |
| 101 |
if ( $n ) { |
| 102 |
$this->output( $n . "\n" ); |
| 103 |
} |
| 104 |
$end = $n + self::RTI_CHUNK_SIZE - 1; |
| 105 |
$queryBuilder = clone $queryBuilderTemplate; |
| 106 |
$res = $queryBuilder->where( [ |
| 107 |
$dbw->expr( 'page_id', '>=', $n )->and( 'page_id', '<=', $end ), |
| 108 |
'page_latest = rev_id' |
| 109 |
] )->caller( __METHOD__ )->fetchResultSet(); |
| 110 |
|
| 111 |
foreach ( $res as $s ) { |
| 112 |
|
| 113 |
|
| 114 |
if ( $s->page_namespace < 0 ) { |
| 115 |
continue; |
| 116 |
} |
| 117 |
|
| 118 |
$title = Title::makeTitle( $s->page_namespace, $s->page_title ); |
| 119 |
try { |
| 120 |
$revRecord = $revStore->newRevisionFromRow( $s ); |
| 121 |
$content = $revRecord->getContent( SlotRecord::MAIN ); |
| 122 |
|
| 123 |
$u = new SearchUpdate( $s->page_id, $title, $content ); |
| 124 |
$u->doUpdate(); |
| 125 |
} catch ( MWContentSerializationException $ex ) { |
| 126 |
$this->output( "Failed to deserialize content of revision {$s->rev_id} of page " |
| 127 |
. "`" . $title->getPrefixedDBkey() . "`!\n" ); |
| 128 |
} |
| 129 |
} |
| 130 |
$n += self::RTI_CHUNK_SIZE; |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
private function dropMysqlTextIndex() { |
| 138 |
$dbw = $this->getDB( DB_PRIMARY ); |
| 139 |
$searchindex = $dbw->tableName( 'searchindex' ); |
| 140 |
if ( $dbw->indexExists( 'searchindex', 'si_title', __METHOD__ ) ) { |
| 141 |
$this->output( "Dropping index...\n" ); |
| 142 |
$sql = "ALTER TABLE $searchindex DROP INDEX si_title, DROP INDEX si_text"; |
| 143 |
$dbw->query( $sql, __METHOD__ ); |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
private function createMysqlTextIndex() { |
| 151 |
$dbw = $this->getPrimaryDB(); |
| 152 |
$searchindex = $dbw->tableName( 'searchindex' ); |
| 153 |
$this->output( "\nRebuild the index...\n" ); |
| 154 |
foreach ( [ 'si_title', 'si_text' ] as $field ) { |
| 155 |
$sql = "ALTER TABLE $searchindex ADD FULLTEXT $field ($field)"; |
| 156 |
$dbw->query( $sql, __METHOD__ ); |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
private function clearSearchIndex() { |
| 164 |
$dbw = $this->getPrimaryDB(); |
| 165 |
$this->output( 'Clearing searchindex table...' ); |
| 166 |
$dbw->newDeleteQueryBuilder() |
| 167 |
->deleteFrom( 'searchindex' ) |
| 168 |
->where( '*' ) |
| 169 |
->caller( __METHOD__ )->execute(); |
| 170 |
$this->output( "Done\n" ); |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
|
| 175 |
$maintClass = RebuildTextIndex::class; |
| 176 |
require_once RUN_MAINTENANCE_IF_MAIN; |
| 177 |
|