MediaWiki  master
attachLatest.php
Go to the documentation of this file.
1 <?php
29 
30 require_once __DIR__ . '/Maintenance.php';
31 
38 class AttachLatest extends Maintenance {
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_MASTER );
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  $lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
60  $dbDomain = $lbFactory->getLocalDomainID();
61 
62  $n = 0;
63  foreach ( $result as $row ) {
64  $pageId = intval( $row->page_id );
65  $title = Title::makeTitle( $row->page_namespace, $row->page_title );
66  $name = $title->getPrefixedText();
67  $latestTime = $dbw->selectField( 'revision',
68  'MAX(rev_timestamp)',
69  [ 'rev_page' => $pageId ],
70  __METHOD__ );
71  if ( !$latestTime ) {
72  $this->output( "$dbDomain $pageId [[$name]] can't find latest rev time?!\n" );
73  continue;
74  }
75 
76  $revRecord = MediaWikiServices::getInstance()
77  ->getRevisionLookup()
78  ->getRevisionByTimestamp( $title, $latestTime, RevisionLookup::READ_LATEST );
79  if ( $revRecord === null ) {
80  $this->output(
81  "$dbDomain $pageId [[$name]] latest time $latestTime, can't find revision id\n"
82  );
83  continue;
84  }
85 
86  $id = $revRecord->getId();
87  $this->output( "$dbDomain $pageId [[$name]] latest time $latestTime, rev id $id\n" );
88  if ( $this->hasOption( 'fix' ) ) {
89  $page = WikiPage::factory( $title );
90  $page->updateRevisionOn( $dbw, $revRecord );
91  $lbFactory->waitForReplication();
92  }
93  $n++;
94  }
95  $this->output( "Done! Processed $n pages.\n" );
96  if ( !$this->hasOption( 'fix' ) ) {
97  $this->output( "This was a dry run; rerun with --fix to update page_latest.\n" );
98  }
99  }
100 }
101 
102 $maintClass = AttachLatest::class;
103 require_once RUN_MAINTENANCE_IF_MAIN;
RUN_MAINTENANCE_IF_MAIN
const RUN_MAINTENANCE_IF_MAIN
Definition: Maintenance.php:38
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:163
AttachLatest\execute
execute()
Do the actual work.
Definition: attachLatest.php:47
Maintenance\addDescription
addDescription( $text)
Set the description text.
Definition: Maintenance.php:327
Maintenance
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
Definition: Maintenance.php:55
AttachLatest
Maintenance script to correct wrong values in the page_latest field in the database.
Definition: attachLatest.php:38
Revision\RevisionLookup
Service for looking up page revisions.
Definition: RevisionLookup.php:38
$maintClass
$maintClass
Definition: attachLatest.php:102
WikiPage\factory
static factory(Title $title)
Create a WikiPage object of the appropriate class for the given title.
Definition: WikiPage.php:159
Maintenance\addOption
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
Definition: Maintenance.php:245
$title
$title
Definition: testCompression.php:38
Title\makeTitle
static makeTitle( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:591
DB_MASTER
const DB_MASTER
Definition: defines.php:26
AttachLatest\__construct
__construct()
Default constructor.
Definition: attachLatest.php:39
Maintenance\getDB
getDB( $db, $groups=[], $dbDomain=false)
Returns a database to be used by current maintenance script.
Definition: Maintenance.php:1370
Maintenance\output
output( $out, $channel=null)
Throw some output to the user.
Definition: Maintenance.php:434
Maintenance\hasOption
hasOption( $name)
Checks to see if a particular option exists.
Definition: Maintenance.php:266