OSDN Git Service

Merge branch 'skinnable-master'
[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 <<<<<<< HEAD
29 function def($s)
30 {
31         if ( isset($_POST[$s]) )
32                 echo addslashes(stripslashes($_POST[$s]));
33         else
34         {
35                 if ( i18n::strpos($s, 'host') !== FALSE ) echo "localhost";
36                 if ( i18n::strpos($s, 'username') !== FALSE ) echo "root";
37                 if ( i18n::strpos($s, 'wpprefix') !== FALSE ) echo "wp_";
38         }
39 }
40
41 function error($s)
42 {
43         global $isok;
44         $isok = false;
45         echo "<h3>Error: $s</h3>";
46 }
47
48 // line breaks into properly formatted paragraphs
49 function paragraph($text, $br = false)
50 {
51         $text = trim($text);
52         $text = str_replace("\r", '', $text);
53         $text = preg_replace('/&([^#])(?![a-z]{1,8};)/', '&amp;$1', $text);
54         if ( $text == "" ) return "";
55         $text = $text . "\n"; // just to make things a little easier, pad the end
56         $text = preg_replace('|<br/>\s*<br/>|', "\n\n", $text);
57         $text = preg_replace('!(<(?:table|ul|ol|li|pre|form|blockquote|h[1-6])[^>]*>)!', "\n$1", $text); // Space things out a little
58         $text = preg_replace('!(</(?:table|ul|ol|li|pre|form|blockquote|h[1-6])>)!', "$1\n", $text); // Space things out a little
59         $text = preg_replace("/(\r\n|\r)/", "\n", $text); // cross-platform newlines 
60         $text = preg_replace("/\n\n+/", "\n\n", $text); // take care of duplicates
61         $text = preg_replace('/\n?(.+?)(?:\n\s*\n|\z)/s', "\t<p>$1</p>\n", $text); // make paragraphs, including one at the end 
62         $text = preg_replace('|<p>\s*?</p>|', '', $text); // under certain strange conditions it could create a P of entirely whitespace 
63         $text = preg_replace("|<p>(<li.+?)</p>|", "$1", $text); // problem with nested lists
64         // blockquote
65         $text = preg_replace('|<p><blockquote([^>]*)>|i', "<blockquote$1><p>", $text);
66         $text = str_replace('</blockquote></p>', '</p></blockquote>', $text);
67         // now the hard work
68         $text = preg_replace('!<p>\s*(</?(?:table|tr|td|th|div|ul|ol|li|pre|select|form|blockquote|p|h[1-6])[^>]*>)!', "$1", $text);
69         $text = preg_replace('!(</?(?:table|tr|td|th|div|ul|ol|li|pre|select|form|blockquote|p|h[1-6])[^>]*>)\s*</p>|</div>"!', "$1", $text);
70         if ( $br ) $text = preg_replace('|(?<!<br />)\s*\n|', "<br />\n", $text);
71         // optionally make line breaks
72         $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);
73         $text = preg_replace('!<br/>(\s*</?(?:p|li|div|th|pre|td|ul|ol)>)!', '$1', $text);
74         // some cleanup
75         $text = str_replace('</p><br />', '</p>', $text);
76         $text = str_replace("<br />\n</p>", '</p>', $text);
77         return $text;
78 }
79
80 function encoding($s)
81 {
82         global $input;
83         if ( is_callable("iconv") )
84                 return iconv($input, 'utf-8', $s);
85         else
86                 return $s;
87 }
88 =======
89   function def($s){ 
90     if (isset($_POST[$s])) 
91       echo Entity::hsc($_POST[$s]); 
92     else {
93       if (i18n::strpos($s,'host')!==FALSE) echo "localhost";
94       if (i18n::strpos($s,'username')!==FALSE) echo "root";
95       if (i18n::strpos($s,'wpprefix')!==FALSE) echo "wp_";
96     }
97   }
98   
99   function error($s){
100     global $isok;
101     $isok=false;
102     echo "<h3>Error: $s</h3>";
103   }
104   
105   // line breaks into properly formatted paragraphs
106   function paragraph($text, $br = false) {
107     $text=trim($text);
108     $text = str_replace("\r",'',$text);
109     $text = preg_replace('/&([^#])(?![a-z]{1,8};)/', '&amp;$1', $text);
110     if ($text=="") return "";
111     $text = $text . "\n"; // just to make things a little easier, pad the end
112     $text = preg_replace('|<br/>\s*<br/>|', "\n\n", $text);
113     $text = preg_replace('!(<(?:table|ul|ol|li|pre|form|blockquote|h[1-6])[^>]*>)!', "\n$1", $text); // Space things out a little
114     $text = preg_replace('!(</(?:table|ul|ol|li|pre|form|blockquote|h[1-6])>)!', "$1\n", $text); // Space things out a little
115     $text = preg_replace("/(\r\n|\r)/", "\n", $text); // cross-platform newlines 
116     $text = preg_replace("/\n\n+/", "\n\n", $text); // take care of duplicates
117     $text = preg_replace('/\n?(.+?)(?:\n\s*\n|\z)/s', "\t<p>$1</p>\n", $text); // make paragraphs, including one at the end 
118     $text = preg_replace('|<p>\s*?</p>|', '', $text); // under certain strange conditions it could create a P of entirely whitespace 
119     $text = preg_replace("|<p>(<li.+?)</p>|", "$1", $text); // problem with nested lists
120     // blockquote
121     $text = preg_replace('|<p><blockquote([^>]*)>|i', "<blockquote$1><p>", $text);
122     $text = str_replace('</blockquote></p>', '</p></blockquote>', $text);
123     // now the hard work
124     $text = preg_replace('!<p>\s*(</?(?:table|tr|td|th|div|ul|ol|li|pre|select|form|blockquote|p|h[1-6])[^>]*>)!', "$1", $text);
125     $text = preg_replace('!(</?(?:table|tr|td|th|div|ul|ol|li|pre|select|form|blockquote|p|h[1-6])[^>]*>)\s*</p>|</div>"!', "$1", $text); 
126     if ($br) $text = preg_replace('|(?<!<br />)\s*\n|', "<br />\n", $text); // optionally make line breaks
127     $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);
128     $text = preg_replace('!<br/>(\s*</?(?:p|li|div|th|pre|td|ul|ol)>)!', '$1', $text);
129     // some cleanup
130     $text = str_replace('</p><br />','</p>',$text);
131     $text = str_replace("<br />\n</p>",'</p>',$text);
132     return $text; 
133   }
134 >>>>>>> skinnable-master
135
136 ?>
137 <!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Strict//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd'>
138 <html xmlns='http://www.w3.org/1999/xhtml' xml:lang='cs' lang='cs'>
139 <head>
140 <meta http-equiv='Content-Type' content='text/html; charset=utf-8' />
141 <title>WordPress to Nucleus CMS convertor</title>
142 <meta http-equiv='Pragma' content='no-cache' />
143 <meta http-equiv='Cache-Control' content='no-cache, must-revalidate' />
144 <meta http-equiv='Expires' content='-1' />
145 <style type="text/css">
146 body { font-size: 15px; color: #000000; margin: 0; padding: 0; border: 0; font-family: "Trebuchet MS", "Bitstream Vera Sans", verdana, lucida, arial,
147 helvetica, sans-serif; padding-bottom: 25px; background: #adbeef; }
148 #content{ width: 600px; margin: 15px auto 0 auto; padding: 0 20px 20px 20px; background: #ffffff; border: 1px solid #314864; border-top:1px solid #233447}
149 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 }
150 h2{margin:20px 0 5px 0;color:green;font-size:medium}
151 h3{margin:20px 0 5px 0;color:red;font-size:medium}
152 li{margin:5px}
153 fieldset{padding:10px; text-align:right; margin:0 0 0 0}
154 a{text-decoration:none;color:blue}
155 a:hover{text-decoration:underline}
156 </style>
157 </head>
158 <body>
159   <div id='content'>
160   <h1><small>WordPress &raquo;&raquo;</small> Nucleus CMS <small>&raquo;&raquo; convertor</small></h1>
161 <?php
162 if ( !isset($_POST['convert']) )
163 {
164 ?>
165   <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>
166 <?php
167 }
168 else
169 {
170
171         $isok = true;
172
173         // connect to WordPress database
174         $linkwp = @mysql_connect($_POST['wphost'], $_POST['wpusername'], $_POST['wppassword'], true);
175         if ( $linkwp == false )
176                 error("Cannot connect to WordPress DB..");
177         else
178         {
179                 @mysql_select_db($_POST['wpdatabase'], $linkwp) or error("Cannot select WordPress DB...");
180         }
181 <<<<<<< HEAD
182
183         // connect to Nucleus CMS database
184         /*
185         $linkblogcms = @mysql_connect($_POST['blogcmshost'],$_POST['blogcmsusername'],$_POST['blogcmspassword'],true);
186         if($linkblogcms==false) 
187           error("Cannot connect to Nucleus DB..");
188         else {
189           @mysql_select_db($_POST['blogcmsdatabase'],$linkblogcms) or error("Cannot select Nucleus DB...");
190         }
191          */
192
193         if ( !$isok )
194         {
195                 echo "<p>Please correct these errors first!</p><hr />";
196         }
197         else
198         {
199                 /*                 */
200                 /* transfer data ! */
201                 /*                 */
202                 $prefixwp = $_POST['wpprefix'];
203
204                 /* *********************************************** */
205                 echo "<h2>Getting encoding...</h2>";
206                 $query = "SELECT option_value FROM " . $prefixwp . "options WHERE option_name='blog_charset'";
207                 $querywp = mysql_query($query, $linkwp) or die($query);
208                 if ( $row = mysql_fetch_object($querywp) )
209                         $input = $row->option_value;
210                 else
211                         $input = "utf-8";
212                 echo "<p>Encoding: $input</p>";
213
214                 /* *********************************************** */
215                 echo "<h2>Transfering categories...</h2>";
216                 DB::execute("DELETE FROM " . sql_table('category') . " WHERE cdesc='@wordpress'");
217                 $total_num = DB::getValue("SELECT count(*) as result FROM " . sql_table('category'));
218                 $catdd = $total_num;
219                 $total_num++;
220
221                 $query = "SELECT * FROM " . $prefixwp . "term_taxonomy , " . $prefixwp . "terms WHERE " . $prefixwp . "term_taxonomy.term_taxonomy_id = "
222                         . $prefixwp . "terms.term_id AND " . $prefixwp . "term_taxonomy.taxonomy='category'";
223                 $querywp = mysql_query($query, $linkwp) or die($query);
224                 echo "<p>rows to transfer: " . mysql_num_rows($querywp) . "</p>";
225                 echo "<p>";
226                 $cate_map = array();
227                 while ( $row = mysql_fetch_object($querywp) )
228                 {
229                         echo $total_num . ", ";
230                         $cate_map[$row->term_id] = $total_num;
231                         $query = sprintf("INSERT INTO %s (catid,cblog,cname,cdesc) VALUES (%d,1,'%s','@wordpress')"
232                                 , sql_table('category'), intval($total_num), encoding($row->name));
233                         if ( DB::execute($query) === FALSE )
234                         {
235                                 die($query);
236                         }
237                         $total_num++;
238                 }
239                 echo "</p>";
240
241 =======
242
243         // connect to Nucleus CMS database
244         /*
245         $linkblogcms = @mysql_connect($_POST['blogcmshost'],$_POST['blogcmsusername'],$_POST['blogcmspassword'],true);
246         if($linkblogcms==false) 
247           error("Cannot connect to Nucleus DB..");
248         else {
249           @mysql_select_db($_POST['blogcmsdatabase'],$linkblogcms) or error("Cannot select Nucleus DB...");
250         }
251          */
252
253         if ( !$isok )
254         {
255                 echo "<p>Please correct these errors first!</p><hr />";
256         }
257         else
258         {
259                 /*                 */
260                 /* transfer data ! */
261                 /*                 */
262                 $prefixwp = $_POST['wpprefix'];
263
264                 /* *********************************************** */
265                 echo "<h2>Getting encoding...</h2>";
266                 $query = "SELECT option_value FROM " . $prefixwp . "options WHERE option_name='blog_charset'";
267                 $querywp = mysql_query($query, $linkwp) or die($query);
268                 if ( $row = mysql_fetch_object($querywp) )
269                         $input = $row->option_value;
270                 else
271                         $input = "utf-8";
272                 echo "<p>Encoding: $input</p>";
273
274                 /* *********************************************** */
275                 echo "<h2>Transfering categories...</h2>";
276                 DB::execute("DELETE FROM " . sql_table('category') . " WHERE cdesc='@wordpress'");
277                 $total_num = DB::getValue("SELECT count(*) as result FROM " . sql_table('category'));
278                 $catdd = $total_num;
279                 $total_num++;
280
281                 $query = "SELECT * FROM " . $prefixwp . "term_taxonomy , " . $prefixwp . "terms WHERE " . $prefixwp . "term_taxonomy.term_taxonomy_id = "
282                         . $prefixwp . "terms.term_id AND " . $prefixwp . "term_taxonomy.taxonomy='category'";
283                 $querywp = mysql_query($query, $linkwp) or die($query);
284                 echo "<p>rows to transfer: " . mysql_num_rows($querywp) . "</p>";
285                 echo "<p>";
286                 $cate_map = array();
287                 while ( $row = mysql_fetch_object($querywp) )
288                 {
289                         echo $total_num . ", ";
290                         $cate_map[$row->term_id] = $total_num;
291                         $query = sprintf("INSERT INTO %s (catid,cblog,cname,cdesc) VALUES (%d,1,'%s','@wordpress')"
292                                 , sql_table('category'), intval($total_num), encoding($row->name));
293                         if ( DB::execute($query) === FALSE )
294                         {
295                                 die($query);
296                         }
297                         $total_num++;
298                 }
299                 echo "</p>";
300
301 >>>>>>> skinnable-master
302                 /* *********************************************** */
303                 echo "<h2>Transfering posts and comments...</h2>";
304                 DB::execute("DELETE FROM " . sql_table('comment') . " WHERE chost='@wordpress'");
305                 $query = "SELECT * FROM " . $prefixwp . "posts WHERE post_status='publish' ORDER BY ID";
306                 $querywp = mysql_query($query, $linkwp) or die($query);
307                 echo "<p>rows to transfer: " . mysql_num_rows($querywp) . "</p>";
308                 echo "<p>";
309                 $i = 1;
310                 while ( $row = mysql_fetch_object($querywp) )
311                 {
312                         echo $i++ . ", ";
313                         // find category
314                         $query = 'SELECT * FROM ' . $prefixwp . 'term_relationships, ' . $prefixwp . 'term_taxonomy WHERE ' . $prefixwp
315                                 . 'term_relationships.term_taxonomy_id=' . $prefixwp . 'term_taxonomy.term_taxonomy_id AND object_id=' . $row->ID . ' AND '
316                                 . $prefixwp . 'term_taxonomy.taxonomy=\'category\' LIMIT 1';
317                         $catwp = mysql_query($query, $linkwp) or die($query);
318                         $category = mysql_fetch_object($catwp);
319                         $cat = $cate_map[$category->term_id];
320                         if ( empty($cat) ) $cat = 1;
321
322                         // insert post
323                         $query = sprintf("INSERT INTO %s (ititle,ibody,iblog,iauthor,itime,icat) VALUES (%s,%s,1,1,'%s',%d)"
324                                 , sql_table('item')
325                                 , DB::quoteValue(encoding($row->post_title))
326                                 , DB::quoteValue(paragraph(encoding(stripslashes($row->post_content)), false))
327                                 , $row->post_date
328                                 , $cat);
329                         if ( DB::execute($query) === FALSE )
330                         {
331                                 die($query);
332                         }
333                         $itemid = DB::getInsertId();
334
335                         // insert comments
336                         $query = "SELECT * FROM " . $prefixwp . "comments WHERE comment_post_ID=" . $row->ID;
337                         $querywp_detail = mysql_query($query, $linkwp) or die($query);
338                         while ( $row_detail = mysql_fetch_object($querywp_detail) )
339                         {
340                                 $url = $row_detail->comment_author_email;
341                                 if ( !empty($row_detail->comment_author_url) ) $url = $row_detail->comment_author_url;
342                                 $query = sprintf("INSERT INTO %s (cbody,cuser,cmail,cmember,citem,ctime,cip,cblog,chost)"
343                                         . " VALUES (%s,%s,%s,0,%d,'%s',%s,1,'@wordpress')"
344                                         , sql_table('comment')
345                                         , DB::quoteValue(paragraph(encoding(strip_tags(stripslashes($row_detail->comment_content))), true))
346                                         , encoding($row_detail->comment_author)
347                                         , DB::quoteValue($url)
348                                         , $itemid
349                                         , $row_detail->comment_date
350                                         , DB::quoteValue($row_detail->comment_author_IP));
351                                 if ( DB::execute($query) === FALSE )
352                                 {
353                                         die($query);
354                                 }
355                         }
356                 }
357                 echo "</p>";
358
359                 // done
360                 echo "<h2>Done! Enjoy your stay in Nucleus</h2>";
361                 die;
362         }
363
364 }
365 ?>
366   <form method='post' action='./wordpress.php'>
367
368     <h2>WordPress (v3.1.x) Database Info</h2>
369     <fieldset><legend>WP info</legend>
370       <label>Host: <input type='text' name='wphost' size='50' value='<?php def('wphost'); ?>' /></label><br />
371       <label>Username: <input type='text' name='wpusername' size='50' value='<?php def('wpusername'); ?>' /></label><br />
372       <label>Password: <input type='text' name='wppassword' size='50' value='<?php def('wppassword'); ?>' /></label><br />
373       <label>Database Name: <input type='text' name='wpdatabase' size='50' value='<?php def('wpdatabase'); ?>' /></label><br />
374       <label>Table Prefix: <input type='text' name='wpprefix' size='50' value='<?php def('wpprefix'); ?>' /></label>
375     </fieldset>
376
377     <h2>New Nucleus:CMS Database Info</h2>
378     <fieldset><legend>Nucleus:CMS info</legend>
379       <label>Host: <input type='text' name='blogcmshost' size='50' value='<?php def('blogcmshost'); ?>' /></label><br />
380       <label>Username: <input type='text' name='blogcmsusername' size='50' value='<?php def('blogcmsusername'); ?>' /></label><br />
381       <label>Password: <input type='text' name='blogcmspassword' size='50' value='<?php def('blogcmspassword'); ?>' /></label><br />
382       <label>Database Name: <input type='text' name='blogcmsdatabase' size='50' value='<?php def('blogcmsdatabase'); ?>' /></label><br />
383     </fieldset>
384     
385     <h2>Submit</h2>
386     <fieldset>
387       <input type='submit' value='Convert data from WP into Nucleus CMS!' name='convert' />
388     </fieldset>
389
390   </form>
391   </div>
392 </body></html>