Applied and tweaked smart-import patch from Mij, merging with 1.16.
authorPlatonides <platonides@users.mediawiki.org>
Sun, 7 Feb 2010 16:10:14 +0000 (16:10 +0000)
committerPlatonides <platonides@users.mediawiki.org>
Sun, 7 Feb 2010 16:10:14 +0000 (16:10 +0000)
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
maintenance/importImages.inc
maintenance/importImages.php

index 59c611f..53c64ef 100644 (file)
@@ -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 ===
 
index 1f693fa..61baf7c 100644 (file)
@@ -6,6 +6,7 @@
  * @file
  * @ingroup Maintenance
  * @author Rob Church <robchur@gmail.com>
+ * @author Mij <mij@bitchx.it>
  */
 
 /**
@@ -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('#<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];
+}
+
index 9f9bcae..704cb0f 100644 (file)
@@ -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 <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" );
@@ -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=<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);