checkstorage.php

来自「php 开发的内容管理系统」· PHP 代码 · 共 469 行 · 第 1/2 页

PHP
469
字号
							$this->error( 'unfixable', "Error: unrecognised object class \"$className\"", $oldId );
					}
				}
				$dbr->freeResult( $res );
			}

			// Check local concat blob validity
			$externalConcatBlobs = array();
			if ( count( $concatBlobs ) ) {
				$headerLength = 300;
				$res = $dbr->select( 'text', array( 'old_id', 'old_flags', "LEFT(old_text, $headerLength) AS header" ), 
					array( 'old_id IN (' . implode( ',', array_keys( $concatBlobs ) ) . ')' ), $fname );
				while ( $row = $dbr->fetchObject( $res ) ) {
					$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:' ) {
								$this->error( 'unfixable', "Error: unrecognised external storage type \"{$urlParts[0]}", $row->old_id );
							} else {
								$cluster = $urlParts[2];
								$id = $urlParts[3];
								if ( !isset( $externalConcatBlobs[$cluster][$id] ) ) {
									$externalConcatBlobs[$cluster][$id] = array();
								}
								$externalConcatBlobs[$cluster][$id] = array_merge( 
									$externalConcatBlobs[$cluster][$id], $concatBlobs[$row->old_id]
								);
							}
						} else {
							$this->error( 'unfixable', "Error: invalid flags \"{$row->old_flags}\" on concat bulk row {$row->old_id}",
								$concatBlobs[$row->old_id] );
						}
					} elseif ( strcasecmp( substr( $row->header, 0, strlen( CONCAT_HEADER ) ), CONCAT_HEADER ) ) {
						$this->error( 'restore text', "Error: Incorrect object header for concat bulk row {$row->old_id}", 
							$concatBlobs[$row->old_id] );
					} # else good

					unset( $concatBlobs[$row->old_id] );
				}
				$dbr->freeResult( $res );
			}

			// Check targets of unresolved stubs
			$this->checkExternalConcatBlobs( $externalConcatBlobs );

			// next chunk
		}

		print "\n\nErrors:\n";
		foreach( $this->errors as $name => $errors ) {
			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 );
		}
	}


	function error( $type, $msg, $ids ) {
		if ( is_array( $ids ) && count( $ids ) == 1 ) {
			$ids = reset( $ids );
		}
		if ( is_array( $ids ) ) {
			$revIds = array();
			foreach ( $ids as $id ) {
				$revIds = array_merge( $revIds, array_keys( $this->oldIdMap, $id ) );
			}
			print "$msg in text rows " . implode( ', ', $ids ) . 
				", 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 ) {
		$fname = 'CheckStorage::checkExternalConcatBlobs';
		if ( !count( $externalConcatBlobs ) ) {
			return;
		}

		if ( is_null( $this->dbStore ) ) {
			$this->dbStore = new ExternalStoreDB;
		}
		
		foreach ( $externalConcatBlobs as $cluster => $oldIds ) {
			$blobIds = array_keys( $oldIds );
			$extDb =& $this->dbStore->getSlave( $cluster );
			$blobsTable = $this->dbStore->getTable( $extDb );
			$headerLength = strlen( CONCAT_HEADER );
			$res = $extDb->select( $blobsTable, 
				array( 'blob_id', "LEFT(blob_text, $headerLength) AS header" ), 
				array( 'blob_id IN( ' . implode( ',', $blobIds ) . ')' ), $fname );
			while ( $row = $extDb->fetchObject( $res ) ) {
				if ( strcasecmp( $row->header, CONCAT_HEADER ) ) {
					$this->error( 'restore text', "Error: invalid header on target $cluster/{$row->blob_id} of two-part ES URL", 
						$oldIds[$row->blob_id] );
				}
				unset( $oldIds[$row->blob_id] );

			}
			$extDb->freeResult( $res );

			// Print errors for missing blobs rows
			foreach ( $oldIds as $blobId => $oldIds ) {
				$this->error( 'restore text', "Error: missing target $cluster/$blobId for two-part ES URL", $oldIds );
			}
		}
	}

	function restoreText( $revIds, $xml ) {
		global $wgTmpDirectory, $wgDBname;

		if ( !count( $revIds ) ) {
			return;
		}

		print "Restoring text from XML backup...\n";

		$revFileName = "$wgTmpDirectory/broken-revlist-$wgDBname";
		$filteredXmlFileName = "$wgTmpDirectory/filtered-$wgDBname.xml";
		
		// Write revision list
		if ( !file_put_contents( $revFileName, implode( "\n", $revIds ) ) ) {
			echo "Error writing revision list, can't restore text\n";
			return;
		}

		// Run mwdumper
		echo "Filtering XML dump...\n";
		$exitStatus = 0;
		passthru( 'mwdumper ' . 
			wfEscapeShellArg( 
				"--output=file:$filteredXmlFileName",
				"--filter=revlist:$revFileName",
				$xml
			), $exitStatus
		);

		if ( $exitStatus ) {
			echo "mwdumper died with exit status $exitStatus\n";
			return;
		}

		$file = fopen( $filteredXmlFileName, 'r' );
		if ( !$file ) {
			echo "Unable to open filtered XML file\n";
			return;
		}

		$dbr =& wfGetDB( DB_SLAVE );
		$dbw =& wfGetDB( DB_MASTER );
		$dbr->ping();
		$dbw->ping();
		
		$source = new ImportStreamSource( $file );
		$importer = new WikiImporter( $source );
		$importer->setRevisionCallback( array( &$this, 'importRevision' ) );
		$importer->doImport();
	}

	function importRevision( &$revision, &$importer ) {
		$fname = 'CheckStorage::importRevision';

		$id = $revision->getID();
		$text = $revision->getText();
		if ( $text === '' ) {
			// This is what happens if the revision was broken at the time the 
			// dump was made. Unfortunately, it also happens if the revision was 
			// legitimately blank, so there's no way to tell the difference. To
			// be safe, we'll skip it and leave it broken
			$id = $id ? $id : '';
			echo "Revision $id is blank in the dump, may have been broken before export\n";
			return;
		}

		if ( !$id )  {
			// No ID, can't import
			echo "No id tag in revision, can't import\n";
			return;
		}

		// Find text row again
		$dbr =& wfGetDB( DB_SLAVE );
		$oldId = $dbr->selectField( 'revision', 'rev_text_id', array( 'rev_id' => $id ), $fname );
		if ( !$oldId ) {
			echo "Missing revision row for rev_id $id\n";
			return;
		}

		// Compress the text
		$flags = Revision::compressRevisionText( $text );

		// Update the text row
		$dbw->update( 'text', 
			array( 'old_flags' => $flags, 'old_text' => $text ),
			array( 'old_id' => $oldId ),
			$fname, array( 'LIMIT' => 1 )
		);

		// Remove it from the unfixed list and add it to the fixed list
		unset( $this->errors['restore text'][$id] );
		$this->errors['fixed'][$id] = true;
	}
}
?>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?