moniwiki php importer

info

moniwiki migration tool.
language : php + xmlrpc

My english is poor~~ Sorry!
php로 작업하는 예제가 confluence에 없어서 영문이 딸리지만 약간의 영문 설명을 달았습니다.
콩글리쉬이지만 머 대략 코드보면 알아서 할 수 있겠지요.

moniwiki 변경 moniwiki change

wiki.php 프로그램 변경 wiki.php.1004가 원본이라고 생각하면 된다. wiki.php 에서 joon confluence migration 나오는 부분이 이번 변환과 연관된 부분이다.
페이지명을 받아 마이그레이션 스크립트로 연결을 해주면 된다. 해당 스크립트는 migration.php 이다.

samjung auth 부분은 사용자 인증부분이다. 이번 작업과는 관련이 없다.

"migration.php" is moniwiki importer.

[root@intranet migration]# diff wiki.php wiki.php.1004
16,22d15
< // samjung auth
< $WEB_ROOT="/var/www/html";
< require_once("$WEB_ROOT/intranet/include/config.php");
< require_once("$WEB_ROOT/intranet/include/mysql.php");
< require_once("$WEB_ROOT/intranet/include/util.php");
< require_once("$WEB_ROOT/intranet/include/auth.php");
<
3415,3422c3408
<       print "\n<center><b><a href=/confluence/>신규위키로 가기</a></b></center>\n";
<
< # joon confluence migration
< $mypage_name=$this->page->name;
< $mypage_name=_rawurlencode($mypage_name);
<
< echo "<a href=$DOCUMENT_ROOT/migration/migration.php?mypage_name=$mypage_name>$mypage_name 마이그레이션</a><br>";
<
---
>       print "\n<center><b><a href=/testwiki/>testwiki로 가기</a></b> (교육기간중 임시사용)</center>\n";

변환스크립트 importer scripts

변환스크립트 설명 scripts info

아래는 변환스크립트이다. 크게 두 부분으로 앞부분은 모니위키에서 데이타 가져오는 부분, 뒷부분은 xmlrpc 이용하여 confluence에 데이타를 넣는 부분이다.
모니위키에서 페이지명은 urlencode을 하고 영문이나 숫자가 아닌 부분은 %를 _로 바꾸고 소문자로 바꾼다.
페이지에 해당하는 파일을 불러와서 정규표현식을 이용하여 모니위키문법에서 confluence 위키로 바꾸어준다.
정규표현식에 익숙하지 않은 부분이 있어서 현재는 일부부만 변환이 되고있고 완벽하게 지원하지도 않는다.

confluencde에 접속하기 위해 xmlrpc 가 필요하다.

http://phpxmlrpc.sourceforge.net/
위 사이트에서 xmlrpc 라이브러리를 다운로드받는다.
Download xmlrpc library! In this example, xml-rpc directory is "/var/www/html/migration/xmlrpc-2.1/"

사이트의 문서를 참고한다.
Refer to phpxmlrpc documents.

위 라이브러리를 다운로드받은후 문자셋을 변경해야한다.
If you use utf-8 (korean etc...), you must change defalut characterset.
change $GLOBALS['xmlrpc_internalencoding'] to UTF-8.

lib/xmlrpc.inc 에서 아래와 같이 PHP에서 처리하는 문자셋을 ISO-8895-1 에서 UTF-8 로 바꾼다.

#$GLOBALS['xmlrpc_internalencoding']='ISO-8859-1';
        $GLOBALS['xmlrpc_internalencoding']='UTF-8';

간단한 스크립트 예제는 kldp의 문서도 참고한다.
If you are korean, refer to kldp document.
http://kldp.org/HOWTO/html/XML-RPC-HOWTO/xmlrpc-howto-php.html

confluence 사이트에는 이에 대한 예제가 없어서 헤매었다.
I searched confluence site. But in confluence site are not php xmlrpc sample.

해당 페이지제목이 있을 경우 해당 페이지를 없애고 다시 생성을 한다.
기존 페이지가 있을 경우 업그레이드가 가능하도록 하려고 했으나 잘 되지 않았다.
지정한 space에 문서가 들어가며 parentId를 Home 으로 지정하려고 했지만 잘 되지 않았다.

Error? Todo? 수정할 부분

  • full regular expression is not supported. (image, attachments ....)
  • page update is not supported.
  • parentId is not supported.

code 해당 코드

Download migration.php

<?php
include("wikilib.php");
function error_report($message) {
	echo $message;
	exit;
}

$mypage_name=$mypage_name;
$encode_name=preg_replace("/([^a-z0-9]{1})/ie","'_'.strtolower(dechex(ord(substr('\\1',-1))))",$mypage_name);

$path="$DOCUMENT_ROOT/migration/data/text/";
$name=$encode_name;
$filename=$path.$name;
$migration_path="$DOCUMENT_ROOT/migration/data/migration/";
$migration_name=$migration_path.$name;

$fp = fopen($filename,"r");
if(!$fp)
	error_report("$filename open error\n");

while(!feof($fp))
{
	$order = fgets($fp,999);
	$contents .=$order;
}

fclose($fp);

########### wiki converter #################
# list
$contents=eregi_replace(' \*','*',$contents);

# headline
$contents=preg_replace('/======(.*?)======/','
h5.\1 ',$contents);
$contents=preg_replace('/=====(.*?)=====/','
h4.\1 ',$contents);
$contents=preg_replace('/====(.*?)====/','
h3.\1 ',$contents);
$contents=preg_replace('/===(.*?)===/','
h2.\1 ',$contents);
$contents=preg_replace('/==(.*?)==/','
h1.\1 ',$contents);
$contents=preg_replace('/=(.*?)=/','
h1.\1 ',$contents);

# bold
$contents=preg_replace('/\'\'\'(.*?)\'\'\'/','*\1*',$contents);

# datatime
$contents=preg_replace('/\[\[DateTime(.*?)\]\]/','',$contents);

# http link
$contents=preg_replace('/\[http:\/\/(.*?)\]/','http://\1',$contents);

# table
$contents=eregi_replace('\|\|','|',$contents);

# code
$contents=eregi_replace('{{{','\{code\}',$contents);
$contents=eregi_replace('}}}','\{code\}',$contents);

# table of contents 
$contents=eregi_replace('\[\[TableOfContents\]\]','{toc}',$contents);


# Datetime
$contents=preg_replace('/\[\[DateTime(.*?)\]\]/','',$contents);
$contents=preg_replace('/\[\[Date\((.*?)\]\]/','',$contents);

# image, file
$contents=ereg_replace("attachment:([^ ]+).(gif|GIF|jpg|jpeg|JPG|png)","!\\1.\\2!",$contents);
$contents=ereg_replace("[[]attachment:([^ ]+).(doc|hwp|ppt|pdf)","[^\\1.\\2]",$contents);
$contents=ereg_replace("attachment:([^ ]+).(doc|hwp|ppt)","[^\\1.\\2]",$contents);
$contents=ereg_replace("]]","]",$contents);

# wiki link
$contents=ereg_replace("\[wiki:","[",$contents);

# smileye
$contents=ereg_replace("\{\*\}","(*g)",$contents);
$contents=ereg_replace("[/][!][\]","(*g)",$contents);
$contents=ereg_replace("[{][o][}]","(*g)",$contents);

# comments
$contents=ereg_replace("\[\[Comment\]\]","",$contents);

# title
$contents=ereg_replace("^#title ","",$contents);

# user
$contents=ereg_replace("\[joon\]","[~joon]",$contents);
$contents=ereg_replace("\[firechoi\]","[~firechoi]",$contents);
$contents=ereg_replace("\[winchild\]","[~winchild]",$contents);
$contents=ereg_replace("\[마성민\]","[~mrmsm]",$contents);

//echo $contents;

/*
# file write
if(strlen($contents)>0)
{
	$migration_fp = fopen($migration_name,"w");
	
	if(!$migration_fp)
		error_report("$migration_name open error\n");
	
	$migration_write=fwrite($migration_fp,$contents,strlen($contents));
	
	if(!$migration_write)
		error_report("$migration_name write error\n");
	
	fclose($migration_fp);
	echo "<META HTTP-EQUIV=\"refresh\" CONTENT=\"0;URL=http://intranet.sds.co.kr/migration/data/migration/$name\">";
	//readfile($migration_name);
}
else
	error_report("내용이 없습니다");
*/


###################################################
#### php xml-rpc confluence client ################
#### 2007.2.15 joon############### ################
###################################################

# xmlrpc lib
require_once('/var/www/html/migration/xmlrpc-2.1/lib/xmlrpc.inc');
require_once('/var/www/html/migration/xmlrpc-2.1/lib/xmlrpcs.inc');
require_once('/var/www/html/migration/xmlrpc-2.1/lib/xmlrpc_wrappers.inc');

# config
$path="/confluence/rpc/xmlrpc";
$confluence_server="intranet.xxx.co.kr";
$port = "8080";
$confluce_user="xxxx";
$confluce_password="xxxx";

$space="migration";
$title=$mypage_name;

function xml_error_report($result) {
	echo "\n".$result->faultCode();
	echo "\n".$result->faultString();
	echo "\n";
}

# connect
$server = new xmlrpc_client($path, $confluence_server, $port);

# confluence login
$message = new xmlrpcmsg('confluence1.login', array(new xmlrpcval("$confluce_user"), new xmlrpcval("$confluce_password")));
$result = $server->send($message);
if($result->faultCode())
	xml_error_report($result);
$temp=$result->value();
$token=$temp->scalarval();

# getpage
$page = new xmlrpcmsg('confluence1.getPage', array(new xmlrpcval($token), new xmlrpcval("$space"), new xmlrpcval("$title")));
$page_result = $server->send($page);

$page_result->faultCode();

if(!$page_result->faultCode())
{
	$temp2=$page_result->value();
	
	$idval=$temp2->structmem('id');
	$id = $idval->scalarval();
	
	$spaceval=$temp2->structmem('space');
	$space = $spaceval->scalarval();
	
	$titleval=$temp2->structmem('title');
	$title = $titleval->scalarval();
	
	$versionval=$temp2->structmem('version');
	$version = $versionval->scalarval();
	
	$contentval=$temp2->structmem('content');
	$content = $contentval->scalarval();
	
	$parentIdval=$temp2->structmem('parentId');
	$parentId = $parentIdval->scalarval();
	
	# page remove
	$remove_page = new xmlrpcmsg('confluence1.removePage', array(new xmlrpcval("$token"),new xmlrpcval($id)));
	$remove_result = $server->send($remove_page);
	if($remove_result->faultCode())
		xml_error_report($remove_result);
}

# page update
$mypage2 = new xmlrpcval(
  array(
    "space" => new xmlrpcval("$space", "string"),
    "title" => new xmlrpcval("$title", "string"),
    "content" => new xmlrpcval("$contents", "string"),
  ), 
  "struct");

$makepage_act = new xmlrpcmsg('confluence1.storePage', array(new xmlrpcval("$token"),$mypage2));
$makepage_act_result = $server->send($makepage_act);

if($makepage_act_result->faultCode())
	xml_error_report($makepage_act_result);
//echo "$title 페이지를 create 하였습니다<br>\n";

# rewrite
$url="http://intranet.xxxx.co.kr/confluence/";
echo "<META HTTP-EQUIV=\"refresh\" CONTENT=\"0;URL=$url\">";

?>

Labels

moniwiki moniwiki Delete
php php Delete
importer importer Delete
wikiimporter wikiimporter Delete
Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.
  1. Feb 15, 2007

    문태준 says:

    I'm not programmer. This scripts have many bugs.

    I'm not programmer. This scripts have many bugs.

  2. Jan 14, 2008

    taejoon Moon says:

    문법을 변환해야 할것은 위의 코드를 보면 되지만 그래도 편리하게 정리하면 아래와 같다. # ## === aa === = h1 = == ...

    문법을 변환해야 할것은 위의 코드를 보면 되지만 그래도 편리하게 정리하면 아래와 같다.

    # ##
    === aa ===
    h3.
    = h1 =
    == h2 ==
    === h3 ===
    ==== h4 ====
    # Text Effects
    '''bold'''
    *bold*
    # ##
    ####
    attachment:/#####_###.doc
    -> ### ####
    #####
    [aaa] [wiki:aaa ##]
    ##
    [aaaa] -> aaaa
    [http://fdasfas] -> http://fdasfas
    [http://fdasfas aa] -> [aa|http://fdasfas]
    # ###
    *
    *
    *
    *
    **
    **
    # ###
    ####
    attachment:/#####_###.doc
    -> ### ####. !#####_###.doc!
    # ###
    || || ||
    | | |
    # Advanced Formatting
    {{{ }}}
    \{code\} \{code\}
    # data, time, user?
    [[DateTime(2006-08-16T05:14:36)]]
    # confluence Content
    [[TableOfContents]]
    {toc}
    # ##
    [[Comment]]
    ?
    #####
    ??
  3. Jan 14, 2008

    taejoon Moon says:

    참고로 위의 스크립트에서는 첨부파일 이전기능이 빠져있습니다. This scripts is not include attachement file m...

    참고로 위의 스크립트에서는 첨부파일 이전기능이 빠져있습니다.

    This scripts is not include attachement file migration~~