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