]> scripts.mit.edu Git - autoinstalls/mediawiki.git/blob - maintenance/importImages.php
MediaWiki 1.11.0
[autoinstalls/mediawiki.git] / maintenance / importImages.php
1 <?php
2
3 /**
4  * Maintenance script to import one or more images from the local file system into
5  * the wiki without using the web-based interface
6  *
7  * @addtogroup Maintenance
8  * @author Rob Church <robchur@gmail.com>
9  */
10
11 $optionsWithArguments = array( 'extensions', 'overwrite' );
12 require_once( 'commandLine.inc' );
13 require_once( 'importImages.inc.php' );
14 $added = $skipped = $overwritten = 0;
15
16 echo( "Import Images\n\n" );
17
18 # Need a path
19 if( count( $args ) > 0 ) {
20
21         $dir = $args[0];
22
23         # Prepare the list of allowed extensions
24         global $wgFileExtensions;
25         $extensions = isset( $options['extensions'] )
26                 ? explode( ',', strtolower( $options['extensions'] ) )
27                 : $wgFileExtensions;
28
29         # Search the path provided for candidates for import
30         $files = findFiles( $dir, $extensions );
31
32         # Initialise the user for this operation
33         $user = isset( $options['user'] )
34                 ? User::newFromName( $options['user'] )
35                 : User::newFromName( 'Maintenance script' );
36         if( !$user instanceof User )
37                 $user = User::newFromName( 'Maintenance script' );
38         $wgUser = $user;
39
40         # Get the upload comment
41         $comment = isset( $options['comment'] )
42                 ? $options['comment']
43                 : 'Importing image file';
44
45         # Get the license specifier
46         $license = isset( $options['license'] ) ? $options['license'] : '';
47
48         # Batch "upload" operation
49         if( ( $count = count( $files ) ) > 0 ) {
50         
51                 foreach( $files as $file ) {
52                         $base = wfBaseName( $file );
53         
54                         # Validate a title
55                         $title = Title::makeTitleSafe( NS_IMAGE, $base );
56                         if( !is_object( $title ) ) {
57                                 echo( "{$base} could not be imported; a valid title cannot be produced\n" );
58                                 continue;
59                         }
60         
61                         # Check existence
62                         $image = wfLocalFile( $title );
63                         if( $image->exists() ) {
64                                 if( isset( $options['overwrite'] ) ) {
65                                         echo( "{$base} exists, overwriting..." );
66                                         $svar = 'overwritten';
67                                 } else {
68                                         echo( "{$base} exists, skipping\n" );
69                                         $skipped++;
70                                         continue;
71                                 }
72                         } else {
73                                 echo( "Importing {$base}..." );
74                                 $svar = 'added';
75                         }
76
77                         # Import the file       
78                         $archive = $image->publish( $file );
79                         if( WikiError::isError( $archive ) || !$archive->isGood() ) {
80                                 echo( "failed.\n" );
81                                 continue;
82                         }
83
84                         $$svar++;
85                         if ( $image->recordUpload( $archive->value, $comment, $license ) ) {
86                                 # We're done!
87                                 echo( "done.\n" );
88                         } else {
89                                 echo( "failed.\n" );
90                         }
91                         
92                 }
93                 
94                 # Print out some statistics
95                 echo( "\n" );
96                 foreach( array( 'count' => 'Found', 'added' => 'Added',
97                         'skipped' => 'Skipped', 'overwritten' => 'Overwritten' ) as $var => $desc ) {
98                         if( $$var > 0 )
99                                 echo( "{$desc}: {$$var}\n" );
100                 }
101                 
102         } else {
103                 echo( "No suitable files could be found for import.\n" );
104         }
105
106 } else {
107         showUsage();
108 }
109
110 exit();
111
112 function showUsage( $reason = false ) {
113         if( $reason ) {
114                 echo( $reason . "\n" );
115         }
116
117         echo <<<END
118 Imports images and other media files into the wiki
119 USAGE: php importImages.php [options] <dir>
120
121 <dir> : Path to the directory containing images to be imported
122
123 Options:
124 --extensions=<exts>     Comma-separated list of allowable extensions, defaults to \$wgFileExtensions
125 --overwrite                     Overwrite existing images if a conflicting-named image is found
126 --user=<username>       Set username of uploader, default 'Maintenance script'
127 --comment=<text>        Set upload summary comment, default 'Importing image file'
128 --license=<code>        Use an optional license template
129
130 END;
131         exit();
132 }