Use Doxygen @addtogroup instead of phpdoc @package && @subpackage
[lhc/web/wiklou.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 require_once( 'commandLine.inc' );
12 require_once( 'importImages.inc.php' );
13 echo( "Import Images\n\n" );
14
15 # Need a directory and at least one extension
16 if( count( $args ) > 1 ) {
17
18 $dir = array_shift( $args );
19
20 # Check the allowed extensions
21 while( $ext = array_shift( $args ) )
22 $exts[] = ltrim( $ext, '.' );
23
24 # Search the directory given and pull out suitable candidates
25 $files = findFiles( $dir, $exts );
26
27 # Set up a fake user for this operation
28 if( isset( $options['user'] ) ) {
29 $wgUser = User::newFromName( $options['user'] );
30 } else {
31 $wgUser = User::newFromName( 'Image import script' );
32 }
33 if ( $wgUser->isAnon() ) {
34 $wgUser->addToDatabase();
35 }
36
37 # Get the upload comment
38 $comment = isset( $options['comment'] )
39 ? $options['comment']
40 : 'Importing image file';
41
42 # Get the license specifier
43 $license = isset( $options['license'] ) ? $options['license'] : '';
44
45 # Batch "upload" operation
46 foreach( $files as $file ) {
47
48 $base = wfBaseName( $file );
49
50 # Validate a title
51 $title = Title::makeTitleSafe( NS_IMAGE, $base );
52 if( is_object( $title ) ) {
53
54 # Check existence
55 $image = new Image( $title );
56 if( !$image->exists() ) {
57
58 global $wgUploadDirectory;
59
60 # copy() doesn't create paths so if the hash path doesn't exist, we
61 # have to create it
62 makeHashPath( wfGetHashPath( $image->name ) );
63
64 # Stash the file
65 echo( "Saving {$base}..." );
66
67 if( copy( $file, $image->getFullPath() ) ) {
68
69 echo( "importing..." );
70
71 # Grab the metadata
72 $image->loadFromFile();
73
74 # Record the upload
75 if( $image->recordUpload( '', $comment, $license ) ) {
76
77 # We're done!
78 echo( "done.\n" );
79
80 } else {
81 echo( "failed.\n" );
82 }
83
84 } else {
85 echo( "failed.\n" );
86 }
87
88 } else {
89 echo( "{$base} could not be imported; a file with this name exists in the wiki\n" );
90 }
91
92 } else {
93 echo( "{$base} could not be imported; a valid title cannot be produced\n" );
94 }
95
96 }
97
98
99 } else {
100 showUsage();
101 }
102
103 exit();
104
105 function showUsage( $reason = false ) {
106 if( $reason )
107 echo( $reason . "\n" );
108 echo <<<END
109 USAGE: php importImages.php [options] <dir> <ext1> ...
110
111 <dir> : Path to the directory containing images to be imported
112 <ext1+> File extensions to import
113
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
118
119 END;
120 exit();
121 }
122
123 ?>