5 require_once __DIR__ .
'/Maintenance.php';
15 parent::__construct();
17 'Populates normalization column in links tables.'
21 'Table name. Like templatelinks.',
27 'Sleep time (in seconds) between every batch. Default: 0',
35 return __CLASS__ . $this->
getOption(
'table',
'' );
42 if ( !isset( $mapping[$table] ) ) {
43 $this->
output(
"Mapping for this table doesn't exist yet.\n" );
46 $targetColumn = $mapping[$table][
'target_id'];
47 if ( !$dbw->fieldExists( $table, $mapping[$table][
'title'], __METHOD__ ) ) {
48 $this->
output(
"Old fields don't exist. There is no need to run this script\n" );
51 if ( !$dbw->fieldExists( $table, $targetColumn, __METHOD__ ) ) {
52 $this->
output(
"Run update.php to create the $targetColumn column.\n" );
55 if ( !$dbw->tableExists(
'linktarget', __METHOD__ ) ) {
56 $this->
output(
"Run update.php to create the linktarget table.\n" );
60 $this->
output(
"Populating the $targetColumn column\n" );
63 $highestPageId = $dbw->newSelectQueryBuilder()
67 ->caller( __METHOD__ )
68 ->orderBy(
'page_id',
'DESC' )
69 ->fetchResultSet()->fetchRow();
70 if ( !$highestPageId ) {
71 $this->
output(
"Page table is empty.\n" );
74 $highestPageId = $highestPageId[0];
76 while ( $pageId <= $highestPageId ) {
80 $updated += $this->handlePageBatch( $pageId, $mapping, $table );
84 $this->
output(
"Completed normalization of $table, $updated rows updated.\n" );
89 private function handlePageBatch( $lowPageId, $mapping, $table ) {
91 $targetColumn = $mapping[$table][
'target_id'];
92 $pageIdColumn = $mapping[$table][
'page_id'];
94 $highPageId = $lowPageId + $batchSize - 1;
99 $res = $dbw->newSelectQueryBuilder()
100 ->select( [ $mapping[$table][
'ns'], $mapping[$table][
'title'] ] )
103 $targetColumn =>
null,
104 "$pageIdColumn BETWEEN $lowPageId AND $highPageId"
107 ->caller( __METHOD__ )
109 if ( !
$res->numRows() ) {
112 $row =
$res->fetchRow();
113 $ns = $row[$mapping[$table][
'ns']];
114 $titleString = $row[$mapping[$table][
'title']];
116 $this->
output(
"Starting backfill of $ns:$titleString " .
117 "title on pages between $lowPageId and $highPageId\n" );
118 $id = MediaWikiServices::getInstance()->getLinkTargetLookup()->acquireLinkTargetId(
$title, $dbw );
120 $targetColumn =>
null,
121 $mapping[$table][
'ns'] => $ns,
122 $mapping[$table][
'title'] => $titleString,
123 "$pageIdColumn BETWEEN $lowPageId AND $highPageId"
125 $dbw->update( $table, [ $targetColumn => $id ], $conds, __METHOD__ );
126 $updatedInThisBatch = $dbw->affectedRows();
127 $updated += $updatedInThisBatch;
128 $this->
output(
"Updated $updatedInThisBatch rows\n" );
131 $sleep = (int)$this->
getOption(
'sleep', 0 );
142 require_once RUN_MAINTENANCE_IF_MAIN;
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.
getBatchSize()
Returns batch size.
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.
Maintenance script that populates normalization column in links tables.
doDBUpdates()
Do the actual work.
getUpdateKey()
Get the update key name to go in the update log table.
__construct()
Default constructor.
Represents a page (or page fragment) title within MediaWiki.