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
21 * @ingroup Maintenance
24 use MediaWiki\MediaWikiServices
;
26 require_once __DIR__
. '/Maintenance.php';
29 * Maintenance script to dump a the list of files uploaded,
30 * for feeding to tar or similar.
32 * @ingroup Maintenance
34 class DumpUploads
extends Maintenance
{
35 public function __construct() {
36 parent
::__construct();
37 $this->addDescription( 'Generates list of uploaded files which can be fed to tar or similar.
38 By default, outputs relative paths against the parent directory of $wgUploadDirectory.' );
39 $this->addOption( 'base', 'Set base relative path instead of wiki include root', false, true );
40 $this->addOption( 'local', 'List all local files, used or not. No shared files included' );
41 $this->addOption( 'used', 'Skip local images that are not used' );
42 $this->addOption( 'shared', 'Include images used from shared repository' );
45 public function execute() {
47 $this->mAction
= 'fetchLocal';
48 $this->mBasePath
= $this->getOption( 'base', $IP );
49 $this->mShared
= false;
50 $this->mSharedSupplement
= false;
52 if ( $this->hasOption( 'local' ) ) {
53 $this->mAction
= 'fetchLocal';
56 if ( $this->hasOption( 'used' ) ) {
57 $this->mAction
= 'fetchUsed';
60 if ( $this->hasOption( 'shared' ) ) {
61 if ( $this->hasOption( 'used' ) ) {
62 // Include shared-repo files in the used check
63 $this->mShared
= true;
65 // Grab all local *plus* used shared
66 $this->mSharedSupplement
= true;
69 $this->{$this->mAction
} ( $this->mShared
);
70 if ( $this->mSharedSupplement
) {
71 $this->fetchUsed( true );
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 function fetchUsed( $shared ) {
81 $dbr = $this->getDB( DB_REPLICA
);
82 $image = $dbr->tableName( 'image' );
83 $imagelinks = $dbr->tableName( 'imagelinks' );
85 $sql = "SELECT DISTINCT il_to, img_name
89 $result = $dbr->query( $sql );
91 foreach ( $result as $row ) {
92 $this->outputItem( $row->il_to
, $shared );
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 function fetchLocal( $shared ) {
102 $dbr = $this->getDB( DB_REPLICA
);
103 $result = $dbr->select( 'image',
108 foreach ( $result as $row ) {
109 $this->outputItem( $row->img_name
, $shared );
113 function outputItem( $name, $shared ) {
114 $file = MediaWikiServices
::getInstance()->getRepoGroup()->findFile( $name );
115 if ( $file && $this->filterItem( $file, $shared ) ) {
116 $filename = $file->getLocalRefPath();
117 $rel = wfRelativePath( $filename, $this->mBasePath
);
118 $this->output( "$rel\n" );
120 wfDebug( __METHOD__
. ": base file? $name\n" );
124 function filterItem( $file, $shared ) {
125 return $shared ||
$file->isLocal();
129 $maintClass = DumpUploads
::class;
130 require_once RUN_MAINTENANCE_IF_MAIN
;