ca8d1aa7d5c9fb7110d8a0d86f1fcfd1bc493074
[lhc/web/wiklou.git] / maintenance / attachLatest.php
1 <?php
2 // quick hackjob to fix damages imports on wikisource
3 // page records have page_latest wrong
4
5 /**
6 * Copyright (C) 2005 Brion Vibber <brion@pobox.com>
7 * http://www.mediawiki.org/
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 * http://www.gnu.org/copyleft/gpl.html
23 *
24 * @file
25 * @ingroup Maintenance
26 */
27
28 require_once( "Maintenance.php" );
29
30 class AttachLatest extends Maintenance {
31
32 public function __construct() {
33 parent::__construct();
34 $this->addParam( "fix", "Actually fix the entries, will dry run otherwise" );
35 $this->mDescription = "Fix page_latest entries in the page table";
36 }
37
38 public function execute() {
39 $this->output( "Looking for pages with page_latest set to 0...\n" );
40 $dbw = wfGetDB( DB_MASTER );
41 $result = $dbw->select( 'page',
42 array( 'page_id', 'page_namespace', 'page_title' ),
43 array( 'page_latest' => 0 ),
44 __METHOD__ );
45
46 $n = 0;
47 while( $row = $dbw->fetchObject( $result ) ) {
48 $pageId = intval( $row->page_id );
49 $title = Title::makeTitle( $row->page_namespace, $row->page_title );
50 $name = $title->getPrefixedText();
51 $latestTime = $dbw->selectField( 'revision',
52 'MAX(rev_timestamp)',
53 array( 'rev_page' => $pageId ),
54 __METHOD__ );
55 if( !$latestTime ) {
56 $this->output( wfWikiID()." $pageId [[$name]] can't find latest rev time?!\n" );
57 continue;
58 }
59
60 $revision = Revision::loadFromTimestamp( $dbw, $title, $latestTime );
61 if( is_null( $revision ) ) {
62 $this->output( wfWikiID()." $pageId [[$name]] latest time $latestTime, can't find revision id\n" );
63 continue;
64 }
65 $id = $revision->getId();
66 $this->output( wfWikiID()." $pageId [[$name]] latest time $latestTime, rev id $id\n" );
67 if( $this->hasOption('fix') ) {
68 $article = new Article( $title );
69 $article->updateRevisionOn( $dbw, $revision );
70 }
71 $n++;
72 }
73 $dbw->freeResult( $result );
74 $this->output( "Done! Processed $n pages.\n" );
75 if( !$this->hasOption('fix') ) {
76 $this->output( "This was a dry run; rerun with --fix to update page_latest.\n" );
77 }
78 }
79 }
80
81 $maintClass = "AttachLatest";
82 require_once( DO_MAINTENANCE );