Debian Codegraph @

maintenance/rebuildImages.php

raw symbols SCIP
1 <?php
2 /**
3 * Update image metadata records.
4 *
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.
9 *
10 * Copyright © 2005 Brooke Vibber <[email protected]>
11 * https://www.mediawiki.org/
12 *
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.
17 *
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.
22 *
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
27 *
28 * @file
29 * @author Brooke Vibber <[email protected]>
30 * @ingroup Maintenance
31 */
32
33 // @codeCoverageIgnoreStart
34 require_once __DIR__ . '/Maintenance.php';
35 // @codeCoverageIgnoreEnd
36
37 use MediaWiki\FileRepo\File\FileSelectQueryBuilder;
38 use MediaWiki\Specials\SpecialUpload;
39 use MediaWiki\User\User;
40 use Wikimedia\Rdbms\IMaintainableDatabase;
41
42 /**
43 * Maintenance script to update image metadata records.
44 *
45 * @ingroup Maintenance
46 */
47 class ImageBuilder extends Maintenance {
48 /**
49 * @var IMaintainableDatabase
50 */
51 protected $dbw;
52
53 /** @var bool */
54 private $dryrun;
55
56 /** @var LocalRepo|null */
57 private $repo;
58
59 /** @var int */
60 private $updated;
61
62 /** @var int */
63 private $processed;
64
65 /** @var int */
66 private $count;
67
68 /** @var float */
69 private $startTime;
70
71 /** @var string */
72 private $table;
73
74 public function __construct() {
75 parent::__construct();
76 $this->addDescription( 'Script to update image metadata records' );
77
78 $this->addOption( 'missing', 'Check for files without associated database record' );
79 $this->addOption( 'dry-run', 'Only report, don\'t update the database' );
80 }
81
82 public function execute() {
83 $this->dbw = $this->getPrimaryDB();
84 $this->dryrun = $this->hasOption( 'dry-run' );
85 if ( $this->dryrun ) {
86 $this->getServiceContainer()->getReadOnlyMode()
87 ->setReason( 'Dry run mode, image upgrades are suppressed' );
88 }
89
90 if ( $this->hasOption( 'missing' ) ) {
91 $this->crawlMissing();
92 } else {
93 $this->build();
94 }
95 }
96
97 /**
98 * @return LocalRepo
99 */
100 private function getRepo() {
101 if ( $this->repo === null ) {
102 $this->repo = $this->getServiceContainer()->getRepoGroup()
103 ->newCustomLocalRepo( [
104 // make sure to update old, but compatible img_metadata fields.
105 'updateCompatibleMetadata' => true
106 ] );
107 }
108
109 return $this->repo;
110 }
111
112 private function build() {
113 $this->buildImage();
114 $this->buildOldImage();
115 }
116
117 /**
118 * @param int $count
119 * @param string $table
120 */
121 private function init( $count, $table ) {
122 $this->processed = 0;
123 $this->updated = 0;
124 $this->count = $count;
125 $this->startTime = microtime( true );
126 $this->table = $table;
127 }
128
129 private function progress( $updated ) {
130 $this->updated += $updated;
131 $this->processed++;
132 if ( $this->processed % 100 != 0 ) {
133 return;
134 }
135 $portion = $this->processed / $this->count;
136 $updateRate = $this->updated / $this->processed;
137
138 $now = microtime( true );
139 $delta = $now - $this->startTime;
140 $estimatedTotalTime = $delta / $portion;
141 $eta = $this->startTime + $estimatedTotalTime;
142 $rate = $this->processed / $delta;
143
144 $this->output( sprintf( "%s: %6.2f%% done on %s; ETA %s [%d/%d] %.2f/sec <%.2f%% updated>\n",
145 wfTimestamp( TS_DB, intval( $now ) ),
146 $portion * 100.0,
147 $this->table,
148 wfTimestamp( TS_DB, intval( $eta ) ),
149 $this->processed,
150 $this->count,
151 $rate,
152 $updateRate * 100.0 ) );
153 flush();
154 }
155
156 private function buildTable( $table, $queryBuilder, $callback ) {
157 $count = $this->dbw->newSelectQueryBuilder()
158 ->select( 'count(*)' )
159 ->from( $table )
160 ->caller( __METHOD__ )->fetchField();
161 $this->init( $count, $table );
162 $this->output( "Processing $table...\n" );
163
164 $result = $queryBuilder->caller( __METHOD__ )->fetchResultSet();
165
166 foreach ( $result as $row ) {
167 $update = call_user_func( $callback, $row );
168 if ( $update ) {
169 $this->progress( 1 );
170 } else {
171 $this->progress( 0 );
172 }
173 }
174 $this->output( "Finished $table... $this->updated of $this->processed rows updated\n" );
175 }
176
177 private function buildImage() {
178 $callback = [ $this, 'imageCallback' ];
179 $this->buildTable( 'image', FileSelectQueryBuilder::newForFile( $this->getReplicaDB() ), $callback );
180 }
181
182 private function imageCallback( $row ) {
183 // Create a File object from the row
184 // This will also upgrade it
185 $file = $this->getRepo()->newFileFromRow( $row );
186
187 return $file->getUpgraded();
188 }
189
190 private function buildOldImage() {
191 $this->buildTable( 'oldimage', FileSelectQueryBuilder::newForOldFile( $this->getReplicaDB() ),
192 [ $this, 'oldimageCallback' ] );
193 }
194
195 private function oldimageCallback( $row ) {
196 // Create a File object from the row
197 // This will also upgrade it
198 if ( $row->oi_archive_name == '' ) {
199 $this->output( "Empty oi_archive_name for oi_name={$row->oi_name}\n" );
200
201 return false;
202 }
203 $file = $this->getRepo()->newFileFromRow( $row );
204
205 return $file->getUpgraded();
206 }
207
208 private function crawlMissing() {
209 $this->getRepo()->enumFiles( [ $this, 'checkMissingImage' ] );
210 }
211
212 public function checkMissingImage( $fullpath ) {
213 $filename = wfBaseName( $fullpath );
214 $row = $this->dbw->newSelectQueryBuilder()
215 ->select( [ 'img_name' ] )
216 ->from( 'image' )
217 ->where( [ 'img_name' => $filename ] )
218 ->caller( __METHOD__ )->fetchRow();
219
220 if ( !$row ) {
221 // file not registered
222 $this->addMissingImage( $filename, $fullpath );
223 }
224 }
225
226 private function addMissingImage( $filename, $fullpath ) {
227 $timestamp = $this->dbw->timestamp( $this->getRepo()->getFileTimestamp( $fullpath ) );
228 $services = $this->getServiceContainer();
229
230 $altname = $services->getContentLanguage()->checkTitleEncoding( $filename );
231 if ( $altname != $filename ) {
232 if ( $this->dryrun ) {
233 $filename = $altname;
234 $this->output( "Estimating transcoding... $altname\n" );
235 } else {
236 // @fixme create renameFile()
237 // @phan-suppress-next-line PhanUndeclaredMethod See comment above...
238 $filename = $this->renameFile( $filename );
239 }
240 }
241
242 if ( $filename == '' ) {
243 $this->output( "Empty filename for $fullpath\n" );
244
245 return;
246 }
247 if ( !$this->dryrun ) {
248 $file = $services->getRepoGroup()->getLocalRepo()->newFile( $filename );
249 $pageText = SpecialUpload::getInitialPageText(
250 '(recovered file, missing upload log entry)'
251 );
252 $user = User::newSystemUser( User::MAINTENANCE_SCRIPT_USER, [ 'steal' => true ] );
253 $status = $file->recordUpload3(
254 '',
255 '(recovered file, missing upload log entry)',
256 $pageText,
257 $user,
258 false,
259 $timestamp
260 );
261 if ( !$status->isOK() ) {
262 $this->output( "Error uploading file $fullpath\n" );
263
264 return;
265 }
266 }
267 $this->output( $fullpath . "\n" );
268 }
269 }
270
271 // @codeCoverageIgnoreStart
272 $maintClass = ImageBuilder::class;
273 require_once RUN_MAINTENANCE_IF_MAIN;
274 // @codeCoverageIgnoreEnd