MediaWiki  master
populateBacklinkNamespace.php
Go to the documentation of this file.
1 <?php
24 require_once __DIR__ . '/Maintenance.php';
25 
27 
34  public function __construct() {
35  parent::__construct();
36  $this->addDescription( 'Populate the *_from_namespace fields' );
37  $this->addOption( 'lastUpdatedId', "Highest page_id with updated links", false, true );
38  }
39 
40  protected function getUpdateKey() {
41  return 'populate *_from_namespace';
42  }
43 
44  protected function updateSkippedMessage() {
45  return '*_from_namespace column of backlink tables already populated.';
46  }
47 
48  public function doDBUpdates() {
49  $db = $this->getDB( DB_PRIMARY );
50 
51  $this->output( "Updating *_from_namespace fields in links tables.\n" );
52 
53  $start = $this->getOption( 'lastUpdatedId' );
54  if ( !$start ) {
55  $start = $db->selectField( 'page', 'MIN(page_id)', '', __METHOD__ );
56  }
57  if ( !$start ) {
58  $this->output( "Nothing to do." );
59  return false;
60  }
61  $end = $db->selectField( 'page', 'MAX(page_id)', '', __METHOD__ );
62  $batchSize = $this->getBatchSize();
63 
64  # Do remaining chunk
65  $end += $batchSize - 1;
66  $blockStart = $start;
67  $blockEnd = $start + $batchSize - 1;
68  $lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
69  while ( $blockEnd <= $end ) {
70  $this->output( "...doing page_id from $blockStart to $blockEnd\n" );
71  $cond = "page_id BETWEEN " . (int)$blockStart . " AND " . (int)$blockEnd;
72  $res = $db->select( 'page', [ 'page_id', 'page_namespace' ], $cond, __METHOD__ );
73  foreach ( $res as $row ) {
74  $db->update( 'pagelinks',
75  [ 'pl_from_namespace' => $row->page_namespace ],
76  [ 'pl_from' => $row->page_id ],
77  __METHOD__
78  );
79  $db->update( 'templatelinks',
80  [ 'tl_from_namespace' => $row->page_namespace ],
81  [ 'tl_from' => $row->page_id ],
82  __METHOD__
83  );
84  $db->update( 'imagelinks',
85  [ 'il_from_namespace' => $row->page_namespace ],
86  [ 'il_from' => $row->page_id ],
87  __METHOD__
88  );
89  }
90  $blockStart += $batchSize - 1;
91  $blockEnd += $batchSize - 1;
92  $lbFactory->waitForReplication();
93  }
94  return true;
95  }
96 }
97 
98 $maintClass = PopulateBacklinkNamespace::class;
99 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.
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.
Service locator for MediaWiki core services.
Maintenance script to populate *_from_namespace fields.
getUpdateKey()
Get the update key name to go in the update log table.
updateSkippedMessage()
Message to show that the update was done already and was just skipped.
const DB_PRIMARY
Definition: defines.php:28