3 * Update image metadata records.
5 * Usage: php rebuildImages.php [--missing] [--dry-run]
7 * --missing Crawl the uploads dir for images without records, and
10 * Copyright © 2005 Brion Vibber <brion@pobox.com>
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
29 * @author Brion Vibber <brion at pobox.com>
30 * @ingroup Maintenance
33 require_once __DIR__
. '/Maintenance.php';
36 * Maintenance script to update image metadata records.
38 * @ingroup Maintenance
40 class ImageBuilder
extends Maintenance
{
47 function __construct() {
48 parent
::__construct();
50 global $wgUpdateCompatibleMetadata;
51 // make sure to update old, but compatible img_metadata fields.
52 $wgUpdateCompatibleMetadata = true;
54 $this->mDescription
= 'Script to update image metadata records';
56 $this->addOption( 'missing', 'Check for files without associated database record' );
57 $this->addOption( 'dry-run', 'Only report, don\'t update the database' );
60 public function execute() {
61 $this->dbw
= $this->getDB( DB_MASTER
);
62 $this->dryrun
= $this->hasOption( 'dry-run' );
63 if ( $this->dryrun
) {
64 $GLOBALS['wgReadOnly'] = 'Dry run mode, image upgrades are suppressed';
67 if ( $this->hasOption( 'missing' ) ) {
68 $this->crawlMissing();
78 if ( !isset( $this->repo
) ) {
79 $this->repo
= RepoGroup
::singleton()->getLocalRepo();
87 $this->buildOldImage();
90 function init( $count, $table ) {
93 $this->count
= $count;
94 $this->startTime
= microtime( true );
95 $this->table
= $table;
98 function progress( $updated ) {
99 $this->updated +
= $updated;
101 if ( $this->processed %
100 != 0 ) {
104 $portion = $this->processed
/ $this->count
;
105 $updateRate = $this->updated
/ $this->processed
;
107 $now = microtime( true );
108 $delta = $now - $this->startTime
;
109 $estimatedTotalTime = $delta / $portion;
110 $eta = $this->startTime +
$estimatedTotalTime;
111 $rate = $this->processed
/ $delta;
113 $this->output( sprintf( "%s: %6.2f%% done on %s; ETA %s [%d/%d] %.2f/sec <%.2f%% updated>\n",
114 wfTimestamp( TS_DB
, intval( $now ) ),
117 wfTimestamp( TS_DB
, intval( $eta ) ),
121 $updateRate * 100.0 ) );
125 function buildTable( $table, $key, $callback ) {
126 $count = $this->dbw
->selectField( $table, 'count(*)', '', __METHOD__
);
127 $this->init( $count, $table );
128 $this->output( "Processing $table...\n" );
130 $result = $this->getDB( DB_SLAVE
)->select( $table, '*', array(), __METHOD__
);
132 foreach ( $result as $row ) {
133 $update = call_user_func( $callback, $row, null );
135 $this->progress( 1 );
137 $this->progress( 0 );
140 $this->output( "Finished $table... $this->updated of $this->processed rows updated\n" );
143 function buildImage() {
144 $callback = array( $this, 'imageCallback' );
145 $this->buildTable( 'image', 'img_name', $callback );
148 function imageCallback( $row, $copy ) {
149 // Create a File object from the row
150 // This will also upgrade it
151 $file = $this->getRepo()->newFileFromRow( $row );
153 return $file->getUpgraded();
156 function buildOldImage() {
157 $this->buildTable( 'oldimage', 'oi_archive_name', array( $this, 'oldimageCallback' ) );
160 function oldimageCallback( $row, $copy ) {
161 // Create a File object from the row
162 // This will also upgrade it
163 if ( $row->oi_archive_name
== '' ) {
164 $this->output( "Empty oi_archive_name for oi_name={$row->oi_name}\n" );
168 $file = $this->getRepo()->newFileFromRow( $row );
170 return $file->getUpgraded();
173 function crawlMissing() {
174 $this->getRepo()->enumFiles( array( $this, 'checkMissingImage' ) );
177 function checkMissingImage( $fullpath ) {
178 $filename = wfBaseName( $fullpath );
179 $row = $this->dbw
->selectRow( 'image',
181 array( 'img_name' => $filename ),
184 if ( !$row ) { // file not registered
185 $this->addMissingImage( $filename, $fullpath );
189 function addMissingImage( $filename, $fullpath ) {
192 $timestamp = $this->dbw
->timestamp( $this->getRepo()->getFileTimestamp( $fullpath ) );
194 $altname = $wgContLang->checkTitleEncoding( $filename );
195 if ( $altname != $filename ) {
196 if ( $this->dryrun
) {
197 $filename = $altname;
198 $this->output( "Estimating transcoding... $altname\n" );
200 # @todo FIXME: create renameFile()
201 $filename = $this->renameFile( $filename );
205 if ( $filename == '' ) {
206 $this->output( "Empty filename for $fullpath\n" );
210 if ( !$this->dryrun
) {
211 $file = wfLocalFile( $filename );
212 if ( !$file->recordUpload(
214 '(recovered file, missing upload log entry)',
221 $this->output( "Error uploading file $fullpath\n" );
226 $this->output( $fullpath . "\n" );
230 $maintClass = 'ImageBuilder';
231 require_once RUN_MAINTENANCE_IF_MAIN
;