2005-03-13 01:16:21 +00:00
|
|
|
<?php
|
|
|
|
|
/**
|
2012-07-25 19:31:06 +00:00
|
|
|
* Look for 'orphan' revisions hooked to pages which don't exist and
|
|
|
|
|
* 'childless' pages with no revisions.
|
2005-03-13 01:16:21 +00:00
|
|
|
* Then, kill the poor widows and orphans.
|
|
|
|
|
* Man this is depressing.
|
|
|
|
|
*
|
2012-07-25 19:31:06 +00:00
|
|
|
* Copyright © 2005 Brion Vibber <brion@pobox.com>
|
2014-03-20 15:45:01 +00:00
|
|
|
* https://www.mediawiki.org/
|
2009-08-02 19:35:17 +00:00
|
|
|
*
|
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
|
|
|
|
*
|
2012-07-25 19:31:06 +00:00
|
|
|
* @file
|
2005-03-13 01:16:21 +00:00
|
|
|
* @author <brion@pobox.com>
|
WARNING: HUGE COMMIT
Doxygen documentation update:
* Changed alls @addtogroup to @ingroup. @addtogroup adds the comment to the group description, but doesn't add the file, class, function, ... to the group like @ingroup does. See for example http://svn.wikimedia.org/doc/group__SpecialPage.html where it's impossible to see related files, classes, ... that should belong to that group.
* Added @file to file description, it seems that it should be explicitely decalred for file descriptions, otherwise doxygen will think that the comment document the first class, variabled, function, ... that is in that file.
* Removed some empty comments
* Removed some ?>
Added following groups:
* ExternalStorage
* JobQueue
* MaintenanceLanguage
One more thing: there are still a lot of warnings when generating the doc.
2008-05-20 17:13:28 +00:00
|
|
|
* @ingroup Maintenance
|
2005-03-13 01:16:21 +00:00
|
|
|
*/
|
|
|
|
|
|
2013-05-17 00:16:59 +00:00
|
|
|
require_once __DIR__ . '/Maintenance.php';
|
2005-03-13 01:16:21 +00:00
|
|
|
|
2017-03-30 20:46:06 +00:00
|
|
|
use Wikimedia\Rdbms\IMaintainableDatabase;
|
|
|
|
|
|
2012-07-25 19:31:06 +00:00
|
|
|
/**
|
|
|
|
|
* Maintenance script that looks for 'orphan' revisions hooked to pages which
|
|
|
|
|
* don't exist and 'childless' pages with no revisions.
|
|
|
|
|
*
|
|
|
|
|
* @ingroup Maintenance
|
|
|
|
|
*/
|
2009-08-02 19:35:17 +00:00
|
|
|
class Orphans extends Maintenance {
|
|
|
|
|
public function __construct() {
|
|
|
|
|
parent::__construct();
|
2016-01-30 02:48:47 +00:00
|
|
|
$this->addDescription( "Look for 'orphan' revisions hooked to pages which don't exist\n" .
|
2014-04-23 18:09:13 +00:00
|
|
|
"and 'childless' pages with no revisions\n" .
|
|
|
|
|
"Then, kill the poor widows and orphans\n" .
|
2016-01-30 02:48:47 +00:00
|
|
|
"Man this is depressing"
|
|
|
|
|
);
|
2009-08-02 19:35:17 +00:00
|
|
|
$this->addOption( 'fix', 'Actually fix broken entries' );
|
2005-03-13 01:16:21 +00:00
|
|
|
}
|
2006-01-07 13:31:29 +00:00
|
|
|
|
2009-08-02 19:35:17 +00:00
|
|
|
public function execute() {
|
|
|
|
|
$this->checkOrphans( $this->hasOption( 'fix' ) );
|
|
|
|
|
$this->checkSeparation( $this->hasOption( 'fix' ) );
|
|
|
|
|
# Does not work yet, do not use
|
|
|
|
|
# $this->checkWidows( $this->hasOption( 'fix' ) );
|
2005-03-13 01:16:21 +00:00
|
|
|
}
|
2006-01-07 13:31:29 +00:00
|
|
|
|
2009-08-02 19:35:17 +00:00
|
|
|
/**
|
|
|
|
|
* Lock the appropriate tables for the script
|
2017-03-30 20:46:06 +00:00
|
|
|
* @param IMaintainableDatabase $db
|
2016-12-15 21:59:32 +00:00
|
|
|
* @param string[] $extraTable The name of any extra tables to lock (eg: text)
|
2009-08-02 19:35:17 +00:00
|
|
|
*/
|
2016-02-17 09:09:32 +00:00
|
|
|
private function lockTables( $db, $extraTable = [] ) {
|
|
|
|
|
$tbls = [ 'page', 'revision', 'redirect' ];
|
2012-01-16 21:54:24 +00:00
|
|
|
if ( $extraTable ) {
|
|
|
|
|
$tbls = array_merge( $tbls, $extraTable );
|
|
|
|
|
}
|
2016-02-17 09:09:32 +00:00
|
|
|
$db->lockTables( [], $tbls, __METHOD__, false );
|
2005-03-13 01:16:21 +00:00
|
|
|
}
|
2006-01-07 13:31:29 +00:00
|
|
|
|
2009-08-02 19:35:17 +00:00
|
|
|
/**
|
|
|
|
|
* Check for orphan revisions
|
2014-04-17 20:48:32 +00:00
|
|
|
* @param bool $fix Whether to fix broken revisions when found
|
2009-08-02 19:35:17 +00:00
|
|
|
*/
|
|
|
|
|
private function checkOrphans( $fix ) {
|
2015-12-31 00:07:37 +00:00
|
|
|
$dbw = $this->getDB( DB_MASTER );
|
2009-08-02 19:35:17 +00:00
|
|
|
$page = $dbw->tableName( 'page' );
|
|
|
|
|
$revision = $dbw->tableName( 'revision' );
|
2010-12-04 03:20:14 +00:00
|
|
|
|
2010-05-22 16:50:39 +00:00
|
|
|
if ( $fix ) {
|
2009-08-02 19:35:17 +00:00
|
|
|
$this->lockTables( $dbw );
|
|
|
|
|
}
|
2010-12-04 03:20:14 +00:00
|
|
|
|
2014-04-22 21:25:04 +00:00
|
|
|
$this->output( "Checking for orphan revision table entries... "
|
|
|
|
|
. "(this may take a while on a large wiki)\n" );
|
2009-08-02 19:35:17 +00:00
|
|
|
$result = $dbw->query( "
|
|
|
|
|
SELECT *
|
|
|
|
|
FROM $revision LEFT OUTER JOIN $page ON rev_page=page_id
|
|
|
|
|
WHERE page_id IS NULL
|
2010-05-22 16:50:39 +00:00
|
|
|
" );
|
2013-01-06 10:52:40 +00:00
|
|
|
$orphans = $result->numRows();
|
2010-05-22 16:50:39 +00:00
|
|
|
if ( $orphans > 0 ) {
|
2009-08-02 19:35:17 +00:00
|
|
|
global $wgContLang;
|
2014-04-22 21:25:04 +00:00
|
|
|
|
2009-08-02 19:35:17 +00:00
|
|
|
$this->output( "$orphans orphan revisions...\n" );
|
2014-04-22 21:25:04 +00:00
|
|
|
$this->output( sprintf(
|
|
|
|
|
"%10s %10s %14s %20s %s\n",
|
|
|
|
|
'rev_id', 'rev_page', 'rev_timestamp', 'rev_user_text', 'rev_comment'
|
|
|
|
|
) );
|
|
|
|
|
|
2010-05-22 16:50:39 +00:00
|
|
|
foreach ( $result as $row ) {
|
2009-08-02 19:35:17 +00:00
|
|
|
$comment = ( $row->rev_comment == '' )
|
|
|
|
|
? ''
|
|
|
|
|
: '(' . $wgContLang->truncate( $row->rev_comment, 40 ) . ')';
|
|
|
|
|
$this->output( sprintf( "%10d %10d %14s %20s %s\n",
|
|
|
|
|
$row->rev_id,
|
|
|
|
|
$row->rev_page,
|
|
|
|
|
$row->rev_timestamp,
|
|
|
|
|
$wgContLang->truncate( $row->rev_user_text, 17 ),
|
|
|
|
|
$comment ) );
|
2010-05-22 16:50:39 +00:00
|
|
|
if ( $fix ) {
|
2016-02-17 09:09:32 +00:00
|
|
|
$dbw->delete( 'revision', [ 'rev_id' => $row->rev_id ] );
|
2009-08-02 19:35:17 +00:00
|
|
|
}
|
|
|
|
|
}
|
2010-05-22 16:50:39 +00:00
|
|
|
if ( !$fix ) {
|
2009-08-02 19:35:17 +00:00
|
|
|
$this->output( "Run again with --fix to remove these entries automatically.\n" );
|
2005-03-13 01:16:21 +00:00
|
|
|
}
|
2009-08-02 19:35:17 +00:00
|
|
|
} else {
|
|
|
|
|
$this->output( "No orphans! Yay!\n" );
|
2005-03-13 01:16:21 +00:00
|
|
|
}
|
2010-12-04 03:20:14 +00:00
|
|
|
|
2010-05-22 16:50:39 +00:00
|
|
|
if ( $fix ) {
|
2010-09-02 21:54:53 +00:00
|
|
|
$dbw->unlockTables( __METHOD__ );
|
2005-03-13 01:16:21 +00:00
|
|
|
}
|
|
|
|
|
}
|
2006-01-07 13:31:29 +00:00
|
|
|
|
2009-08-02 19:35:17 +00:00
|
|
|
/**
|
2014-04-17 20:48:32 +00:00
|
|
|
* @param bool $fix
|
2009-08-02 19:35:17 +00:00
|
|
|
* @todo DON'T USE THIS YET! It will remove entries which have children,
|
|
|
|
|
* but which aren't properly attached (eg if page_latest is bogus
|
|
|
|
|
* but valid revisions do exist)
|
|
|
|
|
*/
|
|
|
|
|
private function checkWidows( $fix ) {
|
2015-12-31 00:07:37 +00:00
|
|
|
$dbw = $this->getDB( DB_MASTER );
|
2009-08-02 19:35:17 +00:00
|
|
|
$page = $dbw->tableName( 'page' );
|
|
|
|
|
$revision = $dbw->tableName( 'revision' );
|
|
|
|
|
|
2010-05-22 16:50:39 +00:00
|
|
|
if ( $fix ) {
|
2009-08-02 19:35:17 +00:00
|
|
|
$this->lockTables( $dbw );
|
|
|
|
|
}
|
2006-01-07 13:31:29 +00:00
|
|
|
|
2014-04-22 21:25:04 +00:00
|
|
|
$this->output( "\nChecking for childless page table entries... "
|
|
|
|
|
. "(this may take a while on a large wiki)\n" );
|
2009-08-02 19:35:17 +00:00
|
|
|
$result = $dbw->query( "
|
|
|
|
|
SELECT *
|
|
|
|
|
FROM $page LEFT OUTER JOIN $revision ON page_latest=rev_id
|
|
|
|
|
WHERE rev_id IS NULL
|
2010-05-22 16:50:39 +00:00
|
|
|
" );
|
2013-01-06 10:52:40 +00:00
|
|
|
$widows = $result->numRows();
|
2010-05-22 16:50:39 +00:00
|
|
|
if ( $widows > 0 ) {
|
2009-08-02 19:35:17 +00:00
|
|
|
$this->output( "$widows childless pages...\n" );
|
|
|
|
|
$this->output( sprintf( "%10s %11s %2s %s\n", 'page_id', 'page_latest', 'ns', 'page_title' ) );
|
2010-05-22 16:50:39 +00:00
|
|
|
foreach ( $result as $row ) {
|
2009-08-02 19:35:17 +00:00
|
|
|
printf( "%10d %11d %2d %s\n",
|
2005-03-13 07:18:13 +00:00
|
|
|
$row->page_id,
|
|
|
|
|
$row->page_latest,
|
2009-08-02 19:35:17 +00:00
|
|
|
$row->page_namespace,
|
|
|
|
|
$row->page_title );
|
2010-05-22 16:50:39 +00:00
|
|
|
if ( $fix ) {
|
2016-02-17 09:09:32 +00:00
|
|
|
$dbw->delete( 'page', [ 'page_id' => $row->page_id ] );
|
2005-03-13 07:18:13 +00:00
|
|
|
}
|
|
|
|
|
}
|
2010-05-22 16:50:39 +00:00
|
|
|
if ( !$fix ) {
|
2009-08-02 19:35:17 +00:00
|
|
|
$this->output( "Run again with --fix to remove these entries automatically.\n" );
|
|
|
|
|
}
|
2005-03-13 07:18:13 +00:00
|
|
|
} else {
|
2009-08-02 19:35:17 +00:00
|
|
|
$this->output( "No childless pages! Yay!\n" );
|
|
|
|
|
}
|
2010-12-04 03:20:14 +00:00
|
|
|
|
2010-05-22 16:50:39 +00:00
|
|
|
if ( $fix ) {
|
2010-09-02 21:54:53 +00:00
|
|
|
$dbw->unlockTables( __METHOD__ );
|
2005-03-13 07:18:13 +00:00
|
|
|
}
|
|
|
|
|
}
|
2006-01-07 13:31:29 +00:00
|
|
|
|
2009-08-02 19:35:17 +00:00
|
|
|
/**
|
|
|
|
|
* Check for pages where page_latest is wrong
|
2014-04-17 20:48:32 +00:00
|
|
|
* @param bool $fix Whether to fix broken entries
|
2009-08-02 19:35:17 +00:00
|
|
|
*/
|
|
|
|
|
private function checkSeparation( $fix ) {
|
2015-12-31 00:07:37 +00:00
|
|
|
$dbw = $this->getDB( DB_MASTER );
|
2013-04-18 18:48:44 +00:00
|
|
|
$page = $dbw->tableName( 'page' );
|
2009-08-02 19:35:17 +00:00
|
|
|
$revision = $dbw->tableName( 'revision' );
|
2010-12-04 03:20:14 +00:00
|
|
|
|
2010-05-22 16:50:39 +00:00
|
|
|
if ( $fix ) {
|
2016-02-17 09:09:32 +00:00
|
|
|
$this->lockTables( $dbw, [ 'user', 'text' ] );
|
2009-08-02 19:35:17 +00:00
|
|
|
}
|
2010-12-04 03:20:14 +00:00
|
|
|
|
2014-04-22 21:25:04 +00:00
|
|
|
$this->output( "\nChecking for pages whose page_latest links are incorrect... "
|
|
|
|
|
. "(this may take a while on a large wiki)\n" );
|
2009-08-02 19:35:17 +00:00
|
|
|
$result = $dbw->query( "
|
|
|
|
|
SELECT *
|
|
|
|
|
FROM $page LEFT OUTER JOIN $revision ON page_latest=rev_id
|
2010-05-22 16:50:39 +00:00
|
|
|
" );
|
2009-08-02 19:35:17 +00:00
|
|
|
$found = 0;
|
2010-05-22 16:50:39 +00:00
|
|
|
foreach ( $result as $row ) {
|
2009-08-02 19:35:17 +00:00
|
|
|
$result2 = $dbw->query( "
|
|
|
|
|
SELECT MAX(rev_timestamp) as max_timestamp
|
|
|
|
|
FROM $revision
|
|
|
|
|
WHERE rev_page=$row->page_id
|
|
|
|
|
" );
|
|
|
|
|
$row2 = $dbw->fetchObject( $result2 );
|
2010-05-22 16:50:39 +00:00
|
|
|
if ( $row2 ) {
|
|
|
|
|
if ( $row->rev_timestamp != $row2->max_timestamp ) {
|
|
|
|
|
if ( $found == 0 ) {
|
2009-08-02 19:35:17 +00:00
|
|
|
$this->output( sprintf( "%10s %10s %14s %14s\n",
|
|
|
|
|
'page_id', 'rev_id', 'timestamp', 'max timestamp' ) );
|
|
|
|
|
}
|
|
|
|
|
++$found;
|
|
|
|
|
$this->output( sprintf( "%10d %10d %14s %14s\n",
|
|
|
|
|
$row->page_id,
|
|
|
|
|
$row->page_latest,
|
|
|
|
|
$row->rev_timestamp,
|
|
|
|
|
$row2->max_timestamp ) );
|
2010-05-22 16:50:39 +00:00
|
|
|
if ( $fix ) {
|
2009-08-02 19:35:17 +00:00
|
|
|
# ...
|
|
|
|
|
$maxId = $dbw->selectField(
|
|
|
|
|
'revision',
|
|
|
|
|
'rev_id',
|
2016-02-17 09:09:32 +00:00
|
|
|
[
|
2013-04-18 18:48:44 +00:00
|
|
|
'rev_page' => $row->page_id,
|
2016-02-17 09:09:32 +00:00
|
|
|
'rev_timestamp' => $row2->max_timestamp ] );
|
2009-08-02 19:35:17 +00:00
|
|
|
$this->output( "... updating to revision $maxId\n" );
|
|
|
|
|
$maxRev = Revision::newFromId( $maxId );
|
|
|
|
|
$title = Title::makeTitle( $row->page_namespace, $row->page_title );
|
2012-01-16 21:54:24 +00:00
|
|
|
$article = WikiPage::factory( $title );
|
2009-08-02 19:35:17 +00:00
|
|
|
$article->updateRevisionOn( $dbw, $maxRev );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
$this->output( "wtf\n" );
|
|
|
|
|
}
|
|
|
|
|
}
|
2010-12-04 03:20:14 +00:00
|
|
|
|
2010-05-22 16:50:39 +00:00
|
|
|
if ( $found ) {
|
2009-08-02 19:35:17 +00:00
|
|
|
$this->output( "Found $found pages with incorrect latest revision.\n" );
|
|
|
|
|
} else {
|
|
|
|
|
$this->output( "No pages with incorrect latest revision. Yay!\n" );
|
|
|
|
|
}
|
2010-05-22 16:50:39 +00:00
|
|
|
if ( !$fix && $found > 0 ) {
|
2009-08-02 19:35:17 +00:00
|
|
|
$this->output( "Run again with --fix to remove these entries automatically.\n" );
|
|
|
|
|
}
|
2010-12-04 03:20:14 +00:00
|
|
|
|
2010-05-22 16:50:39 +00:00
|
|
|
if ( $fix ) {
|
2010-09-02 21:54:53 +00:00
|
|
|
$dbw->unlockTables( __METHOD__ );
|
2009-08-02 19:35:17 +00:00
|
|
|
}
|
2005-03-13 07:18:13 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2009-08-02 19:35:17 +00:00
|
|
|
$maintClass = "Orphans";
|
2013-05-07 23:00:15 +00:00
|
|
|
require_once RUN_MAINTENANCE_IF_MAIN;
|