Fix SQLite patch-(page|template)links-fix-pk.sql column order
[lhc/web/wiklou.git] / maintenance / dumpUploads.php
1 <?php
2 /**
3 * Dump a the list of files uploaded, for feeding to tar or similar.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @ingroup Maintenance
22 */
23
24 use MediaWiki\MediaWikiServices;
25
26 require_once __DIR__ . '/Maintenance.php';
27
28 /**
29 * Maintenance script to dump a the list of files uploaded,
30 * for feeding to tar or similar.
31 *
32 * @ingroup Maintenance
33 */
34 class DumpUploads extends Maintenance {
35 public function __construct() {
36 parent::__construct();
37 $this->addDescription( 'Generates list of uploaded files which can be fed to tar or similar.
38 By default, outputs relative paths against the parent directory of $wgUploadDirectory.' );
39 $this->addOption( 'base', 'Set base relative path instead of wiki include root', false, true );
40 $this->addOption( 'local', 'List all local files, used or not. No shared files included' );
41 $this->addOption( 'used', 'Skip local images that are not used' );
42 $this->addOption( 'shared', 'Include images used from shared repository' );
43 }
44
45 public function execute() {
46 global $IP;
47 $this->mAction = 'fetchLocal';
48 $this->mBasePath = $this->getOption( 'base', $IP );
49 $this->mShared = false;
50 $this->mSharedSupplement = false;
51
52 if ( $this->hasOption( 'local' ) ) {
53 $this->mAction = 'fetchLocal';
54 }
55
56 if ( $this->hasOption( 'used' ) ) {
57 $this->mAction = 'fetchUsed';
58 }
59
60 if ( $this->hasOption( 'shared' ) ) {
61 if ( $this->hasOption( 'used' ) ) {
62 // Include shared-repo files in the used check
63 $this->mShared = true;
64 } else {
65 // Grab all local *plus* used shared
66 $this->mSharedSupplement = true;
67 }
68 }
69 $this->{$this->mAction} ( $this->mShared );
70 if ( $this->mSharedSupplement ) {
71 $this->fetchUsed( true );
72 }
73 }
74
75 /**
76 * Fetch a list of used images from a particular image source.
77 *
78 * @param bool $shared True to pass shared-dir settings to hash func
79 */
80 function fetchUsed( $shared ) {
81 $dbr = $this->getDB( DB_REPLICA );
82 $image = $dbr->tableName( 'image' );
83 $imagelinks = $dbr->tableName( 'imagelinks' );
84
85 $sql = "SELECT DISTINCT il_to, img_name
86 FROM $imagelinks
87 LEFT JOIN $image
88 ON il_to=img_name";
89 $result = $dbr->query( $sql );
90
91 foreach ( $result as $row ) {
92 $this->outputItem( $row->il_to, $shared );
93 }
94 }
95
96 /**
97 * Fetch a list of all images from a particular image source.
98 *
99 * @param bool $shared True to pass shared-dir settings to hash func
100 */
101 function fetchLocal( $shared ) {
102 $dbr = $this->getDB( DB_REPLICA );
103 $result = $dbr->select( 'image',
104 [ 'img_name' ],
105 '',
106 __METHOD__ );
107
108 foreach ( $result as $row ) {
109 $this->outputItem( $row->img_name, $shared );
110 }
111 }
112
113 function outputItem( $name, $shared ) {
114 $file = MediaWikiServices::getInstance()->getRepoGroup()->findFile( $name );
115 if ( $file && $this->filterItem( $file, $shared ) ) {
116 $filename = $file->getLocalRefPath();
117 $rel = wfRelativePath( $filename, $this->mBasePath );
118 $this->output( "$rel\n" );
119 } else {
120 wfDebug( __METHOD__ . ": base file? $name\n" );
121 }
122 }
123
124 function filterItem( $file, $shared ) {
125 return $shared || $file->isLocal();
126 }
127 }
128
129 $maintClass = DumpUploads::class;
130 require_once RUN_MAINTENANCE_IF_MAIN;