Localisation updates from https://translatewiki.net.
[mediawiki.git] / maintenance / dumpUploads.php
blobc476fa594e0c0eb75d282b94736680dfa904f332
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 use MediaWiki\Maintenance\Maintenance;
26 // @codeCoverageIgnoreStart
27 require_once __DIR__ . '/Maintenance.php';
28 // @codeCoverageIgnoreEnd
30 /**
31 * Maintenance script to dump a the list of files uploaded,
32 * for feeding to tar or similar.
34 * @ingroup Maintenance
36 class DumpUploads extends Maintenance {
37 /** @var string */
38 private $mBasePath;
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' );
50 public function execute() {
51 global $IP;
52 $this->mBasePath = $this->getOption( 'base', $IP );
53 $shared = false;
54 $sharedSupplement = false;
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;
66 if ( $this->hasOption( 'local' ) ) {
67 $this->fetchLocal( $shared );
68 } elseif ( $this->hasOption( 'used' ) ) {
69 $this->fetchUsed( $shared );
70 } else {
71 $this->fetchLocal( $shared );
74 if ( $sharedSupplement ) {
75 $this->fetchUsed( true );
79 /**
80 * Fetch a list of used images from a particular image source.
82 * @param bool $shared True to pass shared-dir settings to hash func
84 private function fetchUsed( $shared ) {
85 $dbr = $this->getReplicaDB();
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();
95 foreach ( $result as $row ) {
96 $this->outputItem( $row->il_to, $shared );
101 * Fetch a list of all images from a particular image source.
103 * @param bool $shared True to pass shared-dir settings to hash func
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();
113 foreach ( $result as $row ) {
114 $this->outputItem( $row->img_name, $shared );
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" );
129 private function filterItem( $file, $shared ) {
130 return $shared || $file->isLocal();
134 // @codeCoverageIgnoreStart
135 $maintClass = DumpUploads::class;
136 require_once RUN_MAINTENANCE_IF_MAIN;
137 // @codeCoverageIgnoreEnd