HTMLForm: throw a warning when no callback is set
[mediawiki.git] / maintenance / rebuildImages.php
blobfe3b35c93cb4a84901a536eb3fb691daf5b8ec50
1 <?php
2 /**
3 * Script to update image metadata records
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.
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
28 * @file
29 * @author Brion Vibber <brion at pobox.com>
30 * @ingroup Maintenance
33 require_once( dirname( __FILE__ ) . '/Maintenance.php' );
35 class ImageBuilder extends Maintenance {
37 /**
38 * @var DatabaseBase
40 protected $dbw;
42 function __construct() {
43 parent::__construct();
45 global $wgUpdateCompatibleMetadata;
46 //make sure to update old, but compatible img_metadata fields.
47 $wgUpdateCompatibleMetadata = true;
49 $this->mDescription = 'Script to update image metadata records';
51 $this->addOption( 'missing', 'Check for files without associated database record' );
52 $this->addOption( 'dry-run', 'Only report, don\'t update the database' );
55 public function execute() {
56 $this->dbw = wfGetDB( DB_MASTER );
57 $this->maxLag = 10; # if slaves are lagged more than 10 secs, wait
58 $this->dryrun = $this->hasOption( 'dry-run' );
59 if ( $this->dryrun ) {
60 $GLOBALS['wgReadOnly'] = 'Dry run mode, image upgrades are suppressed';
63 if ( $this->hasOption( 'missing' ) ) {
64 $this->crawlMissing();
65 } else {
66 $this->build();
70 /**
71 * @return FileRepo
73 function getRepo() {
74 if ( !isset( $this->repo ) ) {
75 $this->repo = RepoGroup::singleton()->getLocalRepo();
77 return $this->repo;
80 function build() {
81 $this->buildImage();
82 $this->buildOldImage();
85 function init( $count, $table ) {
86 $this->processed = 0;
87 $this->updated = 0;
88 $this->count = $count;
89 $this->startTime = wfTime();
90 $this->table = $table;
93 function progress( $updated ) {
94 $this->updated += $updated;
95 $this->processed++;
96 if ( $this->processed % 100 != 0 ) {
97 return;
99 $portion = $this->processed / $this->count;
100 $updateRate = $this->updated / $this->processed;
102 $now = wfTime();
103 $delta = $now - $this->startTime;
104 $estimatedTotalTime = $delta / $portion;
105 $eta = $this->startTime + $estimatedTotalTime;
106 $rate = $this->processed / $delta;
108 $this->output( sprintf( "%s: %6.2f%% done on %s; ETA %s [%d/%d] %.2f/sec <%.2f%% updated>\n",
109 wfTimestamp( TS_DB, intval( $now ) ),
110 $portion * 100.0,
111 $this->table,
112 wfTimestamp( TS_DB, intval( $eta ) ),
113 $this->processed,
114 $this->count,
115 $rate,
116 $updateRate * 100.0 ) );
117 flush();
120 function buildTable( $table, $key, $callback ) {
121 $count = $this->dbw->selectField( $table, 'count(*)', '', __METHOD__ );
122 $this->init( $count, $table );
123 $this->output( "Processing $table...\n" );
125 $result = wfGetDB( DB_SLAVE )->select( $table, '*', array(), __METHOD__ );
127 foreach ( $result as $row ) {
128 $update = call_user_func( $callback, $row, null );
129 if ( $update ) {
130 $this->progress( 1 );
131 } else {
132 $this->progress( 0 );
135 $this->output( "Finished $table... $this->updated of $this->processed rows updated\n" );
138 function buildImage() {
139 $callback = array( $this, 'imageCallback' );
140 $this->buildTable( 'image', 'img_name', $callback );
143 function imageCallback( $row, $copy ) {
144 // Create a File object from the row
145 // This will also upgrade it
146 $file = $this->getRepo()->newFileFromRow( $row );
147 return $file->getUpgraded();
150 function buildOldImage() {
151 $this->buildTable( 'oldimage', 'oi_archive_name', array( $this, 'oldimageCallback' ) );
154 function oldimageCallback( $row, $copy ) {
155 // Create a File object from the row
156 // This will also upgrade it
157 if ( $row->oi_archive_name == '' ) {
158 $this->output( "Empty oi_archive_name for oi_name={$row->oi_name}\n" );
159 return false;
161 $file = $this->getRepo()->newFileFromRow( $row );
162 return $file->getUpgraded();
165 function crawlMissing() {
166 $this->getRepo()->enumFiles( array( $this, 'checkMissingImage' ) );
169 function checkMissingImage( $fullpath ) {
170 $filename = wfBaseName( $fullpath );
171 $row = $this->dbw->selectRow( 'image',
172 array( 'img_name' ),
173 array( 'img_name' => $filename ),
174 __METHOD__ );
176 if ( !$row ) { // file not registered
177 $this->addMissingImage( $filename, $fullpath );
181 function addMissingImage( $filename, $fullpath ) {
182 global $wgContLang;
184 $timestamp = $this->dbw->timestamp( $this->getRepo()->getFileTimestamp( $fullpath ) );
186 $altname = $wgContLang->checkTitleEncoding( $filename );
187 if ( $altname != $filename ) {
188 if ( $this->dryrun ) {
189 $filename = $altname;
190 $this->output( "Estimating transcoding... $altname\n" );
191 } else {
192 # @FIXME: create renameFile()
193 $filename = $this->renameFile( $filename );
197 if ( $filename == '' ) {
198 $this->output( "Empty filename for $fullpath\n" );
199 return;
201 if ( !$this->dryrun ) {
202 $file = wfLocalFile( $filename );
203 if ( !$file->recordUpload( '', '(recovered file, missing upload log entry)', '', '', '',
204 false, $timestamp ) )
206 $this->output( "Error uploading file $fullpath\n" );
207 return;
210 $this->output( $fullpath . "\n" );
214 $maintClass = 'ImageBuilder';
215 require_once( RUN_MAINTENANCE_IF_MAIN );