Dotclear

source: plugins/importExport/inc/flat/class.flat.import.php @ 877:e012da998f83

Revision 877:e012da998f83, 27.3 KB checked in by Dsls <dsls@…>, 13 years ago (diff)

Entries passwords will be a dedicated plugin.

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

Sites map