unset( $parts[ count( $parts ) - 1 ] );
$fname = implode( '.', $parts );
return array( $fname, $ext );
+}
+
+/**
+ * Find an auxilliary file with the given extension, matching
+ * the give base file path. $maxStrip determines how many extensions
+ * may be stripped from the original file name before appending the
+ * new extension. For example, with $maxStrip = 1 (the default),
+ * file files acme.foo.bar.txt and acme.foo.txt would be auxilliary
+ * files for acme.foo.bar and the extension ".txt". With $maxStrip = 2,
+ * acme.txt would also be acceptable.
+ *
+ * @param $file base path
+ * @param $auxExtension the extension to be appended to the base path
+ * @param $maxStrip the maximum number of extensions to strip from the base path (default: 1)
+ * @return string or false
+ */
+function findAuxFile( $file, $auxExtension, $maxStrip = 1 ) {
+ if ( strpos( $auxExtension, '.' ) !== 0 ) {
+ $auxExtension = '.' . $auxExtension;
+ }
+
+ $d = dirname( $file );
+ $n = basename( $file );
+
+ while ( $maxStrip >= 0 ) {
+ $f = $d . '/' . $n . $auxExtension;
+
+ if ( file_exists( $f ) ) {
+ return $f;
+ }
+
+ $idx = strpos( $n, '.' );
+ if ( !$idx ) break;
+
+ $n = substr( $n, 0, $idx );
+ $maxStrip -= 1;
+ }
+
+ return false;
}
\ No newline at end of file
* @author Rob Church <robchur@gmail.com>
*/
-$optionsWithArguments = array( 'extensions', 'overwrite' );
+$optionsWithArgs = array( 'extensions', 'comment-file', 'comment-ext' );
require_once( 'commandLine.inc' );
require_once( 'importImages.inc.php' );
$added = $skipped = $overwritten = 0;
$wgUser = $user;
# Get the upload comment
- $comment = isset( $options['comment'] )
- ? $options['comment']
- : 'Importing image file';
+ $comment = 'Importing image file';
+
+ if ( isset( $options['comment-file'] ) ) {
+ $comment = file_get_contents( $options['comment-file'] );
+ if ( $comment === false || $comment === NULL ) {
+ die( "failed to read comment file: {$options['comment-file']}\n" );
+ }
+ }
+ else if ( isset( $options['comment'] ) ) {
+ $comment = $options['comment'];
+ }
+
+ $commentExt = isset( $options['comment-ext'] ) ? $options['comment-ext'] : false;
# Get the license specifier
$license = isset( $options['license'] ) ? $options['license'] : '';
$svar = 'added';
}
+ # Find comment text
+ $commentText = false;
+
+ if ( $commentExt ) {
+ $f = findAuxFile( $file, $commentExt );
+ if ( !$f ) {
+ echo( " No comment file with extension {$commentExt} found for {$file}, using default comment. " );
+ } else {
+ $commentText = file_get_contents( $f );
+ if ( !$f ) {
+ echo( " Failed to load comment file {$f}, using default comment. " );
+ }
+ }
+ }
+
+ if ( !$commentText ) {
+ $commentText = $comment;
+ }
+
# Import the file
- $archive = $image->publish( $file );
- if( WikiError::isError( $archive ) || !$archive->isGood() ) {
- echo( "failed.\n" );
- continue;
+ if ( isset( $options['dry'] ) ) {
+ echo( " publishing {$file}... " );
+ } else {
+ $archive = $image->publish( $file );
+ if( WikiError::isError( $archive ) || !$archive->isGood() ) {
+ echo( "failed.\n" );
+ continue;
+ }
}
$$svar++;
- if ( $image->recordUpload( $archive->value, $comment, $license ) ) {
+ if ( isset( $options['dry'] ) ) {
+ echo( "done.\n" );
+ } else if ( $image->recordUpload( $archive->value, $commentText, $license ) ) {
# We're done!
echo( "done.\n" );
} else {
Options:
--extensions=<exts> Comma-separated list of allowable extensions, defaults to \$wgFileExtensions
---overwrite Overwrite existing images if a conflicting-named image is found
+--overwrite Overwrite existing images if a conflicting-named image is found
--user=<username> Set username of uploader, default 'Maintenance script'
--comment=<text> Set upload summary comment, default 'Importing image file'
+--comment-file=<file> Set upload summary comment the the content of <file>.
+--comment-ext=<ext> Causes the comment for each file to be loaded from a file with the same name
+ but the extension <ext>.
--license=<code> Use an optional license template
+--dry Dry run, don't import anything
END;
exit();