Dotclear

source: plugins/importExport/inc/flat/class.flat.import.php @ 841:e1c6ec0a0736

Revision 841:e1c6ec0a0736, 27.4 KB checked in by JcDenis, 13 years ago (diff)

Flat import errors are now more verbose, fixes #888

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          $post_password = $post->post_password ? (string) $post->post_password : null;
443         
444          $this->cur_post->post_id            = (integer) $post->post_id;
445          $this->cur_post->blog_id            = (string) $post->blog_id;
446          $this->cur_post->user_id            = (string) $this->getUserId($post->user_id);
447          $this->cur_post->cat_id             = $cat_id;
448          $this->cur_post->post_dt            = (string) $post->post_dt;
449          $this->cur_post->post_creadt        = (string) $post->post_creadt;
450          $this->cur_post->post_upddt         = (string) $post->post_upddt;
451          $this->cur_post->post_password      = $post_password;
452          $this->cur_post->post_type          = (string) $post->post_type;
453          $this->cur_post->post_format        = (string) $post->post_format;
454          $this->cur_post->post_url           = (string) $post->post_url;
455          $this->cur_post->post_lang          = (string) $post->post_lang;
456          $this->cur_post->post_title         = (string) $post->post_title;
457          $this->cur_post->post_excerpt       = (string) $post->post_excerpt;
458          $this->cur_post->post_excerpt_xhtml = (string) $post->post_excerpt_xhtml;
459          $this->cur_post->post_content       = (string) $post->post_content;
460          $this->cur_post->post_content_xhtml = (string) $post->post_content_xhtml;
461          $this->cur_post->post_notes         = (string) $post->post_notes;
462          $this->cur_post->post_words         = (string) $post->post_words;
463          $this->cur_post->post_meta          = (string) $post->post_meta;
464          $this->cur_post->post_status        = (integer) $post->post_status;
465          $this->cur_post->post_selected      = (integer) $post->post_selected;
466          $this->cur_post->post_open_comment  = (integer) $post->post_open_comment;
467          $this->cur_post->post_open_tb       = (integer) $post->post_open_tb;
468          $this->cur_post->nb_comment         = (integer) $post->nb_comment;
469          $this->cur_post->nb_trackback       = (integer) $post->nb_trackback;
470         
471          $this->cur_post->post_tz = $post->exists('post_tz') ? (string) $post->post_tz : 'UTC';
472         
473          $this->cur_post->insert();
474     }
475     
476     private function insertMeta($meta)
477     {
478          $this->cur_meta->clean();
479         
480          $this->cur_meta->meta_id   = (string) $meta->meta_id;
481          $this->cur_meta->meta_type = (string) $meta->meta_type;
482          $this->cur_meta->post_id   = (integer) $meta->post_id;
483         
484          $this->cur_meta->insert();
485     }
486     
487     private function insertMedia($media)
488     {
489          $this->cur_media->clean();
490         
491          $this->cur_media->media_id      = (integer) $media->media_id;
492          $this->cur_media->user_id       = (string) $media->user_id;
493          $this->cur_media->media_path    = (string) $media->media_path;
494          $this->cur_media->media_title   = (string) $media->media_title;
495          $this->cur_media->media_file    = (string) $media->media_file;
496          $this->cur_media->media_meta    = (string) $media->media_meta;
497          $this->cur_media->media_dt      = (string) $media->media_dt;
498          $this->cur_media->media_creadt  = (string) $media->media_creadt;
499          $this->cur_media->media_upddt   = (string) $media->media_upddt;
500          $this->cur_media->media_private = (integer) $media->media_private;
501         
502          $this->cur_media->media_dir = $media->exists('media_dir') ? (string) $media->media_dir : dirname($media->media_file);
503         
504          if (!$this->mediaExists()) {
505               $this->cur_media->insert();
506          }
507     }
508     
509     private function insertPostMedia($post_media)
510     {
511          $this->cur_post_media->clean();
512         
513          $this->cur_post_media->media_id = (integer) $post_media->media_id;
514          $this->cur_post_media->post_id  = (integer) $post_media->post_id;
515         
516          $this->cur_post_media->insert();
517     }
518     
519     private function insertLog($log)
520     {
521          $this->cur_log->clean();
522         
523          $this->cur_log->log_id    = (integer) $log->log_id;
524          $this->cur_log->user_id   = (string) $log->user_id;
525          $this->cur_log->log_table = (string) $log->log_table;
526          $this->cur_log->log_dt    = (string) $log->log_dt;
527          $this->cur_log->log_ip    = (string) $log->log_ip;
528          $this->cur_log->log_msg   = (string) $log->log_msg;
529         
530          $this->cur_log->insert();
531     }
532     
533     private function insertPing($ping)
534     {
535          $this->cur_ping->clean();
536         
537          $this->cur_ping->post_id  = (integer) $ping->post_id;
538          $this->cur_ping->ping_url = (string) $ping->ping_url;
539          $this->cur_ping->ping_dt  = (string) $ping->ping_dt;
540         
541          $this->cur_ping->insert();
542     }
543     
544     private function insertComment($comment)
545     {
546          $this->cur_comment->clean();
547         
548          $this->cur_comment->comment_id          = (integer) $comment->comment_id;
549          $this->cur_comment->post_id             = (integer) $comment->post_id;
550          $this->cur_comment->comment_dt          = (string) $comment->comment_dt;
551          $this->cur_comment->comment_upddt       = (string) $comment->comment_upddt;
552          $this->cur_comment->comment_author      = (string) $comment->comment_author;
553          $this->cur_comment->comment_email       = (string) $comment->comment_email;
554          $this->cur_comment->comment_site        = (string) $comment->comment_site;
555          $this->cur_comment->comment_content     = (string) $comment->comment_content;
556          $this->cur_comment->comment_words       = (string) $comment->comment_words;
557          $this->cur_comment->comment_ip          = (string) $comment->comment_ip;
558          $this->cur_comment->comment_status      = (integer) $comment->comment_status;
559          $this->cur_comment->comment_spam_status = (integer) $comment->comment_spam_status;
560          $this->cur_comment->comment_trackback   = (integer) $comment->comment_trackback;
561         
562          $this->cur_comment->comment_tz = $comment->exists('comment_tz') ? (string) $comment->comment_tz : 'UTC';
563          $this->cur_comment->comment_spam_filter = $comment->exists('comment_spam_filter') ? (integer) $comment->comment_spam_filter : null;
564         
565          $this->cur_comment->insert();
566     }
567     
568     private function insertSpamRule($spamrule)
569     {
570          $this->cur_spamrule->clean();
571         
572          $this->cur_spamrule->rule_id      = (integer) $spamrule->rule_id;
573          $this->cur_spamrule->blog_id      = !$spamrule->blog_id ? null : (string) $spamrule->blog_id;
574          $this->cur_spamrule->rule_type    = (string) $spamrule->rule_type;
575          $this->cur_spamrule->rule_content = (string) $spamrule->rule_content;
576         
577          $this->cur_spamrule->insert();
578     }
579     
580     private function insertCategorySingle($category)
581     {
582          $this->cur_category->clean();
583         
584          $m = $this->searchCategory($this->stack['categories'],$category->cat_url);
585         
586          $old_id = $category->cat_id;
587          if ($m !== false)
588          {
589               $cat_id = $m;
590          }
591          else
592          {
593               $cat_id = $this->stack['cat_id'];
594               $category->cat_id = $cat_id;
595               $category->blog_id = $this->blog_id;
596               
597               $this->insertCategory($category);
598               $this->stack['cat_id']++;
599          }
600         
601          $this->old_ids['category'][(integer) $old_id] = $cat_id;
602     }
603     
604     private function insertLinkSingle($link)
605     {
606          $link->blog_id = $this->blog_id;
607          $link->link_id = $this->stack['link_id'];
608         
609          $this->insertLink($link);
610          $this->stack['link_id']++;
611     }
612     
613     private function insertPostSingle($post)
614     {
615          if (!$post->cat_id || isset($this->old_ids['category'][(integer) $post->cat_id])) {
616               $post_id = $this->stack['post_id'];
617               $this->old_ids['post'][(integer) $post->post_id] = $post_id;
618               
619               $cat_id = $post->cat_id ? $this->old_ids['category'][(integer) $post->cat_id] : null;
620               
621               $post->post_id = $post_id;
622               $post->cat_id = $cat_id;
623               $post->blog_id = $this->blog_id;
624               
625               $post->post_url = $this->core->blog->getPostURL(
626                    $post->post_url,$post->post_dt,$post->post_title,$post->post_id
627               );
628               
629               $this->insertPost($post);
630               $this->stack['post_id']++;
631          } else {
632               self::throwIdError($post->__name,$post->__line,'category');
633          }
634     }
635     
636     private function insertMetaSingle($meta)
637     {
638          if (isset($this->old_ids['post'][(integer) $meta->post_id])) {
639               $meta->post_id = $this->old_ids['post'][(integer) $meta->post_id];
640               $this->insertMeta($meta);
641          } else {
642               self::throwIdError($meta->__name,$meta->__line,'post');
643          }
644     }
645     
646     private function insertMediaSingle($media)
647     {
648          $media_id = $this->stack['media_id'];
649          $old_id = $media->media_id;
650         
651          $media->media_id = $media_id;
652          $media->media_path = $this->core->blog->settings->system->public_path;
653          $media->user_id = $this->getUserId($media->user_id);
654         
655          $this->insertMedia($media);
656          $this->stack['media_id']++;
657          $this->old_ids['media'][(integer) $old_id] = $media_id;
658     }
659     
660     private function insertPostMediaSingle($post_media)
661     {
662          if (isset($this->old_ids['media'][(integer) $post_media->media_id]) &&
663               isset($this->old_ids['post'][(integer) $post_media->post_id])) {
664               $post_media->media_id = $this->old_ids['media'][(integer) $post_media->media_id];
665               $post_media->post_id = $this->old_ids['post'][(integer) $post_media->post_id];
666               
667               $this->insertPostMedia($post_media);
668          } elseif (!isset($this->old_ids['media'][(integer) $post_media->media_id])) {
669               self::throwIdError($post_media->__name,$post_media->__line,'media');
670          }else {
671               self::throwIdError($post_media->__name,$post_media->__line,'post');
672          }
673     }
674     
675     private function insertPingSingle($ping)
676     {
677          if (isset($this->old_ids['post'][(integer) $ping->post_id])) {
678               $ping->post_id = $this->old_ids['post'][(integer) $ping->post_id];
679               
680               $this->insertPing($ping);
681          } else {
682               self::throwIdError($ping->__name,$ping->__line,'post');
683          }
684     }
685     
686     private function insertCommentSingle($comment)
687     {
688          if (isset($this->old_ids['post'][(integer) $comment->post_id])) {
689               $comment_id = $this->stack['comment_id'];
690               
691               $comment->comment_id = $comment_id;
692               $comment->post_id = $this->old_ids['post'][(integer) $comment->post_id];
693               
694               $this->insertComment($comment);
695               $this->stack['comment_id']++;
696          } else {
697               self::throwIdError($comment->__name,$comment->__line,'post');
698          }
699     }
700     
701     private static function throwIdError($name,$line,$related)
702     {
703          throw new Exception(sprintf(
704               __('ID of "%3$s" does not match on record "%1$s" at line %2$s of backup file.'),
705               html::escapeHTML($name),
706               html::escapeHTML($line),
707               html::escapeHTML($related)
708          ));
709     }
710     
711     public function searchCategory($rs,$url)
712     {
713          while ($rs->fetch())
714          {
715               if ($rs->cat_url == $url) {
716                    return $rs->cat_id;
717               }
718          }
719         
720          return false;
721     }
722     
723     public function getUserId($user_id)
724     {
725          if (!$this->userExists($user_id))
726          {
727               if ($this->core->auth->isSuperAdmin())
728               {
729                    # Sanitizes user_id and create a lambda user
730                    $user_id = preg_replace('/[^A-Za-z0-9]$/','',$user_id);
731                    $user_id .= strlen($user_id) < 2 ? '-a' : '';
732                   
733                    # We change user_id, we need to check again
734                    if (!$this->userExists($user_id))
735                    {
736                         $this->cur_user->clean();
737                         $this->cur_user->user_id = (string) $user_id;
738                         $this->cur_user->user_pwd = md5(uniqid());
739                         
740                         $this->core->addUser($this->cur_user);
741                         
742                         $this->stack['users'][$user_id] = true;
743                    }
744               }
745               else
746               {
747                    # Returns current user id
748                    $user_id = $this->core->auth->userID();
749               }
750          }
751         
752          return $user_id;
753     }
754     
755     private function userExists($user_id)
756     {
757          if (isset($this->stack['users'][$user_id])) {
758               return $this->stack['users'][$user_id];
759          }
760         
761          $strReq = 'SELECT user_id '.
762                    'FROM '.$this->prefix.'user '.
763                    "WHERE user_id = '".$this->con->escape($user_id)."' ";
764         
765          $rs = $this->con->select($strReq);
766         
767          $this->stack['users'][$user_id] = !$rs->isEmpty();
768          return $this->stack['users'][$user_id];
769     }
770     
771     private function prefExists($pref_ws,$pref_id,$user_id)
772     {
773          $strReq = 'SELECT pref_id,pref_ws,user_id '.
774                    'FROM '.$this->prefix.'pref '.
775                    "WHERE pref_id = '".$this->con->escape($pref_id)."' ".
776                    "AND pref_ws = '".$this->con->escape($pref_ws)."' ";
777          if (!$user_id) {
778               $strReq .= "AND user_id IS NULL ";
779          } else {
780               $strReq .= "AND user_id = '".$this->con->escape($user_id)."' ";
781          }
782         
783          $rs = $this->con->select($strReq);
784         
785          return !$rs->isEmpty();
786     }
787     
788     private function mediaExists()
789     {
790          $strReq = 'SELECT media_id '.
791                    'FROM '.$this->prefix.'media '.
792                    "WHERE media_path = '".$this->cur_media->media_path."' ".
793                    "AND media_file = '".$this->cur_media->media_file."' ";
794         
795          $rs = $this->con->select($strReq);
796         
797          return !$rs->isEmpty();
798     }
799     
800     private function prepareDC12line(&$line)
801     {
802          $settings = array('dc_theme','dc_nb_post_per_page','dc_allow_comments',
803          'dc_allow_trackbacks','dc_comment_pub','dc_comments_ttl',
804          'dc_wiki_comments','dc_use_smilies','dc_date_format','dc_time_format',
805          'dc_url_scan');
806         
807          switch ($line->__name)
808          {
809               case 'categorie':
810                    $line->substitute('cat_libelle','cat_title');
811                    $line->substitute('cat_libelle_url','cat_url');
812                    $line->__name = 'category';
813                    $line->blog_id = 'default';
814                    break;
815               case 'link':
816                    $line->substitute('href','link_href');
817                    $line->substitute('label','link_title');
818                    $line->substitute('title','link_desc');
819                    $line->substitute('lang','link_lang');
820                    $line->substitute('rel','link_xfn');
821                    $line->substitute('position','link_position');
822                    $line->blog_id = 'default';
823                    break;
824               case 'post':
825                    $line->substitute('post_titre','post_title');
826                    $line->post_title = html::decodeEntities($line->post_title);
827                    $line->post_url = date('Y/m/d/',strtotime($line->post_dt)).$line->post_id.'-'.$line->post_titre_url;
828                    $line->post_url = substr($line->post_url,0,255);
829                    $line->post_format = $line->post_content_wiki == '' ? 'xhtml' : 'wiki';
830                    $line->post_content_xhtml = $line->post_content;
831                    $line->post_excerpt_xhtml = $line->post_chapo;
832                   
833                    if ($line->post_format == 'wiki') {
834                         $line->post_content = $line->post_content_wiki;
835                         $line->post_excerpt = $line->post_chapo_wiki;
836                    } else {
837                         $line->post_content = $line->post_content;
838                         $line->post_excerpt = $line->post_chapo;
839                    }
840                   
841                    $line->post_status = (integer) $line->post_pub;
842                    $line->post_type = 'post';
843                    $line->blog_id = 'default';
844                   
845                    $line->drop('post_titre_url','post_content_wiki','post_chapo','post_chapo_wiki','post_pub');
846                   
847                    break;
848               case 'post_meta':
849                    $line->drop('meta_id');
850                    $line->substitute('meta_key','meta_type');
851                    $line->substitute('meta_value','meta_id');
852                    $line->__name = 'meta';
853                    $line->blog_id = 'default';
854                    break;
855               case 'comment':
856                    $line->substitute('comment_auteur','comment_author');
857                    if ($line->comment_site != '' && !preg_match('!^http://.*$!', $line->comment_site,$m)) {
858                         $line->comment_site = 'http://'.$line->comment_site;
859                    }
860                    $line->comment_status = (integer) $line->comment_pub;
861                    $line->drop('comment_pub');
862                    break;
863          }
864         
865          # --BEHAVIOR-- importPrepareDC12
866          $this->core->callBehavior('importPrepareDC12',$line,$this,$this->core);
867     }
868}
869?>
Note: See TracBrowser for help on using the repository browser.

Sites map