* regression fix: make dumpUploads.php work again
[lhc/web/wiklou.git] / maintenance / dumpUploads.php
1 <?php
2
3 require_once 'commandLine.inc';
4
5 class UploadDumper {
6 function __construct( $args ) {
7 global $IP, $wgUseSharedUploads;
8 $this->mAction = 'fetchLocal';
9 $this->mBasePath = $IP;
10 $this->mShared = false;
11 $this->mSharedSupplement = false;
12
13 if( isset( $args['help'] ) ) {
14 $this->mAction = 'help';
15 }
16
17 if( isset( $args['base'] ) ) {
18 $this->mBasePath = $args['base'];
19 }
20
21 if( isset( $args['local'] ) ) {
22 $this->mAction = 'fetchLocal';
23 }
24
25 if( isset( $args['used'] ) ) {
26 $this->mAction = 'fetchUsed';
27 }
28
29 if( isset( $args['shared'] ) ) {
30 if( isset( $args['used'] ) ) {
31 // Include shared-repo files in the used check
32 $this->mShared = true;
33 } else {
34 // Grab all local *plus* used shared
35 $this->mSharedSupplement = true;
36 }
37 }
38 }
39
40 function run() {
41 $this->{$this->mAction}( $this->mShared );
42 if( $this->mSharedSupplement ) {
43 $this->fetchUsed( true );
44 }
45 }
46
47 function help() {
48 echo <<<END
49 Generates list of uploaded files which can be fed to tar or similar.
50 By default, outputs relative paths against the parent directory of
51 \$wgUploadDirectory.
52
53 Usage:
54 php dumpUploads.php [options] > list-o-files.txt
55
56 Options:
57 --base=<path> Set base relative path instead of wiki include root
58
59 --local List all local files, used or not. No shared files included.
60 --used Skip local images that are not used
61 --shared Include images used from shared repository
62
63 END;
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 echo "$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 $dumper = new UploadDumper( $options );
119 $dumper->run();
120