OSDN Git Service

a20f493c2b1bae6fac1c75aac86cd1ef0f969b80
[nucleus-jp/nucleus-next.git] / nucleus / convert / livejournal.php
1 <?php
2
3 /*
4  * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)
5  * Copyright (C) 2002-2006 The Nucleus Group
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  * (see nucleus/documentation/index.html#license for more info)
12  */
13
14 /**
15  * This script will import a Live Journal blog into a Nucleus blog, using
16  * a easy to use wizard.
17  *
18  * Notes:
19  *   - Templates are not converted
20  *   - Nucleus should already be installed
21  *       - Members should exist for all teammembers
22  *
23  * @license http://nucleuscms.org/license.txt GNU General Public License
24  * @copyright Copyright (C) 2002-2006 The Nucleus Group
25  * @version $Id: livejournal.php 1624 2012-01-09 11:36:20Z sakamocchi $
26  */
27
28 include("../../config.php");
29 include("functions.inc.php");
30 include($DIR_LIBS . "ADMIN.php");
31 include($DIR_LIBS . "MEDIA.php");
32
33 if (!$member->isLoggedIn()) {
34         convert_showLogin('livejournal.php');
35 }
36
37 if (!$member->isAdmin()) {
38         convert_doError('Only Super-Admins are allowed to perform blog conversions');
39 }
40
41 $ver = convert_getNucleusVersion();
42 if ($ver > 210)
43         convert_doError("You should check the Nucleus website for updates to this convert tool. This one might not work with your current Nucleus installation.");
44
45 // include PRAX lib (to read XML files easily)
46 include ('PRAX.php');
47
48 switch($action) {
49         case "assignMembers":
50                 bc_assignMembers(); break;
51         case "showOverview":
52                 bc_showOverview(); break;
53         case "doConversion":
54                 bc_doConversion(); break;
55         case "login": // drop through
56         default:
57                 bc_getlivejournalID();
58 }
59
60 // step 1: get the Blogger Blog ID
61 function bc_getlivejournalID() {
62         global $HTTP_SERVER_VARS, $PHP_SELF;
63
64         convert_head();
65
66         ?>
67                 <div class="note">
68                 <b>Note:</b> This conversion tool was written for LiveJournal blogs.            </div>
69
70                 <h1>Step 1: Exporting to a file</h1>
71
72                 <p>
73                 The first step in the conversion is to export one month's Live Journal entries into one single file. This can be done with an export tool you can read about in the LiveJournal.com F.A.Q. <a href="http://www.livejournal.com/support/faqbrowse.bml?faqid=8" target="_blank">here</a>. The export tool itself can be found <a href="http://www.livejournal.com/export.bml" target="_blank">here</a>. The settings you will need are as follows:
74 <ul>
75
76 <p><li><b>Format</b>: XML</li>
77 <p><li><b>Encoding</b>: Unicode</li>
78 <p><li><b>Fields</b>:<br></li>
79 <ul><li>Event</li>
80 <li>Subject</li>
81 <li>Event Time</li></ul>
82 <p></b>(all others should be unselected)<br>
83 </ul>
84
85  Save the resulting file as <b>ljent.xml</b>.
86
87
88                 <h2>Importing</h2>
89
90                 <p>
91                 Now you have a file called <b>ljent.xml</b>. Upload it in the same directory as the convert files (/nucleus/convert) and continue to the next step.
92                 </p>
93
94                 <p>
95                 <form method="post" action="livejournal.php">
96                 <input type="submit" value="Next Step: Assign Member" />
97                 <input type="hidden" name="action" value="assignMembers" />
98                 </form>
99                 </p>
100
101         <?php
102
103         convert_foot();
104 }
105
106
107 function bc_assignMembers() {
108         global $HTTP_POST_VARS, $CONF;
109
110         // some checks
111         if (!file_exists('ljent.xml'))
112                 convert_doError("ljent.xml not found. Make sure it is in the correct directory");
113         if (!is_readable('ljent.xml'))
114                 convert_doError("The ljent.xml file is not readable. Make sure the file permissions are set correctly so PHP can access it.");
115
116         convert_head();
117
118         ?>
119                 <form method="post" action="livejournal.php">
120
121
122                 <h1>Step 2: Assign Members to Authors</h1>
123
124                 <p>
125                  Please assign a Nucleus Member to these entries.
126                 </p>
127
128
129                 <table>
130                 <tr>
131                         <th>Blogger Author</th>
132                         <th>Nucleus Member</th>
133                         <th>Blog Admin?</th>
134                 </tr>
135
136                 <?php
137
138                 $blog = new RAX();
139                 $blog->openfile('ljent.xml');
140                 $blog->record_delim = 'entry';
141                 $blog->parse();
142
143
144                 $blog->close(); // close the file
145                 $idx = 0;
146
147 {
148         ?>
149                 <tr>
150                         <td>
151                                 <b>default</b>
152                                 <input name="default" value="default" type="hidden" />
153                         </td>
154                         <td>
155                 <?php
156                         $query =  "SELECT mname as text, mnumber as value"
157                                    . " FROM ".sql_table('member');
158
159                         $template['name'] = 'memberid[' . $idx . ']';
160                         showlist($query,'select',$template);
161                 ?>
162                         </td>
163                         <td>
164                                 <input name="admin[<?php echo $idx?>]" type="checkbox" value="1" id="admin<?php echo $idx?>" /><label for="admin<?php echo $idx?>">Blog Admin</label>
165                         </td>
166                 </tr>
167         <?php
168         $idx++;
169 } // while
170
171
172                 ?>
173                 </table>
174
175
176                 <h1>Choose Destination Weblog</h1>
177
178                 <p>
179                 There are two options: you can either choose an existing blog to add the Live Journal entries into, or you can choose to create a new weblog.
180                 </p>
181
182                 <div>
183                         <input name="createnew" value="0" type="radio" checked='checked' id="createnew_no" /><label for="createnew_no">Choose existing weblog to add to:</label>
184
185                         <?php
186                                         $query =  "SELECT bname as text, bnumber as value"
187                                                    . " FROM ".sql_table('blog');
188                                         $template['name'] = 'blogid';
189                                         $template['selected'] = $CONF['DefaultBlog'];
190                                         showlist($query,'select',$template);
191                         ?>
192                 </div>
193                 <div>
194                         <input name="createnew" value="1" type="radio" id="createnew_yes" /><label for="createnew_yes">Create new weblog</label>
195                         <ul>
196                                 <li>New blog name: <input name="newblogname" /></li>
197                                 <li>Blog owner:
198                                 <?php
199                                         $query =  "SELECT mname as text, mnumber as value"
200                                                    . " FROM ".sql_table('member');
201
202                                         $template['name'] = 'newowner';
203                                         showlist($query,'select',$template);
204                                 ?>
205                                 </li>
206                         </ul>
207                 </div>
208
209                 <h1>Do the conversion!</h1>
210                 <p>
211                 <input type="hidden" name="authorcount" value="<?php echo $idx?>" />
212                 <input type="submit" value="Step 3: Do the conversion!" />
213                 <input type="hidden" name="action" value="doConversion" />
214                 </p>
215
216                 <div class="note">
217                 <b>Note:</b> Clicking the button once is enough, even if it takes a while to complete.
218                 </div>
219
220                 </form>
221         <?php
222
223         convert_foot();
224
225 }
226
227
228 function bc_doConversion() {
229         global $HTTP_POST_VARS, $manager;
230
231         // 1. get all data
232         $authorcount = intval($HTTP_POST_VARS['authorcount']);
233         $author = $HTTP_POST_VARS['author'];
234
235         for ($i=0;$i<$authorcount;$i++) {
236                 $key = $author[$i];
237                 $memberid[$key] = intval($HTTP_POST_VARS['memberid'][$i]);
238                 $isadmin[$key] = intval($HTTP_POST_VARS['admin'][$i]);
239         }
240
241         $createnew = intval($HTTP_POST_VARS['createnew']);
242         $newblogname = stripslashes($HTTP_POST_VARS['newblogname']);
243         $newowner = intval($HTTP_POST_VARS['newowner']);
244
245         $nucleus_blogid = intval($HTTP_POST_VARS['blogid']);
246
247
248         // 2. check data
249
250         convert_head();
251
252         ?>
253                 <h1>Step 3: Converting...</h1>
254
255                 <p>
256                 Please be patient. Don't hit reload! The conversion progress should be showing below.
257                 </p>
258         <?php
259
260         // try to extend time limit
261         // surpress error messages when not allowed to do so
262         @set_time_limit(1200);
263
264         echo "<pre>";
265
266         echo "Authors: <br/>";
267         for ($i=0;$i<$authorcount;$i++) {
268                 echo  "\tAuthor=" . $author[$i];
269                 echo " ID=" . $memberid[$author[$i]];
270                 echo " ADMIN=" . $isadmin[$author[$i]];
271                 echo "<br />";
272         }
273         echo "Create New Weblog = $createnew (name='$newblogname', owner=$newowner, dest=$nucleus_blogid)<br />";
274
275         echo "</pre>";
276
277         // create blog
278         if ($createnew == 1) {
279                 // choose unique name
280                 $shortname = 'blogger';
281                 if (Blog::exists($shortname)) {
282                         $idx = 1;
283                         while (Blog::exists($shortname . $idx))
284                                 $idx++;
285                         $shortname = $shortname . $idx;
286                 }
287
288                 $nucleus_blogid = convert_addToBlog($newblogname, $shortname, $newowner);
289                 echo "<pre>New blog created</pre>";
290         }
291
292         // add authors to blog team
293         $b =& $manager->getBlog($nucleus_blogid);
294         global $catid;
295         $catid = $b->getDefaultCategory();
296
297         for ($i=0;$i<$authorcount;$i++)
298                 $b->addTeamMember($memberid[$author[$i]],$isadmin[$author[$i]]);
299
300
301         // 3. go through ljent.xml file
302
303         $blog = new RAX();
304         $blog->openfile('ljent.xml');
305         $blog->record_delim = 'entry';
306         $blog->parse();
307
308         while ( $entry = $blog->readRecord() )  {
309                 $row = $entry->getRow();
310
311                 bc_convertOneItem($row, $memberid, $nucleus_blogid);
312         }
313
314         $blog->close(); // close the file
315
316         echo "<pre>All done!</pre>";
317
318         convert_foot();
319
320 }
321
322 function bc_convertOneItem($row, $memberid, $nucleus_blogid) {
323         global $catid;
324
325         $nucl_id = $memberid[$row['author']];
326
327         $timestamp = date("Y-m-d H:i:s",bc_transformDate($row['eventtime']));
328
329         echo "<pre>";
330         echo "<b>entry</b>:" .  Entity::hsc(substr($row['event'],0,20)) . "...(time: " . $timestamp . ") \n";
331         echo "<b>author</b>: " . $row['author'] ;
332         echo " (nucleus-id: " . $nucl_id . ")";
333         echo "</pre>";
334
335         $title = $row['subject']; // use the title when it is in the XML file
336         convert_addToItem($title, $row['event'], '', $nucleus_blogid, $nucl_id, $timestamp, 0, $catid, 0, 0);
337
338 }
339
340 function bc_transformDate($eventtime) {
341 // 2003-07-03 23:59:00
342 if (eregi("(.*)-(.*)-(.*) (.*):(.*):(.*)",$eventtime,$regs) != false) {
343 return mktime($regs[4],$regs[5],$regs[6],$regs[2],$regs[3],$regs[1]);
344 } else {
345 return 0;
346 }
347 }
348
349
350
351
352
353 ?>