MediaWiki REL1_40
attachLatest.php
Go to the documentation of this file.
1<?php
30
31require_once __DIR__ . '/Maintenance.php';
32
40 public function __construct() {
41 parent::__construct();
42 $this->addOption( "fix", "Actually fix the entries, will dry run otherwise" );
43 $this->addOption( "regenerate-all",
44 "Regenerate the page_latest field for all records in table page" );
45 $this->addDescription( 'Fix page_latest entries in the page table' );
46 }
47
48 public function execute() {
49 $this->output( "Looking for pages with page_latest set to 0...\n" );
50 $dbw = $this->getDB( DB_PRIMARY );
51 $conds = [ 'page_latest' => 0 ];
52 if ( $this->hasOption( 'regenerate-all' ) ) {
53 $conds = '';
54 }
55 $result = $dbw->newSelectQueryBuilder()
56 ->select( [ 'page_id', 'page_namespace', 'page_title' ] )
57 ->from( 'page' )
58 ->where( $conds )
59 ->caller( __METHOD__ )
60 ->fetchResultSet();
61
62 $services = MediaWikiServices::getInstance();
63 $dbDomain = $services->getDBLoadBalancerFactory()->getLocalDomainID();
64 $wikiPageFactory = $services->getWikiPageFactory();
65 $revisionLookup = $services->getRevisionLookup();
66
67 $n = 0;
68 foreach ( $result as $row ) {
69 $pageId = intval( $row->page_id );
70 $title = Title::makeTitle( $row->page_namespace, $row->page_title );
71 $name = $title->getPrefixedText();
72 $latestTime = $dbw->newSelectQueryBuilder()
73 ->select( 'MAX(rev_timestamp)' )
74 ->from( 'revision' )
75 ->where( [ 'rev_page' => $pageId ] )
76 ->caller( __METHOD__ )
77 ->fetchField();
78 if ( !$latestTime ) {
79 $this->output( "$dbDomain $pageId [[$name]] can't find latest rev time?!\n" );
80 continue;
81 }
82
83 $revRecord = $revisionLookup->getRevisionByTimestamp( $title, $latestTime, RevisionLookup::READ_LATEST );
84 if ( $revRecord === null ) {
85 $this->output(
86 "$dbDomain $pageId [[$name]] latest time $latestTime, can't find revision id\n"
87 );
88 continue;
89 }
90
91 $id = $revRecord->getId();
92 $this->output( "$dbDomain $pageId [[$name]] latest time $latestTime, rev id $id\n" );
93 if ( $this->hasOption( 'fix' ) ) {
94 $page = $wikiPageFactory->newFromTitle( $title );
95 $page->updateRevisionOn( $dbw, $revRecord );
96 $this->waitForReplication();
97 }
98 $n++;
99 }
100 $this->output( "Done! Processed $n pages.\n" );
101 if ( !$this->hasOption( 'fix' ) ) {
102 $this->output( "This was a dry run; rerun with --fix to update page_latest.\n" );
103 }
104 }
105}
106
107$maintClass = AttachLatest::class;
108require_once RUN_MAINTENANCE_IF_MAIN;
getDB()
$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.
waitForReplication()
Wait for replica DBs to catch up.
hasOption( $name)
Checks to see if a particular option was set.
addDescription( $text)
Set the description text.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
Service locator for MediaWiki core services.
Represents a title within MediaWiki.
Definition Title.php:82
Service for looking up page revisions.
const DB_PRIMARY
Definition defines.php:28