WARNING: HUGE COMMIT
[lhc/web/wiklou.git] / maintenance / dumpUploads.php
1 <?php
2 /**
3 * @file
4 * @ingroup Maintenance
5 */
6
7 require_once 'commandLine.inc';
8
9 class UploadDumper {
10 function __construct( $args ) {
11 global $IP, $wgUseSharedUploads;
12 $this->mAction = 'fetchLocal';
13 $this->mBasePath = $IP;
14 $this->mShared = false;
15 $this->mSharedSupplement = false;
16
17 if( isset( $args['help'] ) ) {
18 $this->mAction = 'help';
19 }
20
21 if( isset( $args['base'] ) ) {
22 $this->mBasePath = $args['base'];
23 }
24
25 if( isset( $args['local'] ) ) {
26 $this->mAction = 'fetchLocal';
27 }
28
29 if( isset( $args['used'] ) ) {
30 $this->mAction = 'fetchUsed';
31 }
32
33 if( isset( $args['shared'] ) ) {
34 if( isset( $args['used'] ) ) {
35 // Include shared-repo files in the used check
36 $this->mShared = true;
37 } else {
38 // Grab all local *plus* used shared
39 $this->mSharedSupplement = true;
40 }
41 }
42 }
43
44 function run() {
45 $this->{$this->mAction}( $this->mShared );
46 if( $this->mSharedSupplement ) {
47 $this->fetchUsed( true );
48 }
49 }
50
51 function help() {
52 echo <<<END
53 Generates list of uploaded files which can be fed to tar or similar.
54 By default, outputs relative paths against the parent directory of
55 \$wgUploadDirectory.
56
57 Usage:
58 php dumpUploads.php [options] > list-o-files.txt
59
60 Options:
61 --base=<path> Set base relative path instead of wiki include root
62
63 --local List all local files, used or not. No shared files included.
64 --used Skip local images that are not used
65 --shared Include images used from shared repository
66
67 END;
68 }
69
70 /**
71 * Fetch a list of all or used images from a particular image source.
72 * @param string $table
73 * @param string $directory Base directory where files are located
74 * @param bool $shared true to pass shared-dir settings to hash func
75 */
76 function fetchUsed( $shared ) {
77 $dbr = wfGetDB( DB_SLAVE );
78 $image = $dbr->tableName( 'image' );
79 $imagelinks = $dbr->tableName( 'imagelinks' );
80
81 $sql = "SELECT DISTINCT il_to, img_name
82 FROM $imagelinks
83 LEFT OUTER JOIN $image
84 ON il_to=img_name";
85 $result = $dbr->query( $sql );
86
87 foreach( $result as $row ) {
88 $this->outputItem( $row->il_to, $shared );
89 }
90 $dbr->freeResult( $result );
91 }
92
93 function fetchLocal( $shared ) {
94 $dbr = wfGetDB( DB_SLAVE );
95 $result = $dbr->select( 'image',
96 array( 'img_name' ),
97 '',
98 __METHOD__ );
99
100 foreach( $result as $row ) {
101 $this->outputItem( $row->img_name, $shared );
102 }
103 $dbr->freeResult( $result );
104 }
105
106 function outputItem( $name, $shared ) {
107 $file = wfFindFile( $name );
108 if( $file && $this->filterItem( $file, $shared ) ) {
109 $filename = $file->getFullPath();
110 $rel = wfRelativePath( $filename, $this->mBasePath );
111 echo "$rel\n";
112 } else {
113 wfDebug( __METHOD__ . ": base file? $name\n" );
114 }
115 }
116
117 function filterItem( $file, $shared ) {
118 return $shared || $file->isLocal();
119 }
120 }
121
122 $dumper = new UploadDumper( $options );
123 $dumper->run();
124