MediaWiki  1.34.0
populateBacklinkNamespace.php
Go to the documentation of this file.
1 <?php
24 require_once __DIR__ . '/Maintenance.php';
25 
32  public function __construct() {
33  parent::__construct();
34  $this->addDescription( 'Populate the *_from_namespace fields' );
35  $this->addOption( 'lastUpdatedId', "Highest page_id with updated links", false, true );
36  }
37 
38  protected function getUpdateKey() {
39  return 'populate *_from_namespace';
40  }
41 
42  protected function updateSkippedMessage() {
43  return '*_from_namespace column of backlink tables already populated.';
44  }
45 
46  public function doDBUpdates() {
47  $db = $this->getDB( DB_MASTER );
48 
49  $this->output( "Updating *_from_namespace fields in links tables.\n" );
50 
51  $start = $this->getOption( 'lastUpdatedId' );
52  if ( !$start ) {
53  $start = $db->selectField( 'page', 'MIN(page_id)', '', __METHOD__ );
54  }
55  if ( !$start ) {
56  $this->output( "Nothing to do." );
57  return false;
58  }
59  $end = $db->selectField( 'page', 'MAX(page_id)', '', __METHOD__ );
60  $batchSize = $this->getBatchSize();
61 
62  # Do remaining chunk
63  $end += $batchSize - 1;
64  $blockStart = $start;
65  $blockEnd = $start + $batchSize - 1;
66  while ( $blockEnd <= $end ) {
67  $this->output( "...doing page_id from $blockStart to $blockEnd\n" );
68  $cond = "page_id BETWEEN " . (int)$blockStart . " AND " . (int)$blockEnd;
69  $res = $db->select( 'page', [ 'page_id', 'page_namespace' ], $cond, __METHOD__ );
70  foreach ( $res as $row ) {
71  $db->update( 'pagelinks',
72  [ 'pl_from_namespace' => $row->page_namespace ],
73  [ 'pl_from' => $row->page_id ],
74  __METHOD__
75  );
76  $db->update( 'templatelinks',
77  [ 'tl_from_namespace' => $row->page_namespace ],
78  [ 'tl_from' => $row->page_id ],
79  __METHOD__
80  );
81  $db->update( 'imagelinks',
82  [ 'il_from_namespace' => $row->page_namespace ],
83  [ 'il_from' => $row->page_id ],
84  __METHOD__
85  );
86  }
87  $blockStart += $batchSize - 1;
88  $blockEnd += $batchSize - 1;
90  }
91  return true;
92  }
93 }
94 
95 $maintClass = PopulateBacklinkNamespace::class;
96 require_once RUN_MAINTENANCE_IF_MAIN;
RUN_MAINTENANCE_IF_MAIN
const RUN_MAINTENANCE_IF_MAIN
Definition: Maintenance.php:39
PopulateBacklinkNamespace
Maintenance script to populate *_from_namespace fields.
Definition: populateBacklinkNamespace.php:31
Maintenance\addDescription
addDescription( $text)
Set the description text.
Definition: Maintenance.php:348
$res
$res
Definition: testCompression.php:52
wfWaitForSlaves
wfWaitForSlaves( $ifWritesSince=null, $wiki=false, $cluster=false, $timeout=null)
Waits for the replica DBs to catch up to the master position.
Definition: GlobalFunctions.php:2718
LoggedUpdateMaintenance
Class for scripts that perform database maintenance and want to log the update in updatelog so we can...
Definition: Maintenance.php:1727
Maintenance\addOption
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
Definition: Maintenance.php:267
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:42
PopulateBacklinkNamespace\__construct
__construct()
Default constructor.
Definition: populateBacklinkNamespace.php:32
Maintenance\getDB
getDB( $db, $groups=[], $dbDomain=false)
Returns a database to be used by current maintenance script.
Definition: Maintenance.php:1396
Maintenance\getOption
getOption( $name, $default=null)
Get an option, or return the default.
Definition: Maintenance.php:302
Maintenance\getBatchSize
getBatchSize()
Returns batch size.
Definition: Maintenance.php:386
Maintenance\output
output( $out, $channel=null)
Throw some output to the user.
Definition: Maintenance.php:453
PopulateBacklinkNamespace\doDBUpdates
doDBUpdates()
Do the actual work.
Definition: populateBacklinkNamespace.php:46
$maintClass
$maintClass
Definition: populateBacklinkNamespace.php:95
PopulateBacklinkNamespace\getUpdateKey
getUpdateKey()
Get the update key name to go in the update log table.
Definition: populateBacklinkNamespace.php:38