maintenance/version: Namespace and rename
[mediawiki.git] / maintenance / dumpUploads.php
blobfb423402fff9acec384853550dc86c5744c2f2d2
1 <?php
2 /**
3 * Dump a the list of files uploaded, for feeding to tar or similar.
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.
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.
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
20 * @file
21 * @ingroup Maintenance
24 require_once __DIR__ . '/Maintenance.php';
26 /**
27 * Maintenance script to dump a the list of files uploaded,
28 * for feeding to tar or similar.
30 * @ingroup Maintenance
32 class DumpUploads extends Maintenance {
33 /** @var string */
34 private $mBasePath;
36 public function __construct() {
37 parent::__construct();
38 $this->addDescription( 'Generates list of uploaded files which can be fed to tar or similar.
39 By 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' );
46 public function execute() {
47 global $IP;
48 $this->mBasePath = $this->getOption( 'base', $IP );
49 $shared = false;
50 $sharedSupplement = false;
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;
62 if ( $this->hasOption( 'local' ) ) {
63 $this->fetchLocal( $shared );
64 } elseif ( $this->hasOption( 'used' ) ) {
65 $this->fetchUsed( $shared );
66 } else {
67 $this->fetchLocal( $shared );
70 if ( $sharedSupplement ) {
71 $this->fetchUsed( true );
75 /**
76 * Fetch a list of used images from a particular image source.
78 * @param bool $shared True to pass shared-dir settings to hash func
80 private function fetchUsed( $shared ) {
81 $dbr = $this->getDB( DB_REPLICA );
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();
91 foreach ( $result as $row ) {
92 $this->outputItem( $row->il_to, $shared );
96 /**
97 * Fetch a list of all images from a particular image source.
99 * @param bool $shared True to pass shared-dir settings to hash func
101 private function fetchLocal( $shared ) {
102 $dbr = $this->getDB( DB_REPLICA );
103 $result = $dbr->newSelectQueryBuilder()
104 ->select( 'img_name' )
105 ->from( 'image' )
106 ->caller( __METHOD__ )
107 ->fetchResultSet();
109 foreach ( $result as $row ) {
110 $this->outputItem( $row->img_name, $shared );
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" );
125 private function filterItem( $file, $shared ) {
126 return $shared || $file->isLocal();
130 $maintClass = DumpUploads::class;
131 require_once RUN_MAINTENANCE_IF_MAIN;