OSDN Git Service

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