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 * @ingroup Maintenance
23 require_once( dirname(__FILE__
) . '/Maintenance.php' );
25 class UploadDumper
extends Maintenance
{
26 public function __construct() {
27 parent
::__construct();
28 $this->mDescription
= "Generates list of uploaded files which can be fed to tar or similar.
29 By default, outputs relative paths against the parent directory of \$wgUploadDirectory.";
30 $this->addOption( 'base', 'Set base relative path instead of wiki include root', false, true );
31 $this->addOption( 'local', 'List all local files, used or not. No shared files included' );
32 $this->addOption( 'used', 'Skip local images that are not used' );
33 $this->addOption( 'shared', 'Include images used from shared repository' );
36 public function execute() {
37 global $IP, $wgUseSharedUploads;
38 $this->mAction
= 'fetchLocal';
39 $this->mBasePath
= $this->getOption( 'base', $IP );
40 $this->mShared
= false;
41 $this->mSharedSupplement
= false;
43 if( $this->hasOption('local') ) {
44 $this->mAction
= 'fetchLocal';
47 if( $this->hasOption('used') ) {
48 $this->mAction
= 'fetchUsed';
51 if( $this->hasOption('shared') ) {
52 if( $this->hasOption('used') ) {
53 // Include shared-repo files in the used check
54 $this->mShared
= true;
56 // Grab all local *plus* used shared
57 $this->mSharedSupplement
= true;
60 $this->{$this->mAction
}( $this->mShared
);
61 if( $this->mSharedSupplement
) {
62 $this->fetchUsed( true );
67 * Fetch a list of all or used images from a particular image source.
68 * @param string $table
69 * @param string $directory Base directory where files are located
70 * @param bool $shared true to pass shared-dir settings to hash func
72 function fetchUsed( $shared ) {
73 $dbr = wfGetDB( DB_SLAVE
);
74 $image = $dbr->tableName( 'image' );
75 $imagelinks = $dbr->tableName( 'imagelinks' );
77 $sql = "SELECT DISTINCT il_to, img_name
79 LEFT OUTER JOIN $image
81 $result = $dbr->query( $sql );
83 foreach( $result as $row ) {
84 $this->outputItem( $row->il_to
, $shared );
86 $dbr->freeResult( $result );
89 function fetchLocal( $shared ) {
90 $dbr = wfGetDB( DB_SLAVE
);
91 $result = $dbr->select( 'image',
96 foreach( $result as $row ) {
97 $this->outputItem( $row->img_name
, $shared );
99 $dbr->freeResult( $result );
102 function outputItem( $name, $shared ) {
103 $file = wfFindFile( $name );
104 if( $file && $this->filterItem( $file, $shared ) ) {
105 $filename = $file->getFullPath();
106 $rel = wfRelativePath( $filename, $this->mBasePath
);
107 $this->output( "$rel\n" );
109 wfDebug( __METHOD__
. ": base file? $name\n" );
113 function filterItem( $file, $shared ) {
114 return $shared ||
$file->isLocal();
118 $maintClass = "UploadDumper";
119 require_once( DO_MAINTENANCE
);