#9896 Documentation for wgSquidServers. Patch by Loic Dachary.
[mediawiki.git] / maintenance / importImages.php
blobf2d53989dc4f5640bc8c4d8a42b4cf789929d522
1 <?php
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
7 * @addtogroup Maintenance
8 * @author Rob Church <robchur@gmail.com>
9 */
11 require_once( 'commandLine.inc' );
12 require_once( 'importImages.inc.php' );
13 echo( "Import Images\n\n" );
15 # Need a directory and at least one extension
16 if( count( $args ) > 1 ) {
18 $dir = array_shift( $args );
20 # Check the allowed extensions
21 while( $ext = array_shift( $args ) ) {
22 $exts[] = ltrim( $ext, '.' );
25 # Search the directory given and pull out suitable candidates
26 $files = findFiles( $dir, $exts );
28 # Initialise the user for this operation
29 $user = isset( $options['user'] )
30 ? User::newFromName( $options['user'] )
31 : User::newFromName( 'Maintenance script' );
32 if( !$user instanceof User )
33 $user = User::newFromName( 'Maintenance script' );
34 $wgUser = $user;
36 # Get the upload comment
37 $comment = isset( $options['comment'] )
38 ? $options['comment']
39 : 'Importing image file';
41 # Get the license specifier
42 $license = isset( $options['license'] ) ? $options['license'] : '';
44 # Batch "upload" operation
45 foreach( $files as $file ) {
47 $base = wfBaseName( $file );
49 # Validate a title
50 $title = Title::makeTitleSafe( NS_IMAGE, $base );
51 if( is_object( $title ) ) {
53 # Check existence
54 $image = new Image( $title );
55 if( !$image->exists() ) {
57 global $wgUploadDirectory;
59 # copy() doesn't create paths so if the hash path doesn't exist, we
60 # have to create it
61 makeHashPath( wfGetHashPath( $image->name ) );
63 # Stash the file
64 echo( "Saving {$base}..." );
66 if( copy( $file, $image->getFullPath() ) ) {
68 echo( "importing..." );
70 # Grab the metadata
71 $image->loadFromFile();
73 # Record the upload
74 if( $image->recordUpload( '', $comment, $license ) ) {
76 # We're done!
77 echo( "done.\n" );
79 } else {
80 echo( "failed.\n" );
83 } else {
84 echo( "failed.\n" );
87 } else {
88 echo( "{$base} could not be imported; a file with this name exists in the wiki\n" );
91 } else {
92 echo( "{$base} could not be imported; a valid title cannot be produced\n" );
97 } else {
98 showUsage();
101 exit();
103 function showUsage( $reason = false ) {
104 if( $reason ) {
105 echo( $reason . "\n" );
108 echo <<<END
109 USAGE: php importImages.php [options] <dir> <ext1> ...
111 <dir> : Path to the directory containing images to be imported
112 <ext1+> File extensions to import
114 Options:
115 --user=<username> Set username of uploader, default 'Image import script'
116 --comment=<text> Set upload summary comment, default 'Importing image file'
117 --license=<code> Use an optional license template
119 END;
120 exit();