2006-07-20 01:21:41 +00:00
|
|
|
<?php
|
|
|
|
|
/**
|
|
|
|
|
* Fsck for MediaWiki
|
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
|
|
|
*
|
2010-12-16 19:15:12 +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
|
|
|
|
|
*
|
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
|
|
|
* @file
|
|
|
|
|
* @ingroup Maintenance ExternalStorage
|
2006-07-20 01:21:41 +00:00
|
|
|
*/
|
|
|
|
|
|
2016-11-22 23:39:22 +00:00
|
|
|
use MediaWiki\MediaWikiServices;
|
|
|
|
|
|
2006-07-20 01:21:41 +00:00
|
|
|
if ( !defined( 'MEDIAWIKI' ) ) {
|
2016-02-04 00:04:12 +00:00
|
|
|
$optionsWithoutArgs = [ 'fix' ];
|
2013-05-17 00:16:59 +00:00
|
|
|
require_once __DIR__ . '/../commandLine.inc';
|
2006-07-20 01:21:41 +00:00
|
|
|
|
|
|
|
|
$cs = new CheckStorage;
|
|
|
|
|
$fix = isset( $options['fix'] );
|
|
|
|
|
if ( isset( $args[0] ) ) {
|
|
|
|
|
$xml = $args[0];
|
|
|
|
|
} else {
|
|
|
|
|
$xml = false;
|
|
|
|
|
}
|
|
|
|
|
$cs->check( $fix, $xml );
|
|
|
|
|
}
|
|
|
|
|
|
2010-05-22 16:50:39 +00:00
|
|
|
// ----------------------------------------------------------------------------------
|
2006-07-20 01:21:41 +00:00
|
|
|
|
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
|
|
|
/**
|
2012-09-20 20:49:55 +00:00
|
|
|
* Maintenance script to do various checks on external storage.
|
|
|
|
|
*
|
2014-08-23 07:40:00 +00:00
|
|
|
* @fixme this should extend the base Maintenance class
|
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 ExternalStorage
|
|
|
|
|
*/
|
|
|
|
|
class CheckStorage {
|
2011-09-10 07:17:10 +00:00
|
|
|
const CONCAT_HEADER = 'O:27:"concatenatedgziphistoryblob"';
|
2012-09-14 18:57:14 +00:00
|
|
|
public $oldIdMap, $errors;
|
|
|
|
|
public $dbStore = null;
|
2006-07-20 01:21:41 +00:00
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
public $errorDescriptions = [
|
2006-07-20 01:21:41 +00:00
|
|
|
'restore text' => 'Damaged text, need to be restored from a backup',
|
|
|
|
|
'restore revision' => 'Damaged revision row, need to be restored from a backup',
|
|
|
|
|
'unfixable' => 'Unexpected errors with no automated fixing method',
|
|
|
|
|
'fixed' => 'Errors already fixed',
|
|
|
|
|
'fixable' => 'Errors which would already be fixed if --fix was specified',
|
2016-02-17 09:09:32 +00:00
|
|
|
];
|
2006-07-20 01:21:41 +00:00
|
|
|
|
|
|
|
|
function check( $fix = false, $xml = '' ) {
|
2016-09-05 19:55:19 +00:00
|
|
|
$dbr = wfGetDB( DB_REPLICA );
|
2006-07-20 01:21:41 +00:00
|
|
|
if ( $fix ) {
|
|
|
|
|
print "Checking, will fix errors if possible...\n";
|
|
|
|
|
} else {
|
|
|
|
|
print "Checking...\n";
|
|
|
|
|
}
|
2018-02-27 18:37:26 +00:00
|
|
|
$maxRevId = $dbr->selectField( 'revision', 'MAX(rev_id)', '', __METHOD__ );
|
2006-07-20 01:21:41 +00:00
|
|
|
$chunkSize = 1000;
|
2016-02-17 09:09:32 +00:00
|
|
|
$flagStats = [];
|
|
|
|
|
$objectStats = [];
|
|
|
|
|
$knownFlags = [ 'external', 'gzip', 'object', 'utf-8' ];
|
|
|
|
|
$this->errors = [
|
|
|
|
|
'restore text' => [],
|
|
|
|
|
'restore revision' => [],
|
|
|
|
|
'unfixable' => [],
|
|
|
|
|
'fixed' => [],
|
|
|
|
|
'fixable' => [],
|
|
|
|
|
];
|
2006-07-20 01:21:41 +00:00
|
|
|
|
2013-04-18 18:48:44 +00:00
|
|
|
for ( $chunkStart = 1; $chunkStart < $maxRevId; $chunkStart += $chunkSize ) {
|
2006-07-20 01:21:41 +00:00
|
|
|
$chunkEnd = $chunkStart + $chunkSize - 1;
|
2010-05-22 16:50:39 +00:00
|
|
|
// print "$chunkStart of $maxRevId\n";
|
2006-07-20 01:21:41 +00:00
|
|
|
|
|
|
|
|
// Fetch revision rows
|
2016-02-17 09:09:32 +00:00
|
|
|
$this->oldIdMap = [];
|
2010-05-22 16:50:39 +00:00
|
|
|
$dbr->ping();
|
2016-02-17 09:09:32 +00:00
|
|
|
$res = $dbr->select( 'revision', [ 'rev_id', 'rev_text_id' ],
|
|
|
|
|
[ "rev_id BETWEEN $chunkStart AND $chunkEnd" ], __METHOD__ );
|
2010-10-13 22:34:25 +00:00
|
|
|
foreach ( $res as $row ) {
|
2006-07-20 01:21:41 +00:00
|
|
|
$this->oldIdMap[$row->rev_id] = $row->rev_text_id;
|
|
|
|
|
}
|
|
|
|
|
$dbr->freeResult( $res );
|
|
|
|
|
|
|
|
|
|
if ( !count( $this->oldIdMap ) ) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Fetch old_flags
|
|
|
|
|
$missingTextRows = array_flip( $this->oldIdMap );
|
2016-02-17 09:09:32 +00:00
|
|
|
$externalRevs = [];
|
|
|
|
|
$objectRevs = [];
|
2017-12-12 11:27:54 +00:00
|
|
|
$res = $dbr->select(
|
|
|
|
|
'text',
|
|
|
|
|
[ 'old_id', 'old_flags' ],
|
|
|
|
|
[ 'old_id' => $this->oldIdMap ],
|
|
|
|
|
__METHOD__
|
|
|
|
|
);
|
2010-10-13 22:34:25 +00:00
|
|
|
foreach ( $res as $row ) {
|
2011-10-16 03:27:12 +00:00
|
|
|
/**
|
|
|
|
|
* @var $flags int
|
|
|
|
|
*/
|
2006-07-20 01:21:41 +00:00
|
|
|
$flags = $row->old_flags;
|
|
|
|
|
$id = $row->old_id;
|
|
|
|
|
|
|
|
|
|
// Create flagStats row if it doesn't exist
|
2016-02-17 09:09:32 +00:00
|
|
|
$flagStats = $flagStats + [ $flags => 0 ];
|
2006-07-20 01:21:41 +00:00
|
|
|
// Increment counter
|
|
|
|
|
$flagStats[$flags]++;
|
|
|
|
|
|
|
|
|
|
// Not missing
|
|
|
|
|
unset( $missingTextRows[$row->old_id] );
|
|
|
|
|
|
|
|
|
|
// Check for external or object
|
|
|
|
|
if ( $flags == '' ) {
|
2016-02-17 09:09:32 +00:00
|
|
|
$flagArray = [];
|
2006-07-20 01:21:41 +00:00
|
|
|
} else {
|
|
|
|
|
$flagArray = explode( ',', $flags );
|
|
|
|
|
}
|
|
|
|
|
if ( in_array( 'external', $flagArray ) ) {
|
|
|
|
|
$externalRevs[] = $id;
|
|
|
|
|
} elseif ( in_array( 'object', $flagArray ) ) {
|
|
|
|
|
$objectRevs[] = $id;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Check for unrecognised flags
|
|
|
|
|
if ( $flags == '0' ) {
|
|
|
|
|
// This is a known bug from 2004
|
|
|
|
|
// It's safe to just erase the old_flags field
|
|
|
|
|
if ( $fix ) {
|
2017-11-20 00:36:54 +00:00
|
|
|
$this->addError( 'fixed', "Warning: old_flags set to 0", $id );
|
2011-07-01 01:42:48 +00:00
|
|
|
$dbw = wfGetDB( DB_MASTER );
|
2006-07-20 01:21:41 +00:00
|
|
|
$dbw->ping();
|
2016-02-17 09:09:32 +00:00
|
|
|
$dbw->update( 'text', [ 'old_flags' => '' ],
|
|
|
|
|
[ 'old_id' => $id ], __METHOD__ );
|
2006-07-20 01:21:41 +00:00
|
|
|
echo "Fixed\n";
|
|
|
|
|
} else {
|
2017-11-20 00:36:54 +00:00
|
|
|
$this->addError( 'fixable', "Warning: old_flags set to 0", $id );
|
2006-07-20 01:21:41 +00:00
|
|
|
}
|
|
|
|
|
} elseif ( count( array_diff( $flagArray, $knownFlags ) ) ) {
|
2017-11-20 00:36:54 +00:00
|
|
|
$this->addError( 'unfixable', "Error: invalid flags field \"$flags\"", $id );
|
2006-07-20 01:21:41 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
$dbr->freeResult( $res );
|
|
|
|
|
|
|
|
|
|
// Output errors for any missing text rows
|
|
|
|
|
foreach ( $missingTextRows as $oldId => $revId ) {
|
2017-11-20 00:36:54 +00:00
|
|
|
$this->addError( 'restore revision', "Error: missing text row", $oldId );
|
2006-07-20 01:21:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Verify external revisions
|
2016-02-17 09:09:32 +00:00
|
|
|
$externalConcatBlobs = [];
|
|
|
|
|
$externalNormalBlobs = [];
|
2006-07-20 01:21:41 +00:00
|
|
|
if ( count( $externalRevs ) ) {
|
2017-12-12 11:27:54 +00:00
|
|
|
$res = $dbr->select(
|
|
|
|
|
'text',
|
|
|
|
|
[ 'old_id', 'old_flags', 'old_text' ],
|
|
|
|
|
[ 'old_id' => $externalRevs ],
|
|
|
|
|
__METHOD__
|
|
|
|
|
);
|
2010-10-13 22:34:25 +00:00
|
|
|
foreach ( $res as $row ) {
|
2006-07-20 01:21:41 +00:00
|
|
|
$urlParts = explode( '://', $row->old_text, 2 );
|
|
|
|
|
if ( count( $urlParts ) !== 2 || $urlParts[1] == '' ) {
|
2017-11-20 00:36:54 +00:00
|
|
|
$this->addError( 'restore text', "Error: invalid URL \"{$row->old_text}\"", $row->old_id );
|
2006-07-20 01:21:41 +00:00
|
|
|
continue;
|
|
|
|
|
}
|
2010-11-30 18:44:50 +00:00
|
|
|
list( $proto, ) = $urlParts;
|
2006-07-20 01:21:41 +00:00
|
|
|
if ( $proto != 'DB' ) {
|
2017-11-20 00:36:54 +00:00
|
|
|
$this->addError(
|
|
|
|
|
'restore text',
|
|
|
|
|
"Error: invalid external protocol \"$proto\"",
|
|
|
|
|
$row->old_id );
|
2006-07-20 01:21:41 +00:00
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
$path = explode( '/', $row->old_text );
|
|
|
|
|
$cluster = $path[2];
|
|
|
|
|
$id = $path[3];
|
|
|
|
|
if ( isset( $path[4] ) ) {
|
|
|
|
|
$externalConcatBlobs[$cluster][$id][] = $row->old_id;
|
|
|
|
|
} else {
|
|
|
|
|
$externalNormalBlobs[$cluster][$id][] = $row->old_id;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
$dbr->freeResult( $res );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Check external concat blobs for the right header
|
|
|
|
|
$this->checkExternalConcatBlobs( $externalConcatBlobs );
|
2010-12-04 03:20:14 +00:00
|
|
|
|
2006-07-20 01:21:41 +00:00
|
|
|
// Check external normal blobs for existence
|
|
|
|
|
if ( count( $externalNormalBlobs ) ) {
|
|
|
|
|
if ( is_null( $this->dbStore ) ) {
|
|
|
|
|
$this->dbStore = new ExternalStoreDB;
|
|
|
|
|
}
|
|
|
|
|
foreach ( $externalConcatBlobs as $cluster => $xBlobIds ) {
|
|
|
|
|
$blobIds = array_keys( $xBlobIds );
|
|
|
|
|
$extDb =& $this->dbStore->getSlave( $cluster );
|
|
|
|
|
$blobsTable = $this->dbStore->getTable( $extDb );
|
2010-05-22 16:50:39 +00:00
|
|
|
$res = $extDb->select( $blobsTable,
|
2016-02-17 09:09:32 +00:00
|
|
|
[ 'blob_id' ],
|
2017-12-14 01:56:14 +00:00
|
|
|
[ 'blob_id' => $blobIds ],
|
|
|
|
|
__METHOD__
|
|
|
|
|
);
|
2010-10-13 22:34:25 +00:00
|
|
|
foreach ( $res as $row ) {
|
2006-07-20 01:21:41 +00:00
|
|
|
unset( $xBlobIds[$row->blob_id] );
|
|
|
|
|
}
|
|
|
|
|
$extDb->freeResult( $res );
|
|
|
|
|
// Print errors for missing blobs rows
|
|
|
|
|
foreach ( $xBlobIds as $blobId => $oldId ) {
|
2017-11-20 00:36:54 +00:00
|
|
|
$this->addError(
|
|
|
|
|
'restore text',
|
|
|
|
|
"Error: missing target $blobId for one-part ES URL",
|
|
|
|
|
$oldId );
|
2006-07-20 01:21:41 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Check local objects
|
|
|
|
|
$dbr->ping();
|
2016-02-17 09:09:32 +00:00
|
|
|
$concatBlobs = [];
|
|
|
|
|
$curIds = [];
|
2006-07-20 01:21:41 +00:00
|
|
|
if ( count( $objectRevs ) ) {
|
|
|
|
|
$headerLength = 300;
|
2014-04-22 20:23:18 +00:00
|
|
|
$res = $dbr->select(
|
|
|
|
|
'text',
|
2016-02-17 09:09:32 +00:00
|
|
|
[ 'old_id', 'old_flags', "LEFT(old_text, $headerLength) AS header" ],
|
2017-12-12 11:27:54 +00:00
|
|
|
[ 'old_id' => $objectRevs ],
|
2014-04-22 20:23:18 +00:00
|
|
|
__METHOD__
|
|
|
|
|
);
|
2010-10-13 22:34:25 +00:00
|
|
|
foreach ( $res as $row ) {
|
2006-07-20 01:21:41 +00:00
|
|
|
$oldId = $row->old_id;
|
2016-02-17 09:09:32 +00:00
|
|
|
$matches = [];
|
2006-07-20 01:21:41 +00:00
|
|
|
if ( !preg_match( '/^O:(\d+):"(\w+)"/', $row->header, $matches ) ) {
|
2017-11-20 00:36:54 +00:00
|
|
|
$this->addError( 'restore text', "Error: invalid object header", $oldId );
|
2006-07-20 01:21:41 +00:00
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$className = strtolower( $matches[2] );
|
|
|
|
|
if ( strlen( $className ) != $matches[1] ) {
|
2017-11-20 00:36:54 +00:00
|
|
|
$this->addError(
|
2014-04-22 20:23:18 +00:00
|
|
|
'restore text',
|
|
|
|
|
"Error: invalid object header, wrong class name length",
|
|
|
|
|
$oldId
|
|
|
|
|
);
|
2006-07-20 01:21:41 +00:00
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
$objectStats = $objectStats + [ $className => 0 ];
|
2006-07-20 01:21:41 +00:00
|
|
|
$objectStats[$className]++;
|
|
|
|
|
|
|
|
|
|
switch ( $className ) {
|
|
|
|
|
case 'concatenatedgziphistoryblob':
|
|
|
|
|
// Good
|
|
|
|
|
break;
|
|
|
|
|
case 'historyblobstub':
|
|
|
|
|
case 'historyblobcurstub':
|
|
|
|
|
if ( strlen( $row->header ) == $headerLength ) {
|
2017-11-20 00:36:54 +00:00
|
|
|
$this->addError( 'unfixable', "Error: overlong stub header", $oldId );
|
2006-07-20 01:21:41 +00:00
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
$stubObj = unserialize( $row->header );
|
|
|
|
|
if ( !is_object( $stubObj ) ) {
|
2017-11-20 00:36:54 +00:00
|
|
|
$this->addError( 'restore text', "Error: unable to unserialize stub object", $oldId );
|
2006-07-20 01:21:41 +00:00
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
if ( $className == 'historyblobstub' ) {
|
|
|
|
|
$concatBlobs[$stubObj->mOldId][] = $oldId;
|
|
|
|
|
} else {
|
|
|
|
|
$curIds[$stubObj->mCurId][] = $oldId;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
default:
|
2017-11-20 00:36:54 +00:00
|
|
|
$this->addError( 'unfixable', "Error: unrecognised object class \"$className\"", $oldId );
|
2006-07-20 01:21:41 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
$dbr->freeResult( $res );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Check local concat blob validity
|
2016-02-17 09:09:32 +00:00
|
|
|
$externalConcatBlobs = [];
|
2006-07-20 01:21:41 +00:00
|
|
|
if ( count( $concatBlobs ) ) {
|
|
|
|
|
$headerLength = 300;
|
2014-04-22 20:23:18 +00:00
|
|
|
$res = $dbr->select(
|
|
|
|
|
'text',
|
2016-02-17 09:09:32 +00:00
|
|
|
[ 'old_id', 'old_flags', "LEFT(old_text, $headerLength) AS header" ],
|
2017-12-12 11:27:54 +00:00
|
|
|
[ 'old_id' => array_keys( $concatBlobs ) ],
|
2014-04-22 20:23:18 +00:00
|
|
|
__METHOD__
|
|
|
|
|
);
|
2010-10-13 22:34:25 +00:00
|
|
|
foreach ( $res as $row ) {
|
2006-07-20 01:21:41 +00:00
|
|
|
$flags = explode( ',', $row->old_flags );
|
|
|
|
|
if ( in_array( 'external', $flags ) ) {
|
|
|
|
|
// Concat blob is in external storage?
|
|
|
|
|
if ( in_array( 'object', $flags ) ) {
|
|
|
|
|
$urlParts = explode( '/', $row->header );
|
|
|
|
|
if ( $urlParts[0] != 'DB:' ) {
|
2017-11-20 00:36:54 +00:00
|
|
|
$this->addError(
|
2014-04-22 20:23:18 +00:00
|
|
|
'unfixable',
|
|
|
|
|
"Error: unrecognised external storage type \"{$urlParts[0]}",
|
|
|
|
|
$row->old_id
|
|
|
|
|
);
|
2006-07-20 01:21:41 +00:00
|
|
|
} else {
|
|
|
|
|
$cluster = $urlParts[2];
|
|
|
|
|
$id = $urlParts[3];
|
|
|
|
|
if ( !isset( $externalConcatBlobs[$cluster][$id] ) ) {
|
2016-02-17 09:09:32 +00:00
|
|
|
$externalConcatBlobs[$cluster][$id] = [];
|
2006-07-20 01:21:41 +00:00
|
|
|
}
|
2010-05-22 16:50:39 +00:00
|
|
|
$externalConcatBlobs[$cluster][$id] = array_merge(
|
2006-07-20 01:21:41 +00:00
|
|
|
$externalConcatBlobs[$cluster][$id], $concatBlobs[$row->old_id]
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
2017-11-20 00:36:54 +00:00
|
|
|
$this->addError(
|
2014-04-22 20:23:18 +00:00
|
|
|
'unfixable',
|
|
|
|
|
"Error: invalid flags \"{$row->old_flags}\" on concat bulk row {$row->old_id}",
|
2006-07-20 01:21:41 +00:00
|
|
|
$concatBlobs[$row->old_id] );
|
|
|
|
|
}
|
2014-04-22 20:23:18 +00:00
|
|
|
} elseif ( strcasecmp(
|
|
|
|
|
substr( $row->header, 0, strlen( self::CONCAT_HEADER ) ),
|
|
|
|
|
self::CONCAT_HEADER
|
|
|
|
|
) ) {
|
2017-11-20 00:36:54 +00:00
|
|
|
$this->addError(
|
2014-04-22 20:23:18 +00:00
|
|
|
'restore text',
|
|
|
|
|
"Error: Incorrect object header for concat bulk row {$row->old_id}",
|
|
|
|
|
$concatBlobs[$row->old_id]
|
|
|
|
|
);
|
2006-07-20 01:21:41 +00:00
|
|
|
} # else good
|
|
|
|
|
|
|
|
|
|
unset( $concatBlobs[$row->old_id] );
|
|
|
|
|
}
|
|
|
|
|
$dbr->freeResult( $res );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Check targets of unresolved stubs
|
|
|
|
|
$this->checkExternalConcatBlobs( $externalConcatBlobs );
|
|
|
|
|
// next chunk
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
print "\n\nErrors:\n";
|
2010-05-22 16:50:39 +00:00
|
|
|
foreach ( $this->errors as $name => $errors ) {
|
2006-07-20 01:21:41 +00:00
|
|
|
if ( count( $errors ) ) {
|
|
|
|
|
$description = $this->errorDescriptions[$name];
|
|
|
|
|
echo "$description: " . implode( ',', array_keys( $errors ) ) . "\n";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( count( $this->errors['restore text'] ) && $fix ) {
|
|
|
|
|
if ( (string)$xml !== '' ) {
|
|
|
|
|
$this->restoreText( array_keys( $this->errors['restore text'] ), $xml );
|
|
|
|
|
} else {
|
|
|
|
|
echo "Can't fix text, no XML backup specified\n";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
print "\nFlag statistics:\n";
|
|
|
|
|
$total = array_sum( $flagStats );
|
|
|
|
|
foreach ( $flagStats as $flag => $count ) {
|
|
|
|
|
printf( "%-30s %10d %5.2f%%\n", $flag, $count, $count / $total * 100 );
|
|
|
|
|
}
|
|
|
|
|
print "\nLocal object statistics:\n";
|
|
|
|
|
$total = array_sum( $objectStats );
|
|
|
|
|
foreach ( $objectStats as $className => $count ) {
|
|
|
|
|
printf( "%-30s %10d %5.2f%%\n", $className, $count, $count / $total * 100 );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-20 00:36:54 +00:00
|
|
|
function addError( $type, $msg, $ids ) {
|
2006-07-20 01:21:41 +00:00
|
|
|
if ( is_array( $ids ) && count( $ids ) == 1 ) {
|
|
|
|
|
$ids = reset( $ids );
|
|
|
|
|
}
|
|
|
|
|
if ( is_array( $ids ) ) {
|
2016-02-17 09:09:32 +00:00
|
|
|
$revIds = [];
|
2006-07-20 01:21:41 +00:00
|
|
|
foreach ( $ids as $id ) {
|
|
|
|
|
$revIds = array_merge( $revIds, array_keys( $this->oldIdMap, $id ) );
|
|
|
|
|
}
|
2010-05-22 16:50:39 +00:00
|
|
|
print "$msg in text rows " . implode( ', ', $ids ) .
|
2006-07-20 01:21:41 +00:00
|
|
|
", revisions " . implode( ', ', $revIds ) . "\n";
|
|
|
|
|
} else {
|
|
|
|
|
$id = $ids;
|
|
|
|
|
$revIds = array_keys( $this->oldIdMap, $id );
|
|
|
|
|
if ( count( $revIds ) == 1 ) {
|
|
|
|
|
print "$msg in old_id $id, rev_id {$revIds[0]}\n";
|
|
|
|
|
} else {
|
|
|
|
|
print "$msg in old_id $id, revisions " . implode( ', ', $revIds ) . "\n";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
$this->errors[$type] = $this->errors[$type] + array_flip( $revIds );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function checkExternalConcatBlobs( $externalConcatBlobs ) {
|
|
|
|
|
if ( !count( $externalConcatBlobs ) ) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( is_null( $this->dbStore ) ) {
|
|
|
|
|
$this->dbStore = new ExternalStoreDB;
|
|
|
|
|
}
|
2010-12-04 03:20:14 +00:00
|
|
|
|
2006-07-20 01:21:41 +00:00
|
|
|
foreach ( $externalConcatBlobs as $cluster => $oldIds ) {
|
|
|
|
|
$blobIds = array_keys( $oldIds );
|
|
|
|
|
$extDb =& $this->dbStore->getSlave( $cluster );
|
|
|
|
|
$blobsTable = $this->dbStore->getTable( $extDb );
|
2011-09-10 07:17:10 +00:00
|
|
|
$headerLength = strlen( self::CONCAT_HEADER );
|
2010-05-22 16:50:39 +00:00
|
|
|
$res = $extDb->select( $blobsTable,
|
2016-02-17 09:09:32 +00:00
|
|
|
[ 'blob_id', "LEFT(blob_text, $headerLength) AS header" ],
|
2017-12-14 01:56:14 +00:00
|
|
|
[ 'blob_id' => $blobIds ],
|
|
|
|
|
__METHOD__
|
|
|
|
|
);
|
2010-10-13 22:34:25 +00:00
|
|
|
foreach ( $res as $row ) {
|
2011-09-10 07:17:10 +00:00
|
|
|
if ( strcasecmp( $row->header, self::CONCAT_HEADER ) ) {
|
2017-11-20 00:36:54 +00:00
|
|
|
$this->addError(
|
2014-04-22 20:23:18 +00:00
|
|
|
'restore text',
|
|
|
|
|
"Error: invalid header on target $cluster/{$row->blob_id} of two-part ES URL",
|
|
|
|
|
$oldIds[$row->blob_id]
|
|
|
|
|
);
|
2006-07-20 01:21:41 +00:00
|
|
|
}
|
|
|
|
|
unset( $oldIds[$row->blob_id] );
|
|
|
|
|
}
|
|
|
|
|
$extDb->freeResult( $res );
|
|
|
|
|
|
|
|
|
|
// Print errors for missing blobs rows
|
2012-04-07 20:48:40 +00:00
|
|
|
foreach ( $oldIds as $blobId => $oldIds2 ) {
|
2017-11-20 00:36:54 +00:00
|
|
|
$this->addError(
|
2014-04-22 20:23:18 +00:00
|
|
|
'restore text',
|
|
|
|
|
"Error: missing target $cluster/$blobId for two-part ES URL",
|
|
|
|
|
$oldIds2
|
|
|
|
|
);
|
2006-07-20 01:21:41 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function restoreText( $revIds, $xml ) {
|
2012-05-26 03:19:55 +00:00
|
|
|
global $wgDBname;
|
|
|
|
|
$tmpDir = wfTempDir();
|
2006-07-20 01:21:41 +00:00
|
|
|
|
|
|
|
|
if ( !count( $revIds ) ) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
print "Restoring text from XML backup...\n";
|
|
|
|
|
|
2012-05-26 03:19:55 +00:00
|
|
|
$revFileName = "$tmpDir/broken-revlist-$wgDBname";
|
|
|
|
|
$filteredXmlFileName = "$tmpDir/filtered-$wgDBname.xml";
|
2010-12-04 03:20:14 +00:00
|
|
|
|
2006-07-20 01:21:41 +00:00
|
|
|
// Write revision list
|
|
|
|
|
if ( !file_put_contents( $revFileName, implode( "\n", $revIds ) ) ) {
|
|
|
|
|
echo "Error writing revision list, can't restore text\n";
|
2014-04-23 18:09:26 +00:00
|
|
|
|
2006-07-20 01:21:41 +00:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Run mwdumper
|
|
|
|
|
echo "Filtering XML dump...\n";
|
|
|
|
|
$exitStatus = 0;
|
2010-05-22 16:50:39 +00:00
|
|
|
passthru( 'mwdumper ' .
|
|
|
|
|
wfEscapeShellArg(
|
2006-07-20 01:21:41 +00:00
|
|
|
"--output=file:$filteredXmlFileName",
|
|
|
|
|
"--filter=revlist:$revFileName",
|
|
|
|
|
$xml
|
|
|
|
|
), $exitStatus
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if ( $exitStatus ) {
|
|
|
|
|
echo "mwdumper died with exit status $exitStatus\n";
|
2014-04-23 18:09:26 +00:00
|
|
|
|
2006-07-20 01:21:41 +00:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$file = fopen( $filteredXmlFileName, 'r' );
|
|
|
|
|
if ( !$file ) {
|
|
|
|
|
echo "Unable to open filtered XML file\n";
|
2014-04-23 18:09:26 +00:00
|
|
|
|
2006-07-20 01:21:41 +00:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-05 19:55:19 +00:00
|
|
|
$dbr = wfGetDB( DB_REPLICA );
|
2007-01-22 23:50:42 +00:00
|
|
|
$dbw = wfGetDB( DB_MASTER );
|
2006-07-20 01:21:41 +00:00
|
|
|
$dbr->ping();
|
|
|
|
|
$dbw->ping();
|
2010-12-04 03:20:14 +00:00
|
|
|
|
2006-07-20 01:21:41 +00:00
|
|
|
$source = new ImportStreamSource( $file );
|
2014-08-23 07:40:00 +00:00
|
|
|
$importer = new WikiImporter(
|
|
|
|
|
$source,
|
2016-11-22 23:39:22 +00:00
|
|
|
MediaWikiServices::getInstance()->getMainConfig()
|
2014-08-23 07:40:00 +00:00
|
|
|
);
|
2016-02-17 09:09:32 +00:00
|
|
|
$importer->setRevisionCallback( [ $this, 'importRevision' ] );
|
2017-12-22 03:12:28 +00:00
|
|
|
$importer->setNoticeCallback( function ( $msg, $params ) {
|
|
|
|
|
echo wfMessage( $msg, $params )->text() . "\n";
|
|
|
|
|
} );
|
2006-07-20 01:21:41 +00:00
|
|
|
$importer->doImport();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function importRevision( &$revision, &$importer ) {
|
|
|
|
|
$id = $revision->getID();
|
2013-01-26 14:36:54 +00:00
|
|
|
$content = $revision->getContent( Revision::RAW );
|
2018-06-11 09:16:48 +00:00
|
|
|
$id = $id ?: '';
|
2013-01-26 14:36:54 +00:00
|
|
|
|
|
|
|
|
if ( $content === null ) {
|
|
|
|
|
echo "Revision $id is broken, we have no content available\n";
|
2014-04-23 18:09:26 +00:00
|
|
|
|
2013-01-26 14:36:54 +00:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$text = $content->serialize();
|
2006-07-20 01:21:41 +00:00
|
|
|
if ( $text === '' ) {
|
2010-12-04 03:20:14 +00:00
|
|
|
// This is what happens if the revision was broken at the time the
|
|
|
|
|
// dump was made. Unfortunately, it also happens if the revision was
|
2006-07-20 01:21:41 +00:00
|
|
|
// legitimately blank, so there's no way to tell the difference. To
|
|
|
|
|
// be safe, we'll skip it and leave it broken
|
2013-01-26 14:36:54 +00:00
|
|
|
|
2006-07-20 01:21:41 +00:00
|
|
|
echo "Revision $id is blank in the dump, may have been broken before export\n";
|
2014-04-23 18:09:26 +00:00
|
|
|
|
2006-07-20 01:21:41 +00:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2013-04-27 11:23:52 +00:00
|
|
|
if ( !$id ) {
|
2006-07-20 01:21:41 +00:00
|
|
|
// No ID, can't import
|
|
|
|
|
echo "No id tag in revision, can't import\n";
|
2014-04-23 18:09:26 +00:00
|
|
|
|
2006-07-20 01:21:41 +00:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Find text row again
|
2016-09-05 19:55:19 +00:00
|
|
|
$dbr = wfGetDB( DB_REPLICA );
|
2016-02-17 09:09:32 +00:00
|
|
|
$oldId = $dbr->selectField( 'revision', 'rev_text_id', [ 'rev_id' => $id ], __METHOD__ );
|
2006-07-20 01:21:41 +00:00
|
|
|
if ( !$oldId ) {
|
|
|
|
|
echo "Missing revision row for rev_id $id\n";
|
2014-04-23 18:09:26 +00:00
|
|
|
|
2006-07-20 01:21:41 +00:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Compress the text
|
|
|
|
|
$flags = Revision::compressRevisionText( $text );
|
|
|
|
|
|
|
|
|
|
// Update the text row
|
2007-01-22 23:50:42 +00:00
|
|
|
$dbw = wfGetDB( DB_MASTER );
|
2010-05-22 16:50:39 +00:00
|
|
|
$dbw->update( 'text',
|
2016-02-17 09:09:32 +00:00
|
|
|
[ 'old_flags' => $flags, 'old_text' => $text ],
|
|
|
|
|
[ 'old_id' => $oldId ],
|
|
|
|
|
__METHOD__, [ 'LIMIT' => 1 ]
|
2006-07-20 01:21:41 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// Remove it from the unfixed list and add it to the fixed list
|
|
|
|
|
unset( $this->errors['restore text'][$id] );
|
|
|
|
|
$this->errors['fixed'][$id] = true;
|
|
|
|
|
}
|
|
|
|
|
}
|