00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033 $options = array( 'missing', 'dry-run' );
00034
00035 require_once( 'commandLine.inc' );
00036 require_once( 'FiveUpgrade.inc' );
00037
00038 class ImageBuilder extends FiveUpgrade {
00039 function ImageBuilder( $dryrun = false ) {
00040 parent::FiveUpgrade();
00041
00042 $this->maxLag = 10; # if slaves are lagged more than 10 secs, wait
00043 $this->dryrun = $dryrun;
00044 if ( $dryrun ) {
00045 $GLOBALS['wgReadOnly'] = 'Dry run mode, image upgrades are suppressed';
00046 }
00047 }
00048
00049 function getRepo() {
00050 if ( !isset( $this->repo ) ) {
00051 $this->repo = RepoGroup::singleton()->getLocalRepo();
00052 }
00053 return $this->repo;
00054 }
00055
00056 function build() {
00057 $this->buildImage();
00058 $this->buildOldImage();
00059 }
00060
00061 function init( $count, $table ) {
00062 $this->processed = 0;
00063 $this->updated = 0;
00064 $this->count = $count;
00065 $this->startTime = wfTime();
00066 $this->table = $table;
00067 }
00068
00069 function progress( $updated ) {
00070 $this->updated += $updated;
00071 $this->processed++;
00072 if( $this->processed % 100 != 0 ) {
00073 return;
00074 }
00075 $portion = $this->processed / $this->count;
00076 $updateRate = $this->updated / $this->processed;
00077
00078 $now = wfTime();
00079 $delta = $now - $this->startTime;
00080 $estimatedTotalTime = $delta / $portion;
00081 $eta = $this->startTime + $estimatedTotalTime;
00082
00083 printf( "%s: %6.2f%% done on %s; ETA %s [%d/%d] %.2f/sec <%.2f%% updated>\n",
00084 wfTimestamp( TS_DB, intval( $now ) ),
00085 $portion * 100.0,
00086 $this->table,
00087 wfTimestamp( TS_DB, intval( $eta ) ),
00088 $completed,
00089 $this->count,
00090 $rate,
00091 $updateRate * 100.0 );
00092 flush();
00093 }
00094
00095 function buildTable( $table, $key, $callback ) {
00096 $fname = 'ImageBuilder::buildTable';
00097
00098 $count = $this->dbw->selectField( $table, 'count(*)', '', $fname );
00099 $this->init( $count, $table );
00100 $this->log( "Processing $table..." );
00101
00102 $tableName = $this->dbr->tableName( $table );
00103 $sql = "SELECT * FROM $tableName";
00104 $result = $this->dbr->query( $sql, $fname );
00105
00106 while( $row = $this->dbr->fetchObject( $result ) ) {
00107 $update = call_user_func( $callback, $row );
00108 if( $update ) {
00109 $this->progress( 1 );
00110 } else {
00111 $this->progress( 0 );
00112 }
00113 }
00114 $this->log( "Finished $table... $this->updated of $this->processed rows updated" );
00115 $this->dbr->freeResult( $result );
00116 }
00117
00118 function buildImage() {
00119 $callback = array( &$this, 'imageCallback' );
00120 $this->buildTable( 'image', 'img_name', $callback );
00121 }
00122
00123 function imageCallback( $row ) {
00124
00125
00126 $file = $this->getRepo()->newFileFromRow( $row );
00127 return $file->getUpgraded();
00128 }
00129
00130 function buildOldImage() {
00131 $this->buildTable( 'oldimage', 'oi_archive_name',
00132 array( &$this, 'oldimageCallback' ) );
00133 }
00134
00135 function oldimageCallback( $row ) {
00136
00137
00138 if ( $row->oi_archive_name == '' ) {
00139 $this->log( "Empty oi_archive_name for oi_name={$row->oi_name}" );
00140 return false;
00141 }
00142 $file = $this->getRepo()->newFileFromRow( $row );
00143 return $file->getUpgraded();
00144 }
00145
00146 function crawlMissing() {
00147 $repo = RepoGroup::singleton()->getLocalRepo();
00148 $repo->enumFilesInFS( array( $this, 'checkMissingImage' ) );
00149 }
00150
00151 function checkMissingImage( $fullpath ) {
00152 $fname = 'ImageBuilder::checkMissingImage';
00153 $filename = wfBaseName( $fullpath );
00154 if( is_dir( $fullpath ) ) {
00155 return;
00156 }
00157 if( is_link( $fullpath ) ) {
00158 $this->log( "skipping symlink at $fullpath" );
00159 return;
00160 }
00161 $row = $this->dbw->selectRow( 'image',
00162 array( 'img_name' ),
00163 array( 'img_name' => $filename ),
00164 $fname );
00165
00166 if( $row ) {
00167
00168 return;
00169 } else {
00170 $this->addMissingImage( $filename, $fullpath );
00171 }
00172 }
00173
00174 function addMissingImage( $filename, $fullpath ) {
00175 $fname = 'ImageBuilder::addMissingImage';
00176
00177 $timestamp = $this->dbw->timestamp( filemtime( $fullpath ) );
00178
00179 global $wgContLang;
00180 $altname = $wgContLang->checkTitleEncoding( $filename );
00181 if( $altname != $filename ) {
00182 if( $this->dryrun ) {
00183 $filename = $altname;
00184 $this->log( "Estimating transcoding... $altname" );
00185 } else {
00186 $filename = $this->renameFile( $filename );
00187 }
00188 }
00189
00190 if( $filename == '' ) {
00191 $this->log( "Empty filename for $fullpath" );
00192 return;
00193 }
00194 if ( !$this->dryrun ) {
00195 $file = wfLocalFile( $filename );
00196 if ( !$file->recordUpload( '', '(recovered file, missing upload log entry)', '', '', '',
00197 false, $timestamp ) )
00198 {
00199 $this->log( "Error uploading file $fullpath" );
00200 return;
00201 }
00202 }
00203 $this->log( $fullpath );
00204 }
00205 }
00206
00207 $builder = new ImageBuilder( isset( $options['dry-run'] ) );
00208 if( isset( $options['missing'] ) ) {
00209 $builder->crawlMissing();
00210 } else {
00211 $builder->build();
00212 }
00213
00214