MediaWiki master
refreshFileHeaders.php
Go to the documentation of this file.
1<?php
29
30// @codeCoverageIgnoreStart
31require_once __DIR__ . '/Maintenance.php';
32// @codeCoverageIgnoreEnd
33
40 public function __construct() {
41 parent::__construct();
42 $this->addDescription( 'Script to update file HTTP headers' );
43 $this->addOption( 'verbose', 'Output information about each file.', false, false, 'v' );
44 $this->addOption( 'start', 'Name of file to start with', false, true );
45 $this->addOption( 'end', 'Name of file to end with', false, true );
46 $this->addOption( 'media_type', 'Media type to filter for', false, true );
47 $this->addOption( 'major_mime', 'Major mime type to filter for', false, true );
48 $this->addOption( 'minor_mime', 'Minor mime type to filter for', false, true );
49 $this->addOption(
50 'refreshContentType',
51 'Set true to refresh file content type from mime data in db',
52 false,
53 false
54 );
55 $this->setBatchSize( 200 );
56 }
57
58 public function execute() {
59 $repo = $this->getServiceContainer()->getRepoGroup()->getLocalRepo();
60 $start = str_replace( ' ', '_', $this->getOption( 'start', '' ) ); // page on img_name
61 $end = str_replace( ' ', '_', $this->getOption( 'end', '' ) ); // page on img_name
62 // filter by img_media_type
63 $media_type = str_replace( ' ', '_', $this->getOption( 'media_type', '' ) );
64 // filter by img_major_mime
65 $major_mime = str_replace( ' ', '_', $this->getOption( 'major_mime', '' ) );
66 // filter by img_minor_mime
67 $minor_mime = str_replace( ' ', '_', $this->getOption( 'minor_mime', '' ) );
68
69 $count = 0;
70 $dbr = $this->getReplicaDB();
71
72 do {
73 $queryBuilder = FileSelectQueryBuilder::newForFile( $dbr );
74
75 $queryBuilder->where( $dbr->expr( 'img_name', '>', $start ) );
76 if ( $end !== '' ) {
77 $queryBuilder->andWhere( $dbr->expr( 'img_name', '<=', $end ) );
78 }
79
80 if ( $media_type !== '' ) {
81 $queryBuilder->andWhere( [ 'img_media_type' => $media_type ] );
82 }
83
84 if ( $major_mime !== '' ) {
85 $queryBuilder->andWhere( [ 'img_major_mime' => $major_mime ] );
86 }
87
88 if ( $minor_mime !== '' ) {
89 $queryBuilder->andWhere( [ 'img_minor_mime' => $minor_mime ] );
90 }
91
92 $res = $queryBuilder
93 ->orderBy( 'img_name', SelectQueryBuilder::SORT_ASC )
94 ->limit( $this->getBatchSize() )
95 ->caller( __METHOD__ )->fetchResultSet();
96
97 if ( $res->numRows() > 0 ) {
98 $row1 = $res->current();
99 $this->output( "Processing next {$res->numRows()} row(s) starting with {$row1->img_name}.\n" );
100 $res->rewind();
101 }
102
103 $backendOperations = [];
104
105 foreach ( $res as $row ) {
106 $file = $repo->newFileFromRow( $row );
107 $headers = $file->getContentHeaders();
108 if ( $this->getOption( 'refreshContentType', false ) ) {
109 $headers['Content-Type'] = $row->img_major_mime . '/' . $row->img_minor_mime;
110 }
111
112 if ( count( $headers ) ) {
113 $backendOperations[] = [
114 'op' => 'describe', 'src' => $file->getPath(), 'headers' => $headers
115 ];
116 }
117
118 // Do all of the older file versions...
119 foreach ( $file->getHistory() as $oldFile ) {
120 $headers = $oldFile->getContentHeaders();
121 if ( count( $headers ) ) {
122 $backendOperations[] = [
123 'op' => 'describe', 'src' => $oldFile->getPath(), 'headers' => $headers
124 ];
125 }
126 }
127
128 if ( $this->hasOption( 'verbose' ) ) {
129 $this->output( "Queued headers update for file '{$row->img_name}'.\n" );
130 }
131
132 $start = $row->img_name; // advance
133 }
134
135 $backendOperationsCount = count( $backendOperations );
136 $count += $backendOperationsCount;
137
138 $this->output( "Updating headers for {$backendOperationsCount} file(s).\n" );
139 $this->updateFileHeaders( $repo, $backendOperations );
140 } while ( $res->numRows() === $this->getBatchSize() );
141
142 $this->output( "Done. Updated headers for $count file(s).\n" );
143 }
144
149 protected function updateFileHeaders( $repo, $backendOperations ) {
150 $status = $repo->getBackend()->doQuickOperations( $backendOperations );
151
152 if ( !$status->isGood() ) {
153 $this->error( $status );
154 }
155 }
156}
157
158// @codeCoverageIgnoreStart
159$maintClass = RefreshFileHeaders::class;
160require_once RUN_MAINTENANCE_IF_MAIN;
161// @codeCoverageIgnoreEnd
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.
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.