Add rnredirect parameter, to get a random redirect instead of a random page
[mediawiki.git] / maintenance / dumpUploads.php
blobc237feee2b488ef86739b0fa5fd276d26f5c79ae
1 <?php
2 /**
3 * @file
4 * @ingroup Maintenance
5 */
7 require_once 'commandLine.inc';
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;
17 if( isset( $args['help'] ) ) {
18 $this->mAction = 'help';
21 if( isset( $args['base'] ) ) {
22 $this->mBasePath = $args['base'];
25 if( isset( $args['local'] ) ) {
26 $this->mAction = 'fetchLocal';
29 if( isset( $args['used'] ) ) {
30 $this->mAction = 'fetchUsed';
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;
44 function run() {
45 $this->{$this->mAction}( $this->mShared );
46 if( $this->mSharedSupplement ) {
47 $this->fetchUsed( true );
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.
57 Usage:
58 php dumpUploads.php [options] > list-o-files.txt
60 Options:
61 --base=<path> Set base relative path instead of wiki include root
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
67 END;
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
76 function fetchUsed( $shared ) {
77 $dbr = wfGetDB( DB_SLAVE );
78 $image = $dbr->tableName( 'image' );
79 $imagelinks = $dbr->tableName( 'imagelinks' );
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 );
87 foreach( $result as $row ) {
88 $this->outputItem( $row->il_to, $shared );
90 $dbr->freeResult( $result );
93 function fetchLocal( $shared ) {
94 $dbr = wfGetDB( DB_SLAVE );
95 $result = $dbr->select( 'image',
96 array( 'img_name' ),
97 '',
98 __METHOD__ );
100 foreach( $result as $row ) {
101 $this->outputItem( $row->img_name, $shared );
103 $dbr->freeResult( $result );
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" );
117 function filterItem( $file, $shared ) {
118 return $shared || $file->isLocal();
122 $dumper = new UploadDumper( $options );
123 $dumper->run();