| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
require_once __DIR__ . '/Maintenance.php'; |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
class DumpUploads extends Maintenance { |
| 35 |
|
| 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 |
|
| 57 |
$shared = true; |
| 58 |
} else { |
| 59 |
|
| 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 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
private function fetchUsed( $shared ) { |
| 83 |
$dbr = $this->getReplicaDB(); |
| 84 |
|
| 85 |
$result = $dbr->newSelectQueryBuilder() |
| 86 |
->select( [ 'il_to', 'img_name' ] ) |
| 87 |
->distinct() |
| 88 |
->from( 'imagelinks' ) |
| 89 |
->leftJoin( 'image', null, 'il_to=img_name' ) |
| 90 |
->caller( __METHOD__ ) |
| 91 |
->fetchResultSet(); |
| 92 |
|
| 93 |
foreach ( $result as $row ) { |
| 94 |
$this->outputItem( $row->il_to, $shared ); |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
private function fetchLocal( $shared ) { |
| 104 |
$dbr = $this->getReplicaDB(); |
| 105 |
$result = $dbr->newSelectQueryBuilder() |
| 106 |
->select( 'img_name' ) |
| 107 |
->from( 'image' ) |
| 108 |
->caller( __METHOD__ ) |
| 109 |
->fetchResultSet(); |
| 110 |
|
| 111 |
foreach ( $result as $row ) { |
| 112 |
$this->outputItem( $row->img_name, $shared ); |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
private function outputItem( $name, $shared ) { |
| 117 |
$file = $this->getServiceContainer()->getRepoGroup()->findFile( $name ); |
| 118 |
if ( $file && $this->filterItem( $file, $shared ) ) { |
| 119 |
$filename = $file->getLocalRefPath(); |
| 120 |
$rel = wfRelativePath( $filename, $this->mBasePath ); |
| 121 |
$this->output( "$rel\n" ); |
| 122 |
} else { |
| 123 |
wfDebug( __METHOD__ . ": base file? $name" ); |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
private function filterItem( $file, $shared ) { |
| 128 |
return $shared || $file->isLocal(); |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
|
| 133 |
$maintClass = DumpUploads::class; |
| 134 |
require_once RUN_MAINTENANCE_IF_MAIN; |
| 135 |
|