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_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;
104 require_once RUN_MAINTENANCE_IF_MAIN;
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:193
AttachLatest\execute
execute()
Do the actual work.
Definition: attachLatest.php:47
Maintenance\addDescription
addDescription( $text)
Set the description text.
Definition: Maintenance.php:329
Maintenance
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
Definition: Maintenance.php:59
AttachLatest
Maintenance script to correct wrong values in the page_latest field in the database.
Definition: attachLatest.php:38
MediaWiki\Revision\RevisionLookup
Service for looking up page revisions.
Definition: RevisionLookup.php:38
$maintClass
$maintClass
Definition: attachLatest.php:103
Maintenance\addOption
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
Definition: Maintenance.php:249
$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:651
AttachLatest\__construct
__construct()
Default constructor.
Definition: attachLatest.php:39
DB_PRIMARY
const DB_PRIMARY
Definition: defines.php:27
Maintenance\getDB
getDB( $db, $groups=[], $dbDomain=false)
Returns a database to be used by current maintenance script.
Definition: Maintenance.php:1362
Maintenance\output
output( $out, $channel=null)
Throw some output to the user.
Definition: Maintenance.php:435
Maintenance\hasOption
hasOption( $name)
Checks to see if a particular option was set.
Definition: Maintenance.php:271