Fix for r47482: put the RELEASE-NOTES entry in the "bug fixes" section and not the...
[mediawiki.git] / maintenance / rebuildImages.php
blob46b5d0ed8362eecc51a056cf3449208f164d37cd
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 * @file
29 * @author Brion Vibber <brion at pobox.com>
30 * @ingrouo maintenance
33 $options = array( 'missing', 'dry-run' );
35 require_once( 'commandLine.inc' );
36 require_once( 'FiveUpgrade.inc' );
38 class ImageBuilder extends FiveUpgrade {
39 function ImageBuilder( $dryrun = false ) {
40 parent::FiveUpgrade();
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';
49 function getRepo() {
50 if ( !isset( $this->repo ) ) {
51 $this->repo = RepoGroup::singleton()->getLocalRepo();
53 return $this->repo;
56 function build() {
57 $this->buildImage();
58 $this->buildOldImage();
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;
69 function progress( $updated ) {
70 $this->updated += $updated;
71 $this->processed++;
72 if( $this->processed % 100 != 0 ) {
73 return;
75 $portion = $this->processed / $this->count;
76 $updateRate = $this->updated / $this->processed;
78 $now = wfTime();
79 $delta = $now - $this->startTime;
80 $estimatedTotalTime = $delta / $portion;
81 $eta = $this->startTime + $estimatedTotalTime;
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();
95 function buildTable( $table, $key, $callback ) {
96 $fname = 'ImageBuilder::buildTable';
98 $count = $this->dbw->selectField( $table, 'count(*)', '', $fname );
99 $this->init( $count, $table );
100 $this->log( "Processing $table..." );
102 $tableName = $this->dbr->tableName( $table );
103 $sql = "SELECT * FROM $tableName";
104 $result = $this->dbr->query( $sql, $fname );
106 while( $row = $this->dbr->fetchObject( $result ) ) {
107 $update = call_user_func( $callback, $row );
108 if( $update ) {
109 $this->progress( 1 );
110 } else {
111 $this->progress( 0 );
114 $this->log( "Finished $table... $this->updated of $this->processed rows updated" );
115 $this->dbr->freeResult( $result );
118 function buildImage() {
119 $callback = array( &$this, 'imageCallback' );
120 $this->buildTable( 'image', 'img_name', $callback );
123 function imageCallback( $row ) {
124 // Create a File object from the row
125 // This will also upgrade it
126 $file = $this->getRepo()->newFileFromRow( $row );
127 return $file->getUpgraded();
130 function buildOldImage() {
131 $this->buildTable( 'oldimage', 'oi_archive_name',
132 array( &$this, 'oldimageCallback' ) );
135 function oldimageCallback( $row ) {
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;
142 $file = $this->getRepo()->newFileFromRow( $row );
143 return $file->getUpgraded();
146 function crawlMissing() {
147 $repo = RepoGroup::singleton()->getLocalRepo();
148 $repo->enumFilesInFS( array( $this, 'checkMissingImage' ) );
151 function checkMissingImage( $fullpath ) {
152 $fname = 'ImageBuilder::checkMissingImage';
153 $filename = wfBaseName( $fullpath );
154 if( is_dir( $fullpath ) ) {
155 return;
157 if( is_link( $fullpath ) ) {
158 $this->log( "skipping symlink at $fullpath" );
159 return;
161 $row = $this->dbw->selectRow( 'image',
162 array( 'img_name' ),
163 array( 'img_name' => $filename ),
164 $fname );
166 if( $row ) {
167 // already known, move on
168 return;
169 } else {
170 $this->addMissingImage( $filename, $fullpath );
174 function addMissingImage( $filename, $fullpath ) {
175 $fname = 'ImageBuilder::addMissingImage';
177 $timestamp = $this->dbw->timestamp( filemtime( $fullpath ) );
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 );
190 if( $filename == '' ) {
191 $this->log( "Empty filename for $fullpath" );
192 return;
194 if ( !$this->dryrun ) {
195 $file = wfLocalFile( $filename );
196 if ( !$file->recordUpload( '', '(recovered file, missing upload log entry)', '', '', '',
197 false, $timestamp ) )
199 $this->log( "Error uploading file $fullpath" );
200 return;
203 $this->log( $fullpath );
207 $builder = new ImageBuilder( isset( $options['dry-run'] ) );
208 if( isset( $options['missing'] ) ) {
209 $builder->crawlMissing();
210 } else {
211 $builder->build();