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
{
42 function __construct() {
43 parent
::__construct();
45 $this->mDescription
= 'Script to update image metadata records';
47 $this->addOption( 'missing', 'Check for files without associated database record' );
48 $this->addOption( 'dry-run', 'Only report, don\'t update the database' );
51 public function execute() {
52 $this->dbw
= wfGetDB( DB_MASTER
);
53 $this->maxLag
= 10; # if slaves are lagged more than 10 secs, wait
54 $this->dryrun
= $this->hasOption( 'dry-run' );
55 if ( $this->dryrun
) {
56 $GLOBALS['wgReadOnly'] = 'Dry run mode, image upgrades are suppressed';
59 if ( $this->hasOption( 'missing' ) ) {
60 $this->crawlMissing();
67 if ( !isset( $this->repo
) ) {
68 $this->repo
= RepoGroup
::singleton()->getLocalRepo();
75 $this->buildOldImage();
78 function init( $count, $table ) {
81 $this->count
= $count;
82 $this->startTime
= wfTime();
83 $this->table
= $table;
86 function progress( $updated ) {
87 $this->updated +
= $updated;
89 if ( $this->processed %
100 != 0 ) {
92 $portion = $this->processed
/ $this->count
;
93 $updateRate = $this->updated
/ $this->processed
;
96 $delta = $now - $this->startTime
;
97 $estimatedTotalTime = $delta / $portion;
98 $eta = $this->startTime +
$estimatedTotalTime;
99 $rate = $this->processed
/ $delta;
101 $this->output( sprintf( "%s: %6.2f%% done on %s; ETA %s [%d/%d] %.2f/sec <%.2f%% updated>\n",
102 wfTimestamp( TS_DB
, intval( $now ) ),
105 wfTimestamp( TS_DB
, intval( $eta ) ),
109 $updateRate * 100.0 ) );
113 function buildTable( $table, $key, $callback ) {
114 $count = $this->dbw
->selectField( $table, 'count(*)', '', __METHOD__
);
115 $this->init( $count, $table );
116 $this->output( "Processing $table...\n" );
118 $result = wfGetDB( DB_SLAVE
)->select( $table, '*', array(), __METHOD__
);
120 foreach ( $result as $row ) {
121 $update = call_user_func( $callback, $row, null );
123 $this->progress( 1 );
125 $this->progress( 0 );
128 $this->output( "Finished $table... $this->updated of $this->processed rows updated\n" );
131 function buildImage() {
132 $callback = array( $this, 'imageCallback' );
133 $this->buildTable( 'image', 'img_name', $callback );
136 function imageCallback( $row, $copy ) {
137 // Create a File object from the row
138 // This will also upgrade it
139 $file = $this->getRepo()->newFileFromRow( $row );
140 return $file->getUpgraded();
143 function buildOldImage() {
144 $this->buildTable( 'oldimage', 'oi_archive_name',
145 array( $this, 'oldimageCallback' ) );
148 function oldimageCallback( $row, $copy ) {
149 // Create a File object from the row
150 // This will also upgrade it
151 if ( $row->oi_archive_name
== '' ) {
152 $this->output( "Empty oi_archive_name for oi_name={$row->oi_name}\n" );
155 $file = $this->getRepo()->newFileFromRow( $row );
156 return $file->getUpgraded();
159 function crawlMissing() {
160 $repo = RepoGroup
::singleton()->getLocalRepo();
161 $repo->enumFilesInFS( array( $this, 'checkMissingImage' ) );
164 function checkMissingImage( $fullpath ) {
165 $filename = wfBaseName( $fullpath );
166 if ( is_dir( $fullpath ) ) {
169 if ( is_link( $fullpath ) ) {
170 $this->output( "skipping symlink at $fullpath\n" );
173 $row = $this->dbw
->selectRow( 'image',
175 array( 'img_name' => $filename ),
179 // already known, move on
182 $this->addMissingImage( $filename, $fullpath );
186 function addMissingImage( $filename, $fullpath ) {
187 $timestamp = $this->dbw
->timestamp( filemtime( $fullpath ) );
190 $altname = $wgContLang->checkTitleEncoding( $filename );
191 if ( $altname != $filename ) {
192 if ( $this->dryrun
) {
193 $filename = $altname;
194 $this->output( "Estimating transcoding... $altname\n" );
196 $filename = $this->renameFile( $filename );
200 if ( $filename == '' ) {
201 $this->output( "Empty filename for $fullpath\n" );
204 if ( !$this->dryrun
) {
205 $file = wfLocalFile( $filename );
206 if ( !$file->recordUpload( '', '(recovered file, missing upload log entry)', '', '', '',
207 false, $timestamp ) )
209 $this->output( "Error uploading file $fullpath\n" );
213 $this->output( $fullpath . "\n" );
217 $maintClass = 'ImageBuilder';
218 require( RUN_MAINTENANCE_IF_MAIN
);