MediaWiki  REL1_22
refreshFileHeaders.php
Go to the documentation of this file.
00001 <?php
00027 require_once __DIR__ . '/Maintenance.php';
00028 
00034 class RefreshFileHeaders extends Maintenance {
00035     function __construct() {
00036         parent::__construct();
00037         $this->mDescription = 'Script to update file HTTP headers';
00038         $this->addOption( 'verbose', 'Output information about each file.', false, false, 'v' );
00039         $this->addOption( 'start', 'Name of file to start with', false, true );
00040         $this->addOption( 'end', 'Name of file to end with', false, true );
00041         $this->setBatchSize( 200 );
00042     }
00043 
00044     public function execute() {
00045         $repo = RepoGroup::singleton()->getLocalRepo();
00046         $start = str_replace( ' ', '_', $this->getOption( 'start', '' ) ); // page on img_name
00047         $end = str_replace( ' ', '_', $this->getOption( 'end', '' ) ); // page on img_name
00048 
00049         $count = 0;
00050         $dbr = wfGetDB( DB_SLAVE );
00051         do {
00052             $conds = array( "img_name > {$dbr->addQuotes( $start )}" );
00053             if ( strlen( $end ) ) {
00054                 $conds[] = "img_name <= {$dbr->addQuotes( $end )}";
00055             }
00056             $res = $dbr->select( 'image', '*', $conds,
00057                 __METHOD__, array( 'LIMIT' => $this->mBatchSize, 'ORDER BY' => 'img_name ASC' ) );
00058             foreach ( $res as $row ) {
00059                 $file = $repo->newFileFromRow( $row );
00060                 $headers = $file->getStreamHeaders();
00061                 if ( count( $headers ) ) {
00062                     $this->updateFileHeaders( $file, $headers );
00063                 }
00064                 // Do all of the older file versions...
00065                 foreach ( $file->getHistory() as $oldFile ) {
00066                     $headers = $oldFile->getStreamHeaders();
00067                     if ( count( $headers ) ) {
00068                         $this->updateFileHeaders( $oldFile, $headers );
00069                     }
00070                 }
00071                 if ( $this->hasOption( 'verbose' ) ) {
00072                     $this->output( "Updated headers for file '{$row->img_name}'.\n" );
00073                 }
00074                 ++$count;
00075                 $start = $row->img_name; // advance
00076             }
00077         } while ( $res->numRows() > 0 );
00078 
00079         $this->output( "Done. Updated headers for $count file(s).\n" );
00080     }
00081 
00082     protected function updateFileHeaders( File $file, array $headers ) {
00083         $status = $file->getRepo()->getBackend()->describe( array(
00084             'src' => $file->getPath(), 'headers' => $headers
00085         ) );
00086         if ( !$status->isGood() ) {
00087             $this->error( "Encountered error: " . print_r( $status, true ) );
00088         }
00089     }
00090 }
00091 
00092 $maintClass = 'RefreshFileHeaders';
00093 require_once RUN_MAINTENANCE_IF_MAIN;