c8f1667b0854539ae6b83f7f47ce25c44ea3f26b
[lhc/web/wiklou.git] / maintenance / dumpUploads.php
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 * @ingroup Maintenance
21 */
22
23 require_once( dirname(__FILE__) . '/Maintenance.php' );
24
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' );
34 }
35
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;
42
43 if( $this->hasOption('local') ) {
44 $this->mAction = 'fetchLocal';
45 }
46
47 if( $this->hasOption('used') ) {
48 $this->mAction = 'fetchUsed';
49 }
50
51 if( $this->hasOption('shared') ) {
52 if( $this->hasOption('used') ) {
53 // Include shared-repo files in the used check
54 $this->mShared = true;
55 } else {
56 // Grab all local *plus* used shared
57 $this->mSharedSupplement = true;
58 }
59 }
60 $this->{$this->mAction}( $this->mShared );
61 if( $this->mSharedSupplement ) {
62 $this->fetchUsed( true );
63 }
64 }
65
66 /**
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
71 */
72 function fetchUsed( $shared ) {
73 $dbr = wfGetDB( DB_SLAVE );
74 $image = $dbr->tableName( 'image' );
75 $imagelinks = $dbr->tableName( 'imagelinks' );
76
77 $sql = "SELECT DISTINCT il_to, img_name
78 FROM $imagelinks
79 LEFT OUTER JOIN $image
80 ON il_to=img_name";
81 $result = $dbr->query( $sql );
82
83 foreach( $result as $row ) {
84 $this->outputItem( $row->il_to, $shared );
85 }
86 $dbr->freeResult( $result );
87 }
88
89 function fetchLocal( $shared ) {
90 $dbr = wfGetDB( DB_SLAVE );
91 $result = $dbr->select( 'image',
92 array( 'img_name' ),
93 '',
94 __METHOD__ );
95
96 foreach( $result as $row ) {
97 $this->outputItem( $row->img_name, $shared );
98 }
99 $dbr->freeResult( $result );
100 }
101
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" );
108 } else {
109 wfDebug( __METHOD__ . ": base file? $name\n" );
110 }
111 }
112
113 function filterItem( $file, $shared ) {
114 return $shared || $file->isLocal();
115 }
116 }
117
118 $maintClass = "UploadDumper";
119 require_once( DO_MAINTENANCE );