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