RefreshFileHeaders::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 7
nc 1
nop 0
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 34 and the first side effect is on line 27.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
/**
3
 * Refresh file headers from metadata.
4
 *
5
 * Usage: php refreshFileHeaders.php
6
 *
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.
11
 *
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.
16
 *
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
21
 *
22
 * @file
23
 * @author Aaron Schulz
24
 * @ingroup Maintenance
25
 */
26
27
require_once __DIR__ . '/Maintenance.php';
28
29
/**
30
 * Maintenance script to refresh file headers from metadata
31
 *
32
 * @ingroup Maintenance
33
 */
34
class RefreshFileHeaders extends Maintenance {
35
	function __construct() {
36
		parent::__construct();
37
		$this->addDescription( 'Script to update file HTTP headers' );
38
		$this->addOption( 'verbose', 'Output information about each file.', false, false, 'v' );
39
		$this->addOption( 'start', 'Name of file to start with', false, true );
40
		$this->addOption( 'end', 'Name of file to end with', false, true );
41
		$this->setBatchSize( 200 );
42
	}
43
44
	public function execute() {
45
		$repo = RepoGroup::singleton()->getLocalRepo();
46
		$start = str_replace( ' ', '_', $this->getOption( 'start', '' ) ); // page on img_name
47
		$end = str_replace( ' ', '_', $this->getOption( 'end', '' ) ); // page on img_name
48
49
		$count = 0;
50
		$dbr = $this->getDB( DB_REPLICA );
51
		do {
52
			$conds = [ "img_name > {$dbr->addQuotes( $start )}" ];
53
			if ( strlen( $end ) ) {
54
				$conds[] = "img_name <= {$dbr->addQuotes( $end )}";
55
			}
56
			$res = $dbr->select( 'image', '*', $conds,
57
				__METHOD__, [ 'LIMIT' => $this->mBatchSize, 'ORDER BY' => 'img_name ASC' ] );
58
			foreach ( $res as $row ) {
59
				$file = $repo->newFileFromRow( $row );
60
				$headers = $file->getStreamHeaders();
61
				if ( count( $headers ) ) {
62
					$this->updateFileHeaders( $file, $headers );
63
				}
64
				// Do all of the older file versions...
65
				foreach ( $file->getHistory() as $oldFile ) {
66
					$headers = $oldFile->getStreamHeaders();
67
					if ( count( $headers ) ) {
68
						$this->updateFileHeaders( $oldFile, $headers );
69
					}
70
				}
71
				if ( $this->hasOption( 'verbose' ) ) {
72
					$this->output( "Updated headers for file '{$row->img_name}'.\n" );
73
				}
74
				++$count;
75
				$start = $row->img_name; // advance
76
			}
77
		} while ( $res->numRows() > 0 );
78
79
		$this->output( "Done. Updated headers for $count file(s).\n" );
80
	}
81
82
	protected function updateFileHeaders( File $file, array $headers ) {
83
		$status = $file->getRepo()->getBackend()->describe( [
84
			'src' => $file->getPath(), 'headers' => $headers
85
		] );
86
		if ( !$status->isGood() ) {
87
			$this->error( "Encountered error: " . print_r( $status, true ) );
88
		}
89
	}
90
}
91
92
$maintClass = 'RefreshFileHeaders';
93
require_once RUN_MAINTENANCE_IF_MAIN;
94