3 * Script to 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 * http://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( dirname( __FILE__
) . '/Maintenance.php' );
35 class ImageBuilder
extends Maintenance
{
36 function __construct() {
37 parent
::__construct();
39 $this->mDescription
= 'Script to update image metadata records';
41 $this->addOption( 'missing', 'Check for files without associated database record' );
42 $this->addOption( 'dry-run', 'Only report, don\'t update the database' );
45 public function execute() {
46 $this->dbw
= wfGetDB( DB_MASTER
);
47 $this->maxLag
= 10; # if slaves are lagged more than 10 secs, wait
48 $this->dryrun
= $this->hasOption( 'dry-run' );
49 if ( $this->dryrun
) {
50 $GLOBALS['wgReadOnly'] = 'Dry run mode, image upgrades are suppressed';
53 if ( $this->hasOption( 'missing' ) ) {
54 $this->crawlMissing();
61 if ( !isset( $this->repo
) ) {
62 $this->repo
= RepoGroup
::singleton()->getLocalRepo();
69 $this->buildOldImage();
72 function init( $count, $table ) {
75 $this->count
= $count;
76 $this->startTime
= wfTime();
77 $this->table
= $table;
80 function progress( $updated ) {
81 $this->updated +
= $updated;
83 if ( $this->processed %
100 != 0 ) {
86 $portion = $this->processed
/ $this->count
;
87 $updateRate = $this->updated
/ $this->processed
;
90 $delta = $now - $this->startTime
;
91 $estimatedTotalTime = $delta / $portion;
92 $eta = $this->startTime +
$estimatedTotalTime;
93 $rate = $this->processed
/ $delta;
95 $this->output( sprintf( "%s: %6.2f%% done on %s; ETA %s [%d/%d] %.2f/sec <%.2f%% updated>\n",
96 wfTimestamp( TS_DB
, intval( $now ) ),
99 wfTimestamp( TS_DB
, intval( $eta ) ),
103 $updateRate * 100.0 ) );
107 function buildTable( $table, $key, $callback ) {
108 $count = $this->dbw
->selectField( $table, 'count(*)', '', __METHOD__
);
109 $this->init( $count, $table );
110 $this->output( "Processing $table...\n" );
112 $result = wfGetDB( DB_SLAVE
)->select( $table, '*', array(), __METHOD__
);
114 foreach ( $result as $row ) {
115 $update = call_user_func( $callback, $row, null );
117 $this->progress( 1 );
119 $this->progress( 0 );
122 $this->output( "Finished $table... $this->updated of $this->processed rows updated\n" );
125 function buildImage() {
126 $callback = array( $this, 'imageCallback' );
127 $this->buildTable( 'image', 'img_name', $callback );
130 function imageCallback( $row, $copy ) {
131 // Create a File object from the row
132 // This will also upgrade it
133 $file = $this->getRepo()->newFileFromRow( $row );
134 return $file->getUpgraded();
137 function buildOldImage() {
138 $this->buildTable( 'oldimage', 'oi_archive_name',
139 array( $this, 'oldimageCallback' ) );
142 function oldimageCallback( $row, $copy ) {
143 // Create a File object from the row
144 // This will also upgrade it
145 if ( $row->oi_archive_name
== '' ) {
146 $this->output( "Empty oi_archive_name for oi_name={$row->oi_name}\n" );
149 $file = $this->getRepo()->newFileFromRow( $row );
150 return $file->getUpgraded();
153 function crawlMissing() {
154 $repo = RepoGroup
::singleton()->getLocalRepo();
155 $repo->enumFilesInFS( array( $this, 'checkMissingImage' ) );
158 function checkMissingImage( $fullpath ) {
159 $filename = wfBaseName( $fullpath );
160 if ( is_dir( $fullpath ) ) {
163 if ( is_link( $fullpath ) ) {
164 $this->output( "skipping symlink at $fullpath\n" );
167 $row = $this->dbw
->selectRow( 'image',
169 array( 'img_name' => $filename ),
173 // already known, move on
176 $this->addMissingImage( $filename, $fullpath );
180 function addMissingImage( $filename, $fullpath ) {
181 $timestamp = $this->dbw
->timestamp( filemtime( $fullpath ) );
184 $altname = $wgContLang->checkTitleEncoding( $filename );
185 if ( $altname != $filename ) {
186 if ( $this->dryrun
) {
187 $filename = $altname;
188 $this->output( "Estimating transcoding... $altname\n" );
190 $filename = $this->renameFile( $filename );
194 if ( $filename == '' ) {
195 $this->output( "Empty filename for $fullpath\n" );
198 if ( !$this->dryrun
) {
199 $file = wfLocalFile( $filename );
200 if ( !$file->recordUpload( '', '(recovered file, missing upload log entry)', '', '', '',
201 false, $timestamp ) )
203 $this->output( "Error uploading file $fullpath\n" );
207 $this->output( $fullpath . "\n" );
211 $maintClass = 'ImageBuilder';
212 require( RUN_MAINTENANCE_IF_MAIN
);