Dotclear

source: plugins/importExport/inc/flat/class.dc.import.php @ 0:54703be25dd6

Revision 0:54703be25dd6, 25.9 KB checked in by Dsls <dsls@…>, 14 years ago (diff)

2.3 branch (trunk) first checkin

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

Sites map