Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 108 |
|
0.00% |
0 / 14 |
CRAP | |
0.00% |
0 / 1 |
ImageBuilder | |
0.00% |
0 / 108 |
|
0.00% |
0 / 14 |
756 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
execute | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
12 | |||
getRepo | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
6 | |||
build | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
init | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
2 | |||
progress | |
0.00% |
0 / 21 |
|
0.00% |
0 / 1 |
6 | |||
buildTable | |
0.00% |
0 / 13 |
|
0.00% |
0 / 1 |
12 | |||
buildImage | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
imageCallback | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
buildOldImage | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
oldimageCallback | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 | |||
crawlMissing | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
checkMissingImage | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
6 | |||
addMissingImage | |
0.00% |
0 / 29 |
|
0.00% |
0 / 1 |
42 |
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 <bvibber@wikimedia.org> |
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 <bvibber@wikimedia.org> |
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\Maintenance\Maintenance; |
39 | use MediaWiki\Specials\SpecialUpload; |
40 | use MediaWiki\User\User; |
41 | use Wikimedia\Rdbms\IMaintainableDatabase; |
42 | |
43 | /** |
44 | * Maintenance script to update image metadata records. |
45 | * |
46 | * @ingroup Maintenance |
47 | */ |
48 | class ImageBuilder extends Maintenance { |
49 | /** |
50 | * @var IMaintainableDatabase |
51 | */ |
52 | protected $dbw; |
53 | |
54 | /** @var bool */ |
55 | private $dryrun; |
56 | |
57 | /** @var LocalRepo|null */ |
58 | private $repo; |
59 | |
60 | /** @var int */ |
61 | private $updated; |
62 | |
63 | /** @var int */ |
64 | private $processed; |
65 | |
66 | /** @var int */ |
67 | private $count; |
68 | |
69 | /** @var float */ |
70 | private $startTime; |
71 | |
72 | /** @var string */ |
73 | private $table; |
74 | |
75 | public function __construct() { |
76 | parent::__construct(); |
77 | $this->addDescription( 'Script to update image metadata records' ); |
78 | |
79 | $this->addOption( 'missing', 'Check for files without associated database record' ); |
80 | $this->addOption( 'dry-run', 'Only report, don\'t update the database' ); |
81 | } |
82 | |
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' ); |
89 | } |
90 | |
91 | if ( $this->hasOption( 'missing' ) ) { |
92 | $this->crawlMissing(); |
93 | } else { |
94 | $this->build(); |
95 | } |
96 | } |
97 | |
98 | /** |
99 | * @return LocalRepo |
100 | */ |
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 | ] ); |
108 | } |
109 | |
110 | return $this->repo; |
111 | } |
112 | |
113 | private function build() { |
114 | $this->buildImage(); |
115 | $this->buildOldImage(); |
116 | } |
117 | |
118 | /** |
119 | * @param int $count |
120 | * @param string $table |
121 | */ |
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; |
128 | } |
129 | |
130 | private function progress( $updated ) { |
131 | $this->updated += $updated; |
132 | $this->processed++; |
133 | if ( $this->processed % 100 != 0 ) { |
134 | return; |
135 | } |
136 | $portion = $this->processed / $this->count; |
137 | $updateRate = $this->updated / $this->processed; |
138 | |
139 | $now = microtime( true ); |
140 | $delta = $now - $this->startTime; |
141 | $estimatedTotalTime = $delta / $portion; |
142 | $eta = $this->startTime + $estimatedTotalTime; |
143 | $rate = $this->processed / $delta; |
144 | |
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(); |
155 | } |
156 | |
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" ); |
164 | |
165 | $result = $queryBuilder->caller( __METHOD__ )->fetchResultSet(); |
166 | |
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 ); |
173 | } |
174 | } |
175 | $this->output( "Finished $table... $this->updated of $this->processed rows updated\n" ); |
176 | } |
177 | |
178 | private function buildImage() { |
179 | $callback = [ $this, 'imageCallback' ]; |
180 | $this->buildTable( 'image', FileSelectQueryBuilder::newForFile( $this->getReplicaDB() ), $callback ); |
181 | } |
182 | |
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 ); |
187 | |
188 | return $file->getUpgraded(); |
189 | } |
190 | |
191 | private function buildOldImage() { |
192 | $this->buildTable( 'oldimage', FileSelectQueryBuilder::newForOldFile( $this->getReplicaDB() ), |
193 | [ $this, 'oldimageCallback' ] ); |
194 | } |
195 | |
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" ); |
201 | |
202 | return false; |
203 | } |
204 | $file = $this->getRepo()->newFileFromRow( $row ); |
205 | |
206 | return $file->getUpgraded(); |
207 | } |
208 | |
209 | private function crawlMissing() { |
210 | $this->getRepo()->enumFiles( [ $this, 'checkMissingImage' ] ); |
211 | } |
212 | |
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(); |
220 | |
221 | if ( !$row ) { |
222 | // file not registered |
223 | $this->addMissingImage( $filename, $fullpath ); |
224 | } |
225 | } |
226 | |
227 | private function addMissingImage( $filename, $fullpath ) { |
228 | $timestamp = $this->dbw->timestamp( $this->getRepo()->getFileTimestamp( $fullpath ) ); |
229 | $services = $this->getServiceContainer(); |
230 | |
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 ); |
240 | } |
241 | } |
242 | |
243 | if ( $filename == '' ) { |
244 | $this->output( "Empty filename for $fullpath\n" ); |
245 | |
246 | return; |
247 | } |
248 | if ( !$this->dryrun ) { |
249 | $file = $services->getRepoGroup()->getLocalRepo()->newFile( $filename ); |
250 | $pageText = SpecialUpload::getInitialPageText( |
251 | '(recovered file, missing upload log entry)' |
252 | ); |
253 | $user = User::newSystemUser( User::MAINTENANCE_SCRIPT_USER, [ 'steal' => true ] ); |
254 | $status = $file->recordUpload3( |
255 | '', |
256 | '(recovered file, missing upload log entry)', |
257 | $pageText, |
258 | $user, |
259 | false, |
260 | $timestamp |
261 | ); |
262 | if ( !$status->isOK() ) { |
263 | $this->output( "Error uploading file $fullpath\n" ); |
264 | |
265 | return; |
266 | } |
267 | } |
268 | $this->output( $fullpath . "\n" ); |
269 | } |
270 | } |
271 | |
272 | // @codeCoverageIgnoreStart |
273 | $maintClass = ImageBuilder::class; |
274 | require_once RUN_MAINTENANCE_IF_MAIN; |
275 | // @codeCoverageIgnoreEnd |