source: trunk/content/plugins/pligg_importer/pligg_importer.php @ 1081

Revision 1081, 14.9 KB checked in by nick_ramsay, 3 years ago (diff)

[trunk] Copied branch to loacl trunk folder, updating to Hotaru 1.0.5 from there.

Line 
1<?php
2/**
3 * name: Pligg Importer
4 * description: Imports and converts a Pligg database to Hotaru CMS
5 * version: 0.8
6 * folder: pligg_importer
7 * class: PliggImporter
8 * hooks: admin_plugin_settings, admin_sidebar_plugin_settings, admin_header_include
9 * author: Nick Ramsay
10 * authorurl: http://hotarucms.org/member.php?1-Nick
11 *
12 * PHP version 5
13 *
14 * LICENSE: Hotaru CMS is free software: you can redistribute it and/or
15 * modify it under the terms of the GNU General Public License as
16 * published by the Free Software Foundation, either version 3 of
17 * the License, or (at your option) any later version.
18 *
19 * Hotaru CMS is distributed in the hope that it will be useful, but WITHOUT
20 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21 * FITNESS FOR A PARTICULAR PURPOSE.
22 *
23 * You should have received a copy of the GNU General Public License along
24 * with Hotaru CMS. If not, see http://www.gnu.org/licenses/.
25 *
26 * @category  Content Management System
27 * @package   HotaruCMS
28 * @author    Nick Ramsay <admin@hotarucms.org>
29 * @copyright Copyright (c) 2009, Hotaru CMS
30 * @license   http://www.gnu.org/copyleft/gpl.html GNU General Public License
31 * @link      http://www.hotarucms.org/
32 */
33
34
35class PliggImporter
36{
37   
38    /**
39     * Pligg Importer page in Admin Settings
40     */
41    public function admin_plugin_settings($h)
42    {
43        $pliggimp_path = PLUGINS . "/pligg_importer/";
44       
45        include_once($pliggimp_path . "pliggimp_categories.php");
46        include_once($pliggimp_path . "pliggimp_links.php");
47        include_once($pliggimp_path . "pliggimp_comments.php");
48        include_once($pliggimp_path . "pliggimp_tags.php");
49        include_once($pliggimp_path . "pliggimp_users.php");
50        include_once($pliggimp_path . "pliggimp_votes.php");
51   
52        if ($h->cage->post->testAlpha('submitted'))
53        {
54            // Save uploaded file and show the result
55            $file_name = $this->save_uploaded_file($h);
56            $table = $h->cage->post->testAlpha('table');
57            $this->upload_result($h, $file_name, $table);
58        }
59        elseif ($h->cage->get->keyExists('cleaner'))
60        {
61            $this->character_cleaner($h);
62        }
63        elseif ($h->cage->get->keyExists('step'))
64        {
65            $step = $h->cage->get->testInt('step');
66            $file_name = $h->cage->get->getMixedString2('file_name');
67           
68            if (!isset($file_name) || !$file_name) {
69                // Go to page
70                $function_name = "page_" . $step;
71               
72                if ($step == 7) {
73                    $this->$function_name($h);
74                } else {
75                    $class_name = "PliggImp" . $step;
76                    $classStep = new $class_name();
77                    $classStep->$function_name($h);
78                }
79            } else {
80                // Upload file
81                $this->process_file($h, $step, $file_name);
82            }
83        }
84        else
85        {
86            $this->page_welcome($h);    // Page One - welcome and instructions
87        }
88       
89        return true;
90    }
91   
92   
93    /**
94     * Page 1 - welcome message, instructions and checks plugins are active
95     */
96    public function page_welcome($h)
97    {
98        // FIRST PAGE WITH UPLOAD FORM
99        echo "<div id='pliggimp'>";
100        echo "<h2>Welcome to the Pligg Importer</h2>";
101        echo "<p>This plugin will attempt to import a Pligg or SWCMS site into Hotaru CMS. <b>Be warned, if you have a large website, it can take a long time to do the import and you may exceed various time and size limits on your server</b>. Please read through the <a href='http://hotarucms.org/showpost.php?p=472'>Pligg Importer forum thread</a> for suggestions.</p>";
102       
103        echo "<table>";
104        echo "<tr><td id='before_begin'>Before you begin, you'll need to...</td></tr>";
105       
106        echo "<tr><td><span class='bold_red'>1.</span> Export the following Pligg database tables as <b>XML files</b>. If using phpMyAdmin, go to each table and click the \"Export\" tab. Then select \"XML\" and save with no compression.: <i>categories, links, comments, tags, users</i> and <i>votes</i></td></tr>";
107       
108        echo "<tr><td><span class='bold_red'>3.</span> Make sure your Pligg username is at least 4 characters. Learn more about that <a href='http://hotarucms.org/showpost.php?p=472&postcount=9'>here in the forums</a>.</td></tr>";
109       
110        echo "<tr><td><span class='bold_red'>2.</span> Change your Hotaru profile. So you don't delete yourself when importing the Users data, make sure your Hotaru username and password match your old 'god' account from Pligg. </td></tr>";
111
112        echo "<tr><td><span class='bold_red'>4.</span> Make sure that the \"uploads\" folder in the <i>pligg_importer</i> folder is writable (chmod 777)</td></tr>";
113
114        echo "</table>";
115         
116        echo "<h2 style='color: green;'>Click \"Import a Pligg Database\" to begin...</b></h2><br />";
117        echo "<a class='pliggimp_next' href='" . BASEURL . "admin_index.php?page=plugin_settings&amp;plugin=pligg_importer&amp;step=1'>Import a Pligg Database</a><br /><br />";
118       
119        echo "<h2>Character Cleaner</h2><br />";
120        echo "The Character Cleaner should be used <b><i>after</i></b> importing a Pligg database and <b><i>only</i></b> if you are having trouble with strange characters in posts. What it does is simply strip common problem characters from post titles and content. <b>Note: </b> This script may take some time to run depending on the size of your database.";
121        echo "<br /><a class='pliggimp_next' href='" . BASEURL . "admin_index.php?page=plugin_settings&amp;plugin=pligg_importer&amp;cleaner=1'>Run the Character Cleaner</a>";
122       
123        echo "</div> <!-- close pliggimp div -->";
124    }
125   
126   
127    /**
128     * Show the result of the file upload and offer link to continue
129     *
130     * @param str $file_name
131     * @param str $table table name
132     */
133    public function upload_result($h, $file_name, $table)
134    {
135            echo "<h2>Upload Results</h2>";
136            echo "<div>";
137           
138            switch($table) {
139                case "Categories":
140                    $step = 1;
141                    break;
142                case "Links":
143                    $step = 2;
144                    break;
145                case "Comments":
146                    $step = 3;
147                    break;
148                case "Tags":
149                    $step = 4;
150                    break;
151                case "Users":
152                    $step = 5;
153                    break;
154                case "Votes":
155                    $step = 6;
156                    break;
157                default:
158                    $step = 0;
159                    break;
160            }
161           
162            if ($file_name)
163            {
164                echo "<span style='color: green;'><i>" . $file_name . "</i> has been uploaded successfully.</span> <a class='pliggimp_next' href='" . BASEURL . "admin_index.php?page=plugin_settings&amp;plugin=pligg_importer&amp;file_name=" . $file_name . "&amp;" . "step=" . $step . "'>Continue</a>";
165               
166                echo "<h2>Click \"Continue\" to start the import</h2>";
167                echo "Please note that large tables with thousands of records may take some time to import, and <b>any existing data will be overwritten.</b>";
168            }
169            else
170            {
171                echo "<span style='color: red;'>Import aborted.</span> <a href='" . BASEURL . "admin_index.php?page=plugin_settings&amp;plugin=pligg_importer'>Click here</a> to return to the start.";
172            }
173            echo "</div>";
174    }
175   
176   
177    /**
178     * Upload and save the file in the plugin's uploads folder
179     *
180     * @return string|false
181     */
182    public function save_uploaded_file($h)
183    {
184        /* *****************************
185         * ****************************/
186         
187         // EDIT THIS TO INCREASE FILE SIZE LIMIT
188        $size_limit = 104857600; // 100MB
189       
190        /* *****************************
191         * ****************************/
192         
193        $tmp_filepath = $h->cage->files->getRaw('/file/tmp_name');
194        $file_name = basename($h->cage->files->getMixedString2('/file/name'));
195        $file_type = $h->cage->files->testPage('/file/type');
196        $file_size = $h->cage->files->testInt('/file/size');
197        $file_error = $h->cage->files->testInt('/file/error');
198        $destination = PLUGINS . "pligg_importer/uploads/";
199       
200        if ($file_type == "text/xml" && $file_size < $size_limit)
201        {
202            if ($file_error > 0)
203            {
204                $h->message = "Error: code " . $file_error;
205                $h->messageType = "red";
206                $h->showMessage();
207                return false;
208            }
209            else
210            {
211                if (!move_uploaded_file($tmp_filepath, $destination . $file_name)) {
212                    $h->message = "Failed to move the file to the pligg_importer uploads folder.";
213                    $h->messageType = "red";
214                    $h->showMessage();
215                    return false;
216                }
217               
218                $h->message = "Uploaded succesfully!";
219                $h->messageType = "green";
220                $h->showMessage();
221                $h->vars['status'] = "uploaded";
222                           
223                return $file_name;
224            }
225        }
226        else
227        {   
228            if ($file_type != "text/xml") {
229                $h->message = "Invalid file: Must be <i>text/xml</i>";
230            } elseif ($file_size >= $size_limit) {
231                $h->message = "Invalid file: Exceeded " .
232                    display_filesize($file_size);
233            }
234           
235            $h->messageType = "red";
236            $h->showMessage();
237            return false;
238        }
239    }
240   
241    /**
242     * Import data into the database from an XML file
243     *
244     * @param int $step
245     * @param str $file_name
246     */
247    public function process_file($h, $step = 0, $file_name = '')
248    {
249        $uploads_folder = PLUGINS . "pligg_importer/uploads/";
250        $xml = simplexml_load_file($uploads_folder . $file_name);
251   
252        echo "<h2>Importing data from <i>" . $xml->getName() . "</i></h2>";
253           
254        switch($step) {
255            case 1:
256                $this->create_temp_table($h);
257                $cats = new PliggImp1();
258                $cats->step1($h, $xml, $file_name);
259                break;
260            case 2:
261                $links = new PliggImp2();
262                $links->step2($h, $xml, $file_name);
263                break;
264            case 3:
265                $comms = new PliggImp3();
266                $comms->step3($h, $xml, $file_name);
267                break;
268            case 4:
269                $tags = new PliggImp4();
270                $tags->step4($h, $xml, $file_name);
271                break;
272            case 5:
273                $users = new PliggImp5();
274                $users->step5($h, $xml, $file_name);
275                break;
276            case 6:
277                $votes = new PliggImp6();
278                $votes->step6($h, $xml, $file_name);
279                break;
280            default:
281                break;
282        }
283       
284        echo "<br /><br />";
285   
286    }
287   
288   
289    /**
290     * Create a temporary table
291     */
292    function create_temp_table($h)
293    {
294        // PLIGGIMP_TEMP TABLE - stores mappings between old and new data.
295       
296        // Drop and rebuild the table if it already exists
297        $sql = 'DROP TABLE IF EXISTS `' . DB_PREFIX . 'pliggimp_temp`;';
298        $h->db->query($sql);
299       
300        $sql = "CREATE TABLE `" . DB_PREFIX . "pliggimp_temp` (
301          `pliggimp_id` int(20) NOT NULL AUTO_INCREMENT PRIMARY KEY,
302          `pliggimp_setting` varchar(64) NOT NULL,
303          `pliggimp_old_value` int(20) NOT NULL DEFAULT 0,
304          `pliggimp_new_value` int(20) NOT NULL DEFAULT 0,
305          `pliggimp_updatedts` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
306          `pliggimp_updateby` int(20) NOT NULL DEFAULT 0
307        ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci COMMENT='Pligg Importer Temporary Data';";
308   
309        $h->db->query($sql);
310    }
311       
312    /**
313     * Import complete
314     */
315    public function page_7($h)
316    {
317        // Drop the pliggimp_temp table...
318        $sql = 'DROP TABLE IF EXISTS `' . DB_PREFIX . 'pliggimp_temp`;';
319        $h->db->query($sql);
320       
321        // Delete the uploaded XML files
322        $h->deleteFiles(PLUGINS . 'pligg_importer/uploads/');
323       
324        // Import Complete
325       
326        $h->message = "Congratulations! You have completed the import.";
327        $h->messageType = 'green';
328        $h->showMessage();
329       
330        echo "<div id='pliggimp'>";
331        echo "<h2>Pligg Import Complete</h2>";
332        echo "<p>Before you take a look at your site, please understand that because Hotaru CMS is different form Pligg in many ways, not everything from your Pligg database could be included. However, most of your content has been imported so your site shouldn't be too disrupted by the changeover. <b>Okay, we're done, go take a look!</b></p>";
333        echo "</div>";
334    }
335   
336   
337    /**
338     * Corrects botched utf-8 content
339     */
340    function character_cleaner($h)
341    {
342        $sql = "SELECT post_id, post_title, post_content FROM " . TABLE_POSTS;
343        $content = $h->db->get_results($h->db->prepare($sql));
344               
345        if ($content) {
346            foreach ($content as $item) {
347                $item->post_title = strip_foreign_characters(urldecode($item->post_title));
348                $item->post_content = strip_foreign_characters(urldecode($item->post_content));
349                           
350                $sql = "UPDATE " . TABLE_POSTS . " SET post_title = %s, post_content = %s WHERE post_id = %d";
351                $h->db->query($h->db->prepare($sql, urlencode($item->post_title), urlencode($item->post_content), $item->post_id));
352            }
353        }
354       
355        $sql = "SELECT comment_id, comment_content FROM " . TABLE_COMMENTS;
356        $content = $h->db->get_results($h->db->prepare($sql));
357               
358        if ($content) {
359            foreach ($content as $item) {
360                $item->comment_content = strip_foreign_characters(urldecode($item->comment_content));
361                           
362                $sql = "UPDATE " . TABLE_COMMENTS . " SET comment_content = %s WHERE comment_id = %d";
363                $h->db->query($h->db->prepare($sql, urlencode($item->comment_content), $item->comment_id));
364            }
365        }
366       
367        $h->message = "Post titles and descriptions updated";
368        $h->messageType = "green";
369        $h->showMessage();
370    }
371}
372
373?>
Note: See TracBrowser for help on using the repository browser.