(bug 11218) Add 'allrev' parameter option to feedwatchlist, to allow multiple revisio...
[mediawiki.git] / maintenance / rebuildImages.php
blobdfdb3b20ee3ee24d7e65a2e49044bcbd3ac828e2
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 (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
28 * @author Brion Vibber <brion at pobox.com>
29 * @addtogroup maintenance
32 $options = array( 'missing', 'dry-run' );
34 require_once( 'commandLine.inc' );
35 require_once( 'FiveUpgrade.inc' );
37 class ImageBuilder extends FiveUpgrade {
38 function ImageBuilder( $dryrun = false ) {
39 parent::FiveUpgrade();
41 $this->maxLag = 10; # if slaves are lagged more than 10 secs, wait
42 $this->dryrun = $dryrun;
43 if ( $dryrun ) {
44 $GLOBALS['wgReadOnly'] = 'Dry run mode, image upgrades are suppressed';
48 function getRepo() {
49 if ( !isset( $this->repo ) ) {
50 $this->repo = RepoGroup::singleton()->getLocalRepo();
52 return $this->repo;
55 function build() {
56 $this->buildImage();
57 $this->buildOldImage();
60 function init( $count, $table ) {
61 $this->processed = 0;
62 $this->updated = 0;
63 $this->count = $count;
64 $this->startTime = wfTime();
65 $this->table = $table;
68 function progress( $updated ) {
69 $this->updated += $updated;
70 $this->processed++;
71 if( $this->processed % 100 != 0 ) {
72 return;
74 $portion = $this->processed / $this->count;
75 $updateRate = $this->updated / $this->processed;
77 $now = wfTime();
78 $delta = $now - $this->startTime;
79 $estimatedTotalTime = $delta / $portion;
80 $eta = $this->startTime + $estimatedTotalTime;
82 printf( "%s: %6.2f%% done on %s; ETA %s [%d/%d] %.2f/sec <%.2f%% updated>\n",
83 wfTimestamp( TS_DB, intval( $now ) ),
84 $portion * 100.0,
85 $this->table,
86 wfTimestamp( TS_DB, intval( $eta ) ),
87 $completed, // $completed does not appear to be defined.
88 $this->count,
89 $rate, // $rate does not appear to be defined.
90 $updateRate * 100.0 );
91 flush();
94 function buildTable( $table, $key, $callback ) {
95 $fname = 'ImageBuilder::buildTable';
97 $count = $this->dbw->selectField( $table, 'count(*)', '', $fname );
98 $this->init( $count, $table );
99 $this->log( "Processing $table..." );
101 $tableName = $this->dbr->tableName( $table );
102 $sql = "SELECT * FROM $tableName";
103 $result = $this->dbr->query( $sql, $fname );
105 while( $row = $this->dbr->fetchObject( $result ) ) {
106 $update = call_user_func( $callback, $row );
107 if( $update ) {
108 $this->progress( 1 );
109 } else {
110 $this->progress( 0 );
113 $this->log( "Finished $table... $this->updated of $this->processed rows updated" );
114 $this->dbr->freeResult( $result );
117 function buildImage() {
118 $callback = array( &$this, 'imageCallback' );
119 $this->buildTable( 'image', 'img_name', $callback );
122 function imageCallback( $row ) {
123 // Create a File object from the row
124 // This will also upgrade it
125 $file = $this->getRepo()->newFileFromRow( $row );
126 return $file->getUpgraded();
129 function buildOldImage() {
130 $this->buildTable( 'oldimage', 'oi_archive_name',
131 array( &$this, 'oldimageCallback' ) );
134 function oldimageCallback( $row ) {
135 // Create a File object from the row
136 // This will also upgrade it
137 if ( $row->oi_archive_name == '' ) {
138 $this->log( "Empty oi_archive_name for oi_name={$row->oi_name}" );
139 return false;
141 $file = $this->getRepo()->newFileFromRow( $row );
142 return $file->getUpgraded();
145 function crawlMissing() {
146 $repo = RepoGroup::singleton()->getLocalRepo();
147 $repo->enumFilesInFS( array( $this, 'checkMissingImage' ) );
150 function checkMissingImage( $fullpath ) {
151 $fname = 'ImageBuilder::checkMissingImage';
152 $filename = wfBaseName( $fullpath );
153 if( is_dir( $fullpath ) ) {
154 return;
156 if( is_link( $fullpath ) ) {
157 $this->log( "skipping symlink at $fullpath" );
158 return;
160 $row = $this->dbw->selectRow( 'image',
161 array( 'img_name' ),
162 array( 'img_name' => $filename ),
163 $fname );
165 if( $row ) {
166 // already known, move on
167 return;
168 } else {
169 $this->addMissingImage( $filename, $fullpath );
173 function addMissingImage( $filename, $fullpath ) {
174 $fname = 'ImageBuilder::addMissingImage';
176 $timestamp = $this->dbw->timestamp( filemtime( $fullpath ) );
178 global $wgContLang;
179 $altname = $wgContLang->checkTitleEncoding( $filename );
180 if( $altname != $filename ) {
181 if( $this->dryrun ) {
182 $filename = $altname;
183 $this->log( "Estimating transcoding... $altname" );
184 } else {
185 $filename = $this->renameFile( $filename );
189 if( $filename == '' ) {
190 $this->log( "Empty filename for $fullpath" );
191 return;
193 if ( !$this->dryrun ) {
194 $file = wfLocalFile( $filename );
195 if ( !$file->recordUpload( '', '(recovered file, missing upload log entry)', '', '', '',
196 false, $timestamp ) )
198 $this->log( "Error uploading file $fullpath" );
199 return;
202 $this->log( $fullpath );
206 $builder = new ImageBuilder( isset( $options['dry-run'] ) );
207 if( isset( $options['missing'] ) ) {
208 $builder->crawlMissing();
209 } else {
210 $builder->build();