Dotclear

source: plugins/importExport/inc/flat/class.dc.import.php @ 233:3a034fd6b76c

Revision 233:3a034fd6b76c, 26.5 KB checked in by Franck <carnet.franck.paul@…>, 14 years ago (diff)

Import/Export? de la table des préférences utilisateurs

  • Uniquement sur un export/import complet
  • Fonctionne comme pour les utilisateurs (pas d'écrasement)
Line 
1<?php
2# -- BEGIN LICENSE BLOCK ---------------------------------------
3#
4# This file is part of Dotclear 2.
5#
6# Copyright (c) 2003-2010 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 dcImport extends backupFile
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          $this->cur_pref->clean();
374         
375          $this->cur_pref->pref_id    = (string) $pref->pref_id;
376          $this->cur_pref->user_id    = !$pref->user_id ? null : (string) $pref->user_id;
377          $this->cur_pref->pref_ws    = (string) $pref->pref_ws;
378          $this->cur_pref->pref_value = (string) $pref->pref_value;
379          $this->cur_pref->pref_type  = (string) $pref->pref_type;
380          $this->cur_pref->pref_label = (string) $pref->pref_label;
381         
382          $this->cur_pref->insert();
383     }
384     
385     private function insertUser($user)
386     {
387          if ($this->userExists($user->user_id)) {
388               return;
389          }
390         
391          $this->cur_user->clean();
392         
393          $this->cur_user->user_id           = (string) $user->user_id;
394          $this->cur_user->user_super        = (integer) $user->user_super;
395          $this->cur_user->user_pwd          = (string) $user->user_pwd;
396          $this->cur_user->user_recover_key  = (string) $user->user_recover_key;
397          $this->cur_user->user_name         = (string) $user->user_name;
398          $this->cur_user->user_firstname    = (string) $user->user_firstname;
399          $this->cur_user->user_displayname  = (string) $user->user_displayname;
400          $this->cur_user->user_email        = (string) $user->user_email;
401          $this->cur_user->user_url          = (string) $user->user_url;
402          $this->cur_user->user_default_blog = !$user->user_default_blog ? null : (string) $user->user_default_blog;
403          $this->cur_user->user_lang         = (string) $user->user_lang;
404          $this->cur_user->user_tz           = (string) $user->user_tz;
405          $this->cur_user->user_post_status  = (integer) $user->user_post_status;
406          $this->cur_user->user_creadt       = (string) $user->user_creadt;
407          $this->cur_user->user_upddt        = (string) $user->user_upddt;
408         
409          $this->cur_user->user_desc = $user->exists('user_desc') ? (string) $user->user_desc : null;
410          $this->cur_user->user_options = $user->exists('user_options') ? (string) $user->user_options : null;
411          $this->cur_user->user_status = $user->exists('user_status') ? (integer) $user->user_status : 1;
412         
413          $this->cur_user->insert();
414         
415          $this->stack['users'][$user->user_id] = true;
416     }
417     
418     private function insertPermissions($permissions)
419     {
420          $this->cur_permissions->clean();
421         
422          $this->cur_permissions->user_id     = (string) $permissions->user_id;
423          $this->cur_permissions->blog_id     = (string) $permissions->blog_id;
424          $this->cur_permissions->permissions = (string) $permissions->permissions;
425         
426          $this->cur_permissions->insert();
427     }
428     
429     private function insertPost($post)
430     {
431          $this->cur_post->clean();
432         
433          $cat_id = (integer) $post->cat_id;
434          if (!$cat_id) {
435               $cat_id = null;
436          }
437         
438          $post_password = $post->post_password ? (string) $post->post_password : null;
439         
440          $this->cur_post->post_id            = (integer) $post->post_id;
441          $this->cur_post->blog_id            = (string) $post->blog_id;
442          $this->cur_post->user_id            = (string) $this->getUserId($post->user_id);
443          $this->cur_post->cat_id             = $cat_id;
444          $this->cur_post->post_dt            = (string) $post->post_dt;
445          $this->cur_post->post_creadt        = (string) $post->post_creadt;
446          $this->cur_post->post_upddt         = (string) $post->post_upddt;
447          $this->cur_post->post_password      = $post_password;
448          $this->cur_post->post_type          = (string) $post->post_type;
449          $this->cur_post->post_format        = (string) $post->post_format;
450          $this->cur_post->post_url           = (string) $post->post_url;
451          $this->cur_post->post_lang          = (string) $post->post_lang;
452          $this->cur_post->post_title         = (string) $post->post_title;
453          $this->cur_post->post_excerpt       = (string) $post->post_excerpt;
454          $this->cur_post->post_excerpt_xhtml = (string) $post->post_excerpt_xhtml;
455          $this->cur_post->post_content       = (string) $post->post_content;
456          $this->cur_post->post_content_xhtml = (string) $post->post_content_xhtml;
457          $this->cur_post->post_notes         = (string) $post->post_notes;
458          $this->cur_post->post_words         = (string) $post->post_words;
459          $this->cur_post->post_meta          = (string) $post->post_meta;
460          $this->cur_post->post_status        = (integer) $post->post_status;
461          $this->cur_post->post_selected      = (integer) $post->post_selected;
462          $this->cur_post->post_open_comment  = (integer) $post->post_open_comment;
463          $this->cur_post->post_open_tb       = (integer) $post->post_open_tb;
464          $this->cur_post->nb_comment         = (integer) $post->nb_comment;
465          $this->cur_post->nb_trackback       = (integer) $post->nb_trackback;
466         
467          $this->cur_post->post_tz = $post->exists('post_tz') ? (string) $post->post_tz : 'UTC';
468         
469          $this->cur_post->insert();
470     }
471     
472     private function insertMeta($meta)
473     {
474          $this->cur_meta->clean();
475         
476          $this->cur_meta->meta_id   = (string) $meta->meta_id;
477          $this->cur_meta->meta_type = (string) $meta->meta_type;
478          $this->cur_meta->post_id   = (integer) $meta->post_id;
479         
480          $this->cur_meta->insert();
481     }
482     
483     private function insertMedia($media)
484     {
485          $this->cur_media->clean();
486         
487          $this->cur_media->media_id      = (integer) $media->media_id;
488          $this->cur_media->user_id       = (string) $media->user_id;
489          $this->cur_media->media_path    = (string) $media->media_path;
490          $this->cur_media->media_title   = (string) $media->media_title;
491          $this->cur_media->media_file    = (string) $media->media_file;
492          $this->cur_media->media_meta    = (string) $media->media_meta;
493          $this->cur_media->media_dt      = (string) $media->media_dt;
494          $this->cur_media->media_creadt  = (string) $media->media_creadt;
495          $this->cur_media->media_upddt   = (string) $media->media_upddt;
496          $this->cur_media->media_private = (integer) $media->media_private;
497         
498          $this->cur_media->media_dir = $media->exists('media_dir') ? (string) $media->media_dir : dirname($media->media_file);
499         
500          if (!$this->mediaExists()) {
501               $this->cur_media->insert();
502          }
503     }
504     
505     private function insertPostMedia($post_media)
506     {
507          $this->cur_post_media->clean();
508         
509          $this->cur_post_media->media_id = (integer) $post_media->media_id;
510          $this->cur_post_media->post_id  = (integer) $post_media->post_id;
511         
512          $this->cur_post_media->insert();
513     }
514     
515     private function insertLog($log)
516     {
517          $this->cur_log->clean();
518         
519          $this->cur_log->log_id    = (integer) $log->log_id;
520          $this->cur_log->user_id   = (string) $log->user_id;
521          $this->cur_log->log_table = (string) $log->log_table;
522          $this->cur_log->log_dt    = (string) $log->log_dt;
523          $this->cur_log->log_ip    = (string) $log->log_ip;
524          $this->cur_log->log_msg   = (string) $log->log_msg;
525         
526          $this->cur_log->insert();
527     }
528     
529     private function insertPing($ping)
530     {
531          $this->cur_ping->clean();
532         
533          $this->cur_ping->post_id  = (integer) $ping->post_id;
534          $this->cur_ping->ping_url = (string) $ping->ping_url;
535          $this->cur_ping->ping_dt  = (string) $ping->ping_dt;
536         
537          $this->cur_ping->insert();
538     }
539     
540     private function insertComment($comment)
541     {
542          $this->cur_comment->clean();
543         
544          $this->cur_comment->comment_id          = (integer) $comment->comment_id;
545          $this->cur_comment->post_id             = (integer) $comment->post_id;
546          $this->cur_comment->comment_dt          = (string) $comment->comment_dt;
547          $this->cur_comment->comment_upddt       = (string) $comment->comment_upddt;
548          $this->cur_comment->comment_author      = (string) $comment->comment_author;
549          $this->cur_comment->comment_email       = (string) $comment->comment_email;
550          $this->cur_comment->comment_site        = (string) $comment->comment_site;
551          $this->cur_comment->comment_content     = (string) $comment->comment_content;
552          $this->cur_comment->comment_words       = (string) $comment->comment_words;
553          $this->cur_comment->comment_ip          = (string) $comment->comment_ip;
554          $this->cur_comment->comment_status      = (integer) $comment->comment_status;
555          $this->cur_comment->comment_spam_status = (integer) $comment->comment_spam_status;
556          $this->cur_comment->comment_trackback   = (integer) $comment->comment_trackback;
557         
558          $this->cur_comment->comment_tz = $comment->exists('comment_tz') ? (string) $comment->comment_tz : 'UTC';
559          $this->cur_comment->comment_spam_filter = $comment->exists('comment_spam_filter') ? (integer) $comment->comment_spam_filter : null;
560         
561          $this->cur_comment->insert();
562     }
563     
564     private function insertSpamRule($spamrule)
565     {
566          $this->cur_spamrule->clean();
567         
568          $this->cur_spamrule->rule_id      = (integer) $spamrule->rule_id;
569          $this->cur_spamrule->blog_id      = !$spamrule->blog_id ? null : (string) $spamrule->blog_id;
570          $this->cur_spamrule->rule_type    = (string) $spamrule->rule_type;
571          $this->cur_spamrule->rule_content = (string) $spamrule->rule_content;
572         
573          $this->cur_spamrule->insert();
574     }
575     
576     private function insertCategorySingle($category)
577     {
578          $this->cur_category->clean();
579         
580          $m = $this->searchCategory($this->stack['categories'],$category->cat_url);
581         
582          $old_id = $category->cat_id;
583          if ($m !== false)
584          {
585               $cat_id = $m;
586          }
587          else
588          {
589               $cat_id = $this->stack['cat_id'];
590               $category->cat_id = $cat_id;
591               $category->blog_id = $this->blog_id;
592               
593               $this->insertCategory($category);
594               $this->stack['cat_id']++;
595          }
596         
597          $this->old_ids['category'][(integer) $old_id] = $cat_id;
598     }
599     
600     private function insertLinkSingle($link)
601     {
602          $link->blog_id = $this->blog_id;
603          $link->link_id = $this->stack['link_id'];
604         
605          $this->insertLink($link);
606          $this->stack['link_id']++;
607     }
608     
609     private function insertPostSingle($post)
610     {
611          if (!$post->cat_id || isset($this->old_ids['category'][(integer) $post->cat_id])) {
612               $post_id = $this->stack['post_id'];
613               $this->old_ids['post'][(integer) $post->post_id] = $post_id;
614               
615               $cat_id = $post->cat_id ? $this->old_ids['category'][(integer) $post->cat_id] : null;
616               
617               $post->post_id = $post_id;
618               $post->cat_id = $cat_id;
619               $post->blog_id = $this->blog_id;
620               
621               $post->post_url = $this->core->blog->getPostURL(
622                    $post->post_url,$post->post_dt,$post->post_title,$post->post_id
623               );
624               
625               $this->insertPost($post);
626               $this->stack['post_id']++;
627          } else {
628               throw new Exception(__('The backup file does not appear to be well formed.'));
629          }
630     }
631     
632     private function insertMetaSingle($meta)
633     {
634          if (isset($this->old_ids['post'][(integer) $meta->post_id])) {
635               $meta->post_id = $this->old_ids['post'][(integer) $meta->post_id];
636               $this->insertMeta($meta);
637          } else {
638               throw new Exception(__('The backup file does not appear to be well formed.'));
639          }
640     }
641     
642     private function insertMediaSingle($media)
643     {
644          $media_id = $this->stack['media_id'];
645          $old_id = $media->media_id;
646         
647          $media->media_id = $media_id;
648          $media->media_path = $this->core->blog->settings->system->public_path;
649          $media->user_id = $this->getUserId($media->user_id);
650         
651          $this->insertMedia($media);
652          $this->stack['media_id']++;
653          $this->old_ids['media'][(integer) $old_id] = $media_id;
654     }
655     
656     private function insertPostMediaSingle($post_media)
657     {
658          if (isset($this->old_ids['media'][(integer) $post_media->media_id]) &&
659               isset($this->old_ids['post'][(integer) $post_media->post_id])) {
660               $post_media->media_id = $this->old_ids['media'][(integer) $post_media->media_id];
661               $post_media->post_id = $this->old_ids['post'][(integer) $post_media->post_id];
662               
663               $this->insertPostMedia($post_media);
664          } else {
665               throw new Exception(__('The backup file does not appear to be well formed.'));
666          }
667     }
668     
669     private function insertPingSingle($ping)
670     {
671          if (isset($this->old_ids['post'][(integer) $ping->post_id])) {
672               $ping->post_id = $this->old_ids['post'][(integer) $ping->post_id];
673               
674               $this->insertPing($ping);
675          } else {
676               throw new Exception(__('The backup file does not appear to be well formed.'));
677          }
678     }
679     
680     private function insertCommentSingle($comment)
681     {
682          if (isset($this->old_ids['post'][(integer) $comment->post_id])) {
683               $comment_id = $this->stack['comment_id'];
684               
685               $comment->comment_id = $comment_id;
686               $comment->post_id = $this->old_ids['post'][(integer) $comment->post_id];
687               
688               $this->insertComment($comment);
689               $this->stack['comment_id']++;
690          } else {
691               throw new Exception(__('The backup file does not appear to be well formed.'));
692          }
693     }
694     
695     public function searchCategory($rs,$url)
696     {
697          while ($rs->fetch())
698          {
699               if ($rs->cat_url == $url) {
700                    return $rs->cat_id;
701               }
702          }
703         
704          return false;
705     }
706     
707     public function getUserId($user_id)
708     {
709          if (!$this->userExists($user_id))
710          {
711               if ($this->core->auth->isSuperAdmin())
712               {
713                    # Sanitizes user_id and create a lambda user
714                    $user_id = preg_replace('/[^A-Za-z0-9]$/','',$user_id);
715                    $user_id .= strlen($user_id) < 2 ? '-a' : '';
716                   
717                    # We change user_id, we need to check again
718                    if (!$this->userExists($user_id))
719                    {
720                         $this->cur_user->clean();
721                         $this->cur_user->user_id = (string) $user_id;
722                         $this->cur_user->user_pwd = md5(uniqid());
723                         
724                         $this->core->addUser($this->cur_user);
725                         
726                         $this->stack['users'][$user_id] = true;
727                    }
728               }
729               else
730               {
731                    # Returns current user id
732                    $user_id = $this->core->auth->userID();
733               }
734          }
735         
736          return $user_id;
737     }
738     
739     private function userExists($user_id)
740     {
741          if (isset($this->stack['users'][$user_id])) {
742               return $this->stack['users'][$user_id];
743          }
744         
745          $strReq = 'SELECT user_id '.
746                    'FROM '.$this->prefix.'user '.
747                    "WHERE user_id = '".$this->con->escape($user_id)."' ";
748         
749          $rs = $this->con->select($strReq);
750         
751          $this->stack['users'][$user_id] = !$rs->isEmpty();
752          return $this->stack['users'][$user_id];
753     }
754     
755     private function mediaExists()
756     {
757          $strReq = 'SELECT media_id '.
758                    'FROM '.$this->prefix.'media '.
759                    "WHERE media_path = '".$this->cur_media->media_path."' ".
760                    "AND media_file = '".$this->cur_media->media_file."' ";
761         
762          $rs = $this->con->select($strReq);
763         
764          return !$rs->isEmpty();
765     }
766     
767     private function prepareDC12line(&$line)
768     {
769          $settings = array('dc_theme','dc_nb_post_per_page','dc_allow_comments',
770          'dc_allow_trackbacks','dc_comment_pub','dc_comments_ttl',
771          'dc_wiki_comments','dc_use_smilies','dc_date_format','dc_time_format',
772          'dc_url_scan');
773         
774          switch ($line->__name)
775          {
776               case 'categorie':
777                    $line->substitute('cat_libelle','cat_title');
778                    $line->substitute('cat_libelle_url','cat_url');
779                    $line->__name = 'category';
780                    $line->blog_id = 'default';
781                    break;
782               case 'link':
783                    $line->substitute('href','link_href');
784                    $line->substitute('label','link_title');
785                    $line->substitute('title','link_desc');
786                    $line->substitute('lang','link_lang');
787                    $line->substitute('rel','link_xfn');
788                    $line->substitute('position','link_position');
789                    $line->blog_id = 'default';
790                    break;
791               case 'post':
792                    $line->substitute('post_titre','post_title');
793                    $line->post_title = html::decodeEntities($line->post_title);
794                    $line->post_url = date('Y/m/d/',strtotime($line->post_dt)).$line->post_id.'-'.$line->post_titre_url;
795                    $line->post_url = substr($line->post_url,0,255);
796                    $line->post_format = $line->post_content_wiki == '' ? 'xhtml' : 'wiki';
797                    $line->post_content_xhtml = $line->post_content;
798                    $line->post_excerpt_xhtml = $line->post_chapo;
799                   
800                    if ($line->post_format == 'wiki') {
801                         $line->post_content = $line->post_content_wiki;
802                         $line->post_excerpt = $line->post_chapo_wiki;
803                    } else {
804                         $line->post_content = $line->post_content;
805                         $line->post_excerpt = $line->post_chapo;
806                    }
807                   
808                    $line->post_status = (integer) $line->post_pub;
809                    $line->post_type = 'post';
810                    $line->blog_id = 'default';
811                   
812                    $line->drop('post_titre_url','post_content_wiki','post_chapo','post_chapo_wiki','post_pub');
813                   
814                    break;
815               case 'post_meta':
816                    $line->drop('meta_id');
817                    $line->substitute('meta_key','meta_type');
818                    $line->substitute('meta_value','meta_id');
819                    $line->__name = 'meta';
820                    $line->blog_id = 'default';
821                    break;
822               case 'comment':
823                    $line->substitute('comment_auteur','comment_author');
824                    if ($line->comment_site != '' && !preg_match('!^http://.*$!', $line->comment_site,$m)) {
825                         $line->comment_site = 'http://'.$line->comment_site;
826                    }
827                    $line->comment_status = (integer) $line->comment_pub;
828                    $line->drop('comment_pub');
829                    break;
830          }
831         
832          # --BEHAVIOR-- importPrepareDC12
833          $this->core->callBehavior('importPrepareDC12',$line,$this,$this->core);
834     }
835}
836?>
Note: See TracBrowser for help on using the repository browser.

Sites map