Merge "CSSMin: Clean up $remote trailing slash fix"
[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;
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 used images from a particular image source.
68 *
69 * @param $shared Boolean: true to pass shared-dir settings to hash func
70 */
71 function fetchUsed( $shared ) {
72 $dbr = wfGetDB( DB_SLAVE );
73 $image = $dbr->tableName( 'image' );
74 $imagelinks = $dbr->tableName( 'imagelinks' );
75
76 $sql = "SELECT DISTINCT il_to, img_name
77 FROM $imagelinks
78 LEFT OUTER JOIN $image
79 ON il_to=img_name";
80 $result = $dbr->query( $sql );
81
82 foreach ( $result as $row ) {
83 $this->outputItem( $row->il_to, $shared );
84 }
85 }
86
87 /**
88 * Fetch a list of all images from a particular image source.
89 *
90 * @param $shared Boolean: true to pass shared-dir settings to hash func
91 */
92 function fetchLocal( $shared ) {
93 $dbr = wfGetDB( DB_SLAVE );
94 $result = $dbr->select( 'image',
95 array( 'img_name' ),
96 '',
97 __METHOD__ );
98
99 foreach ( $result as $row ) {
100 $this->outputItem( $row->img_name, $shared );
101 }
102 }
103
104 function outputItem( $name, $shared ) {
105 $file = wfFindFile( $name );
106 if ( $file && $this->filterItem( $file, $shared ) ) {
107 $filename = $file->getPath();
108 $rel = wfRelativePath( $filename, $this->mBasePath );
109 $this->output( "$rel\n" );
110 } else {
111 wfDebug( __METHOD__ . ": base file? $name\n" );
112 }
113 }
114
115 function filterItem( $file, $shared ) {
116 return $shared || $file->isLocal();
117 }
118 }
119
120 $maintClass = "UploadDumper";
121 require_once( RUN_MAINTENANCE_IF_MAIN );