User:VasilievVV/checkUnregistered.php

From mediawiki.org
function wfCheckUnregisteredInPublic() {
	$repo = RepoGroup::singleton()->getLocalRepo();
	$root = $repo->directory;
	$dirs = array();
	for( $i = 0; $i < 16; $i++ )
		for( $j = 0; $j < 16; $j++ ) {
			$hex1 = wfBaseConvert( $i, 10, 16 );
			$hex2 = wfBaseConvert( $j, 10, 16 );
			$path = "{$root}/{$hex1}/{$hex1}{$hex2}";
			if( is_dir( $path ) )
				$dirs[] = $path;
		}
	echo "Checking files in public repo... 0% checked\n";
	$i = 0;
	$dbr = wfGetDB( DB_SLAVE );
	foreach( $dirs as $dir ) {
		$i++;
		$h = opendir( $dir );
		while( $file = readdir( $h ) ) {
			$whitelist = array( '.', '..' );
			if( in_array( $file, $whitelist ) )
				continue;
			$fpath = "{$dir}/{$file}";

			$hash = md5( $file );
			if( substr( $dir, strlen( $dir ) - 4 ) != "{$hash[0]}/{$hash[0]}{$hash[1]}" ) {
				echo "WARNING: file {$fpath} is stored in the wrong direcroty\n";
			}

			$row = $dbr->selectRow( 'image', '*', array( 'img_name' => $file ), __METHOD__ );
			$f = wfLocalFile( $file );
			if( !$f->exists() ) {
				echo "WARNING: file {$file} is not registered in the database\n";
				continue;
			}
			if( $row->img_sha1 != File::sha1Base36( $fpath ) ) {
				echo "WARNING: file {$file} has SHA-1 hash that doesn't match with stored in the database\n";
			}
		}
		closedir( $h );
		echo " " . wfPercent( $i / count( $dirs ) * 100, 0 ) . " checked\n";
	}
}