* Add --help and --quiet options to parser test runner
[lhc/web/wiklou.git] / maintenance / importUseModWiki.php
index ee6f371..c0a4b24 100644 (file)
@@ -1,18 +1,27 @@
 <?php
 
-/*
-       Import data from a UseModWiki into a PediaWiki wiki
-       2003-02-09 Brion VIBBER <brion@pobox.com>
-       Based loosely on Magnus's code from 2001-2002
-
-         Updated limited version to get something working temporarily
-         2003-10-09
-         Be sure to run the link & index rebuilding scripts!
-
-  */
+/**
+ * Import data from a UseModWiki into a PediaWiki wiki
+ * 2003-02-09 Brion VIBBER <brion@pobox.com>
+ * Based loosely on Magnus's code from 2001-2002
+ *
+ * Updated limited version to get something working temporarily
+ * 2003-10-09
+ * Be sure to run the link & index rebuilding scripts!
+ *
+ * Some more munging for charsets etc
+ * 2003-11-28
+ *
+ * @todo document
+ * @package MediaWiki
+ * @subpackage Maintenance
+ */
+
+/** Set these correctly! */
+$wgImportEncoding = "CP1252"; /* We convert all to UTF-8 */
+$wgRootDirectory = "/home/usemod/wiki-ia/lib-http/db/wiki";
 
 /* globals */
-$wgRootDirectory = "/Users/brion/src/wiki/convert/wiki-fy/lib-http/db/wiki";
 $wgFieldSeparator = "\xb3"; # Some wikis may use different char
        $FS = $wgFieldSeparator ;
        $FS1 = $FS."1" ;
@@ -211,8 +220,10 @@ function importPage( $title )
        
        # Current revision:
        $text = wfStrencode( recodeText( $page->text ) );
+       $comment = wfStrencode( recodeText( $page->summary ) );
        $minor = ($page->minor ? 1 : 0);
        list( $userid, $username ) = checkUserCache( $page->username, $page->host );
+       $username = wfStrencode( recodeText( $username ) );
        $timestamp = wfUnix2Timestamp( $page->ts );
        $redirect = ( preg_match( '/^#REDIRECT/', $page->text ) ? 1 : 0 );
        $random = mt_rand() / mt_getrandmax();
@@ -238,7 +249,7 @@ INSERT
                $username = wfStrencode( recodeText( $username ) );
                $timestamp = wfUnix2Timestamp( $rev->ts );
                $inverse = wfInvertTimestamp( $timestamp );
-               $comment = wfStrencode( recodeText( $rev->text ) );
+               $comment = wfStrencode( recodeText( $rev->summary ) );
                
                if($any) $sql .= ",";
                $sql .= "\n\t($namespace,'$newtitle','$text','$comment',$userid,'$username','$timestamp','$inverse',$minor)";
@@ -250,12 +261,35 @@ INSERT
 
 # Whee!
 function recodeText( $string ) {
+       global $wgImportEncoding;
        # For currently latin-1 wikis
        $string = str_replace( "\r\n", "\n", $string );
-       # return iconv( "CP1252", "UTF-8", $string );
-       return utf8_encode( $string );
+       $string = iconv( $wgImportEncoding, "UTF-8", $string );
+       $string = wfMungeToUtf8( $string ); # Any old &#1234; stuff
+       return $string;
 }
 
+function wfUtf8Sequence($codepoint) {
+       if($codepoint <     0x80) return chr($codepoint);
+       if($codepoint <    0x800) return chr($codepoint >>  6 & 0x3f | 0xc0) .
+                                     chr($codepoint       & 0x3f | 0x80);
+    if($codepoint <  0x10000) return chr($codepoint >> 12 & 0x0f | 0xe0) .
+                                     chr($codepoint >>  6 & 0x3f | 0x80) .
+                                     chr($codepoint       & 0x3f | 0x80);
+       if($codepoint < 0x100000) return chr($codepoint >> 18 & 0x07 | 0xf0) . # Double-check this
+                                        chr($codepoint >> 12 & 0x3f | 0x80) .
+                                     chr($codepoint >>  6 & 0x3f | 0x80) .
+                                     chr($codepoint       & 0x3f | 0x80);
+       # Doesn't yet handle outside the BMP
+       return "&#$codepoint;";
+}
+
+function wfMungeToUtf8($string) {
+       $string = preg_replace ( '/&#([0-9]+);/e', 'wfUtf8Sequence($1)', $string );
+       $string = preg_replace ( '/&#x([0-9a-f]+);/ie', 'wfUtf8Sequence(0x$1)', $string );
+       # Should also do named entities here
+       return $string;
+}
 
 function wfStrencode( $string ) {
        return mysql_escape_string( $string );