MediaWiki  1.34.0
attachLatest.php
Go to the documentation of this file.
1 <?php
28 
29 require_once __DIR__ . '/Maintenance.php';
30 
37 class AttachLatest extends Maintenance {
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;
99 require_once RUN_MAINTENANCE_IF_MAIN;
RUN_MAINTENANCE_IF_MAIN
const RUN_MAINTENANCE_IF_MAIN
Definition: Maintenance.php:39
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:117
AttachLatest\execute
execute()
Do the actual work.
Definition: attachLatest.php:46
Maintenance\addDescription
addDescription( $text)
Set the description text.
Definition: Maintenance.php:348
Maintenance
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
Definition: Maintenance.php:82
AttachLatest
Maintenance script to correct wrong values in the page_latest field in the database.
Definition: attachLatest.php:37
$maintClass
$maintClass
Definition: attachLatest.php:98
WikiPage\factory
static factory(Title $title)
Create a WikiPage object of the appropriate class for the given title.
Definition: WikiPage.php:142
Maintenance\addOption
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
Definition: Maintenance.php:267
$title
$title
Definition: testCompression.php:34
Title\makeTitle
static makeTitle( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:586
DB_MASTER
const DB_MASTER
Definition: defines.php:26
AttachLatest\__construct
__construct()
Default constructor.
Definition: attachLatest.php:38
Maintenance\getDB
getDB( $db, $groups=[], $dbDomain=false)
Returns a database to be used by current maintenance script.
Definition: Maintenance.php:1396
Revision\loadFromTimestamp
static loadFromTimestamp( $db, $title, $timestamp)
Load the revision for the given title with the given timestamp.
Definition: Revision.php:296
Maintenance\output
output( $out, $channel=null)
Throw some output to the user.
Definition: Maintenance.php:453
Maintenance\hasOption
hasOption( $name)
Checks to see if a particular option exists.
Definition: Maintenance.php:288