checkstorage.php
来自「php 开发的内容管理系统」· PHP 代码 · 共 469 行 · 第 1/2 页
PHP
469 行
<?php
/**
* Fsck for MediaWiki
*/
define( 'CONCAT_HEADER', 'O:27:"concatenatedgziphistoryblob"' );
if ( !defined( 'MEDIAWIKI' ) ) {
require_once( dirname(__FILE__) . '/../commandLine.inc' );
require_once( 'ExternalStore.php' );
require_once( 'ExternalStoreDB.php' );
require_once( 'SpecialImport.php' );
$cs = new CheckStorage;
$fix = isset( $options['fix'] );
if ( isset( $args[0] ) ) {
$xml = $args[0];
} else {
$xml = false;
}
$cs->check( $fix, $xml );
}
//----------------------------------------------------------------------------------
class CheckStorage
{
var $oldIdMap, $errors;
var $dbStore = null;
var $errorDescriptions = array(
'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',
);
function check( $fix = false, $xml = '' ) {
$fname = 'checkStorage';
$dbr =& wfGetDB( DB_SLAVE );
if ( $fix ) {
$dbw =& wfGetDB( DB_MASTER );
print "Checking, will fix errors if possible...\n";
} else {
print "Checking...\n";
}
$maxRevId = $dbr->selectField( 'revision', 'MAX(rev_id)', false, $fname );
$chunkSize = 1000;
$flagStats = array();
$objectStats = array();
$knownFlags = array( 'external', 'gzip', 'object', 'utf-8' );
$this->errors = array(
'restore text' => array(),
'restore revision' => array(),
'unfixable' => array(),
'fixed' => array(),
'fixable' => array(),
);
for ( $chunkStart = 1 ; $chunkStart < $maxRevId; $chunkStart += $chunkSize ) {
$chunkEnd = $chunkStart + $chunkSize - 1;
//print "$chunkStart of $maxRevId\n";
// Fetch revision rows
$this->oldIdMap = array();
$dbr->ping();
$res = $dbr->select( 'revision', array( 'rev_id', 'rev_text_id' ),
array( "rev_id BETWEEN $chunkStart AND $chunkEnd" ), $fname );
while ( $row = $dbr->fetchObject( $res ) ) {
$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 );
$externalRevs = array();
$objectRevs = array();
$res = $dbr->select( 'text', array( 'old_id', 'old_flags' ),
'old_id IN (' . implode( ',', $this->oldIdMap ) . ')', $fname );
while ( $row = $dbr->fetchObject( $res ) ) {
$flags = $row->old_flags;
$id = $row->old_id;
// Create flagStats row if it doesn't exist
$flagStats = $flagStats + array( $flags => 0 );
// Increment counter
$flagStats[$flags]++;
// Not missing
unset( $missingTextRows[$row->old_id] );
// Check for external or object
if ( $flags == '' ) {
$flagArray = array();
} 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 ) {
$this->error( 'fixed', "Warning: old_flags set to 0", $id );
$dbw->ping();
$dbw->update( 'text', array( 'old_flags' => '' ),
array( 'old_id' => $id ), $fname );
echo "Fixed\n";
} else {
$this->error( 'fixable', "Warning: old_flags set to 0", $id );
}
} elseif ( count( array_diff( $flagArray, $knownFlags ) ) ) {
$this->error( 'unfixable', "Error: invalid flags field \"$flags\"", $id );
}
}
$dbr->freeResult( $res );
// Output errors for any missing text rows
foreach ( $missingTextRows as $oldId => $revId ) {
$this->error( 'restore revision', "Error: missing text row", $oldId );
}
// Verify external revisions
$externalConcatBlobs = array();
$externalNormalBlobs = array();
if ( count( $externalRevs ) ) {
$res = $dbr->select( 'text', array( 'old_id', 'old_flags', 'old_text' ),
array( 'old_id IN (' . implode( ',', $externalRevs ) . ')' ), $fname );
while ( $row = $dbr->fetchObject( $res ) ) {
$urlParts = explode( '://', $row->old_text, 2 );
if ( count( $urlParts ) !== 2 || $urlParts[1] == '' ) {
$this->error( 'restore text', "Error: invalid URL \"{$row->old_text}\"", $row->old_id );
continue;
}
list( $proto, $path ) = $urlParts;
if ( $proto != 'DB' ) {
$this->error( 'restore text', "Error: invalid external protocol \"$proto\"", $row->old_id );
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 );
// 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 );
$res = $extDb->select( $blobsTable,
array( 'blob_id' ),
array( 'blob_id IN( ' . implode( ',', $blobIds ) . ')' ), $fname );
while ( $row = $extDb->fetchObject( $res ) ) {
unset( $xBlobIds[$row->blob_id] );
}
$extDb->freeResult( $res );
// Print errors for missing blobs rows
foreach ( $xBlobIds as $blobId => $oldId ) {
$this->error( 'restore text', "Error: missing target $blobId for one-part ES URL", $oldId );
}
}
}
// Check local objects
$dbr->ping();
$concatBlobs = array();
$curIds = array();
if ( count( $objectRevs ) ) {
$headerLength = 300;
$res = $dbr->select( 'text', array( 'old_id', 'old_flags', "LEFT(old_text, $headerLength) AS header" ),
array( 'old_id IN (' . implode( ',', $objectRevs ) . ')' ), $fname );
while ( $row = $dbr->fetchObject( $res ) ) {
$oldId = $row->old_id;
if ( !preg_match( '/^O:(\d+):"(\w+)"/', $row->header, $matches ) ) {
$this->error( 'restore text', "Error: invalid object header", $oldId );
continue;
}
$className = strtolower( $matches[2] );
if ( strlen( $className ) != $matches[1] ) {
$this->error( 'restore text', "Error: invalid object header, wrong class name length", $oldId );
continue;
}
$objectStats = $objectStats + array( $className => 0 );
$objectStats[$className]++;
switch ( $className ) {
case 'concatenatedgziphistoryblob':
// Good
break;
case 'historyblobstub':
case 'historyblobcurstub':
if ( strlen( $row->header ) == $headerLength ) {
$this->error( 'unfixable', "Error: overlong stub header", $oldId );
continue;
}
$stubObj = unserialize( $row->header );
if ( !is_object( $stubObj ) ) {
$this->error( 'restore text', "Error: unable to unserialize stub object", $oldId );
continue;
}
if ( $className == 'historyblobstub' ) {
$concatBlobs[$stubObj->mOldId][] = $oldId;
} else {
$curIds[$stubObj->mCurId][] = $oldId;
}
break;
default:
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?