MediaWiki master
dumpUploads.php
Go to the documentation of this file.
1<?php
24require_once __DIR__ . '/Maintenance.php';
25
32class DumpUploads extends Maintenance {
34 private $mBasePath;
35
36 public function __construct() {
37 parent::__construct();
38 $this->addDescription( 'Generates list of uploaded files which can be fed to tar or similar.
39By default, outputs relative paths against the parent directory of $wgUploadDirectory.' );
40 $this->addOption( 'base', 'Set base relative path instead of wiki include root', false, true );
41 $this->addOption( 'local', 'List all local files, used or not. No shared files included' );
42 $this->addOption( 'used', 'Skip local images that are not used' );
43 $this->addOption( 'shared', 'Include images used from shared repository' );
44 }
45
46 public function execute() {
47 global $IP;
48 $this->mBasePath = $this->getOption( 'base', $IP );
49 $shared = false;
50 $sharedSupplement = false;
51
52 if ( $this->hasOption( 'shared' ) ) {
53 if ( $this->hasOption( 'used' ) ) {
54 // Include shared-repo files in the used check
55 $shared = true;
56 } else {
57 // Grab all local *plus* used shared
58 $sharedSupplement = true;
59 }
60 }
61
62 if ( $this->hasOption( 'local' ) ) {
63 $this->fetchLocal( $shared );
64 } elseif ( $this->hasOption( 'used' ) ) {
65 $this->fetchUsed( $shared );
66 } else {
67 $this->fetchLocal( $shared );
68 }
69
70 if ( $sharedSupplement ) {
71 $this->fetchUsed( true );
72 }
73 }
74
80 private function fetchUsed( $shared ) {
81 $dbr = $this->getReplicaDB();
82
83 $result = $dbr->newSelectQueryBuilder()
84 ->select( [ 'il_to', 'img_name' ] )
85 ->distinct()
86 ->from( 'imagelinks' )
87 ->leftJoin( 'image', null, 'il_to=img_name' )
88 ->caller( __METHOD__ )
89 ->fetchResultSet();
90
91 foreach ( $result as $row ) {
92 $this->outputItem( $row->il_to, $shared );
93 }
94 }
95
101 private function fetchLocal( $shared ) {
102 $dbr = $this->getReplicaDB();
103 $result = $dbr->newSelectQueryBuilder()
104 ->select( 'img_name' )
105 ->from( 'image' )
106 ->caller( __METHOD__ )
107 ->fetchResultSet();
108
109 foreach ( $result as $row ) {
110 $this->outputItem( $row->img_name, $shared );
111 }
112 }
113
114 private function outputItem( $name, $shared ) {
115 $file = $this->getServiceContainer()->getRepoGroup()->findFile( $name );
116 if ( $file && $this->filterItem( $file, $shared ) ) {
117 $filename = $file->getLocalRefPath();
118 $rel = wfRelativePath( $filename, $this->mBasePath );
119 $this->output( "$rel\n" );
120 } else {
121 wfDebug( __METHOD__ . ": base file? $name" );
122 }
123 }
124
125 private function filterItem( $file, $shared ) {
126 return $shared || $file->isLocal();
127 }
128}
129
130$maintClass = DumpUploads::class;
131require_once RUN_MAINTENANCE_IF_MAIN;
wfDebug( $text, $dest='all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
wfRelativePath( $path, $from)
Generate a relative path name to the given file.
if(!defined( 'MEDIAWIKI')) if(ini_get('mbstring.func_overload')) if(!defined( 'MW_ENTRY_POINT')) global $IP
Environment checks.
Definition Setup.php:99
Maintenance script to dump a the list of files uploaded, for feeding to tar or similar.
execute()
Do the actual work.
__construct()
Default constructor.
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
output( $out, $channel=null)
Throw some output to the user.
hasOption( $name)
Checks to see if a particular option was set.
getServiceContainer()
Returns the main service container.
addDescription( $text)
Set the description text.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
getOption( $name, $default=null)
Get an option, or return the default.
$maintClass