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