MediaWiki  1.34.0
refreshFileHeaders.php
Go to the documentation of this file.
1 <?php
26 require_once __DIR__ . '/Maintenance.php';
27 
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 );
43  $this->addOption(
44  'refreshContentType',
45  'Set true to refresh file content type from mime data in db',
46  false,
47  false
48  );
49  $this->setBatchSize( 200 );
50  }
51 
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', '' ) );
62 
63  $count = 0;
64  $dbr = $this->getDB( DB_REPLICA );
65 
66  $fileQuery = LocalFile::getQueryInfo();
67 
68  do {
69  $conds = [ "img_name > {$dbr->addQuotes( $start )}" ];
70 
71  if ( strlen( $end ) ) {
72  $conds[] = "img_name <= {$dbr->addQuotes( $end )}";
73  }
74 
75  if ( strlen( $media_type ) ) {
76  $conds[] = "img_media_type = {$dbr->addQuotes( $media_type )}";
77  }
78 
79  if ( strlen( $major_mime ) ) {
80  $conds[] = "img_major_mime = {$dbr->addQuotes( $major_mime )}";
81  }
82 
83  if ( strlen( $minor_mime ) ) {
84  $conds[] = "img_minor_mime = {$dbr->addQuotes( $minor_mime )}";
85  }
86 
87  $res = $dbr->select( $fileQuery['tables'],
88  $fileQuery['fields'],
89  $conds,
90  __METHOD__,
91  [
92  'LIMIT' => $this->getBatchSize(),
93  'ORDER BY' => 'img_name ASC'
94  ],
95  $fileQuery['joins']
96  );
97 
98  if ( $res->numRows() > 0 ) {
99  $row1 = $res->current();
100  $this->output( "Processing next {$res->numRows()} row(s) starting with {$row1->img_name}.\n" );
101  $res->rewind();
102  }
103 
104  $backendOperations = [];
105 
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;
111  }
112 
113  if ( count( $headers ) ) {
114  $backendOperations[] = [
115  'op' => 'describe', 'src' => $file->getPath(), 'headers' => $headers
116  ];
117  }
118 
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
125  ];
126  }
127  }
128 
129  if ( $this->hasOption( 'verbose' ) ) {
130  $this->output( "Queued headers update for file '{$row->img_name}'.\n" );
131  }
132 
133  $start = $row->img_name; // advance
134  }
135 
136  $backendOperationsCount = count( $backendOperations );
137  $count += $backendOperationsCount;
138 
139  $this->output( "Updating headers for {$backendOperationsCount} file(s).\n" );
140  $this->updateFileHeaders( $repo, $backendOperations );
141  } while ( $res->numRows() === $this->getBatchSize() );
142 
143  $this->output( "Done. Updated headers for $count file(s).\n" );
144  }
145 
150  protected function updateFileHeaders( $repo, $backendOperations ) {
151  $status = $repo->getBackend()->doQuickOperations( $backendOperations );
152 
153  if ( !$status->isGood() ) {
154  $this->error( "Encountered error: " . print_r( $status, true ) );
155  }
156  }
157 }
158 
159 $maintClass = RefreshFileHeaders::class;
160 require_once RUN_MAINTENANCE_IF_MAIN;
RUN_MAINTENANCE_IF_MAIN
const RUN_MAINTENANCE_IF_MAIN
Definition: Maintenance.php:39
RepoGroup\singleton
static singleton()
Definition: RepoGroup.php:60
RefreshFileHeaders
Maintenance script to refresh file headers from metadata.
Definition: refreshFileHeaders.php:33
Maintenance\addDescription
addDescription( $text)
Set the description text.
Definition: Maintenance.php:348
$file
if(PHP_SAPI !='cli-server') if(!isset( $_SERVER['SCRIPT_FILENAME'])) $file
Item class for a filearchive table row.
Definition: router.php:42
Maintenance
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
Definition: Maintenance.php:82
RefreshFileHeaders\execute
execute()
Do the actual work.
Definition: refreshFileHeaders.php:52
$res
$res
Definition: testCompression.php:52
$maintClass
$maintClass
Definition: refreshFileHeaders.php:159
$dbr
$dbr
Definition: testCompression.php:50
Maintenance\addOption
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
Definition: Maintenance.php:267
DB_REPLICA
const DB_REPLICA
Definition: defines.php:25
Maintenance\getDB
getDB( $db, $groups=[], $dbDomain=false)
Returns a database to be used by current maintenance script.
Definition: Maintenance.php:1396
RefreshFileHeaders\updateFileHeaders
updateFileHeaders( $repo, $backendOperations)
Definition: refreshFileHeaders.php:150
LocalFile\getQueryInfo
static getQueryInfo(array $options=[])
Return the tables, fields, and join conditions to be selected to create a new localfile object.
Definition: LocalFile.php:216
$status
return $status
Definition: SyntaxHighlight.php:347
Maintenance\getOption
getOption( $name, $default=null)
Get an option, or return the default.
Definition: Maintenance.php:302
Maintenance\getBatchSize
getBatchSize()
Returns batch size.
Definition: Maintenance.php:386
Maintenance\error
error( $err, $die=0)
Throw an error to the user.
Definition: Maintenance.php:481
Maintenance\output
output( $out, $channel=null)
Throw some output to the user.
Definition: Maintenance.php:453
Maintenance\hasOption
hasOption( $name)
Checks to see if a particular option exists.
Definition: Maintenance.php:288
RefreshFileHeaders\__construct
__construct()
Default constructor.
Definition: refreshFileHeaders.php:34
Maintenance\setBatchSize
setBatchSize( $s=0)
Set the batch size.
Definition: Maintenance.php:394