* @file
* @ingroup Maintenance
* @author Rob Church <robchur@gmail.com>
+ * @author Mij <mij@bitchx.it>
*/
/**
}
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('#<ii comment="([^"]*)" />#', $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('#<ii user="([^"]*)" />#', $body, $matches) == 0) {
+ return false;
+ }
+
+ return $matches[1];
+}
+
/**
* 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 <robchur@gmail.com>
+ * @author Mij <mij@bitchx.it>
*/
-$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" );
$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() ) {
--dry Dry run, don't import anything
--protect=<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);