Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 47 |
|
0.00% |
0 / 6 |
CRAP | |
0.00% |
0 / 1 |
DumpUploads | |
0.00% |
0 / 47 |
|
0.00% |
0 / 6 |
272 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
2 | |||
execute | |
0.00% |
0 / 15 |
|
0.00% |
0 / 1 |
42 | |||
fetchUsed | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
6 | |||
fetchLocal | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
6 | |||
outputItem | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
12 | |||
filterItem | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
6 |
1 | <?php |
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 | use MediaWiki\Maintenance\Maintenance; |
25 | |
26 | // @codeCoverageIgnoreStart |
27 | require_once __DIR__ . '/Maintenance.php'; |
28 | // @codeCoverageIgnoreEnd |
29 | |
30 | /** |
31 | * Maintenance script to dump a the list of files uploaded, |
32 | * for feeding to tar or similar. |
33 | * |
34 | * @ingroup Maintenance |
35 | */ |
36 | class DumpUploads extends Maintenance { |
37 | /** @var string */ |
38 | private $mBasePath; |
39 | |
40 | public function __construct() { |
41 | parent::__construct(); |
42 | $this->addDescription( 'Generates list of uploaded files which can be fed to tar or similar. |
43 | By default, outputs relative paths against the parent directory of $wgUploadDirectory.' ); |
44 | $this->addOption( 'base', 'Set base relative path instead of wiki include root', false, true ); |
45 | $this->addOption( 'local', 'List all local files, used or not. No shared files included' ); |
46 | $this->addOption( 'used', 'Skip local images that are not used' ); |
47 | $this->addOption( 'shared', 'Include images used from shared repository' ); |
48 | } |
49 | |
50 | public function execute() { |
51 | global $IP; |
52 | $this->mBasePath = $this->getOption( 'base', $IP ); |
53 | $shared = false; |
54 | $sharedSupplement = false; |
55 | |
56 | if ( $this->hasOption( 'shared' ) ) { |
57 | if ( $this->hasOption( 'used' ) ) { |
58 | // Include shared-repo files in the used check |
59 | $shared = true; |
60 | } else { |
61 | // Grab all local *plus* used shared |
62 | $sharedSupplement = true; |
63 | } |
64 | } |
65 | |
66 | if ( $this->hasOption( 'local' ) ) { |
67 | $this->fetchLocal( $shared ); |
68 | } elseif ( $this->hasOption( 'used' ) ) { |
69 | $this->fetchUsed( $shared ); |
70 | } else { |
71 | $this->fetchLocal( $shared ); |
72 | } |
73 | |
74 | if ( $sharedSupplement ) { |
75 | $this->fetchUsed( true ); |
76 | } |
77 | } |
78 | |
79 | /** |
80 | * Fetch a list of used images from a particular image source. |
81 | * |
82 | * @param bool $shared True to pass shared-dir settings to hash func |
83 | */ |
84 | private function fetchUsed( $shared ) { |
85 | $dbr = $this->getReplicaDB(); |
86 | |
87 | $result = $dbr->newSelectQueryBuilder() |
88 | ->select( [ 'il_to', 'img_name' ] ) |
89 | ->distinct() |
90 | ->from( 'imagelinks' ) |
91 | ->leftJoin( 'image', null, 'il_to=img_name' ) |
92 | ->caller( __METHOD__ ) |
93 | ->fetchResultSet(); |
94 | |
95 | foreach ( $result as $row ) { |
96 | $this->outputItem( $row->il_to, $shared ); |
97 | } |
98 | } |
99 | |
100 | /** |
101 | * Fetch a list of all images from a particular image source. |
102 | * |
103 | * @param bool $shared True to pass shared-dir settings to hash func |
104 | */ |
105 | private function fetchLocal( $shared ) { |
106 | $dbr = $this->getReplicaDB(); |
107 | $result = $dbr->newSelectQueryBuilder() |
108 | ->select( 'img_name' ) |
109 | ->from( 'image' ) |
110 | ->caller( __METHOD__ ) |
111 | ->fetchResultSet(); |
112 | |
113 | foreach ( $result as $row ) { |
114 | $this->outputItem( $row->img_name, $shared ); |
115 | } |
116 | } |
117 | |
118 | private function outputItem( $name, $shared ) { |
119 | $file = $this->getServiceContainer()->getRepoGroup()->findFile( $name ); |
120 | if ( $file && $this->filterItem( $file, $shared ) ) { |
121 | $filename = $file->getLocalRefPath(); |
122 | $rel = wfRelativePath( $filename, $this->mBasePath ); |
123 | $this->output( "$rel\n" ); |
124 | } else { |
125 | wfDebug( __METHOD__ . ": base file? $name" ); |
126 | } |
127 | } |
128 | |
129 | private function filterItem( $file, $shared ) { |
130 | return $shared || $file->isLocal(); |
131 | } |
132 | } |
133 | |
134 | // @codeCoverageIgnoreStart |
135 | $maintClass = DumpUploads::class; |
136 | require_once RUN_MAINTENANCE_IF_MAIN; |
137 | // @codeCoverageIgnoreEnd |