]> scripts.mit.edu Git - autoinstalls/mediawiki.git/blob - maintenance/dumpUploads.php
MediaWiki 1.11.0-scripts
[autoinstalls/mediawiki.git] / maintenance / dumpUploads.php
1 <?php
2
3 require_once 'commandLine.inc';
4
5 class UploadDumper {
6         
7         function __construct( $args ) {
8                 global $IP, $wgUseSharedUploads;
9                 $this->mAction = 'fetchUsed';
10                 $this->mBasePath = $IP;
11                 $this->mShared = $wgUseSharedUploads;
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         
22         function run() {
23                 $this->{$this->mAction}();
24         }
25         
26         function help() {
27                 echo <<<END
28 Generates list of uploaded files which can be fed to tar or similar.
29 By default, outputs relative paths against the parent directory of
30 \$wgUploadDirectory.
31
32 Usage:
33 php dumpUploads.php [options] > list-o-files.txt
34
35 Options:
36 --base=<path>  Set base relative path instead of wiki include root
37
38 FIXME: other options not implemented yet ;)
39
40 --local        List all local files, used or not. No shared files included.
41 --used         Skip local images that are not used
42 --shared       Include images used from shared repository
43
44 END;
45         }
46         
47         /**
48          * Fetch a list of all or used images from a particular image source.
49          * @param string $table
50          * @param string $directory Base directory where files are located
51          * @param bool $shared true to pass shared-dir settings to hash func
52          */
53         function fetchUsed() {
54                 $dbr = wfGetDB( DB_SLAVE );
55                 $image = $dbr->tableName( 'image' );
56                 $imagelinks = $dbr->tableName( 'imagelinks' );
57                 
58                 $sql = "SELECT DISTINCT il_to, img_name
59                         FROM $imagelinks
60                         LEFT OUTER JOIN $image
61                         ON il_to=img_name";
62                 $result = $dbr->query( $sql );
63                 
64                 while( $row = $dbr->fetchObject( $result ) ) {
65                         if( is_null( $row->img_name ) ) {
66                                 if( $this->mShared ) {
67                                         $this->outputShared( $row->il_to );
68                                 }
69                         } else {
70                                 $this->outputLocal( $row->il_to );
71                         }
72                 }
73                 $dbr->freeResult( $result );
74         }
75         
76         function outputLocal( $name ) {
77                 global $wgUploadDirectory;
78                 return $this->outputItem( $name, $wgUploadDirectory, false );
79         }
80         
81         function outputShared( $name ) {
82                 global $wgSharedUploadDirectory;
83                 return $this->outputItem( $name, $wgSharedUploadDirectory, true );
84         }
85         
86         function outputItem( $name, $directory, $shared ) {
87                 $filename = $directory .
88                         wfGetHashPath( $name, $shared ) .
89                         $name;
90                 $rel = $this->relativePath( $filename, $this->mBasePath );
91                 echo "$rel\n";
92         }
93
94         /**
95          * Return a relative path to $path from the base directory $base
96          * For instance relativePath( '/foo/bar/baz', '/foo' ) should return
97          * 'bar/baz'.
98          */
99         function relativePath( $path, $base) {
100                 $path = explode( DIRECTORY_SEPARATOR, $path );
101                 $base = explode( DIRECTORY_SEPARATOR, $base );
102                 while( count( $base ) && $path[0] == $base[0] ) {
103                         array_shift( $path );
104                         array_shift( $base );
105                 }
106                 foreach( $base as $prefix ) {
107                         array_unshift( $path, '..' );
108                 }
109                 return implode( DIRECTORY_SEPARATOR, $path );
110         }
111 }
112
113 $dumper = new UploadDumper( $options );
114 $dumper->run();
115