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_MASTER );
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;
RUN_MAINTENANCE_IF_MAIN
const RUN_MAINTENANCE_IF_MAIN
Definition: Maintenance.php:38
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:154
PopulateBacklinkNamespace
Maintenance script to populate *_from_namespace fields.
Definition: populateBacklinkNamespace.php:33
Maintenance\addDescription
addDescription( $text)
Set the description text.
Definition: Maintenance.php:327
$res
$res
Definition: testCompression.php:57
LoggedUpdateMaintenance
Class for scripts that perform database maintenance and want to log the update in updatelog so we can...
Definition: LoggedUpdateMaintenance.php:26
Maintenance\addOption
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
Definition: Maintenance.php:245
DB_MASTER
const DB_MASTER
Definition: defines.php:26
PopulateBacklinkNamespace\updateSkippedMessage
updateSkippedMessage()
Message to show that the update was done already and was just skipped.
Definition: populateBacklinkNamespace.php:44
PopulateBacklinkNamespace\__construct
__construct()
Default constructor.
Definition: populateBacklinkNamespace.php:34
Maintenance\getDB
getDB( $db, $groups=[], $dbDomain=false)
Returns a database to be used by current maintenance script.
Definition: Maintenance.php:1366
Maintenance\getOption
getOption( $name, $default=null)
Get an option, or return the default.
Definition: Maintenance.php:281
Maintenance\getBatchSize
getBatchSize()
Returns batch size.
Definition: Maintenance.php:366
Maintenance\output
output( $out, $channel=null)
Throw some output to the user.
Definition: Maintenance.php:434
PopulateBacklinkNamespace\doDBUpdates
doDBUpdates()
Do the actual work.
Definition: populateBacklinkNamespace.php:48
$maintClass
$maintClass
Definition: populateBacklinkNamespace.php:98
PopulateBacklinkNamespace\getUpdateKey
getUpdateKey()
Get the update key name to go in the update log table.
Definition: populateBacklinkNamespace.php:40