Dotclear

source: plugins/importExport/inc/flat/class.flat.import.php @ 3323:a7a3516ead72

Revision 3323:a7a3516ead72, 27.8 KB checked in by franck <carnet.franck.paul@…>, 9 years ago (diff)

Cope with post.post_position field during flat import, closes #2200

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

Sites map