Localisation updates from https://translatewiki.net.
[mediawiki.git] / maintenance / rebuildImages.php
blob2dae3d185d8ba4db3ad4e8e3231ef2f5927e7cc9
1 <?php
2 /**
3 * Update image metadata records.
5 * Usage: php rebuildImages.php [--missing] [--dry-run]
6 * Options:
7 * --missing Crawl the uploads dir for images without records, and
8 * add them only.
10 * Copyright © 2005 Brooke Vibber <bvibber@wikimedia.org>
11 * https://www.mediawiki.org/
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License along
24 * with this program; if not, write to the Free Software Foundation, Inc.,
25 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
26 * http://www.gnu.org/copyleft/gpl.html
28 * @file
29 * @author Brooke Vibber <bvibber@wikimedia.org>
30 * @ingroup Maintenance
33 // @codeCoverageIgnoreStart
34 require_once __DIR__ . '/Maintenance.php';
35 // @codeCoverageIgnoreEnd
37 use MediaWiki\FileRepo\File\FileSelectQueryBuilder;
38 use MediaWiki\Maintenance\Maintenance;
39 use MediaWiki\Specials\SpecialUpload;
40 use MediaWiki\User\User;
41 use Wikimedia\Rdbms\IMaintainableDatabase;
43 /**
44 * Maintenance script to update image metadata records.
46 * @ingroup Maintenance
48 class ImageBuilder extends Maintenance {
49 /**
50 * @var IMaintainableDatabase
52 protected $dbw;
54 /** @var bool */
55 private $dryrun;
57 /** @var LocalRepo|null */
58 private $repo;
60 /** @var int */
61 private $updated;
63 /** @var int */
64 private $processed;
66 /** @var int */
67 private $count;
69 /** @var float */
70 private $startTime;
72 /** @var string */
73 private $table;
75 public function __construct() {
76 parent::__construct();
77 $this->addDescription( 'Script to update image metadata records' );
79 $this->addOption( 'missing', 'Check for files without associated database record' );
80 $this->addOption( 'dry-run', 'Only report, don\'t update the database' );
83 public function execute() {
84 $this->dbw = $this->getPrimaryDB();
85 $this->dryrun = $this->hasOption( 'dry-run' );
86 if ( $this->dryrun ) {
87 $this->getServiceContainer()->getReadOnlyMode()
88 ->setReason( 'Dry run mode, image upgrades are suppressed' );
91 if ( $this->hasOption( 'missing' ) ) {
92 $this->crawlMissing();
93 } else {
94 $this->build();
98 /**
99 * @return LocalRepo
101 private function getRepo() {
102 if ( $this->repo === null ) {
103 $this->repo = $this->getServiceContainer()->getRepoGroup()
104 ->newCustomLocalRepo( [
105 // make sure to update old, but compatible img_metadata fields.
106 'updateCompatibleMetadata' => true
107 ] );
110 return $this->repo;
113 private function build() {
114 $this->buildImage();
115 $this->buildOldImage();
119 * @param int $count
120 * @param string $table
122 private function init( $count, $table ) {
123 $this->processed = 0;
124 $this->updated = 0;
125 $this->count = $count;
126 $this->startTime = microtime( true );
127 $this->table = $table;
130 private function progress( $updated ) {
131 $this->updated += $updated;
132 $this->processed++;
133 if ( $this->processed % 100 != 0 ) {
134 return;
136 $portion = $this->processed / $this->count;
137 $updateRate = $this->updated / $this->processed;
139 $now = microtime( true );
140 $delta = $now - $this->startTime;
141 $estimatedTotalTime = $delta / $portion;
142 $eta = $this->startTime + $estimatedTotalTime;
143 $rate = $this->processed / $delta;
145 $this->output( sprintf( "%s: %6.2f%% done on %s; ETA %s [%d/%d] %.2f/sec <%.2f%% updated>\n",
146 wfTimestamp( TS_DB, intval( $now ) ),
147 $portion * 100.0,
148 $this->table,
149 wfTimestamp( TS_DB, intval( $eta ) ),
150 $this->processed,
151 $this->count,
152 $rate,
153 $updateRate * 100.0 ) );
154 flush();
157 private function buildTable( $table, $queryBuilder, $callback ) {
158 $count = $this->dbw->newSelectQueryBuilder()
159 ->select( 'count(*)' )
160 ->from( $table )
161 ->caller( __METHOD__ )->fetchField();
162 $this->init( $count, $table );
163 $this->output( "Processing $table...\n" );
165 $result = $queryBuilder->caller( __METHOD__ )->fetchResultSet();
167 foreach ( $result as $row ) {
168 $update = call_user_func( $callback, $row );
169 if ( $update ) {
170 $this->progress( 1 );
171 } else {
172 $this->progress( 0 );
175 $this->output( "Finished $table... $this->updated of $this->processed rows updated\n" );
178 private function buildImage() {
179 $callback = [ $this, 'imageCallback' ];
180 $this->buildTable( 'image', FileSelectQueryBuilder::newForFile( $this->getReplicaDB() ), $callback );
183 private function imageCallback( $row ) {
184 // Create a File object from the row
185 // This will also upgrade it
186 $file = $this->getRepo()->newFileFromRow( $row );
188 return $file->getUpgraded();
191 private function buildOldImage() {
192 $this->buildTable( 'oldimage', FileSelectQueryBuilder::newForOldFile( $this->getReplicaDB() ),
193 [ $this, 'oldimageCallback' ] );
196 private function oldimageCallback( $row ) {
197 // Create a File object from the row
198 // This will also upgrade it
199 if ( $row->oi_archive_name == '' ) {
200 $this->output( "Empty oi_archive_name for oi_name={$row->oi_name}\n" );
202 return false;
204 $file = $this->getRepo()->newFileFromRow( $row );
206 return $file->getUpgraded();
209 private function crawlMissing() {
210 $this->getRepo()->enumFiles( [ $this, 'checkMissingImage' ] );
213 public function checkMissingImage( $fullpath ) {
214 $filename = wfBaseName( $fullpath );
215 $row = $this->dbw->newSelectQueryBuilder()
216 ->select( [ 'img_name' ] )
217 ->from( 'image' )
218 ->where( [ 'img_name' => $filename ] )
219 ->caller( __METHOD__ )->fetchRow();
221 if ( !$row ) {
222 // file not registered
223 $this->addMissingImage( $filename, $fullpath );
227 private function addMissingImage( $filename, $fullpath ) {
228 $timestamp = $this->dbw->timestamp( $this->getRepo()->getFileTimestamp( $fullpath ) );
229 $services = $this->getServiceContainer();
231 $altname = $services->getContentLanguage()->checkTitleEncoding( $filename );
232 if ( $altname != $filename ) {
233 if ( $this->dryrun ) {
234 $filename = $altname;
235 $this->output( "Estimating transcoding... $altname\n" );
236 } else {
237 // @fixme create renameFile()
238 // @phan-suppress-next-line PhanUndeclaredMethod See comment above...
239 $filename = $this->renameFile( $filename );
243 if ( $filename == '' ) {
244 $this->output( "Empty filename for $fullpath\n" );
246 return;
248 if ( !$this->dryrun ) {
249 $file = $services->getRepoGroup()->getLocalRepo()->newFile( $filename );
250 $pageText = SpecialUpload::getInitialPageText(
251 '(recovered file, missing upload log entry)'
253 $user = User::newSystemUser( User::MAINTENANCE_SCRIPT_USER, [ 'steal' => true ] );
254 $status = $file->recordUpload3(
256 '(recovered file, missing upload log entry)',
257 $pageText,
258 $user,
259 false,
260 $timestamp
262 if ( !$status->isOK() ) {
263 $this->output( "Error uploading file $fullpath\n" );
265 return;
268 $this->output( $fullpath . "\n" );
272 // @codeCoverageIgnoreStart
273 $maintClass = ImageBuilder::class;
274 require_once RUN_MAINTENANCE_IF_MAIN;
275 // @codeCoverageIgnoreEnd