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 (C) 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 * @ingrouo maintenance
33 $options = array( 'missing', 'dry-run' );
35 require_once( 'commandLine.inc' );
36 require_once( 'FiveUpgrade.inc' );
38 class ImageBuilder
extends FiveUpgrade
{
39 function ImageBuilder( $dryrun = false ) {
40 parent
::FiveUpgrade();
42 $this->maxLag
= 10; # if slaves are lagged more than 10 secs, wait
43 $this->dryrun
= $dryrun;
45 $GLOBALS['wgReadOnly'] = 'Dry run mode, image upgrades are suppressed';
50 if ( !isset( $this->repo
) ) {
51 $this->repo
= RepoGroup
::singleton()->getLocalRepo();
58 $this->buildOldImage();
61 function init( $count, $table ) {
64 $this->count
= $count;
65 $this->startTime
= wfTime();
66 $this->table
= $table;
69 function progress( $updated ) {
70 $this->updated +
= $updated;
72 if( $this->processed %
100 != 0 ) {
75 $portion = $this->processed
/ $this->count
;
76 $updateRate = $this->updated
/ $this->processed
;
79 $delta = $now - $this->startTime
;
80 $estimatedTotalTime = $delta / $portion;
81 $eta = $this->startTime +
$estimatedTotalTime;
83 printf( "%s: %6.2f%% done on %s; ETA %s [%d/%d] %.2f/sec <%.2f%% updated>\n",
84 wfTimestamp( TS_DB
, intval( $now ) ),
87 wfTimestamp( TS_DB
, intval( $eta ) ),
88 $completed, // $completed does not appear to be defined.
90 $rate, // $rate does not appear to be defined.
91 $updateRate * 100.0 );
95 function buildTable( $table, $key, $callback ) {
96 $fname = 'ImageBuilder::buildTable';
98 $count = $this->dbw
->selectField( $table, 'count(*)', '', $fname );
99 $this->init( $count, $table );
100 $this->log( "Processing $table..." );
102 $tableName = $this->dbr
->tableName( $table );
103 $sql = "SELECT * FROM $tableName";
104 $result = $this->dbr
->query( $sql, $fname );
106 while( $row = $this->dbr
->fetchObject( $result ) ) {
107 $update = call_user_func( $callback, $row );
109 $this->progress( 1 );
111 $this->progress( 0 );
114 $this->log( "Finished $table... $this->updated of $this->processed rows updated" );
115 $this->dbr
->freeResult( $result );
118 function buildImage() {
119 $callback = array( &$this, 'imageCallback' );
120 $this->buildTable( 'image', 'img_name', $callback );
123 function imageCallback( $row ) {
124 // Create a File object from the row
125 // This will also upgrade it
126 $file = $this->getRepo()->newFileFromRow( $row );
127 return $file->getUpgraded();
130 function buildOldImage() {
131 $this->buildTable( 'oldimage', 'oi_archive_name',
132 array( &$this, 'oldimageCallback' ) );
135 function oldimageCallback( $row ) {
136 // Create a File object from the row
137 // This will also upgrade it
138 if ( $row->oi_archive_name
== '' ) {
139 $this->log( "Empty oi_archive_name for oi_name={$row->oi_name}" );
142 $file = $this->getRepo()->newFileFromRow( $row );
143 return $file->getUpgraded();
146 function crawlMissing() {
147 $repo = RepoGroup
::singleton()->getLocalRepo();
148 $repo->enumFilesInFS( array( $this, 'checkMissingImage' ) );
151 function checkMissingImage( $fullpath ) {
152 $fname = 'ImageBuilder::checkMissingImage';
153 $filename = wfBaseName( $fullpath );
154 if( is_dir( $fullpath ) ) {
157 if( is_link( $fullpath ) ) {
158 $this->log( "skipping symlink at $fullpath" );
161 $row = $this->dbw
->selectRow( 'image',
163 array( 'img_name' => $filename ),
167 // already known, move on
170 $this->addMissingImage( $filename, $fullpath );
174 function addMissingImage( $filename, $fullpath ) {
175 $fname = 'ImageBuilder::addMissingImage';
177 $timestamp = $this->dbw
->timestamp( filemtime( $fullpath ) );
180 $altname = $wgContLang->checkTitleEncoding( $filename );
181 if( $altname != $filename ) {
182 if( $this->dryrun
) {
183 $filename = $altname;
184 $this->log( "Estimating transcoding... $altname" );
186 $filename = $this->renameFile( $filename );
190 if( $filename == '' ) {
191 $this->log( "Empty filename for $fullpath" );
194 if ( !$this->dryrun
) {
195 $file = wfLocalFile( $filename );
196 if ( !$file->recordUpload( '', '(recovered file, missing upload log entry)', '', '', '',
197 false, $timestamp ) )
199 $this->log( "Error uploading file $fullpath" );
203 $this->log( $fullpath );
207 $builder = new ImageBuilder( isset( $options['dry-run'] ) );
208 if( isset( $options['missing'] ) ) {
209 $builder->crawlMissing();