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