| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
f |
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
require_once __DIR__ . '/Maintenance.php'; |
| 32 |
|
| 33 |
|
| 34 |
use MediaWiki\FileRepo\File\FileSelectQueryBuilder; |
| 35 |
use Wikimedia\Rdbms\IExpression; |
| 36 |
use Wikimedia\Rdbms\IMaintainableDatabase; |
| 37 |
use Wikimedia\Rdbms\IReadableDatabase; |
| 38 |
use Wikimedia\Rdbms\LikeValue; |
| 39 |
use Wikimedia\Rdbms\SelectQueryBuilder; |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
class RefreshImageMetadata extends Maintenance { |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
protected $dbw; |
| 52 |
|
| 53 |
public function __construct() { |
| 54 |
parent::__construct(); |
| 55 |
|
| 56 |
$this->addDescription( 'Script to update image metadata records' ); |
| 57 |
$this->setBatchSize( 200 ); |
| 58 |
|
| 59 |
$this->addOption( |
| 60 |
'force', |
| 61 |
'Reload metadata from file even if the metadata looks ok', |
| 62 |
false, |
| 63 |
false, |
| 64 |
'f' |
| 65 |
); |
| 66 |
$this->addOption( |
| 67 |
'broken-only', |
| 68 |
'Only fix really broken records, leave old but still compatible records alone.' |
| 69 |
); |
| 70 |
$this->addOption( |
| 71 |
'convert-to-json', |
| 72 |
'Fix records with an out of date serialization format.' |
| 73 |
); |
| 74 |
$this->addOption( |
| 75 |
'split', |
| 76 |
'Enable splitting out large metadata items to the text table. Implies --convert-to-json.' |
| 77 |
); |
| 78 |
$this->addOption( |
| 79 |
'verbose', |
| 80 |
'Output extra information about each upgraded/non-upgraded file.', |
| 81 |
false, |
| 82 |
false, |
| 83 |
'v' |
| 84 |
); |
| 85 |
$this->addOption( 'start', 'Name of file to start with', false, true ); |
| 86 |
$this->addOption( 'end', 'Name of file to end with', false, true ); |
| 87 |
|
| 88 |
$this->addOption( |
| 89 |
'mediatype', |
| 90 |
'Only refresh files with this media type, e.g. BITMAP, UNKNOWN etc.', |
| 91 |
false, |
| 92 |
true |
| 93 |
); |
| 94 |
$this->addOption( |
| 95 |
'mime', |
| 96 |
"Only refresh files with this MIME type. Can accept wild-card 'image/*'. " |
| 97 |
. "Potentially inefficient unless 'mediatype' is also specified", |
| 98 |
false, |
| 99 |
true |
| 100 |
); |
| 101 |
$this->addOption( |
| 102 |
'metadata-contains', |
| 103 |
'(Inefficient!) Only refresh files where the img_metadata field ' |
| 104 |
. 'contains this string. Can be used if its known a specific ' |
| 105 |
. 'property was being extracted incorrectly.', |
| 106 |
false, |
| 107 |
true |
| 108 |
); |
| 109 |
$this->addOption( |
| 110 |
'sleep', |
| 111 |
'Time to sleep between each batch (in seconds). Default: 0', |
| 112 |
false, |
| 113 |
true |
| 114 |
); |
| 115 |
$this->addOption( 'oldimage', 'Run and refresh on oldimage table.' ); |
| 116 |
} |
| 117 |
|
| 118 |
public function execute() { |
| 119 |
$force = $this->hasOption( 'force' ); |
| 120 |
$brokenOnly = $this->hasOption( 'broken-only' ); |
| 121 |
$verbose = $this->hasOption( 'verbose' ); |
| 122 |
$start = $this->getOption( 'start', false ); |
| 123 |
$split = $this->hasOption( 'split' ); |
| 124 |
$sleep = (int)$this->getOption( 'sleep', 0 ); |
| 125 |
$reserialize = $this->hasOption( 'convert-to-json' ); |
| 126 |
$oldimage = $this->hasOption( 'oldimage' ); |
| 127 |
|
| 128 |
$dbw = $this->getPrimaryDB(); |
| 129 |
if ( $oldimage ) { |
| 130 |
$fieldPrefix = 'oi_'; |
| 131 |
$queryBuilderTemplate = FileSelectQueryBuilder::newForOldFile( $dbw ); |
| 132 |
} else { |
| 133 |
$fieldPrefix = 'img_'; |
| 134 |
$queryBuilderTemplate = FileSelectQueryBuilder::newForFile( $dbw ); |
| 135 |
} |
| 136 |
|
| 137 |
$upgraded = 0; |
| 138 |
$leftAlone = 0; |
| 139 |
$error = 0; |
| 140 |
$batchSize = intval( $this->getBatchSize() ); |
| 141 |
if ( $batchSize <= 0 ) { |
| 142 |
$this->fatalError( "Batch size is too low...", 12 ); |
| 143 |
} |
| 144 |
$repo = $this->newLocalRepo( $force, $brokenOnly, $reserialize, $split ); |
| 145 |
$this->setConditions( $dbw, $queryBuilderTemplate, $fieldPrefix ); |
| 146 |
$queryBuilderTemplate |
| 147 |
->orderBy( $fieldPrefix . 'name', SelectQueryBuilder::SORT_ASC ) |
| 148 |
->limit( $batchSize ); |
| 149 |
|
| 150 |
$batchCondition = []; |
| 151 |
|
| 152 |
if ( $start !== false ) { |
| 153 |
$batchCondition[] = $dbw->expr( $fieldPrefix . 'name', '>=', $start ); |
| 154 |
} |
| 155 |
do { |
| 156 |
$queryBuilder = clone $queryBuilderTemplate; |
| 157 |
$res = $queryBuilder->andWhere( $batchCondition ) |
| 158 |
->caller( __METHOD__ )->fetchResultSet(); |
| 159 |
$nameField = $fieldPrefix . 'name'; |
| 160 |
if ( $res->numRows() > 0 ) { |
| 161 |
$row1 = $res->current(); |
| 162 |
$this->output( "Processing next {$res->numRows()} row(s) starting with {$row1->$nameField}.\n" ); |
| 163 |
$res->rewind(); |
| 164 |
} |
| 165 |
|
| 166 |
foreach ( $res as $row ) { |
| 167 |
try { |
| 168 |
|
| 169 |
$file = $repo->newFileFromRow( $row ); |
| 170 |
$file->maybeUpgradeRow(); |
| 171 |
if ( $file->getUpgraded() ) { |
| 172 |
|
| 173 |
$upgraded++; |
| 174 |
$this->output( "Refreshed File:{$row->$nameField}.\n" ); |
| 175 |
} else { |
| 176 |
$leftAlone++; |
| 177 |
if ( $force ) { |
| 178 |
$file->upgradeRow(); |
| 179 |
if ( $verbose ) { |
| 180 |
$this->output( "Forcibly refreshed File:{$row->$nameField}.\n" ); |
| 181 |
} |
| 182 |
} else { |
| 183 |
if ( $verbose ) { |
| 184 |
$this->output( "Skipping File:{$row->$nameField}.\n" ); |
| 185 |
} |
| 186 |
} |
| 187 |
} |
| 188 |
} catch ( Exception $e ) { |
| 189 |
$this->output( "{$row->$nameField} failed. {$e->getMessage()}\n" ); |
| 190 |
} |
| 191 |
} |
| 192 |
if ( $res->numRows() > 0 ) { |
| 193 |
|
| 194 |
$batchCondition = [ $dbw->expr( $fieldPrefix . 'name', '>', $row->$nameField ) ]; |
| 195 |
} |
| 196 |
$this->waitForReplication(); |
| 197 |
if ( $sleep ) { |
| 198 |
sleep( $sleep ); |
| 199 |
} |
| 200 |
} while ( $res->numRows() === $batchSize ); |
| 201 |
|
| 202 |
$total = $upgraded + $leftAlone; |
| 203 |
if ( $force ) { |
| 204 |
$this->output( "\nFinished refreshing file metadata for $total files. " |
| 205 |
. "$upgraded needed to be refreshed, $leftAlone did not need to " |
| 206 |
. "be but were refreshed anyways, and $error refreshes were suspicious.\n" ); |
| 207 |
} else { |
| 208 |
$this->output( "\nFinished refreshing file metadata for $total files. " |
| 209 |
. "$upgraded were refreshed, $leftAlone were already up to date, " |
| 210 |
. "and $error refreshes were suspicious.\n" ); |
| 211 |
} |
| 212 |
} |
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
private function setConditions( IReadableDatabase $dbw, SelectQueryBuilder $queryBuilder, $fieldPrefix ) { |
| 221 |
$end = $this->getOption( 'end', false ); |
| 222 |
$mime = $this->getOption( 'mime', false ); |
| 223 |
$mediatype = $this->getOption( 'mediatype', false ); |
| 224 |
$like = $this->getOption( 'metadata-contains', false ); |
| 225 |
|
| 226 |
if ( $end !== false ) { |
| 227 |
$queryBuilder->andWhere( $dbw->expr( $fieldPrefix . 'name', '<=', $end ) ); |
| 228 |
} |
| 229 |
if ( $mime !== false ) { |
| 230 |
[ $major, $minor ] = File::splitMime( $mime ); |
| 231 |
$queryBuilder->andWhere( [ $fieldPrefix . 'major_mime' => $major ] ); |
| 232 |
if ( $minor !== '*' ) { |
| 233 |
$queryBuilder->andWhere( [ $fieldPrefix . 'minor_mime' => $minor ] ); |
| 234 |
} |
| 235 |
} |
| 236 |
if ( $mediatype !== false ) { |
| 237 |
$queryBuilder->andWhere( [ $fieldPrefix . 'media_type' => $mediatype ] ); |
| 238 |
} |
| 239 |
if ( $like ) { |
| 240 |
$queryBuilder->andWhere( |
| 241 |
$dbw->expr( $fieldPrefix . 'metadata', IExpression::LIKE, |
| 242 |
new LikeValue( $dbw->anyString(), $like, $dbw->anyString() ) ) |
| 243 |
); |
| 244 |
} |
| 245 |
} |
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
private function newLocalRepo( $force, $brokenOnly, $reserialize, $split ): LocalRepo { |
| 256 |
if ( $brokenOnly && $force ) { |
| 257 |
$this->fatalError( 'Cannot use --broken-only and --force together. ', 2 ); |
| 258 |
} |
| 259 |
$reserialize = $reserialize || $split; |
| 260 |
if ( $brokenOnly && $reserialize ) { |
| 261 |
$this->fatalError( 'Cannot use --broken-only with --convert-to-json or --split. ', |
| 262 |
2 ); |
| 263 |
} |
| 264 |
|
| 265 |
$overrides = [ |
| 266 |
'updateCompatibleMetadata' => !$brokenOnly, |
| 267 |
]; |
| 268 |
if ( $reserialize ) { |
| 269 |
$overrides['reserializeMetadata'] = true; |
| 270 |
$overrides['useJsonMetadata'] = true; |
| 271 |
} |
| 272 |
if ( $split ) { |
| 273 |
$overrides['useSplitMetadata'] = true; |
| 274 |
} |
| 275 |
|
| 276 |
return $this->getServiceContainer()->getRepoGroup() |
| 277 |
->newCustomLocalRepo( $overrides ); |
| 278 |
} |
| 279 |
} |
| 280 |
|
| 281 |
|
| 282 |
$maintClass = RefreshImageMetadata::class; |
| 283 |
require_once RUN_MAINTENANCE_IF_MAIN; |
| 284 |
|