Dotclear

source: plugins/importExport/inc/flat/class.flat.import.php @ 3148:54da6965988e

Revision 3148:54da6965988e, 27.7 KB checked in by franck <carnet.franck.paul@…>, 10 years ago (diff)

Fix types of some imported fields (flat import), closes #2148

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

Sites map