From: Aaron Schulz Date: Tue, 27 Nov 2012 00:09:01 +0000 (-0800) Subject: Added a script to populate file HTTP headers for existing files. X-Git-Tag: 1.31.0-rc.0~21488 X-Git-Url: http://git.cyclocoop.org/url?a=commitdiff_plain;h=f52cf9b1ec990ff2e96e7ad8ee0252c9a4152f9a;p=lhc%2Fweb%2Fwiklou.git Added a script to populate file HTTP headers for existing files. Change-Id: I17a0e3d0e84fea1638afe18f2ee3d36ea6be831a --- diff --git a/maintenance/refreshFileHeaders.php b/maintenance/refreshFileHeaders.php new file mode 100644 index 0000000000..74f0f35d07 --- /dev/null +++ b/maintenance/refreshFileHeaders.php @@ -0,0 +1,93 @@ +mDescription = 'Script to update file HTTP headers'; + $this->addOption( 'verbose', 'Output information about each file.', false, false, 'v' ); + $this->addOption( 'start', 'Name of file to start with', false, true ); + $this->addOption( 'end', 'Name of file to end with', false, true ); + $this->setBatchSize( 200 ); + } + + public function execute() { + $repo = RepoGroup::singleton()->getLocalRepo(); + $start = str_replace( ' ', '_', $this->getOption( 'start', '' ) ); // page on img_name + $end = str_replace( ' ', '_', $this->getOption( 'end', '' ) ); // page on img_name + + $count = 0; + $dbr = wfGetDB( DB_SLAVE ); + do { + $conds = array( "img_name > {$dbr->addQuotes( $start )}" ); + if ( strlen( $end ) ) { + $conds[] = "img_name <= {$dbr->addQuotes( $end )}"; + } + $res = $dbr->select( 'image', '*', $conds, + __METHOD__, array( 'LIMIT' => $this->mBatchSize, 'ORDER BY' => 'img_name ASC' ) ); + foreach ( $res as $row ) { + $file = $repo->newFileFromRow( $row ); + $headers = $file->getStreamHeaders(); + if ( count( $headers ) ) { + $this->updateFileHeaders( $file, $headers ); + } + // Do all of the older file versions... + foreach ( $file->getHistory() as $oldFile ) { + $headers = $oldFile->getStreamHeaders(); + if ( count( $headers ) ) { + $this->updateFileHeaders( $oldFile, $headers ); + } + } + if ( $this->hasOption( 'verbose' ) ) { + $this->output( "Updated headers for file '{$row->img_name}'.\n" ); + } + ++$count; + $start = $row->img_name; // advance + } + } while ( $res->numRows() > 0 ); + + $this->output( "Done. Updated headers for $count file(s).\n" ); + } + + protected function updateFileHeaders( File $file, array $headers ) { + $status = $file->getRepo()->getBackend()->describe( array( + 'src' => $file->getPath(), 'headers' => $headers + ) ); + if ( !$status->isGood() ) { + $this->error( "Encountered error: " . print_r( $status, true ) ); + } + } +} + +$maintClass = 'RefreshFileHeaders'; +require_once( RUN_MAINTENANCE_IF_MAIN );