MediaWiki  1.34.0
dumpUploads.php
Go to the documentation of this file.
1 <?php
25 
26 require_once __DIR__ . '/Maintenance.php';
27 
34 class DumpUploads extends Maintenance {
36  private $mBasePath;
37 
38  public function __construct() {
39  parent::__construct();
40  $this->addDescription( 'Generates list of uploaded files which can be fed to tar or similar.
41 By default, outputs relative paths against the parent directory of $wgUploadDirectory.' );
42  $this->addOption( 'base', 'Set base relative path instead of wiki include root', false, true );
43  $this->addOption( 'local', 'List all local files, used or not. No shared files included' );
44  $this->addOption( 'used', 'Skip local images that are not used' );
45  $this->addOption( 'shared', 'Include images used from shared repository' );
46  }
47 
48  public function execute() {
49  global $IP;
50  $this->mBasePath = $this->getOption( 'base', $IP );
51  $shared = false;
52  $sharedSupplement = false;
53 
54  if ( $this->hasOption( 'shared' ) ) {
55  if ( $this->hasOption( 'used' ) ) {
56  // Include shared-repo files in the used check
57  $shared = true;
58  } else {
59  // Grab all local *plus* used shared
60  $sharedSupplement = true;
61  }
62  }
63 
64  if ( $this->hasOption( 'local' ) ) {
65  $this->fetchLocal( $shared );
66  } elseif ( $this->hasOption( 'used' ) ) {
67  $this->fetchUsed( $shared );
68  } else {
69  $this->fetchLocal( $shared );
70  }
71 
72  if ( $sharedSupplement ) {
73  $this->fetchUsed( true );
74  }
75  }
76 
82  function fetchUsed( $shared ) {
83  $dbr = $this->getDB( DB_REPLICA );
84  $image = $dbr->tableName( 'image' );
85  $imagelinks = $dbr->tableName( 'imagelinks' );
86 
87  $sql = "SELECT DISTINCT il_to, img_name
88  FROM $imagelinks
89  LEFT JOIN $image
90  ON il_to=img_name";
91  $result = $dbr->query( $sql );
92 
93  foreach ( $result as $row ) {
94  $this->outputItem( $row->il_to, $shared );
95  }
96  }
97 
103  function fetchLocal( $shared ) {
104  $dbr = $this->getDB( DB_REPLICA );
105  $result = $dbr->select( 'image',
106  [ 'img_name' ],
107  '',
108  __METHOD__ );
109 
110  foreach ( $result as $row ) {
111  $this->outputItem( $row->img_name, $shared );
112  }
113  }
114 
115  function outputItem( $name, $shared ) {
116  $file = MediaWikiServices::getInstance()->getRepoGroup()->findFile( $name );
117  if ( $file && $this->filterItem( $file, $shared ) ) {
118  $filename = $file->getLocalRefPath();
119  $rel = wfRelativePath( $filename, $this->mBasePath );
120  $this->output( "$rel\n" );
121  } else {
122  wfDebug( __METHOD__ . ": base file? $name\n" );
123  }
124  }
125 
126  function filterItem( $file, $shared ) {
127  return $shared || $file->isLocal();
128  }
129 }
130 
131 $maintClass = DumpUploads::class;
132 require_once RUN_MAINTENANCE_IF_MAIN;
RUN_MAINTENANCE_IF_MAIN
const RUN_MAINTENANCE_IF_MAIN
Definition: Maintenance.php:39
$maintClass
$maintClass
Definition: dumpUploads.php:131
DumpUploads\outputItem
outputItem( $name, $shared)
Definition: dumpUploads.php:115
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:117
Maintenance\addDescription
addDescription( $text)
Set the description text.
Definition: Maintenance.php:348
DumpUploads\__construct
__construct()
Default constructor.
Definition: dumpUploads.php:38
$file
if(PHP_SAPI !='cli-server') if(!isset( $_SERVER['SCRIPT_FILENAME'])) $file
Item class for a filearchive table row.
Definition: router.php:42
Maintenance
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
Definition: Maintenance.php:82
DumpUploads\filterItem
filterItem( $file, $shared)
Definition: dumpUploads.php:126
DumpUploads\$mBasePath
string $mBasePath
Definition: dumpUploads.php:36
$dbr
$dbr
Definition: testCompression.php:50
Maintenance\addOption
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
Definition: Maintenance.php:267
$IP
$IP
Definition: update.php:3
DB_REPLICA
const DB_REPLICA
Definition: defines.php:25
wfDebug
wfDebug( $text, $dest='all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
Definition: GlobalFunctions.php:913
DumpUploads
Maintenance script to dump a the list of files uploaded, for feeding to tar or similar.
Definition: dumpUploads.php:34
DumpUploads\execute
execute()
Do the actual work.
Definition: dumpUploads.php:48
Maintenance\getDB
getDB( $db, $groups=[], $dbDomain=false)
Returns a database to be used by current maintenance script.
Definition: Maintenance.php:1396
Maintenance\getOption
getOption( $name, $default=null)
Get an option, or return the default.
Definition: Maintenance.php:302
DumpUploads\fetchLocal
fetchLocal( $shared)
Fetch a list of all images from a particular image source.
Definition: dumpUploads.php:103
DumpUploads\fetchUsed
fetchUsed( $shared)
Fetch a list of used images from a particular image source.
Definition: dumpUploads.php:82
Maintenance\output
output( $out, $channel=null)
Throw some output to the user.
Definition: Maintenance.php:453
wfRelativePath
wfRelativePath( $path, $from)
Generate a relative path name to the given file.
Definition: GlobalFunctions.php:2412
Maintenance\hasOption
hasOption( $name)
Checks to see if a particular option exists.
Definition: Maintenance.php:288