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