MediaWiki  REL1_20
dumpUploads.php
Go to the documentation of this file.
00001 <?php
00024 require_once( __DIR__ . '/Maintenance.php' );
00025 
00032 class UploadDumper extends Maintenance {
00033         public function __construct() {
00034                 parent::__construct();
00035                 $this->mDescription = "Generates list of uploaded files which can be fed to tar or similar.
00036 By default, outputs relative paths against the parent directory of \$wgUploadDirectory.";
00037                 $this->addOption( 'base', 'Set base relative path instead of wiki include root', false, true );
00038                 $this->addOption( 'local', 'List all local files, used or not. No shared files included' );
00039                 $this->addOption( 'used', 'Skip local images that are not used' );
00040                 $this->addOption( 'shared', 'Include images used from shared repository' );
00041         }
00042 
00043         public function execute() {
00044                 global $IP;
00045                 $this->mAction = 'fetchLocal';
00046                 $this->mBasePath = $this->getOption( 'base', $IP );
00047                 $this->mShared = false;
00048                 $this->mSharedSupplement = false;
00049 
00050                 if ( $this->hasOption( 'local' ) ) {
00051                         $this->mAction = 'fetchLocal';
00052                 }
00053 
00054                 if ( $this->hasOption( 'used' ) ) {
00055                         $this->mAction = 'fetchUsed';
00056                 }
00057 
00058                 if ( $this->hasOption( 'shared' ) ) {
00059                         if ( $this->hasOption( 'used' ) ) {
00060                                 // Include shared-repo files in the used check
00061                                 $this->mShared = true;
00062                         } else {
00063                                 // Grab all local *plus* used shared
00064                                 $this->mSharedSupplement = true;
00065                         }
00066                 }
00067                 $this-> { $this->mAction } ( $this->mShared );
00068                 if ( $this->mSharedSupplement ) {
00069                         $this->fetchUsed( true );
00070                 }
00071         }
00072 
00078         function fetchUsed( $shared ) {
00079                 $dbr = wfGetDB( DB_SLAVE );
00080                 $image = $dbr->tableName( 'image' );
00081                 $imagelinks = $dbr->tableName( 'imagelinks' );
00082 
00083                 $sql = "SELECT DISTINCT il_to, img_name
00084                         FROM $imagelinks
00085                         LEFT OUTER JOIN $image
00086                         ON il_to=img_name";
00087                 $result = $dbr->query( $sql );
00088 
00089                 foreach ( $result as $row ) {
00090                         $this->outputItem( $row->il_to, $shared );
00091                 }
00092         }
00093 
00099         function fetchLocal( $shared ) {
00100                 $dbr = wfGetDB( DB_SLAVE );
00101                 $result = $dbr->select( 'image',
00102                         array( 'img_name' ),
00103                         '',
00104                         __METHOD__ );
00105 
00106                 foreach ( $result as $row ) {
00107                         $this->outputItem( $row->img_name, $shared );
00108                 }
00109         }
00110 
00111         function outputItem( $name, $shared ) {
00112                 $file = wfFindFile( $name );
00113                 if ( $file && $this->filterItem( $file, $shared ) ) {
00114                         $filename = $file->getPath();
00115                         $rel = wfRelativePath( $filename, $this->mBasePath );
00116                         $this->output( "$rel\n" );
00117                 } else {
00118                         wfDebug( __METHOD__ . ": base file? $name\n" );
00119                 }
00120         }
00121 
00122         function filterItem( $file, $shared ) {
00123                 return $shared || $file->isLocal();
00124         }
00125 }
00126 
00127 $maintClass = "UploadDumper";
00128 require_once( RUN_MAINTENANCE_IF_MAIN );