From 5ae7a0147a8ddd5e4c74a9e3d0764aeb930c1e82 Mon Sep 17 00:00:00 2001 From: Platonides Date: Sun, 7 Feb 2010 16:10:14 +0000 Subject: [PATCH] Applied and tweaked smart-import patch from Mij, merging with 1.16. This adds --source-wiki-url parameter to importImages.php for a wiki from which to fetch the original uploader and comment. Useful when going from local uploads to a shared repository. Original code at http://www.howtopedia.org/public/mw-smart-import.tbz "I release it with the least restrictive license applicable, considering the code is derivative work of the respective maintenance scripts from the MW distribution. Feel free to commit this or modifications of it to your repos." http://lists.wikimedia.org/pipermail/mediawiki-l/2010-February/033230.html --- RELEASE-NOTES | 2 + maintenance/importImages.inc | 25 ++++++++++- maintenance/importImages.php | 83 +++++++++++++++++++++++------------- 3 files changed, 80 insertions(+), 30 deletions(-) diff --git a/RELEASE-NOTES b/RELEASE-NOTES index 59c611f273..53c64ef887 100644 --- a/RELEASE-NOTES +++ b/RELEASE-NOTES @@ -318,6 +318,8 @@ it from source control: http://www.mediawiki.org/wiki/Download_from_SVN the return value * Separate unit test suites under t/ and tests/ were merged and moved to maintenance/tests/. +* importImages.php maintenance script can now use the original uploader and +comment from another wiki. === Bug fixes in 1.16 === diff --git a/maintenance/importImages.inc b/maintenance/importImages.inc index 1f693fa108..61baf7c415 100644 --- a/maintenance/importImages.inc +++ b/maintenance/importImages.inc @@ -6,6 +6,7 @@ * @file * @ingroup Maintenance * @author Rob Church + * @author Mij */ /** @@ -86,4 +87,26 @@ function findAuxFile( $file, $auxExtension, $maxStrip = 1 ) { } return false; -} \ No newline at end of file +} + +# FIXME: Access the api in a saner way and performing just one query (preferably batching files too). +function getFileCommentFromSourceWiki($wiki_host, $file) { + $url = $wiki_host . '/api.php?action=query&format=xml&titles=File:' . $file . '&prop=imageinfo&&iiprop=comment'; + $body = file_get_contents($url); + if (preg_match('##', $body, $matches) == 0) { + return false; + } + + return $matches[1]; +} + +function getFileUserFromSourceWiki($wiki_host, $file) { + $url = $wiki_host . '/api.php?action=query&format=xml&titles=File:' . $file . '&prop=imageinfo&&iiprop=user'; + $body = file_get_contents($url); + if (preg_match('##', $body, $matches) == 0) { + return false; + } + + return $matches[1]; +} + diff --git a/maintenance/importImages.php b/maintenance/importImages.php index 9f9bcaec50..704cb0f4c3 100644 --- a/maintenance/importImages.php +++ b/maintenance/importImages.php @@ -2,16 +2,24 @@ /** * Maintenance script to import one or more images from the local file system into - * the wiki without using the web-based interface + * the wiki without using the web-based interface. + * + * "Smart import" additions: + * - aim: preserve the essential metadata (user, description) when importing medias from an existing wiki + * - process: + * - interface with the source wiki, don't use bare files only (see --source-wiki-url). + * - fetch metadata from source wiki for each file to import. + * - commit the fetched metadata to the destination wiki while submitting. * * @file * @ingroup Maintenance * @author Rob Church + * @author Mij */ -$optionsWithArgs = array( 'extensions', 'comment', 'comment-file', 'comment-ext', 'user', 'license', 'sleep', 'limit', 'from' ); +$optionsWithArgs = array( 'extensions', 'comment', 'comment-file', 'comment-ext', 'user', 'license', 'sleep', 'limit', 'from', 'source-wiki-url' ); require_once( dirname(__FILE__) . '/commandLine.inc' ); -require_once( 'importImages.inc' ); +require_once( dirname(__FILE__) . '/importImages.inc.php' ); $processed = $added = $ignored = $skipped = $overwritten = $failed = 0; echo( "Import Images\n\n" ); @@ -141,36 +149,51 @@ if (isset($options['protect']) && $options['protect'] == 1) $svar = 'added'; } - # Find comment text - $commentText = false; + if (isset( $options['source-wiki-url'])) { + /* find comment text directly from source wiki, through MW's API */ + $real_comment = getFileCommentFromSourceWiki($options['source-wiki-url'], $base); + if ($real_comment === false) + $commentText = $comment; + else + $commentText = $real_comment; + + /* find user directly from source wiki, through MW's API */ + $real_user = getFileUserFromSourceWiki($options['source-wiki-url'], $base); + if ($real_user === false) { + $wgUser = $user; + } else { + $wgUser = User::newFromName($real_user); + if ($wgUser === false) { + # user does not exist in target wiki + echo ("failed: user '$real_user' does not exist in target wiki."); + continue; + } + } + } else { + # 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; + } + } - if ( $commentExt ) { - $f = findAuxFile( $file, $commentExt ); - if ( !$f ) { - echo( " No comment file with extension {$commentExt} found for {$file}. " ); - $commentText = $comment; - } else { - $commentText = file_get_contents( $f ); - if ( !$f ) { - echo( " Failed to load comment file {$f}. " ); - $commentText = $comment; - } else if ( $comment ) { - $commentText = trim( $commentText ) . "\n\n" . trim( $comment ); - } - } - } - - if ( !$commentText ) { - $commentText = $comment; - } - - if ( !$commentText ) { - $commentText = 'Importing image file'; - } # Import the file if ( isset( $options['dry'] ) ) { - echo( " publishing {$file}... " ); + echo( " publishing {$file} by '" . $wgUser->getName() . "', comment '$commentText'... " ); } else { $archive = $image->publish( $file ); if( WikiError::isError( $archive ) || !$archive->isGood() ) { @@ -282,6 +305,8 @@ Options: --dry Dry run, don't import anything --protect= Specify the protect value (autoconfirmed,sysop) --unprotect Unprotects all uploaded images +--source-wiki-url if specified, take User and Comment data for each imported file from this URL. + For example, --source-wiki-url="http://en.wikipedia.org/" TEXT; exit(1); -- 2.20.1