UploadDumper::outputItem()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 10
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 8
nc 2
nop 2
dl 0
loc 10
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 32 and the first side effect is on line 24.

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
 * Dump a the list of files uploaded, for feeding to tar or similar.
4
 *
5
 * This program is free software; you can redistribute it and/or modify
6
 * it under the terms of the GNU General Public License as published by
7
 * the Free Software Foundation; either version 2 of the License, or
8
 * (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License along
16
 * with this program; if not, write to the Free Software Foundation, Inc.,
17
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18
 * http://www.gnu.org/copyleft/gpl.html
19
 *
20
 * @file
21
 * @ingroup Maintenance
22
 */
23
24
require_once __DIR__ . '/Maintenance.php';
25
26
/**
27
 * Maintenance script to dump a the list of files uploaded,
28
 * for feeding to tar or similar.
29
 *
30
 * @ingroup Maintenance
31
 */
32
class UploadDumper extends Maintenance {
33
	public function __construct() {
34
		parent::__construct();
35
		$this->addDescription( 'Generates list of uploaded files which can be fed to tar or similar.
36
By default, outputs relative paths against the parent directory of $wgUploadDirectory.' );
37
		$this->addOption( 'base', 'Set base relative path instead of wiki include root', false, true );
38
		$this->addOption( 'local', 'List all local files, used or not. No shared files included' );
39
		$this->addOption( 'used', 'Skip local images that are not used' );
40
		$this->addOption( 'shared', 'Include images used from shared repository' );
41
	}
42
43
	public function execute() {
44
		global $IP;
45
		$this->mAction = 'fetchLocal';
0 ignored issues
show
Bug introduced by
The property mAction does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
46
		$this->mBasePath = $this->getOption( 'base', $IP );
0 ignored issues
show
Bug introduced by
The property mBasePath does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
47
		$this->mShared = false;
0 ignored issues
show
Bug introduced by
The property mShared does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
48
		$this->mSharedSupplement = false;
0 ignored issues
show
Bug introduced by
The property mSharedSupplement does not seem to exist. Did you mean mShared?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
49
50
		if ( $this->hasOption( 'local' ) ) {
51
			$this->mAction = 'fetchLocal';
52
		}
53
54
		if ( $this->hasOption( 'used' ) ) {
55
			$this->mAction = 'fetchUsed';
56
		}
57
58
		if ( $this->hasOption( 'shared' ) ) {
59
			if ( $this->hasOption( 'used' ) ) {
60
				// Include shared-repo files in the used check
61
				$this->mShared = true;
62
			} else {
63
				// Grab all local *plus* used shared
64
				$this->mSharedSupplement = true;
0 ignored issues
show
Bug introduced by
The property mSharedSupplement does not seem to exist. Did you mean mShared?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
65
			}
66
		}
67
		$this->{$this->mAction} ( $this->mShared );
68
		if ( $this->mSharedSupplement ) {
0 ignored issues
show
Bug introduced by
The property mSharedSupplement does not seem to exist. Did you mean mShared?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
69
			$this->fetchUsed( true );
70
		}
71
	}
72
73
	/**
74
	 * Fetch a list of used images from a particular image source.
75
	 *
76
	 * @param bool $shared True to pass shared-dir settings to hash func
77
	 */
78
	function fetchUsed( $shared ) {
79
		$dbr = $this->getDB( DB_REPLICA );
80
		$image = $dbr->tableName( 'image' );
81
		$imagelinks = $dbr->tableName( 'imagelinks' );
82
83
		$sql = "SELECT DISTINCT il_to, img_name
84
			FROM $imagelinks
85
			LEFT OUTER JOIN $image
86
			ON il_to=img_name";
87
		$result = $dbr->query( $sql );
88
89
		foreach ( $result as $row ) {
90
			$this->outputItem( $row->il_to, $shared );
91
		}
92
	}
93
94
	/**
95
	 * Fetch a list of all images from a particular image source.
96
	 *
97
	 * @param bool $shared True to pass shared-dir settings to hash func
98
	 */
99
	function fetchLocal( $shared ) {
100
		$dbr = $this->getDB( DB_REPLICA );
101
		$result = $dbr->select( 'image',
102
			[ 'img_name' ],
103
			'',
104
			__METHOD__ );
105
106
		foreach ( $result as $row ) {
107
			$this->outputItem( $row->img_name, $shared );
108
		}
109
	}
110
111
	function outputItem( $name, $shared ) {
112
		$file = wfFindFile( $name );
113
		if ( $file && $this->filterItem( $file, $shared ) ) {
114
			$filename = $file->getLocalRefPath();
115
			$rel = wfRelativePath( $filename, $this->mBasePath );
116
			$this->output( "$rel\n" );
117
		} else {
118
			wfDebug( __METHOD__ . ": base file? $name\n" );
119
		}
120
	}
121
122
	function filterItem( $file, $shared ) {
123
		return $shared || $file->isLocal();
124
	}
125
}
126
127
$maintClass = "UploadDumper";
128
require_once RUN_MAINTENANCE_IF_MAIN;
129