17 parent::__construct();
19 'Populates normalization column in links tables.'
23 'Table name. Like pagelinks.',
29 'Sleep time (in seconds) between every batch. Default: 0',
37 return __CLASS__ . $this->
getOption(
'table',
'' );
42 $mapping = \MediaWiki\Linker\LinksMigration::$mapping;
44 if ( !isset( $mapping[$table] ) ) {
45 $this->
output(
"Mapping for this table doesn't exist yet.\n" );
48 $targetColumn = $mapping[$table][
'target_id'];
49 if ( !$dbw->fieldExists( $table, $mapping[$table][
'title'], __METHOD__ ) ) {
50 $this->
output(
"Old fields don't exist. There is no need to run this script\n" );
53 if ( !$dbw->fieldExists( $table, $targetColumn, __METHOD__ ) ) {
54 $this->
output(
"Run update.php to create the $targetColumn column.\n" );
57 if ( !$dbw->tableExists(
'linktarget', __METHOD__ ) ) {
58 $this->
output(
"Run update.php to create the linktarget table.\n" );
62 $this->
output(
"Populating the $targetColumn column\n" );
65 $highestPageId = $dbw->newSelectQueryBuilder()
69 ->caller( __METHOD__ )
70 ->orderBy(
'page_id',
'DESC' )
71 ->fetchResultSet()->fetchRow();
72 if ( !$highestPageId ) {
73 $this->
output(
"Page table is empty.\n" );
76 $highestPageId = $highestPageId[0];
78 while ( $pageId <= $highestPageId ) {
82 $updated += $this->handlePageBatch( $pageId, $mapping, $table );
86 $this->
output(
"Completed normalization of $table, $updated rows updated.\n" );
91 private function handlePageBatch( $lowPageId, $mapping, $table ) {
92 $batchSize = $this->getBatchSize();
93 $targetColumn = $mapping[$table][
'target_id'];
94 $pageIdColumn = $mapping[$table][
'page_id'];
96 $highPageId = $lowPageId + $batchSize - 1;
97 $dbw = $this->getPrimaryDB();
101 $res = $dbw->newSelectQueryBuilder()
102 ->select( [ $mapping[$table][
'ns'], $mapping[$table][
'title'] ] )
105 $targetColumn => [
null, 0 ],
106 $dbw->expr( $pageIdColumn,
'>=', $lowPageId ),
107 $dbw->expr( $pageIdColumn,
'<=', $highPageId ),
110 ->caller( __METHOD__ )
112 if ( !$res->numRows() ) {
115 $row = $res->fetchRow();
116 $ns = $row[$mapping[$table][
'ns']];
117 $titleString = $row[$mapping[$table][
'title']];
118 $title =
new TitleValue( (
int)$ns, $titleString );
119 $this->
output(
"Starting backfill of $ns:$titleString " .
120 "title on pages between $lowPageId and $highPageId\n" );
121 $id = $this->
getServiceContainer()->getLinkTargetLookup()->acquireLinkTargetId( $title, $dbw );
122 $dbw->newUpdateQueryBuilder()
124 ->set( [ $targetColumn => $id ] )
126 $targetColumn => [
null, 0 ],
127 $mapping[$table][
'ns'] => $ns,
128 $mapping[$table][
'title'] => $titleString,
129 $dbw->expr( $pageIdColumn,
'>=', $lowPageId ),
130 $dbw->expr( $pageIdColumn,
'<=', $highPageId ),
132 ->caller( __METHOD__ )->execute();
133 $updatedInThisBatch = $dbw->affectedRows();
134 $updated += $updatedInThisBatch;
135 $this->
output(
"Updated $updatedInThisBatch rows\n" );
138 $sleep = (int)$this->
getOption(
'sleep', 0 );
Class for scripts that perform database maintenance and want to log the update in updatelog so we can...
getDB( $db, $groups=[], $dbDomain=false)
Returns a database to be used by current maintenance script.
output( $out, $channel=null)
Throw some output to the user.
waitForReplication()
Wait for replica DBs to catch up.
getServiceContainer()
Returns the main service container.
addDescription( $text)
Set the description text.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
getOption( $name, $default=null)
Get an option, or return the default.