3 * Refresh file headers from metadata.
5 * Usage: php refreshFileHeaders.php
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 * http://www.gnu.org/copyleft/gpl.html
23 * @ingroup Maintenance
26 require_once __DIR__
. '/Maintenance.php';
29 * Maintenance script to refresh file headers from metadata
31 * @ingroup Maintenance
33 class RefreshFileHeaders
extends Maintenance
{
34 function __construct() {
35 parent
::__construct();
36 $this->addDescription( 'Script to update file HTTP headers' );
37 $this->addOption( 'verbose', 'Output information about each file.', false, false, 'v' );
38 $this->addOption( 'start', 'Name of file to start with', false, true );
39 $this->addOption( 'end', 'Name of file to end with', false, true );
40 $this->addOption( 'media_type', 'Media type to filter for', false, true );
41 $this->addOption( 'major_mime', 'Major mime type to filter for', false, true );
42 $this->addOption( 'minor_mime', 'Minor mime type to filter for', false, true );
45 'Set true to refresh file content type from mime data in db',
49 $this->setBatchSize( 200 );
52 public function execute() {
53 $repo = RepoGroup
::singleton()->getLocalRepo();
54 $start = str_replace( ' ', '_', $this->getOption( 'start', '' ) ); // page on img_name
55 $end = str_replace( ' ', '_', $this->getOption( 'end', '' ) ); // page on img_name
56 // filter by img_media_type
57 $media_type = str_replace( ' ', '_', $this->getOption( 'media_type', '' ) );
58 // filter by img_major_mime
59 $major_mime = str_replace( ' ', '_', $this->getOption( 'major_mime', '' ) );
60 // filter by img_minor_mime
61 $minor_mime = str_replace( ' ', '_', $this->getOption( 'minor_mime', '' ) );
64 $dbr = $this->getDB( DB_REPLICA
);
66 $fileQuery = LocalFile
::getQueryInfo();
69 $conds = [ "img_name > {$dbr->addQuotes( $start )}" ];
71 if ( strlen( $end ) ) {
72 $conds[] = "img_name <= {$dbr->addQuotes( $end )}";
75 if ( strlen( $media_type ) ) {
76 $conds[] = "img_media_type = {$dbr->addQuotes( $media_type )}";
79 if ( strlen( $major_mime ) ) {
80 $conds[] = "img_major_mime = {$dbr->addQuotes( $major_mime )}";
83 if ( strlen( $minor_mime ) ) {
84 $conds[] = "img_minor_mime = {$dbr->addQuotes( $minor_mime )}";
87 $res = $dbr->select( $fileQuery['tables'],
92 'LIMIT' => $this->getBatchSize(),
93 'ORDER BY' => 'img_name ASC'
98 if ( $res->numRows() > 0 ) {
99 $row1 = $res->current();
100 $this->output( "Processing next {$res->numRows()} row(s) starting with {$row1->img_name}.\n" );
104 $backendOperations = [];
106 foreach ( $res as $row ) {
107 $file = $repo->newFileFromRow( $row );
108 $headers = $file->getContentHeaders();
109 if ( $this->getOption( 'refreshContentType', false ) ) {
110 $headers['Content-Type'] = $row->img_major_mime
. '/' . $row->img_minor_mime
;
113 if ( count( $headers ) ) {
114 $backendOperations[] = [
115 'op' => 'describe', 'src' => $file->getPath(), 'headers' => $headers
119 // Do all of the older file versions...
120 foreach ( $file->getHistory() as $oldFile ) {
121 $headers = $oldFile->getContentHeaders();
122 if ( count( $headers ) ) {
123 $backendOperations[] = [
124 'op' => 'describe', 'src' => $oldFile->getPath(), 'headers' => $headers
129 if ( $this->hasOption( 'verbose' ) ) {
130 $this->output( "Queued headers update for file '{$row->img_name}'.\n" );
133 $start = $row->img_name
; // advance
136 $backendOperationsCount = count( $backendOperations );
137 $count +
= $backendOperationsCount;
139 $this->output( "Updating headers for {$backendOperationsCount} file(s).\n" );
140 $this->updateFileHeaders( $repo, $backendOperations );
141 } while ( $res->numRows() === $this->getBatchSize() );
143 $this->output( "Done. Updated headers for $count file(s).\n" );
147 * @param LocalRepo $repo
148 * @param array $backendOperations
150 protected function updateFileHeaders( $repo, $backendOperations ) {
151 $status = $repo->getBackend()->doQuickOperations( $backendOperations );
153 if ( !$status->isGood() ) {
154 $this->error( "Encountered error: " . print_r( $status, true ) );
159 $maintClass = RefreshFileHeaders
::class;
160 require_once RUN_MAINTENANCE_IF_MAIN
;