From 42e97bbebe8f1db9e30ad7a0c3cee6a14a087b82 Mon Sep 17 00:00:00 2001 From: Daniel Kinzler Date: Thu, 27 Nov 2008 13:07:30 +0000 Subject: [PATCH] image import new supports per-image description files --- maintenance/importImages.inc.php | 39 +++++++++++++++++++++ maintenance/importImages.php | 59 ++++++++++++++++++++++++++------ 2 files changed, 88 insertions(+), 10 deletions(-) diff --git a/maintenance/importImages.inc.php b/maintenance/importImages.inc.php index 53895778aa..3d9d08f115 100644 --- a/maintenance/importImages.inc.php +++ b/maintenance/importImages.inc.php @@ -46,4 +46,43 @@ function splitFilename( $filename ) { 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 diff --git a/maintenance/importImages.php b/maintenance/importImages.php index 63bbec5fc7..a39c502c2e 100644 --- a/maintenance/importImages.php +++ b/maintenance/importImages.php @@ -9,7 +9,7 @@ * @author Rob Church */ -$optionsWithArguments = array( 'extensions', 'overwrite' ); +$optionsWithArgs = array( 'extensions', 'comment-file', 'comment-ext' ); require_once( 'commandLine.inc' ); require_once( 'importImages.inc.php' ); $added = $skipped = $overwritten = 0; @@ -39,9 +39,19 @@ if( count( $args ) > 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'] : ''; @@ -75,15 +85,40 @@ if( count( $args ) > 0 ) { $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 { @@ -123,10 +158,14 @@ USAGE: php importImages.php [options] Options: --extensions= 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= Set username of uploader, default 'Maintenance script' --comment= Set upload summary comment, default 'Importing image file' +--comment-file= Set upload summary comment the the content of . +--comment-ext= Causes the comment for each file to be loaded from a file with the same name + but the extension . --license= Use an optional license template +--dry Dry run, don't import anything END; exit(); -- 2.20.1