Merge "Make DBAccessBase use DBConnRef, rename $wiki, and hide getLoadBalancer()"
[lhc/web/wiklou.git] / maintenance / rebuildImages.php
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 Brion Vibber <brion@pobox.com>
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 Brion Vibber <brion at pobox.com>
30 * @ingroup Maintenance
31 */
32
33 require_once __DIR__ . '/Maintenance.php';
34
35 use MediaWiki\MediaWikiServices;
36 use Wikimedia\Rdbms\IMaintainableDatabase;
37
38 /**
39 * Maintenance script to update image metadata records.
40 *
41 * @ingroup Maintenance
42 */
43 class ImageBuilder extends Maintenance {
44
45 /**
46 * @var IMaintainableDatabase
47 */
48 protected $dbw;
49
50 function __construct() {
51 parent::__construct();
52
53 global $wgUpdateCompatibleMetadata;
54 // make sure to update old, but compatible img_metadata fields.
55 $wgUpdateCompatibleMetadata = true;
56
57 $this->addDescription( 'Script to update image metadata records' );
58
59 $this->addOption( 'missing', 'Check for files without associated database record' );
60 $this->addOption( 'dry-run', 'Only report, don\'t update the database' );
61 }
62
63 public function execute() {
64 $this->dbw = $this->getDB( DB_MASTER );
65 $this->dryrun = $this->hasOption( 'dry-run' );
66 if ( $this->dryrun ) {
67 MediaWiki\MediaWikiServices::getInstance()->getReadOnlyMode()
68 ->setReason( 'Dry run mode, image upgrades are suppressed' );
69 }
70
71 if ( $this->hasOption( 'missing' ) ) {
72 $this->crawlMissing();
73 } else {
74 $this->build();
75 }
76 }
77
78 /**
79 * @return LocalRepo
80 */
81 function getRepo() {
82 if ( !isset( $this->repo ) ) {
83 $this->repo = RepoGroup::singleton()->getLocalRepo();
84 }
85
86 return $this->repo;
87 }
88
89 function build() {
90 $this->buildImage();
91 $this->buildOldImage();
92 }
93
94 /**
95 * @param int $count
96 * @param string $table
97 */
98 function init( $count, $table ) {
99 $this->processed = 0;
100 $this->updated = 0;
101 $this->count = $count;
102 $this->startTime = microtime( true );
103 $this->table = $table;
104 }
105
106 function progress( $updated ) {
107 $this->updated += $updated;
108 $this->processed++;
109 if ( $this->processed % 100 != 0 ) {
110 return;
111 }
112 $portion = $this->processed / $this->count;
113 $updateRate = $this->updated / $this->processed;
114
115 $now = microtime( true );
116 $delta = $now - $this->startTime;
117 $estimatedTotalTime = $delta / $portion;
118 $eta = $this->startTime + $estimatedTotalTime;
119 $rate = $this->processed / $delta;
120
121 $this->output( sprintf( "%s: %6.2f%% done on %s; ETA %s [%d/%d] %.2f/sec <%.2f%% updated>\n",
122 wfTimestamp( TS_DB, intval( $now ) ),
123 $portion * 100.0,
124 $this->table,
125 wfTimestamp( TS_DB, intval( $eta ) ),
126 $this->processed,
127 $this->count,
128 $rate,
129 $updateRate * 100.0 ) );
130 flush();
131 }
132
133 function buildTable( $table, $key, $queryInfo, $callback ) {
134 $count = $this->dbw->selectField( $table, 'count(*)', '', __METHOD__ );
135 $this->init( $count, $table );
136 $this->output( "Processing $table...\n" );
137
138 $result = $this->getDB( DB_REPLICA )->select(
139 $queryInfo['tables'], $queryInfo['fields'], [], __METHOD__, [], $queryInfo['joins']
140 );
141
142 foreach ( $result as $row ) {
143 $update = call_user_func( $callback, $row, null );
144 if ( $update ) {
145 $this->progress( 1 );
146 } else {
147 $this->progress( 0 );
148 }
149 }
150 $this->output( "Finished $table... $this->updated of $this->processed rows updated\n" );
151 }
152
153 function buildImage() {
154 $callback = [ $this, 'imageCallback' ];
155 $this->buildTable( 'image', 'img_name', LocalFile::getQueryInfo(), $callback );
156 }
157
158 function imageCallback( $row, $copy ) {
159 // Create a File object from the row
160 // This will also upgrade it
161 $file = $this->getRepo()->newFileFromRow( $row );
162
163 return $file->getUpgraded();
164 }
165
166 function buildOldImage() {
167 $this->buildTable( 'oldimage', 'oi_archive_name', OldLocalFile::getQueryInfo(),
168 [ $this, 'oldimageCallback' ] );
169 }
170
171 function oldimageCallback( $row, $copy ) {
172 // Create a File object from the row
173 // This will also upgrade it
174 if ( $row->oi_archive_name == '' ) {
175 $this->output( "Empty oi_archive_name for oi_name={$row->oi_name}\n" );
176
177 return false;
178 }
179 $file = $this->getRepo()->newFileFromRow( $row );
180
181 return $file->getUpgraded();
182 }
183
184 function crawlMissing() {
185 $this->getRepo()->enumFiles( [ $this, 'checkMissingImage' ] );
186 }
187
188 function checkMissingImage( $fullpath ) {
189 $filename = wfBaseName( $fullpath );
190 $row = $this->dbw->selectRow( 'image',
191 [ 'img_name' ],
192 [ 'img_name' => $filename ],
193 __METHOD__ );
194
195 if ( !$row ) { // file not registered
196 $this->addMissingImage( $filename, $fullpath );
197 }
198 }
199
200 function addMissingImage( $filename, $fullpath ) {
201 $timestamp = $this->dbw->timestamp( $this->getRepo()->getFileTimestamp( $fullpath ) );
202 $services = MediaWikiServices::getInstance();
203
204 $altname = $services->getContentLanguage()->checkTitleEncoding( $filename );
205 if ( $altname != $filename ) {
206 if ( $this->dryrun ) {
207 $filename = $altname;
208 $this->output( "Estimating transcoding... $altname\n" );
209 } else {
210 // @fixme create renameFile()
211 // @phan-suppress-next-line PhanUndeclaredMethod See comment above...
212 $filename = $this->renameFile( $filename );
213 }
214 }
215
216 if ( $filename == '' ) {
217 $this->output( "Empty filename for $fullpath\n" );
218
219 return;
220 }
221 if ( !$this->dryrun ) {
222 $file = $services->getRepoGroup()->getLocalRepo()->newFile( $filename );
223 if ( !$file->recordUpload(
224 '',
225 '(recovered file, missing upload log entry)',
226 '',
227 '',
228 '',
229 false,
230 $timestamp
231 ) ) {
232 $this->output( "Error uploading file $fullpath\n" );
233
234 return;
235 }
236 }
237 $this->output( $fullpath . "\n" );
238 }
239 }
240
241 $maintClass = ImageBuilder::class;
242 require_once RUN_MAINTENANCE_IF_MAIN;