MediaWiki REL1_34
attachLatest.php
Go to the documentation of this file.
1<?php
28
29require_once __DIR__ . '/Maintenance.php';
30
38 public function __construct() {
39 parent::__construct();
40 $this->addOption( "fix", "Actually fix the entries, will dry run otherwise" );
41 $this->addOption( "regenerate-all",
42 "Regenerate the page_latest field for all records in table page" );
43 $this->addDescription( 'Fix page_latest entries in the page table' );
44 }
45
46 public function execute() {
47 $this->output( "Looking for pages with page_latest set to 0...\n" );
48 $dbw = $this->getDB( DB_MASTER );
49 $conds = [ 'page_latest' => 0 ];
50 if ( $this->hasOption( 'regenerate-all' ) ) {
51 $conds = '';
52 }
53 $result = $dbw->select( 'page',
54 [ 'page_id', 'page_namespace', 'page_title' ],
55 $conds,
56 __METHOD__ );
57
58 $lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
59 $dbDomain = $lbFactory->getLocalDomainID();
60
61 $n = 0;
62 foreach ( $result as $row ) {
63 $pageId = intval( $row->page_id );
64 $title = Title::makeTitle( $row->page_namespace, $row->page_title );
65 $name = $title->getPrefixedText();
66 $latestTime = $dbw->selectField( 'revision',
67 'MAX(rev_timestamp)',
68 [ 'rev_page' => $pageId ],
69 __METHOD__ );
70 if ( !$latestTime ) {
71 $this->output( "$dbDomain $pageId [[$name]] can't find latest rev time?!\n" );
72 continue;
73 }
74
75 $revision = Revision::loadFromTimestamp( $dbw, $title, $latestTime );
76 if ( is_null( $revision ) ) {
77 $this->output(
78 "$dbDomain $pageId [[$name]] latest time $latestTime, can't find revision id\n"
79 );
80 continue;
81 }
82 $id = $revision->getId();
83 $this->output( "$dbDomain $pageId [[$name]] latest time $latestTime, rev id $id\n" );
84 if ( $this->hasOption( 'fix' ) ) {
85 $page = WikiPage::factory( $title );
86 $page->updateRevisionOn( $dbw, $revision );
87 $lbFactory->waitForReplication();
88 }
89 $n++;
90 }
91 $this->output( "Done! Processed $n pages.\n" );
92 if ( !$this->hasOption( 'fix' ) ) {
93 $this->output( "This was a dry run; rerun with --fix to update page_latest.\n" );
94 }
95 }
96}
97
98$maintClass = AttachLatest::class;
99require_once RUN_MAINTENANCE_IF_MAIN;
getDB()
const RUN_MAINTENANCE_IF_MAIN
$maintClass
Maintenance script to correct wrong values in the page_latest field in the database.
execute()
Do the actual work.
__construct()
Default constructor.
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
output( $out, $channel=null)
Throw some output to the user.
hasOption( $name)
Checks to see if a particular option exists.
addDescription( $text)
Set the description text.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
MediaWikiServices is the service locator for the application scope of MediaWiki.
static loadFromTimestamp( $db, $title, $timestamp)
Load the revision for the given title with the given timestamp.
Definition Revision.php:296
const DB_MASTER
Definition defines.php:26