Big oops - merged to wrong branch.
[lhc/web/wiklou.git] / maintenance / rebuildImages.php
1 <?php
2 /**
3 * Script to update image metadata records
4 *
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.
9 *
10 * Copyright © 2005 Brion Vibber <brion@pobox.com>
11 * http://www.mediawiki.org/
12 *
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.
17 *
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.
22 *
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
27 *
28 * @file
29 * @author Brion Vibber <brion at pobox.com>
30 * @ingroup Maintenance
31 */
32
33 require_once( dirname( __FILE__ ) . '/Maintenance.php' );
34
35 class ImageBuilder extends Maintenance {
36
37 /**
38 * @var DatabaseBase
39 */
40 protected $dbw;
41
42 function __construct() {
43 parent::__construct();
44
45 global $wgUpdateCompatibleMetadata;
46 //make sure to update old, but compatible img_metadata fields.
47 $wgUpdateCompatibleMetadata = true;
48
49 $this->mDescription = 'Script to update image metadata records';
50
51 $this->addOption( 'missing', 'Check for files without associated database record' );
52 $this->addOption( 'dry-run', 'Only report, don\'t update the database' );
53 }
54
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';
61 }
62
63 if ( $this->hasOption( 'missing' ) ) {
64 $this->crawlMissing();
65 } else {
66 $this->build();
67 }
68 }
69
70 /**
71 * @return FileRepo
72 */
73 function getRepo() {
74 if ( !isset( $this->repo ) ) {
75 $this->repo = RepoGroup::singleton()->getLocalRepo();
76 }
77 return $this->repo;
78 }
79
80 function build() {
81 $this->buildImage();
82 $this->buildOldImage();
83 }
84
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;
91 }
92
93 function progress( $updated ) {
94 $this->updated += $updated;
95 $this->processed++;
96 if ( $this->processed % 100 != 0 ) {
97 return;
98 }
99 $portion = $this->processed / $this->count;
100 $updateRate = $this->updated / $this->processed;
101
102 $now = wfTime();
103 $delta = $now - $this->startTime;
104 $estimatedTotalTime = $delta / $portion;
105 $eta = $this->startTime + $estimatedTotalTime;
106 $rate = $this->processed / $delta;
107
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();
118 }
119
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" );
124
125 $result = wfGetDB( DB_SLAVE )->select( $table, '*', array(), __METHOD__ );
126
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 );
133 }
134 }
135 $this->output( "Finished $table... $this->updated of $this->processed rows updated\n" );
136 }
137
138 function buildImage() {
139 $callback = array( $this, 'imageCallback' );
140 $this->buildTable( 'image', 'img_name', $callback );
141 }
142
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();
148 }
149
150 function buildOldImage() {
151 $this->buildTable( 'oldimage', 'oi_archive_name', array( $this, 'oldimageCallback' ) );
152 }
153
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;
160 }
161 $file = $this->getRepo()->newFileFromRow( $row );
162 return $file->getUpgraded();
163 }
164
165 function crawlMissing() {
166 $this->getRepo()->enumFiles( array( $this, 'checkMissingImage' ) );
167 }
168
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__ );
175
176 if ( !$row ) { // file not registered
177 $this->addMissingImage( $filename, $fullpath );
178 }
179 }
180
181 function addMissingImage( $filename, $fullpath ) {
182 global $wgContLang;
183
184 $timestamp = $this->dbw->timestamp( $this->getRepo()->getFileTimestamp( $fullpath ) );
185
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 );
194 }
195 }
196
197 if ( $filename == '' ) {
198 $this->output( "Empty filename for $fullpath\n" );
199 return;
200 }
201 if ( !$this->dryrun ) {
202 $file = wfLocalFile( $filename );
203 if ( !$file->recordUpload( '', '(recovered file, missing upload log entry)', '', '', '',
204 false, $timestamp ) )
205 {
206 $this->output( "Error uploading file $fullpath\n" );
207 return;
208 }
209 }
210 $this->output( $fullpath . "\n" );
211 }
212 }
213
214 $maintClass = 'ImageBuilder';
215 require_once( RUN_MAINTENANCE_IF_MAIN );