OSDN Git Service

MERGE: リビジョン1796。多言語化処理の観点からstrstr()をi18n::strpos()に変更
[nucleus-jp/nucleus-next.git] / nucleus / convert / wordpress.php
1 <?php
2 /*
3  * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)
4  * Copyright (C) 2005-2006 The Nucleus Group
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  * (see nucleus/documentation/index.html#license for more info)
11  */
12 /* Orginal author: Radek HULAN  */
13 /* http://hulan.cz/             */
14 /*                              */
15 /* Adapted to NucleusCMS from   */
16 /* Blog:CMS by Edmond Hui       */
17 /*                              */
18 /* This script will convert     */
19 /* your WP blog into            */
20 /* Nucleus CMS weblog           */
21 /*
22   v1.1 - add robustness code for category creation and item adding (admun)
23   v1.2 - add sql_table()
24 */
25
26 include("../../config.php");
27
28   function def($s){ 
29     if (isset($_POST[$s])) 
30       echo addslashes(stripslashes($_POST[$s])); 
31     else {
32       if (i18n::strpos($s,'host')!==FALSE) echo "localhost";
33       if (i18n::strpos($s,'username')!==FALSE) echo "root";
34       if (i18n::strpos($s,'wpprefix')!==FALSE) echo "wp_";
35     }
36   }
37   
38   function error($s){
39     global $isok;
40     $isok=false;
41     echo "<h3>Error: $s</h3>";
42   }
43   
44   // line breaks into properly formatted paragraphs
45   function paragraph($text, $br = false) {
46     $text=trim($text);
47     $text = str_replace("\r",'',$text);
48     $text = preg_replace('/&([^#])(?![a-z]{1,8};)/', '&amp;$1', $text);
49     if ($text=="") return "";
50     $text = $text . "\n"; // just to make things a little easier, pad the end
51     $text = preg_replace('|<br/>\s*<br/>|', "\n\n", $text);
52     $text = preg_replace('!(<(?:table|ul|ol|li|pre|form|blockquote|h[1-6])[^>]*>)!', "\n$1", $text); // Space things out a little
53     $text = preg_replace('!(</(?:table|ul|ol|li|pre|form|blockquote|h[1-6])>)!', "$1\n", $text); // Space things out a little
54     $text = preg_replace("/(\r\n|\r)/", "\n", $text); // cross-platform newlines 
55     $text = preg_replace("/\n\n+/", "\n\n", $text); // take care of duplicates
56     $text = preg_replace('/\n?(.+?)(?:\n\s*\n|\z)/s', "\t<p>$1</p>\n", $text); // make paragraphs, including one at the end 
57     $text = preg_replace('|<p>\s*?</p>|', '', $text); // under certain strange conditions it could create a P of entirely whitespace 
58     $text = preg_replace("|<p>(<li.+?)</p>|", "$1", $text); // problem with nested lists
59     // blockquote
60     $text = preg_replace('|<p><blockquote([^>]*)>|i', "<blockquote$1><p>", $text);
61     $text = str_replace('</blockquote></p>', '</p></blockquote>', $text);
62     // now the hard work
63     $text = preg_replace('!<p>\s*(</?(?:table|tr|td|th|div|ul|ol|li|pre|select|form|blockquote|p|h[1-6])[^>]*>)!', "$1", $text);
64     $text = preg_replace('!(</?(?:table|tr|td|th|div|ul|ol|li|pre|select|form|blockquote|p|h[1-6])[^>]*>)\s*</p>|</div>"!', "$1", $text); 
65     if ($br) $text = preg_replace('|(?<!<br />)\s*\n|', "<br />\n", $text); // optionally make line breaks
66     $text = preg_replace('!(</?(?:table|tr|td|th|div|dl|dd|dt|ul|ol|li|pre|select|form|blockquote|p|h[1-6])[^>]*>)\s*<br/>!', "$1", $text);
67     $text = preg_replace('!<br/>(\s*</?(?:p|li|div|th|pre|td|ul|ol)>)!', '$1', $text);
68     // some cleanup
69     $text = str_replace('</p><br />','</p>',$text);
70     $text = str_replace("<br />\n</p>",'</p>',$text);
71     return $text; 
72   }
73
74   function encoding($s){
75     global $input;
76     if (is_callable("iconv"))
77       return iconv($input,'utf-8',$s);
78     else 
79       return $s;
80   }
81   
82 ?>
83 <!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Strict//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd'>
84 <html xmlns='http://www.w3.org/1999/xhtml' xml:lang='cs' lang='cs'>
85 <head>
86 <meta http-equiv='Content-Type' content='text/html; charset=utf-8' />
87 <title>WordPress to Nucleus CMS convertor</title>
88 <meta http-equiv='Pragma' content='no-cache' />
89 <meta http-equiv='Cache-Control' content='no-cache, must-revalidate' />
90 <meta http-equiv='Expires' content='-1' />
91 <style type="text/css">
92 body { font-size: 15px; color: #000000; margin: 0; padding: 0; border: 0; font-family: "Trebuchet MS", "Bitstream Vera Sans", verdana, lucida, arial,
93 helvetica, sans-serif; padding-bottom: 25px; background: #adbeef; }
94 #content{ width: 600px; margin: 15px auto 0 auto; padding: 0 20px 20px 20px; background: #ffffff; border: 1px solid #314864; border-top:1px solid #233447}
95 h1{margin:0 -20px 0 -20px;color:#FFF;font-size:x-large;font-weight:bold;background:#336699;padding:15px 0 15px 0; text-align:center }
96 h2{margin:20px 0 5px 0;color:green;font-size:medium}
97 h3{margin:20px 0 5px 0;color:red;font-size:medium}
98 li{margin:5px}
99 fieldset{padding:10px; text-align:right; margin:0 0 0 0}
100 a{text-decoration:none;color:blue}
101 a:hover{text-decoration:underline}
102 </style>
103 </head>
104 <body>
105   <div id='content'>
106   <h1><small>WordPress &raquo;&raquo;</small> Nucleus CMS <small>&raquo;&raquo; convertor</small></h1>
107
108 <?php
109   if (!isset($_POST['convert'])) {
110 ?>
111   <p>This tool will convert your <a href='http://wordpress.org/'>WordPress blog</a> (tested w/ 3.1.x), into <a href='http://nucleuscms.org'> Nucleus CMS weblog</a>. First, install Nucleus, in a default install, and only after that run this tool. It will transfer all categories, posts, and comments into blog #1.</p>
112 <?php
113   } else {
114
115     $isok=true;
116     
117     // connect to WordPress database
118         $linkwp = @mysql_connect($_POST['wphost'],$_POST['wpusername'],$_POST['wppassword'],true);
119     if($linkwp==false) 
120       error("Cannot connect to WordPress DB..");
121     else {
122           @mysql_select_db($_POST['wpdatabase'],$linkwp) or error("Cannot select WordPress DB...");
123         }
124         
125     // connect to Nucleus CMS database
126         $linkblogcms = @mysql_connect($_POST['blogcmshost'],$_POST['blogcmsusername'],$_POST['blogcmspassword'],true);
127     if($linkblogcms==false) 
128       error("Cannot connect to Nucleus DB..");
129     else {
130           @mysql_select_db($_POST['blogcmsdatabase'],$linkblogcms) or error("Cannot select Nucleus DB...");
131         }
132         
133         if (!$isok) {
134           echo "<p>Please correct these errors first!</p><hr />";
135     } else {
136       /*                 */
137       /* transfer data ! */
138       /*                 */
139       $prefixwp=$_POST['wpprefix'];
140       
141       /* *********************************************** */
142       echo "<h2>Getting encoding...</h2>";
143       $query="select option_value from ".$prefixwp."options where option_name='blog_charset'";
144       $querywp=mysql_query($query,$linkwp) or die($query);
145       if ($row=mysql_fetch_object($querywp)) 
146         $input=$row->option_value;
147       else
148         $input="utf-8";
149       echo "<p>Encoding: $input</p>";
150
151       /* *********************************************** */
152       echo "<h2>Transfering categories...</h2>";
153       mysql_query("delete from ". sql_table('category') . " where cdesc='@wordpress'",$linkblogcms);
154       $q = mysql_query("SELECT count(*) as result FROM " . sql_table('category'));
155       $total_row = mysql_fetch_object($q);
156       $total_num = $total_row->result;
157       $catdd = $total_num;
158       $total_num++;
159
160       $query="SELECT * FROM ".$prefixwp."term_taxonomy , ".$prefixwp."terms WHERE ".$prefixwp."term_taxonomy.term_taxonomy_id = ".$prefixwp."terms.term_id AND ".$prefixwp."term_taxonomy.taxonomy='category'";
161       $querywp=mysql_query($query,$linkwp) or die($query);
162       echo "<p>rows to transfer: ".mysql_num_rows($querywp)."</p>";
163       echo "<p>";
164           $cate_map = array();
165       while ($row=mysql_fetch_object($querywp)) {
166         echo $total_num.", ";
167                 $cate_map[$row->term_id] = $total_num;
168         $query=
169           "INSERT INTO " . sql_table('category') .
170           " (catid,cblog,cname,cdesc)  values (".
171           intval($total_num).",1,'".encoding($row->name)."','@wordpress')";
172         $result = mysql_query($query,$linkblogcms) or die($query);
173         $total_num++;
174       }
175       echo "</p>";
176
177       /* *********************************************** */
178       echo "<h2>Transfering posts and comments...</h2>";
179       mysql_query("delete from " . sql_table('comment') . " where chost='@wordpress'",$linkblogcms);
180       $query="select * from ".$prefixwp."posts where post_status='publish' order by ID";
181       $querywp=mysql_query($query,$linkwp) or die($query);
182       echo "<p>rows to transfer: ".mysql_num_rows($querywp)."</p>";
183       echo "<p>";
184       $i=1;
185       while ($row=mysql_fetch_object($querywp)) {
186         echo $i++.", ";
187                 // find category
188                 $query = 'select * from '.$prefixwp.'term_relationships, '.$prefixwp.'term_taxonomy where '.$prefixwp.'term_relationships.term_taxonomy_id='.$prefixwp.'term_taxonomy.term_taxonomy_id and object_id='.$row->ID.' and '.$prefixwp.'term_taxonomy.taxonomy=\'category\' limit 1';
189                 $catwp = mysql_query($query, $linkwp) or die($query);
190                 $category = mysql_fetch_object($catwp);
191                 $cat = $cate_map[$category->term_id];
192                 if (empty($cat)) $cat = 1;
193
194         // insert post
195         $query=
196           "INSERT INTO " . sql_table('item') . " ".
197           "(ititle,ibody,iblog,iauthor,itime,icat) values (".
198           "'".addslashes(encoding($row->post_title))."','".addslashes(paragraph(encoding(stripslashes($row->post_content)),false))."',1,1,'".$row->post_date."',$cat)";
199         $result = mysql_query($query,$linkblogcms) or die($query);
200         $itemid=mysql_insert_id($linkblogcms);
201
202         // insert comments
203         $query="select * from ".$prefixwp."comments where comment_post_ID=".$row->ID;
204         $querywp_detail=mysql_query($query,$linkwp) or die($query);
205         while ($row_detail=mysql_fetch_object($querywp_detail)) {
206           $url=$row_detail->comment_author_email;
207           if (!empty($row_detail->comment_author_url)) $url=$row_detail->comment_author_url;
208           $query=
209             "INSERT INTO " . sql_table('comment') .
210             " (cbody,cuser,cmail,cmember,citem,ctime,cip,cblog,chost) values (".
211               "'".addslashes(paragraph(encoding(strip_tags(stripslashes($row_detail->comment_content))),true))."',".
212               "'".encoding($row_detail->comment_author)."',".
213               "'$url',".
214               "0,".
215               "$itemid,".
216               "'".$row_detail->comment_date."',".
217               "'".$row_detail->comment_author_IP."',".
218               "1,".
219               "'@wordpress')";
220           $result = mysql_query($query,$linkblogcms) or die($query);
221         }
222       }
223       echo "</p>";
224
225       // done
226       echo "<h2>Done! Enjoy your stay in Nucleus</h2>";
227       die;
228     }
229
230         }
231 ?>
232   <form method='post' action='./wordpress.php'>
233
234     <h2>WordPress (v3.1.x) Database Info</h2>
235     <fieldset><legend>WP info</legend>
236       <label>Host: <input type='text' name='wphost' size='50' value='<?php def('wphost'); ?>' /></label><br />
237       <label>Username: <input type='text' name='wpusername' size='50' value='<?php def('wpusername'); ?>' /></label><br />
238       <label>Password: <input type='text' name='wppassword' size='50' value='<?php def('wppassword'); ?>' /></label><br />
239       <label>Database Name: <input type='text' name='wpdatabase' size='50' value='<?php def('wpdatabase'); ?>' /></label><br />
240       <label>Table Prefix: <input type='text' name='wpprefix' size='50' value='<?php def('wpprefix'); ?>' /></label>
241     </fieldset>
242
243     <h2>New Nucleus:CMS Database Info</h2>
244     <fieldset><legend>Nucleus:CMS info</legend>
245       <label>Host: <input type='text' name='blogcmshost' size='50' value='<?php def('blogcmshost'); ?>' /></label><br />
246       <label>Username: <input type='text' name='blogcmsusername' size='50' value='<?php def('blogcmsusername'); ?>' /></label><br />
247       <label>Password: <input type='text' name='blogcmspassword' size='50' value='<?php def('blogcmspassword'); ?>' /></label><br />
248       <label>Database Name: <input type='text' name='blogcmsdatabase' size='50' value='<?php def('blogcmsdatabase'); ?>' /></label><br />
249     </fieldset>
250     
251     <h2>Submit</h2>
252     <fieldset>
253       <input type='submit' value='Convert data from WP into Nucleus CMS!' name='convert' />
254     </fieldset>
255
256   </form>
257   </div>
258 </body></html>