Merged filerepo-work 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 (C) 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 * @author Brion Vibber <brion at pobox.com>
29 * @addtogroup maintenance
30 */
31
32 $options = array( 'missing', 'dry-run' );
33
34 require_once( 'commandLine.inc' );
35 require_once( 'FiveUpgrade.inc' );
36
37 class ImageBuilder extends FiveUpgrade {
38 function ImageBuilder( $dryrun = false ) {
39 parent::FiveUpgrade();
40
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';
45 }
46 }
47
48 function getRepo() {
49 if ( !isset( $this->repo ) ) {
50 $this->repo = RepoGroup::singleton()->getLocalRepo();
51 }
52 return $this->repo;
53 }
54
55 function build() {
56 $this->buildImage();
57 $this->buildOldImage();
58 }
59
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;
66 }
67
68 function progress( $updated ) {
69 $this->updated += $updated;
70 $this->processed++;
71 if( $this->processed % 100 != 0 ) {
72 return;
73 }
74 $portion = $this->processed / $this->count;
75 $updateRate = $this->updated / $this->processed;
76
77 $now = wfTime();
78 $delta = $now - $this->startTime;
79 $estimatedTotalTime = $delta / $portion;
80 $eta = $this->startTime + $estimatedTotalTime;
81
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();
92 }
93
94 function buildTable( $table, $key, $callback ) {
95 $fname = 'ImageBuilder::buildTable';
96
97 $count = $this->dbw->selectField( $table, 'count(*)', '', $fname );
98 $this->init( $count, $table );
99 $this->log( "Processing $table..." );
100
101 $tableName = $this->dbr->tableName( $table );
102 $sql = "SELECT * FROM $tableName";
103 $result = $this->dbr->query( $sql, $fname );
104
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 );
111 }
112 }
113 $this->log( "Finished $table... $this->updated of $this->processed rows updated" );
114 $this->dbr->freeResult( $result );
115 }
116
117 function buildImage() {
118 $callback = array( &$this, 'imageCallback' );
119 $this->buildTable( 'image', 'img_name', $callback );
120 }
121
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();
127 }
128
129 function buildOldImage() {
130 $this->buildTable( 'oldimage', 'oi_archive_name',
131 array( &$this, 'oldimageCallback' ) );
132 }
133
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;
140 }
141 $file = $this->getRepo()->newFileFromRow( $row );
142 return $file->getUpgraded();
143 }
144
145 function crawlMissing() {
146 $repo = RepoGroup::singleton()->getLocalRepo();
147 $repo->enumFilesInFS( array( $this, 'checkMissingImage' ) );
148 }
149
150 function checkMissingImage( $fullpath ) {
151 $fname = 'ImageBuilder::checkMissingImage';
152 $filename = wfBaseName( $fullpath );
153 if( is_dir( $fullpath ) ) {
154 return;
155 }
156 if( is_link( $fullpath ) ) {
157 $this->log( "skipping symlink at $fullpath" );
158 return;
159 }
160 $row = $this->dbw->selectRow( 'image',
161 array( 'img_name' ),
162 array( 'img_name' => $filename ),
163 $fname );
164
165 if( $row ) {
166 // already known, move on
167 return;
168 } else {
169 $this->addMissingImage( $filename, $fullpath );
170 }
171 }
172
173 function addMissingImage( $filename, $fullpath ) {
174 $fname = 'ImageBuilder::addMissingImage';
175
176 $size = filesize( $fullpath );
177 $info = $this->imageInfo( $fullpath );
178 $timestamp = $this->dbw->timestamp( filemtime( $fullpath ) );
179
180 global $wgContLang;
181 $altname = $wgContLang->checkTitleEncoding( $filename );
182 if( $altname != $filename ) {
183 if( $this->dryrun ) {
184 $filename = $altname;
185 $this->log( "Estimating transcoding... $altname" );
186 } else {
187 $filename = $this->renameFile( $filename );
188 }
189 }
190
191 if( $filename == '' ) {
192 $this->log( "Empty filename for $fullpath" );
193 return;
194 }
195 if ( !$this->dryrun ) {
196 $file = wfLocalFile( $filename );
197 if ( !$file->recordUpload( '', '(recovered file, missing upload log entry)', '', '', '',
198 false, $timestamp ) )
199 {
200 $this->log( "Error uploading file $fullpath" );
201 return;
202 }
203 }
204 $this->log( $fullpath );
205 }
206 }
207
208 $builder = new ImageBuilder( isset( $options['dry-run'] ) );
209 if( isset( $options['missing'] ) ) {
210 $builder->crawlMissing();
211 } else {
212 $builder->build();
213 }
214
215 ?>