Remove unused global $IP
[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 * @file
29 * @author Brion Vibber <brion at pobox.com>
30 * @ingroup maintenance
31 */
32
33 $options = array( 'missing', 'dry-run' );
34
35 require_once( dirname( __FILE__ ) . '/commandLine.inc' );
36 require_once( 'FiveUpgrade.inc' );
37
38 class ImageBuilder extends FiveUpgrade {
39 function ImageBuilder( $dryrun = false ) {
40 parent::FiveUpgrade();
41
42 $this->maxLag = 10; # if slaves are lagged more than 10 secs, wait
43 $this->dryrun = $dryrun;
44 if ( $dryrun ) {
45 $GLOBALS['wgReadOnly'] = 'Dry run mode, image upgrades are suppressed';
46 }
47 }
48
49 function getRepo() {
50 if ( !isset( $this->repo ) ) {
51 $this->repo = RepoGroup::singleton()->getLocalRepo();
52 }
53 return $this->repo;
54 }
55
56 function build() {
57 $this->buildImage();
58 $this->buildOldImage();
59 }
60
61 function init( $count, $table ) {
62 $this->processed = 0;
63 $this->updated = 0;
64 $this->count = $count;
65 $this->startTime = wfTime();
66 $this->table = $table;
67 }
68
69 function progress( $updated ) {
70 $this->updated += $updated;
71 $this->processed++;
72 if ( $this->processed % 100 != 0 ) {
73 return;
74 }
75 $portion = $this->processed / $this->count;
76 $updateRate = $this->updated / $this->processed;
77
78 $now = wfTime();
79 $delta = $now - $this->startTime;
80 $estimatedTotalTime = $delta / $portion;
81 $eta = $this->startTime + $estimatedTotalTime;
82
83 printf( "%s: %6.2f%% done on %s; ETA %s [%d/%d] %.2f/sec <%.2f%% updated>\n",
84 wfTimestamp( TS_DB, intval( $now ) ),
85 $portion * 100.0,
86 $this->table,
87 wfTimestamp( TS_DB, intval( $eta ) ),
88 $completed, // $completed does not appear to be defined.
89 $this->count,
90 $rate, // $rate does not appear to be defined.
91 $updateRate * 100.0 );
92 flush();
93 }
94
95 function buildTable( $table, $key, $callback ) {
96 $fname = 'ImageBuilder::buildTable';
97
98 $count = $this->dbw->selectField( $table, 'count(*)', '', $fname );
99 $this->init( $count, $table );
100 $this->log( "Processing $table..." );
101
102 $tableName = $this->dbr->tableName( $table );
103 $sql = "SELECT * FROM $tableName";
104 $result = $this->dbr->query( $sql, $fname );
105
106 while ( $row = $this->dbr->fetchObject( $result ) ) {
107 $update = call_user_func( $callback, $row, null );
108 if ( $update ) {
109 $this->progress( 1 );
110 } else {
111 $this->progress( 0 );
112 }
113 }
114 $this->log( "Finished $table... $this->updated of $this->processed rows updated" );
115 $this->dbr->freeResult( $result );
116 }
117
118 function buildImage() {
119 $callback = array( &$this, 'imageCallback' );
120 $this->buildTable( 'image', 'img_name', $callback );
121 }
122
123 function imageCallback( $row, $copy ) {
124 // Create a File object from the row
125 // This will also upgrade it
126 $file = $this->getRepo()->newFileFromRow( $row );
127 return $file->getUpgraded();
128 }
129
130 function buildOldImage() {
131 $this->buildTable( 'oldimage', 'oi_archive_name',
132 array( &$this, 'oldimageCallback' ) );
133 }
134
135 function oldimageCallback( $row, $copy ) {
136 // Create a File object from the row
137 // This will also upgrade it
138 if ( $row->oi_archive_name == '' ) {
139 $this->log( "Empty oi_archive_name for oi_name={$row->oi_name}" );
140 return false;
141 }
142 $file = $this->getRepo()->newFileFromRow( $row );
143 return $file->getUpgraded();
144 }
145
146 function crawlMissing() {
147 $repo = RepoGroup::singleton()->getLocalRepo();
148 $repo->enumFilesInFS( array( $this, 'checkMissingImage' ) );
149 }
150
151 function checkMissingImage( $fullpath ) {
152 $fname = 'ImageBuilder::checkMissingImage';
153 $filename = wfBaseName( $fullpath );
154 if ( is_dir( $fullpath ) ) {
155 return;
156 }
157 if ( is_link( $fullpath ) ) {
158 $this->log( "skipping symlink at $fullpath" );
159 return;
160 }
161 $row = $this->dbw->selectRow( 'image',
162 array( 'img_name' ),
163 array( 'img_name' => $filename ),
164 $fname );
165
166 if ( $row ) {
167 // already known, move on
168 return;
169 } else {
170 $this->addMissingImage( $filename, $fullpath );
171 }
172 }
173
174 function addMissingImage( $filename, $fullpath ) {
175 $fname = 'ImageBuilder::addMissingImage';
176
177 $timestamp = $this->dbw->timestamp( filemtime( $fullpath ) );
178
179 global $wgContLang;
180 $altname = $wgContLang->checkTitleEncoding( $filename );
181 if ( $altname != $filename ) {
182 if ( $this->dryrun ) {
183 $filename = $altname;
184 $this->log( "Estimating transcoding... $altname" );
185 } else {
186 $filename = $this->renameFile( $filename );
187 }
188 }
189
190 if ( $filename == '' ) {
191 $this->log( "Empty filename for $fullpath" );
192 return;
193 }
194 if ( !$this->dryrun ) {
195 $file = wfLocalFile( $filename );
196 if ( !$file->recordUpload( '', '(recovered file, missing upload log entry)', '', '', '',
197 false, $timestamp ) )
198 {
199 $this->log( "Error uploading file $fullpath" );
200 return;
201 }
202 }
203 $this->log( $fullpath );
204 }
205 }
206
207 $builder = new ImageBuilder( isset( $options['dry-run'] ) );
208 if ( isset( $options['missing'] ) ) {
209 $builder->crawlMissing();
210 } else {
211 $builder->build();
212 }
213
214