(bug 4990) Show page source to blocked users on edits, or their modified version...
[mediawiki.git] / maintenance / importImages.php
blob925c64b7207652ff2d1ed1da4774460f50764e86
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 * @package MediaWiki
8 * @subpackage Maintenance
9 * @author Rob Church <robchur@gmail.com>
12 require_once( 'commandLine.inc' );
13 require_once( 'importImages.inc.php' );
14 echo( "Import Images\n\n" );
16 # Need a directory and at least one extension
17 if( count( $args ) > 1 ) {
19 $dir = array_shift( $args );
21 # Check the allowed extensions
22 while( $ext = array_shift( $args ) )
23 $exts[] = ltrim( $ext, '.' );
25 # Search the directory given and pull out suitable candidates
26 $files = findFiles( $dir, $exts );
28 # Set up a fake user for this operation
29 $wgUser = User::newFromName( 'Image import script' );
30 $wgUser->setLoaded( true );
32 # Batch "upload" operation
33 foreach( $files as $file ) {
35 $base = basename( $file );
37 # Validate a title
38 $title = Title::makeTitleSafe( NS_IMAGE, $base );
39 if( is_object( $title ) ) {
41 # Check existence
42 $image = new Image( $title );
43 if( !$image->exists() ) {
45 global $wgUploadDirectory;
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 ) );
51 # Stash the file
52 echo( "Saving {$base}..." );
54 if( copy( $file, $image->getFullPath() ) ) {
56 echo( "importing..." );
58 # Grab the metadata
59 $image->loadFromFile();
61 # Record the upload
62 if( $image->recordUpload( '', 'Importing image file' ) ) {
64 # We're done!
65 echo( "done.\n" );
67 } else {
68 echo( "failed.\n" );
71 } else {
72 echo( "failed.\n" );
75 } else {
76 echo( "{$base} could not be imported; a file with this name exists in the wiki\n" );
79 } else {
80 echo( "{$base} could not be imported; a valid title cannot be produced\n" );
86 } else {
87 showUsage();
90 exit();
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();