MediaWiki master
refreshFileHeaders.php
Go to the documentation of this file.
1<?php
16
17// @codeCoverageIgnoreStart
18require_once __DIR__ . '/Maintenance.php';
19// @codeCoverageIgnoreEnd
20
27 public function __construct() {
28 parent::__construct();
29 $this->addDescription( 'Script to update file HTTP headers' );
30 $this->addOption( 'verbose', 'Output information about each file.', false, false, 'v' );
31 $this->addOption( 'start', 'Name of file to start with', false, true );
32 $this->addOption( 'end', 'Name of file to end with', false, true );
33 $this->addOption( 'media_type', 'Media type to filter for', false, true );
34 $this->addOption( 'major_mime', 'Major mime type to filter for', false, true );
35 $this->addOption( 'minor_mime', 'Minor mime type to filter for', false, true );
36 $this->addOption(
37 'refreshContentType',
38 'Set true to refresh file content type from mime data in db',
39 false,
40 false
41 );
42 $this->setBatchSize( 200 );
43 }
44
45 public function execute() {
46 $repo = $this->getServiceContainer()->getRepoGroup()->getLocalRepo();
47 $start = str_replace( ' ', '_', $this->getOption( 'start', '' ) ); // page on img_name
48 $end = str_replace( ' ', '_', $this->getOption( 'end', '' ) ); // page on img_name
49 // filter by img_media_type
50 $media_type = str_replace( ' ', '_', $this->getOption( 'media_type', '' ) );
51 // filter by img_major_mime
52 $major_mime = str_replace( ' ', '_', $this->getOption( 'major_mime', '' ) );
53 // filter by img_minor_mime
54 $minor_mime = str_replace( ' ', '_', $this->getOption( 'minor_mime', '' ) );
55
56 $count = 0;
57 $dbr = $this->getReplicaDB();
58
59 do {
60 $queryBuilder = FileSelectQueryBuilder::newForFile( $dbr );
61
62 $queryBuilder->where( $dbr->expr( 'img_name', '>', $start ) );
63 if ( $end !== '' ) {
64 $queryBuilder->andWhere( $dbr->expr( 'img_name', '<=', $end ) );
65 }
66
67 if ( $media_type !== '' ) {
68 $queryBuilder->andWhere( [ 'img_media_type' => $media_type ] );
69 }
70
71 if ( $major_mime !== '' ) {
72 $queryBuilder->andWhere( [ 'img_major_mime' => $major_mime ] );
73 }
74
75 if ( $minor_mime !== '' ) {
76 $queryBuilder->andWhere( [ 'img_minor_mime' => $minor_mime ] );
77 }
78
79 $res = $queryBuilder
80 ->orderBy( 'img_name', SelectQueryBuilder::SORT_ASC )
81 ->limit( $this->getBatchSize() )
82 ->caller( __METHOD__ )->fetchResultSet();
83
84 if ( $res->numRows() > 0 ) {
85 $row1 = $res->current();
86 $this->output( "Processing next {$res->numRows()} row(s) starting with {$row1->img_name}.\n" );
87 $res->rewind();
88 }
89
90 $backendOperations = [];
91
92 foreach ( $res as $row ) {
93 $file = $repo->newFileFromRow( $row );
94 $headers = $file->getContentHeaders();
95 if ( $this->getOption( 'refreshContentType', false ) ) {
96 $headers['Content-Type'] = $row->img_major_mime . '/' . $row->img_minor_mime;
97 }
98
99 if ( count( $headers ) ) {
100 $backendOperations[] = [
101 'op' => 'describe', 'src' => $file->getPath(), 'headers' => $headers
102 ];
103 }
104
105 // Do all of the older file versions...
106 foreach ( $file->getHistory() as $oldFile ) {
107 $headers = $oldFile->getContentHeaders();
108 if ( count( $headers ) ) {
109 $backendOperations[] = [
110 'op' => 'describe', 'src' => $oldFile->getPath(), 'headers' => $headers
111 ];
112 }
113 }
114
115 if ( $this->hasOption( 'verbose' ) ) {
116 $this->output( "Queued headers update for file '{$row->img_name}'.\n" );
117 }
118
119 $start = $row->img_name; // advance
120 }
121
122 $backendOperationsCount = count( $backendOperations );
123 $count += $backendOperationsCount;
124
125 $this->output( "Updating headers for {$backendOperationsCount} file(s).\n" );
126 $this->updateFileHeaders( $repo, $backendOperations );
127 } while ( $res->numRows() === $this->getBatchSize() );
128
129 $this->output( "Done. Updated headers for $count file(s).\n" );
130 }
131
136 protected function updateFileHeaders( $repo, $backendOperations ) {
137 $status = $repo->getBackend()->doQuickOperations( $backendOperations );
138
139 if ( !$status->isGood() ) {
140 $this->error( $status );
141 }
142 }
143}
144
145// @codeCoverageIgnoreStart
146$maintClass = RefreshFileHeaders::class;
147require_once RUN_MAINTENANCE_IF_MAIN;
148// @codeCoverageIgnoreEnd
Local repository that stores files in the local filesystem and registers them in the wiki's own datab...
Definition LocalRepo.php:45
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
getBatchSize()
Returns batch size.
output( $out, $channel=null)
Throw some output to the user.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
hasOption( $name)
Checks to see if a particular option was set.
getOption( $name, $default=null)
Get an option, or return the default.
getReplicaDB(string|false $virtualDomain=false)
error( $err, $die=0)
Throw an error to the user.
getServiceContainer()
Returns the main service container.
addDescription( $text)
Set the description text.
Maintenance script to refresh file headers from metadata.
updateFileHeaders( $repo, $backendOperations)
execute()
Do the actual work.
__construct()
Default constructor.
Build SELECT queries with a fluent interface.