Dotclear

source: plugins/importExport/inc/flat/class.flat.import.php @ 3756:66ea0528142b

Revision 3756:66ea0528142b, 33.7 KB checked in by franck <carnet.franck.paul@…>, 7 years ago (diff)

Missed these one (see [3754])

Line 
1<?php
2/**
3 * @brief importExport, a plugin for Dotclear 2
4 *
5 * @package Dotclear
6 * @subpackage Plugins
7 *
8 * @copyright Olivier Meunier & Association Dotclear
9 * @copyright GPL-2.0-only
10 */
11
12if (!defined('DC_RC_PATH')) {return;}
13
14class flatImport extends flatBackup
15{
16    private $core;
17    private $con;
18    private $prefix;
19
20    private $dc_version;
21    private $dc_major;
22    private $mode;
23
24    private $blog_url;
25    private $blog_name;
26    private $blog_desc;
27
28    private $users = array();
29
30    public $old_ids = array(
31        'category' => array(),
32        'post'     => array(),
33        'media'    => array()
34    );
35
36    public $stack = array(
37        'categories' => null,
38        'cat_id'     => 1,
39        'cat_lft'    => array(),
40        'post_id'    => 1,
41        'media_id'   => 1,
42        'comment_id' => 1,
43        'link_id'    => 1,
44        'log_id'     => 1
45    );
46
47    public $has_categories = false;
48
49    public function __construct($core, $file)
50    {
51        parent::__construct($file);
52
53        $first_line = fgets($this->fp);
54        if (strpos($first_line, '///DOTCLEAR|') !== 0) {
55            throw new Exception(__('File is not a DotClear backup.'));
56        }
57
58        @set_time_limit(300);
59
60        $l = explode('|', $first_line);
61
62        if (isset($l[1])) {
63            $this->dc_version = $l[1];
64        }
65
66        $this->mode = isset($l[2]) ? strtolower(trim($l[2])) : 'single';
67        if ($this->mode != 'full' && $this->mode != 'single') {
68            $this->mode = 'single';
69        }
70
71        if (version_compare('1.2', $this->dc_version, '<=') &&
72            version_compare('1.3', $this->dc_version, '>')) {
73            $this->dc_major_version = '1.2';
74        } else {
75            $this->dc_major_version = '2.0';
76        }
77
78        $this->core   = &$core;
79        $this->con    = &$core->con;
80        $this->prefix = $core->prefix;
81
82        $this->cur_blog        = $this->con->openCursor($this->prefix . 'blog');
83        $this->cur_category    = $this->con->openCursor($this->prefix . 'category');
84        $this->cur_link        = $this->con->openCursor($this->prefix . 'link');
85        $this->cur_setting     = $this->con->openCursor($this->prefix . 'setting');
86        $this->cur_user        = $this->con->openCursor($this->prefix . 'user');
87        $this->cur_pref        = $this->con->openCursor($this->prefix . 'pref');
88        $this->cur_permissions = $this->con->openCursor($this->prefix . 'permissions');
89        $this->cur_post        = $this->con->openCursor($this->prefix . 'post');
90        $this->cur_meta        = $this->con->openCursor($this->prefix . 'meta');
91        $this->cur_media       = $this->con->openCursor($this->prefix . 'media');
92        $this->cur_post_media  = $this->con->openCursor($this->prefix . 'post_media');
93        $this->cur_log         = $this->con->openCursor($this->prefix . 'log');
94        $this->cur_ping        = $this->con->openCursor($this->prefix . 'ping');
95        $this->cur_comment     = $this->con->openCursor($this->prefix . 'comment');
96        $this->cur_spamrule    = $this->con->openCursor($this->prefix . 'spamrule');
97        $this->cur_version     = $this->con->openCursor($this->prefix . 'version');
98
99        # --BEHAVIOR-- importInit
100        $this->core->callBehavior('importInit', $this, $this->core);
101    }
102
103    public function getMode()
104    {
105        return $this->mode;
106    }
107
108    public function importSingle()
109    {
110        if ($this->mode != 'single') {
111            throw new Exception(__('File is not a single blog export.'));
112        }
113
114        if (!$this->core->auth->check('admin', $this->core->blog->id)) {
115            throw new Exception(__('Permission denied.'));
116        }
117
118        $this->blog_id = $this->core->blog->id;
119
120        $this->stack['categories'] = $this->con->select(
121            'SELECT cat_id, cat_title, cat_url ' .
122            'FROM ' . $this->prefix . 'category ' .
123            "WHERE blog_id = '" . $this->con->escape($this->blog_id) . "' "
124        );
125
126        $rs                    = $this->con->select('SELECT MAX(cat_id) FROM ' . $this->prefix . 'category');
127        $this->stack['cat_id'] = ((integer) $rs->f(0)) + 1;
128
129        $rs                     = $this->con->select('SELECT MAX(link_id) FROM ' . $this->prefix . 'link');
130        $this->stack['link_id'] = ((integer) $rs->f(0)) + 1;
131
132        $rs                     = $this->con->select('SELECT MAX(post_id) FROM ' . $this->prefix . 'post');
133        $this->stack['post_id'] = ((integer) $rs->f(0)) + 1;
134
135        $rs                      = $this->con->select('SELECT MAX(media_id) FROM ' . $this->prefix . 'media');
136        $this->stack['media_id'] = ((integer) $rs->f(0)) + 1;
137
138        $rs                        = $this->con->select('SELECT MAX(comment_id) FROM ' . $this->prefix . 'comment');
139        $this->stack['comment_id'] = ((integer) $rs->f(0)) + 1;
140
141        $rs                    = $this->con->select('SELECT MAX(log_id) FROM ' . $this->prefix . 'log');
142        $this->stack['log_id'] = ((integer) $rs->f(0)) + 1;
143
144        $rs = $this->con->select(
145            'SELECT MAX(cat_rgt) AS cat_rgt FROM ' . $this->prefix . 'category ' .
146            "WHERE blog_id = '" . $this->con->escape($this->core->blog->id) . "'"
147        );
148
149        if ((integer) $rs->cat_rgt > 0) {
150            $this->has_categories                          = true;
151            $this->stack['cat_lft'][$this->core->blog->id] = (integer) $rs->cat_rgt + 1;
152        }
153
154        $this->con->begin();
155
156        try
157        {
158            $last_line_name = '';
159            $constrained    = array('post', 'meta', 'post_media', 'ping', 'comment');
160
161            while (($line = $this->getLine()) !== false) {
162                # import DC 1.2.x, we fix lines before insert
163                if ($this->dc_major_version == '1.2') {
164                    $this->prepareDC12line($line);
165                }
166
167                if ($last_line_name != $line->__name) {
168                    if (in_array($last_line_name, $constrained)) {
169                        # UNDEFER
170                        if ($this->con->syntax() == 'mysql') {
171                            $this->con->execute('SET foreign_key_checks = 1');
172                        }
173
174                        if ($this->con->syntax() == 'postgresql') {
175                            $this->con->execute('SET CONSTRAINTS ALL DEFERRED');
176                        }
177
178                    }
179
180                    if (in_array($line->__name, $constrained)) {
181                        # DEFER
182                        if ($this->con->syntax() == 'mysql') {
183                            $this->con->execute('SET foreign_key_checks = 0');
184                        }
185
186                        if ($this->con->syntax() == 'postgresql') {
187                            $this->con->execute('SET CONSTRAINTS ALL IMMEDIATE');
188                        }
189
190                    }
191
192                    $last_line_name = $line->__name;
193                }
194
195                switch ($line->__name) {
196                    case 'category':
197                        $this->insertCategorySingle($line);
198                        break;
199                    case 'link':
200                        $this->insertLinkSingle($line);
201                        break;
202                    case 'post':
203                        $this->insertPostSingle($line);
204                        break;
205                    case 'meta':
206                        $this->insertMetaSingle($line);
207                        break;
208                    case 'media':
209                        $this->insertMediaSingle($line);
210                        break;
211                    case 'post_media':
212                        $this->insertPostMediaSingle($line);
213                        break;
214                    case 'ping':
215                        $this->insertPingSingle($line);
216                        break;
217                    case 'comment':
218                        $this->insertCommentSingle($line);
219                        break;
220                }
221
222                # --BEHAVIOR-- importSingle
223                $this->core->callBehavior('importSingle', $line, $this, $this->core);
224            }
225
226            if ($this->con->syntax() == 'mysql') {
227                $this->con->execute('SET foreign_key_checks = 1');
228            }
229
230            if ($this->con->syntax() == 'postgresql') {
231                $this->con->execute('SET CONSTRAINTS ALL DEFERRED');
232            }
233
234        } catch (Exception $e) {
235            @fclose($this->fp);
236            $this->con->rollback();
237            throw new Exception($e->getMessage() . ' - ' . sprintf(__('Error raised at line %s'), $line->__line));
238        }
239        @fclose($this->fp);
240        $this->con->commit();
241    }
242
243    public function importFull()
244    {
245        if ($this->mode != 'full') {
246            throw new Exception(__('File is not a full export.'));
247        }
248
249        if (!$this->core->auth->isSuperAdmin()) {
250            throw new Exception(__('Permission denied.'));
251        }
252
253        $this->con->begin();
254        $this->con->execute('DELETE FROM ' . $this->prefix . 'blog');
255        $this->con->execute('DELETE FROM ' . $this->prefix . 'media');
256        $this->con->execute('DELETE FROM ' . $this->prefix . 'spamrule');
257        $this->con->execute('DELETE FROM ' . $this->prefix . 'setting');
258        $this->con->execute('DELETE FROM ' . $this->prefix . 'log');
259
260        try
261        {
262            while (($line = $this->getLine()) !== false) {
263                switch ($line->__name) {
264                    case 'blog':
265                        $this->insertBlog($line);
266                        break;
267                    case 'category':
268                        $this->insertCategory($line);
269                        break;
270                    case 'link':
271                        $this->insertLink($line);
272                        break;
273                    case 'setting':
274                        $this->insertSetting($line);
275                        break;
276                    case 'user':
277                        $this->insertUser($line);
278                        break;
279                    case 'pref':
280                        $this->insertPref($line);
281                        break;
282                    case 'permissions':
283                        $this->insertPermissions($line);
284                        break;
285                    case 'post':
286                        $this->insertPost($line);
287                        break;
288                    case 'meta':
289                        $this->insertMeta($line);
290                        break;
291                    case 'media':
292                        $this->insertMedia($line);
293                        break;
294                    case 'post_media':
295                        $this->insertPostMedia($line);
296                        break;
297                    case 'log';
298                        $this->insertLog($line);
299                        break;
300                    case 'ping':
301                        $this->insertPing($line);
302                        break;
303                    case 'comment':
304                        $this->insertComment($line);
305                        break;
306                    case 'spamrule':
307                        $this->insertSpamRule($line);
308                        break;
309                }
310                # --BEHAVIOR-- importFull
311                $this->core->callBehavior('importFull', $line, $this, $this->core);
312            }
313        } catch (Exception $e) {
314            @fclose($this->fp);
315            $this->con->rollback();
316            throw new Exception($e->getMessage() . ' - ' . sprintf(__('Error raised at line %s'), $line->__line));
317        }
318        @fclose($this->fp);
319        $this->con->commit();
320    }
321
322    private function insertBlog($blog)
323    {
324        $this->cur_blog->clean();
325
326        $this->cur_blog->blog_id     = (string) $blog->blog_id;
327        $this->cur_blog->blog_uid    = (string) $blog->blog_uid;
328        $this->cur_blog->blog_creadt = (string) $blog->blog_creadt;
329        $this->cur_blog->blog_upddt  = (string) $blog->blog_upddt;
330        $this->cur_blog->blog_url    = (string) $blog->blog_url;
331        $this->cur_blog->blog_name   = (string) $blog->blog_name;
332        $this->cur_blog->blog_desc   = (string) $blog->blog_desc;
333
334        $this->cur_blog->blog_status = $blog->exists('blog_status') ? (integer) $blog->blog_status : 1;
335
336        $this->cur_blog->insert();
337    }
338
339    private function insertCategory($category)
340    {
341        $this->cur_category->clean();
342
343        $this->cur_category->cat_id    = (string) $category->cat_id;
344        $this->cur_category->blog_id   = (string) $category->blog_id;
345        $this->cur_category->cat_title = (string) $category->cat_title;
346        $this->cur_category->cat_url   = (string) $category->cat_url;
347        $this->cur_category->cat_desc  = (string) $category->cat_desc;
348
349        if (!$this->has_categories && $category->exists('cat_lft') && $category->exists('cat_rgt')) {
350            $this->cur_category->cat_lft = (integer) $category->cat_lft;
351            $this->cur_category->cat_rgt = (integer) $category->cat_rgt;
352        } else {
353            if (!isset($this->stack['cat_lft'][$category->blog_id])) {
354                $this->stack['cat_lft'][$category->blog_id] = 2;
355            }
356            $this->cur_category->cat_lft = $this->stack['cat_lft'][$category->blog_id]++;
357            $this->cur_category->cat_rgt = $this->stack['cat_lft'][$category->blog_id]++;
358        }
359
360        $this->cur_category->insert();
361    }
362
363    private function insertLink($link)
364    {
365        $this->cur_link->clean();
366
367        $this->cur_link->link_id       = (integer) $link->link_id;
368        $this->cur_link->blog_id       = (string) $link->blog_id;
369        $this->cur_link->link_href     = (string) $link->link_href;
370        $this->cur_link->link_title    = (string) $link->link_title;
371        $this->cur_link->link_desc     = (string) $link->link_desc;
372        $this->cur_link->link_lang     = (string) $link->link_lang;
373        $this->cur_link->link_xfn      = (string) $link->link_xfn;
374        $this->cur_link->link_position = (integer) $link->link_position;
375
376        $this->cur_link->insert();
377    }
378
379    private function insertSetting($setting)
380    {
381        $this->cur_setting->clean();
382
383        $this->cur_setting->setting_id    = (string) $setting->setting_id;
384        $this->cur_setting->blog_id       = !$setting->blog_id ? null : (string) $setting->blog_id;
385        $this->cur_setting->setting_ns    = (string) $setting->setting_ns;
386        $this->cur_setting->setting_value = (string) $setting->setting_value;
387        $this->cur_setting->setting_type  = (string) $setting->setting_type;
388        $this->cur_setting->setting_label = (string) $setting->setting_label;
389
390        $this->cur_setting->insert();
391    }
392
393    private function insertPref($pref)
394    {
395        if ($this->prefExists($pref->pref_ws, $pref->pref_id, $pref->user_id)) {
396            return;
397        }
398
399        $this->cur_pref->clean();
400
401        $this->cur_pref->pref_id    = (string) $pref->pref_id;
402        $this->cur_pref->user_id    = !$pref->user_id ? null : (string) $pref->user_id;
403        $this->cur_pref->pref_ws    = (string) $pref->pref_ws;
404        $this->cur_pref->pref_value = (string) $pref->pref_value;
405        $this->cur_pref->pref_type  = (string) $pref->pref_type;
406        $this->cur_pref->pref_label = (string) $pref->pref_label;
407
408        $this->cur_pref->insert();
409    }
410
411    private function insertUser($user)
412    {
413        if ($this->userExists($user->user_id)) {
414            return;
415        }
416
417        $this->cur_user->clean();
418
419        $this->cur_user->user_id           = (string) $user->user_id;
420        $this->cur_user->user_super        = (integer) $user->user_super;
421        $this->cur_user->user_pwd          = (string) $user->user_pwd;
422        $this->cur_user->user_recover_key  = (string) $user->user_recover_key;
423        $this->cur_user->user_name         = (string) $user->user_name;
424        $this->cur_user->user_firstname    = (string) $user->user_firstname;
425        $this->cur_user->user_displayname  = (string) $user->user_displayname;
426        $this->cur_user->user_email        = (string) $user->user_email;
427        $this->cur_user->user_url          = (string) $user->user_url;
428        $this->cur_user->user_default_blog = !$user->user_default_blog ? null : (string) $user->user_default_blog;
429        $this->cur_user->user_lang         = (string) $user->user_lang;
430        $this->cur_user->user_tz           = (string) $user->user_tz;
431        $this->cur_user->user_post_status  = (integer) $user->user_post_status;
432        $this->cur_user->user_creadt       = (string) $user->user_creadt;
433        $this->cur_user->user_upddt        = (string) $user->user_upddt;
434
435        $this->cur_user->user_desc    = $user->exists('user_desc') ? (string) $user->user_desc : null;
436        $this->cur_user->user_options = $user->exists('user_options') ? (string) $user->user_options : null;
437        $this->cur_user->user_status  = $user->exists('user_status') ? (integer) $user->user_status : 1;
438
439        $this->cur_user->insert();
440
441        $this->stack['users'][$user->user_id] = true;
442    }
443
444    private function insertPermissions($permissions)
445    {
446        $this->cur_permissions->clean();
447
448        $this->cur_permissions->user_id     = (string) $permissions->user_id;
449        $this->cur_permissions->blog_id     = (string) $permissions->blog_id;
450        $this->cur_permissions->permissions = (string) $permissions->permissions;
451
452        $this->cur_permissions->insert();
453    }
454
455    private function insertPost($post)
456    {
457        $this->cur_post->clean();
458
459        $cat_id = (integer) $post->cat_id;
460        if (!$cat_id) {
461            $cat_id = null;
462        }
463
464        $post_password = $post->post_password ? (string) $post->post_password : null;
465
466        $this->cur_post->post_id            = (integer) $post->post_id;
467        $this->cur_post->blog_id            = (string) $post->blog_id;
468        $this->cur_post->user_id            = (string) $this->getUserId($post->user_id);
469        $this->cur_post->cat_id             = $cat_id;
470        $this->cur_post->post_dt            = (string) $post->post_dt;
471        $this->cur_post->post_creadt        = (string) $post->post_creadt;
472        $this->cur_post->post_upddt         = (string) $post->post_upddt;
473        $this->cur_post->post_password      = $post_password;
474        $this->cur_post->post_type          = (string) $post->post_type;
475        $this->cur_post->post_format        = (string) $post->post_format;
476        $this->cur_post->post_url           = (string) $post->post_url;
477        $this->cur_post->post_lang          = (string) $post->post_lang;
478        $this->cur_post->post_title         = (string) $post->post_title;
479        $this->cur_post->post_excerpt       = (string) $post->post_excerpt;
480        $this->cur_post->post_excerpt_xhtml = (string) $post->post_excerpt_xhtml;
481        $this->cur_post->post_content       = (string) $post->post_content;
482        $this->cur_post->post_content_xhtml = (string) $post->post_content_xhtml;
483        $this->cur_post->post_notes         = (string) $post->post_notes;
484        $this->cur_post->post_words         = (string) $post->post_words;
485        $this->cur_post->post_meta          = (string) $post->post_meta;
486        $this->cur_post->post_status        = (integer) $post->post_status;
487        $this->cur_post->post_selected      = (integer) $post->post_selected;
488        $this->cur_post->post_open_comment  = (integer) $post->post_open_comment;
489        $this->cur_post->post_open_tb       = (integer) $post->post_open_tb;
490        $this->cur_post->nb_comment         = (integer) $post->nb_comment;
491        $this->cur_post->nb_trackback       = (integer) $post->nb_trackback;
492        $this->cur_post->post_position      = (integer) $post->post_position;
493
494        $this->cur_post->post_tz = $post->exists('post_tz') ? (string) $post->post_tz : 'UTC';
495
496        $this->cur_post->insert();
497    }
498
499    private function insertMeta($meta)
500    {
501        $this->cur_meta->clean();
502
503        $this->cur_meta->meta_id   = (string) $meta->meta_id;
504        $this->cur_meta->meta_type = (string) $meta->meta_type;
505        $this->cur_meta->post_id   = (integer) $meta->post_id;
506
507        $this->cur_meta->insert();
508    }
509
510    private function insertMedia($media)
511    {
512        $this->cur_media->clean();
513
514        $this->cur_media->media_id      = (integer) $media->media_id;
515        $this->cur_media->user_id       = (string) $media->user_id;
516        $this->cur_media->media_path    = (string) $media->media_path;
517        $this->cur_media->media_title   = (string) $media->media_title;
518        $this->cur_media->media_file    = (string) $media->media_file;
519        $this->cur_media->media_meta    = (string) $media->media_meta;
520        $this->cur_media->media_dt      = (string) $media->media_dt;
521        $this->cur_media->media_creadt  = (string) $media->media_creadt;
522        $this->cur_media->media_upddt   = (string) $media->media_upddt;
523        $this->cur_media->media_private = (integer) $media->media_private;
524
525        $this->cur_media->media_dir = $media->exists('media_dir') ? (string) $media->media_dir : dirname($media->media_file);
526
527        if (!$this->mediaExists()) {
528            $this->cur_media->insert();
529        }
530    }
531
532    private function insertPostMedia($post_media)
533    {
534        $this->cur_post_media->clean();
535
536        $this->cur_post_media->media_id = (integer) $post_media->media_id;
537        $this->cur_post_media->post_id  = (integer) $post_media->post_id;
538
539        $this->cur_post_media->insert();
540    }
541
542    private function insertLog($log)
543    {
544        $this->cur_log->clean();
545
546        $this->cur_log->log_id    = (integer) $log->log_id;
547        $this->cur_log->user_id   = (string) $log->user_id;
548        $this->cur_log->log_table = (string) $log->log_table;
549        $this->cur_log->log_dt    = (string) $log->log_dt;
550        $this->cur_log->log_ip    = (string) $log->log_ip;
551        $this->cur_log->log_msg   = (string) $log->log_msg;
552
553        $this->cur_log->insert();
554    }
555
556    private function insertPing($ping)
557    {
558        $this->cur_ping->clean();
559
560        $this->cur_ping->post_id  = (integer) $ping->post_id;
561        $this->cur_ping->ping_url = (string) $ping->ping_url;
562        $this->cur_ping->ping_dt  = (string) $ping->ping_dt;
563
564        $this->cur_ping->insert();
565    }
566
567    private function insertComment($comment)
568    {
569        $this->cur_comment->clean();
570
571        $this->cur_comment->comment_id          = (integer) $comment->comment_id;
572        $this->cur_comment->post_id             = (integer) $comment->post_id;
573        $this->cur_comment->comment_dt          = (string) $comment->comment_dt;
574        $this->cur_comment->comment_upddt       = (string) $comment->comment_upddt;
575        $this->cur_comment->comment_author      = (string) $comment->comment_author;
576        $this->cur_comment->comment_email       = (string) $comment->comment_email;
577        $this->cur_comment->comment_site        = (string) $comment->comment_site;
578        $this->cur_comment->comment_content     = (string) $comment->comment_content;
579        $this->cur_comment->comment_words       = (string) $comment->comment_words;
580        $this->cur_comment->comment_ip          = (string) $comment->comment_ip;
581        $this->cur_comment->comment_status      = (integer) $comment->comment_status;
582        $this->cur_comment->comment_spam_status = (string) $comment->comment_spam_status;
583        $this->cur_comment->comment_trackback   = (integer) $comment->comment_trackback;
584
585        $this->cur_comment->comment_tz          = $comment->exists('comment_tz') ? (string) $comment->comment_tz : 'UTC';
586        $this->cur_comment->comment_spam_filter = $comment->exists('comment_spam_filter') ? (string) $comment->comment_spam_filter : null;
587
588        $this->cur_comment->insert();
589    }
590
591    private function insertSpamRule($spamrule)
592    {
593        $this->cur_spamrule->clean();
594
595        $this->cur_spamrule->rule_id      = (integer) $spamrule->rule_id;
596        $this->cur_spamrule->blog_id      = !$spamrule->blog_id ? null : (string) $spamrule->blog_id;
597        $this->cur_spamrule->rule_type    = (string) $spamrule->rule_type;
598        $this->cur_spamrule->rule_content = (string) $spamrule->rule_content;
599
600        $this->cur_spamrule->insert();
601    }
602
603    private function insertCategorySingle($category)
604    {
605        $this->cur_category->clean();
606
607        $m = $this->searchCategory($this->stack['categories'], $category->cat_url);
608
609        $old_id = $category->cat_id;
610        if ($m !== false) {
611            $cat_id = $m;
612        } else {
613            $cat_id            = $this->stack['cat_id'];
614            $category->cat_id  = $cat_id;
615            $category->blog_id = $this->blog_id;
616
617            $this->insertCategory($category);
618            $this->stack['cat_id']++;
619        }
620
621        $this->old_ids['category'][(integer) $old_id] = $cat_id;
622    }
623
624    private function insertLinkSingle($link)
625    {
626        $link->blog_id = $this->blog_id;
627        $link->link_id = $this->stack['link_id'];
628
629        $this->insertLink($link);
630        $this->stack['link_id']++;
631    }
632
633    private function insertPostSingle($post)
634    {
635        if (!$post->cat_id || isset($this->old_ids['category'][(integer) $post->cat_id])) {
636            $post_id                                         = $this->stack['post_id'];
637            $this->old_ids['post'][(integer) $post->post_id] = $post_id;
638
639            $cat_id = $post->cat_id ? $this->old_ids['category'][(integer) $post->cat_id] : null;
640
641            $post->post_id = $post_id;
642            $post->cat_id  = $cat_id;
643            $post->blog_id = $this->blog_id;
644
645            $post->post_url = $this->core->blog->getPostURL(
646                $post->post_url, $post->post_dt, $post->post_title, $post->post_id
647            );
648
649            $this->insertPost($post);
650            $this->stack['post_id']++;
651        } else {
652            self::throwIdError($post->__name, $post->__line, 'category');
653        }
654    }
655
656    private function insertMetaSingle($meta)
657    {
658        if (isset($this->old_ids['post'][(integer) $meta->post_id])) {
659            $meta->post_id = $this->old_ids['post'][(integer) $meta->post_id];
660            $this->insertMeta($meta);
661        } else {
662            self::throwIdError($meta->__name, $meta->__line, 'post');
663        }
664    }
665
666    private function insertMediaSingle($media)
667    {
668        $media_id = $this->stack['media_id'];
669        $old_id   = $media->media_id;
670
671        $media->media_id   = $media_id;
672        $media->media_path = $this->core->blog->settings->system->public_path;
673        $media->user_id    = $this->getUserId($media->user_id);
674
675        $this->insertMedia($media);
676        $this->stack['media_id']++;
677        $this->old_ids['media'][(integer) $old_id] = $media_id;
678    }
679
680    private function insertPostMediaSingle($post_media)
681    {
682        if (isset($this->old_ids['media'][(integer) $post_media->media_id]) &&
683            isset($this->old_ids['post'][(integer) $post_media->post_id])) {
684            $post_media->media_id = $this->old_ids['media'][(integer) $post_media->media_id];
685            $post_media->post_id  = $this->old_ids['post'][(integer) $post_media->post_id];
686
687            $this->insertPostMedia($post_media);
688        } elseif (!isset($this->old_ids['media'][(integer) $post_media->media_id])) {
689            self::throwIdError($post_media->__name, $post_media->__line, 'media');
690        } else {
691            self::throwIdError($post_media->__name, $post_media->__line, 'post');
692        }
693    }
694
695    private function insertPingSingle($ping)
696    {
697        if (isset($this->old_ids['post'][(integer) $ping->post_id])) {
698            $ping->post_id = $this->old_ids['post'][(integer) $ping->post_id];
699
700            $this->insertPing($ping);
701        } else {
702            self::throwIdError($ping->__name, $ping->__line, 'post');
703        }
704    }
705
706    private function insertCommentSingle($comment)
707    {
708        if (isset($this->old_ids['post'][(integer) $comment->post_id])) {
709            $comment_id = $this->stack['comment_id'];
710
711            $comment->comment_id = $comment_id;
712            $comment->post_id    = $this->old_ids['post'][(integer) $comment->post_id];
713
714            $this->insertComment($comment);
715            $this->stack['comment_id']++;
716        } else {
717            self::throwIdError($comment->__name, $comment->__line, 'post');
718        }
719    }
720
721    private static function throwIdError($name, $line, $related)
722    {
723        throw new Exception(sprintf(
724            __('ID of "%3$s" does not match on record "%1$s" at line %2$s of backup file.'),
725            html::escapeHTML($name),
726            html::escapeHTML($line),
727            html::escapeHTML($related)
728        ));
729    }
730
731    public function searchCategory($rs, $url)
732    {
733        while ($rs->fetch()) {
734            if ($rs->cat_url == $url) {
735                return $rs->cat_id;
736            }
737        }
738
739        return false;
740    }
741
742    public function getUserId($user_id)
743    {
744        if (!$this->userExists($user_id)) {
745            if ($this->core->auth->isSuperAdmin()) {
746                # Sanitizes user_id and create a lambda user
747                $user_id = preg_replace('/[^A-Za-z0-9]$/', '', $user_id);
748                $user_id .= strlen($user_id) < 2 ? '-a' : '';
749
750                # We change user_id, we need to check again
751                if (!$this->userExists($user_id)) {
752                    $this->cur_user->clean();
753                    $this->cur_user->user_id  = (string) $user_id;
754                    $this->cur_user->user_pwd = md5(uniqid());
755
756                    $this->core->addUser($this->cur_user);
757
758                    $this->stack['users'][$user_id] = true;
759                }
760            } else {
761                # Returns current user id
762                $user_id = $this->core->auth->userID();
763            }
764        }
765
766        return $user_id;
767    }
768
769    private function userExists($user_id)
770    {
771        if (isset($this->stack['users'][$user_id])) {
772            return $this->stack['users'][$user_id];
773        }
774
775        $strReq = 'SELECT user_id ' .
776        'FROM ' . $this->prefix . 'user ' .
777        "WHERE user_id = '" . $this->con->escape($user_id) . "' ";
778
779        $rs = $this->con->select($strReq);
780
781        $this->stack['users'][$user_id] = !$rs->isEmpty();
782        return $this->stack['users'][$user_id];
783    }
784
785    private function prefExists($pref_ws, $pref_id, $user_id)
786    {
787        $strReq = 'SELECT pref_id,pref_ws,user_id ' .
788        'FROM ' . $this->prefix . 'pref ' .
789        "WHERE pref_id = '" . $this->con->escape($pref_id) . "' " .
790        "AND pref_ws = '" . $this->con->escape($pref_ws) . "' ";
791        if (!$user_id) {
792            $strReq .= "AND user_id IS NULL ";
793        } else {
794            $strReq .= "AND user_id = '" . $this->con->escape($user_id) . "' ";
795        }
796
797        $rs = $this->con->select($strReq);
798
799        return !$rs->isEmpty();
800    }
801
802    private function mediaExists()
803    {
804        $strReq = 'SELECT media_id ' .
805        'FROM ' . $this->prefix . 'media ' .
806        "WHERE media_path = '" . $this->con->escape($this->cur_media->media_path) . "' " .
807        "AND media_file = '" . $this->con->escape($this->cur_media->media_file) . "' ";
808
809        $rs = $this->con->select($strReq);
810
811        return !$rs->isEmpty();
812    }
813
814    private function prepareDC12line(&$line)
815    {
816        $settings = array('dc_theme', 'dc_nb_post_per_page', 'dc_allow_comments',
817            'dc_allow_trackbacks', 'dc_comment_pub', 'dc_comments_ttl',
818            'dc_wiki_comments', 'dc_use_smilies', 'dc_date_format', 'dc_time_format',
819            'dc_url_scan');
820
821        switch ($line->__name) {
822            case 'categorie':
823                $line->substitute('cat_libelle', 'cat_title');
824                $line->substitute('cat_libelle_url', 'cat_url');
825                $line->__name  = 'category';
826                $line->blog_id = 'default';
827                break;
828            case 'link':
829                $line->substitute('href', 'link_href');
830                $line->substitute('label', 'link_title');
831                $line->substitute('title', 'link_desc');
832                $line->substitute('lang', 'link_lang');
833                $line->substitute('rel', 'link_xfn');
834                $line->substitute('position', 'link_position');
835                $line->blog_id = 'default';
836                break;
837            case 'post':
838                $line->substitute('post_titre', 'post_title');
839                $line->post_title         = html::decodeEntities($line->post_title);
840                $line->post_url           = date('Y/m/d/', strtotime($line->post_dt)) . $line->post_id . '-' . $line->post_titre_url;
841                $line->post_url           = substr($line->post_url, 0, 255);
842                $line->post_format        = $line->post_content_wiki == '' ? 'xhtml' : 'wiki';
843                $line->post_content_xhtml = $line->post_content;
844                $line->post_excerpt_xhtml = $line->post_chapo;
845
846                if ($line->post_format == 'wiki') {
847                    $line->post_content = $line->post_content_wiki;
848                    $line->post_excerpt = $line->post_chapo_wiki;
849                } else {
850                    $line->post_content = $line->post_content;
851                    $line->post_excerpt = $line->post_chapo;
852                }
853
854                $line->post_status = (integer) $line->post_pub;
855                $line->post_type   = 'post';
856                $line->blog_id     = 'default';
857
858                $line->drop('post_titre_url', 'post_content_wiki', 'post_chapo', 'post_chapo_wiki', 'post_pub');
859
860                break;
861            case 'post_meta':
862                $line->drop('meta_id');
863                $line->substitute('meta_key', 'meta_type');
864                $line->substitute('meta_value', 'meta_id');
865                $line->__name  = 'meta';
866                $line->blog_id = 'default';
867                break;
868            case 'comment':
869                $line->substitute('comment_auteur', 'comment_author');
870                if ($line->comment_site != '' && !preg_match('!^http://.*$!', $line->comment_site, $m)) {
871                    $line->comment_site = 'http://' . $line->comment_site;
872                }
873                $line->comment_status = (integer) $line->comment_pub;
874                $line->drop('comment_pub');
875                break;
876        }
877
878        # --BEHAVIOR-- importPrepareDC12
879        $this->core->callBehavior('importPrepareDC12', $line, $this, $this->core);
880    }
881}
Note: See TracBrowser for help on using the repository browser.

Sites map