Indentation fix
[mediawiki.git] / maintenance / rebuildImages.php
blob171118312213067478d1f22583e54d6a90f77185
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',
152 array( $this, 'oldimageCallback' ) );
155 function oldimageCallback( $row, $copy ) {
156 // Create a File object from the row
157 // This will also upgrade it
158 if ( $row->oi_archive_name == '' ) {
159 $this->output( "Empty oi_archive_name for oi_name={$row->oi_name}\n" );
160 return false;
162 $file = $this->getRepo()->newFileFromRow( $row );
163 return $file->getUpgraded();
166 function crawlMissing() {
167 $repo = RepoGroup::singleton()->getLocalRepo();
168 $repo->enumFilesInFS( array( $this, 'checkMissingImage' ) );
171 function checkMissingImage( $fullpath ) {
172 $filename = wfBaseName( $fullpath );
173 if ( is_dir( $fullpath ) ) {
174 return;
176 if ( is_link( $fullpath ) ) {
177 $this->output( "skipping symlink at $fullpath\n" );
178 return;
180 $row = $this->dbw->selectRow( 'image',
181 array( 'img_name' ),
182 array( 'img_name' => $filename ),
183 __METHOD__ );
185 if ( $row ) {
186 // already known, move on
187 return;
188 } else {
189 $this->addMissingImage( $filename, $fullpath );
193 function addMissingImage( $filename, $fullpath ) {
194 $timestamp = $this->dbw->timestamp( filemtime( $fullpath ) );
196 global $wgContLang;
197 $altname = $wgContLang->checkTitleEncoding( $filename );
198 if ( $altname != $filename ) {
199 if ( $this->dryrun ) {
200 $filename = $altname;
201 $this->output( "Estimating transcoding... $altname\n" );
202 } else {
203 $filename = $this->renameFile( $filename );
207 if ( $filename == '' ) {
208 $this->output( "Empty filename for $fullpath\n" );
209 return;
211 if ( !$this->dryrun ) {
212 $file = wfLocalFile( $filename );
213 if ( !$file->recordUpload( '', '(recovered file, missing upload log entry)', '', '', '',
214 false, $timestamp ) )
216 $this->output( "Error uploading file $fullpath\n" );
217 return;
220 $this->output( $fullpath . "\n" );
224 $maintClass = 'ImageBuilder';
225 require_once( RUN_MAINTENANCE_IF_MAIN );