MediaWiki master
dumpUploads.php
Go to the documentation of this file.
1<?php
25
26// @codeCoverageIgnoreStart
27require_once __DIR__ . '/Maintenance.php';
28// @codeCoverageIgnoreEnd
29
36class DumpUploads extends Maintenance {
38 private $mBasePath;
39
40 public function __construct() {
41 parent::__construct();
42 $this->addDescription( 'Generates list of uploaded files which can be fed to tar or similar.
43By default, outputs relative paths against the parent directory of $wgUploadDirectory.' );
44 $this->addOption( 'base', 'Set base relative path instead of wiki include root', false, true );
45 $this->addOption( 'local', 'List all local files, used or not. No shared files included' );
46 $this->addOption( 'used', 'Skip local images that are not used' );
47 $this->addOption( 'shared', 'Include images used from shared repository' );
48 }
49
50 public function execute() {
51 global $IP;
52 $this->mBasePath = $this->getOption( 'base', $IP );
53 $shared = false;
54 $sharedSupplement = false;
55
56 if ( $this->hasOption( 'shared' ) ) {
57 if ( $this->hasOption( 'used' ) ) {
58 // Include shared-repo files in the used check
59 $shared = true;
60 } else {
61 // Grab all local *plus* used shared
62 $sharedSupplement = true;
63 }
64 }
65
66 if ( $this->hasOption( 'local' ) ) {
67 $this->fetchLocal( $shared );
68 } elseif ( $this->hasOption( 'used' ) ) {
69 $this->fetchUsed( $shared );
70 } else {
71 $this->fetchLocal( $shared );
72 }
73
74 if ( $sharedSupplement ) {
75 $this->fetchUsed( true );
76 }
77 }
78
84 private function fetchUsed( $shared ) {
85 $dbr = $this->getReplicaDB();
86
87 $result = $dbr->newSelectQueryBuilder()
88 ->select( [ 'il_to', 'img_name' ] )
89 ->distinct()
90 ->from( 'imagelinks' )
91 ->leftJoin( 'image', null, 'il_to=img_name' )
92 ->caller( __METHOD__ )
93 ->fetchResultSet();
94
95 foreach ( $result as $row ) {
96 $this->outputItem( $row->il_to, $shared );
97 }
98 }
99
105 private function fetchLocal( $shared ) {
106 $dbr = $this->getReplicaDB();
107 $result = $dbr->newSelectQueryBuilder()
108 ->select( 'img_name' )
109 ->from( 'image' )
110 ->caller( __METHOD__ )
111 ->fetchResultSet();
112
113 foreach ( $result as $row ) {
114 $this->outputItem( $row->img_name, $shared );
115 }
116 }
117
118 private function outputItem( $name, $shared ) {
119 $file = $this->getServiceContainer()->getRepoGroup()->findFile( $name );
120 if ( $file && $this->filterItem( $file, $shared ) ) {
121 $filename = $file->getLocalRefPath();
122 $rel = wfRelativePath( $filename, $this->mBasePath );
123 $this->output( "$rel\n" );
124 } else {
125 wfDebug( __METHOD__ . ": base file? $name" );
126 }
127 }
128
129 private function filterItem( $file, $shared ) {
130 return $shared || $file->isLocal();
131 }
132}
133
134// @codeCoverageIgnoreStart
135$maintClass = DumpUploads::class;
136require_once RUN_MAINTENANCE_IF_MAIN;
137// @codeCoverageIgnoreEnd
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:105
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.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
hasOption( $name)
Checks to see if a particular option was set.
getOption( $name, $default=null)
Get an option, or return the default.
getServiceContainer()
Returns the main service container.
addDescription( $text)
Set the description text.
$maintClass