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