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