ILIAS  release_4-3 Revision
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilForumXMLParser.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2012 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
4 include_once './Services/Xml/classes/class.ilSaxParser.php';
5 require_once 'Modules/Forum/classes/class.ilForumProperties.php';
6 
8 {
15  private $forum;
16  private $entity;
17  private $mapping = array(
18  'frm' => array(),
19  'thr' => array(),
20  'pos' => array()
21  );
22  private $import_install_id = null;
23  private $user_id_mapping = array();
24 
32  public function __construct($forum, $a_xml_data)
33  {
35  $this->forum = $forum;
36  $this->setXMLContent('<?xml version="1.0" encoding="utf-8"?>'.$a_xml_data);
37  $this->aobject = new ilObjUser(ANONYMOUS_USER_ID);
38  }
39 
45  public function setImportDirectory($a_val)
46  {
47  $this->importDirectory = $a_val;
48  }
49 
55  public function getImportDirectory()
56  {
57  return $this->importDirectory;
58  }
65  public function setHandlers($a_xml_parser)
66  {
67  xml_set_object($a_xml_parser, $this);
68  xml_set_element_handler($a_xml_parser, 'handlerBeginTag', 'handlerEndTag');
69  xml_set_character_data_handler($a_xml_parser, 'handlerCharacterData');
70  }
71 
79  public function handlerBeginTag($a_xml_parser, $a_name, $a_attribs)
80  {
81  switch($a_name)
82  {
83  case 'Forum':
84  $this->entity = 'forum';
85  $this->forumArray = array();
86  break;
87 
88  case 'Thread':
89  $this->entity = 'thread';
90  $this->threadArray = array();
91  break;
92 
93  case 'Post':
94  $this->entity = 'post';
95  $this->postArray = array();
96  break;
97 
98  case 'Content':
99  $this->entity = 'content';
100  $this->contentArray = array();
101  break;
102 
103  /* case 'ContentThumbnail':
104  $this->entity = 'content_thumb';
105  $this->content_thumbArray = array();
106  break;*/
107  }
108  }
109 
116  public function handlerEndTag($a_xml_parser, $a_name)
117  {
118  global $ilDB, $ilUser;
119 
120  $this->cdata = trim($this->cdata);
121  $arrayname = strtolower($this->entity).'Array';
122  $x = &$this->$arrayname;
123 
124  switch($a_name)
125  {
126  case 'Forum':
127  $query_num_posts = "SELECT COUNT(pos_pk) cnt
128  FROM frm_posts
129  WHERE pos_top_fk = ".$ilDB->quote(
130  $this->lastHandledForumId, 'integer');
131 
132  $res_pos = $ilDB->query($query_num_posts);
133  $data_pos = $ilDB->fetchAssoc($res_pos);
134  $num_posts = $data_pos['cnt'];
135 
136  $query_num_threads = "SELECT COUNT(thr_pk) cnt
137  FROM frm_threads
138  WHERE thr_top_fk = ".$ilDB->quote(
139  $this->lastHandledForumId, 'integer');
140 
141  $res_thr = $ilDB->query($query_num_threads);
142  $data_thr = $ilDB->fetchAssoc($res_thr);
143  $num_threads = $data_thr['cnt'];
144 
145  $update_str = "$this->lastHandledForumId#$this->lastHandledThreadId#$this->lastHandledPostId";
146  $ilDB->manipulateF(
147  "UPDATE frm_data
148  SET top_last_post = %s,
149  top_num_posts = %s,
150  top_num_threads = %s
151  WHERE top_frm_fk = %s",
152  array('text', 'integer', 'integer', 'integer'),
153  array($update_str, $num_posts, $num_threads, $this->forum_obj_id)
154  );
155  break;
156 
157  case 'Id':
158  $x['Id'] = $this->cdata;
159  break;
160 
161  case 'ObjId':
162  $x['ObjId'] = $this->cdata;
163  break;
164 
165  case 'Title':
166  $x['Title'] = $this->cdata;
167  break;
168 
169  case 'Description':
170  $x['Description'] = $this->cdata;
171  break;
172 
173  case 'DefaultView':
174  $x['DefaultView'] = $this->cdata;
175  break;
176 
177  case 'Pseudonyms':
178  $x['Pseudonyms'] = $this->cdata;
179  break;
180 
181  case 'Statistics':
182  $x['Statistics'] = $this->cdata;
183  break;
184 
185  case 'PostingActivation':
186  $x['PostingActivation'] = $this->cdata;
187  break;
188 
189  case 'PresetSubject':
190  $x['PresetSubject'] = $this->cdata;
191  break;
192 
193  case 'PresetRe':
194  $x['PresetRe'] = $this->cdata;
195  break;
196 
197  case 'NotificationType':
198  $x['NotificationType'] = $this->cdata;
199  break;
200 
201  case 'ForceNotification':
202  $x['ForceNotification'] = $this->cdata;
203  break;
204 
205  case 'ToggleNotification':
206  $x['ToggleNotification'] = $this->cdata;
207  break;
208 
209  case 'LastPost':
210  $x['LastPost'] = $this->cdata;
211  break;
212 
213  case 'Moderator':
214  $x['Moderator'] = $this->cdata;
215  break;
216 
217  case 'CreateDate':
218  $x['CreateDate'] = $this->cdata;
219  break;
220 
221  case 'UpdateDate':
222  $x['UpdateDate'] = $this->cdata;
223  break;
224 
225  case 'UpdateUserId':
226  $x['UpdateUserId'] = $this->cdata;
227  break;
228 
229  case 'UserId':
230  $x['UserId'] = $this->cdata;
231  if( $this->entity == 'forum' && $this->forumArray )
232  {
234  // createSettings accesses superglobal $_GET array, which can cause problems
235  // with public_notifications of block settings
236  $this->forum->createSettings();
237 
238  $forum_array = $this->getUserIdAndAlias(
239  $this->forumArray['UserId'], ''
240  );
241  // Store old user id
242  $oldUsrId = $ilUser->getId();
243  // Manipulate user object
244  $ilUser->setId($forum_array['usr_id']);
245  // create frm_data
246  $this->forum->saveData( array() );
247  // Restore old user id
248  $ilUser->setId($oldUsrId);
249 
250  $update_forum_array = $this->getUserIdAndAlias(
251  $this->forumArray['UpdateUserId'], ''
252  );
253  // Store old user id
254  $oldUsrId = $_SESSION["AccountId"];
255  // Manipulate user object
256  $_SESSION["AccountId"] = $update_forum_array['usr_id'];
257  $this->forum->setTitle($this->forumArray["Title"]);
258  $this->forum->setDescription($this->forumArray["Description"]);
259  $this->forum->update();
260  // Restore old user id
261  $_SESSION["AccountId"] = $oldUsrId;
262 
263  // create frm_settings
264  $newObjProp = ilForumProperties::getInstance(
265  $this->forum->getId() );
266  $newObjProp->setDefaultView(
267  (int) $this->forumArray['DefaultView'] );
268  $newObjProp->setAnonymisation(
269  (int) $this->forumArray['Pseudonyms'] );
270  $newObjProp->setStatisticsStatus(
271  (int) $this->forumArray['Statistics'] );
272  $newObjProp->setPostActivation(
273  (int) $this->forumArray['PostingActivation'] );
274  $newObjProp->setPresetSubject(
275  (int) $this->forumArray['PresetSubject'] );
276  $newObjProp->setAddReSubject(
277  (int) $this->forumArray['PresetRe'] );
278  $newObjProp->setNotificationType(
279  $this->forumArray['NotificationType'] ?
280  $this->forumArray['NotificationType'] : 'all_users');
281  $newObjProp->setAdminForceNoti(
282  (int) $this->forumArray['ForceNotification'] );
283  $newObjProp->setUserToggleNoti(
284  (int) $this->forumArray['ToggleNotification'] );
285  $newObjProp->insert();
286 
287  $id = $this->getNewForumPk();
288  $this->forum_obj_id = $newObjProp->getObjId();
289  $this->mapping['frm'][$this->forumArray['Id']] = $id;
290  $this->lastHandledForumId = $id;
291 
292  unset($this->forumArray);
293  }
294 
295  break;
296 
297  case 'Thread':
298  $update_str = "$this->lastHandledForumId#$this->lastHandledThreadId#$this->lastHandledPostId";
299  $ilDB->manipulateF(
300  "UPDATE frm_threads
301  SET thr_last_post = %s
302  WHERE thr_pk = %s",
303  array('text', 'integer'),
304  array($update_str, $this->lastHandledThreadId)
305  );
306  break;
307 
308  case 'Subject':
309  $x['Subject'] = $this->cdata;
310  break;
311 
312  case 'Alias':
313  $x['Alias'] = $this->cdata;
314  break;
315 
316  case 'Sticky':
317  $x['Sticky'] = $this->cdata;
318  break;
319 
320  case 'Closed':
321  $x['Closed'] = $this->cdata;
322 
323  if($this->entity == 'thread' && $this->threadArray)
324  {
325  require_once 'Modules/Forum/classes/class.ilForumTopic.php';
326 
327  $this->forumThread = new ilForumTopic();
328  $this->forumThread->setId($this->threadArray['Id']);
329  $this->forumThread->setForumId( $this->lastHandledForumId );
330  $this->forumThread->setSubject($this->threadArray['Subject']);
331  $this->forumThread->setSticky($this->threadArray['Sticky']);
332  $this->forumThread->setClosed($this->threadArray['Closed']);
333  $this->forumThread->setCreateDate($this->threadArray['CreateDate']);
334  $this->forumThread->setChangeDate($this->threadArray['UpdateDate']);
335  $this->forumThread->setImportName($this->threadArray['ImportName']);
336 
337  $usr_data = $this->getUserIdAndAlias(
338  $this->threadArray['UserId'], $this->threadArray['Alias']
339  );
340 
341  $this->forumThread->setUserId($usr_data['usr_id']);
342  $this->forumThread->setUserAlias($usr_data['usr_alias']);
343 
344  $this->forumThread->insert();
345 
346  $this->mapping['thr'][$this->threadArray['Id']] = $this->forumThread->getId();
347  $this->lastHandledThreadId = $this->forumThread->getId();
348 
349  unset($this->threadArray);
350  }
351 
352  break;
353 
354  case 'Post':
355  break;
356 
357  case 'Censorship':
358  $x['Censorship'] = $this->cdata;
359  break;
360 
361  case 'CensorshipMessage':
362  $x['CensorshipMessage'] = $this->cdata;
363  break;
364 
365  case 'Notification':
366  $x['Notification'] = $this->cdata;
367  break;
368 
369  case 'ImportName':
370  $x['ImportName'] = $this->cdata;
371  break;
372 
373  case 'Status':
374  $x['Status'] = $this->cdata;
375  break;
376 
377  case 'Message':
378  $x['Message'] = $this->cdata;
379  break;
380 
381  case 'Lft':
382  $x['Lft'] = $this->cdata;
383  break;
384 
385  case 'Rgt':
386  $x['Rgt'] = $this->cdata;
387  break;
388 
389  case 'Depth':
390  $x['Depth'] = $this->cdata;
391  break;
392 
393  case 'ParentId':
394  $x['ParentId'] = $this->cdata;
395 
396  if($this->entity == 'post' && $this->postArray)
397  {
398  require_once 'Modules/Forum/classes/class.ilForumPost.php';
399 
400  $this->forumPost = new ilForumPost();
401  $this->forumPost->setId($this->postArray['Id']);
402  $this->forumPost->setCensorship($this->postArray['Censorship']);
403  $this->forumPost->setCensorshipComment($this->postArray['CensorshipMessage']);
404  $this->forumPost->setNotification($this->postArray['Notification']);
405  $this->forumPost->setImportName($this->postArray['ImportName']);
406  $this->forumPost->setStatus($this->postArray['Status']);
407  $this->forumPost->setMessage($this->postArray['Message']);
408  $this->forumPost->setSubject($this->postArray['Subject']);
409  $this->forumPost->setLft($this->postArray['Lft']);
410  $this->forumPost->setRgt($this->postArray['Rgt']);
411  $this->forumPost->setDepth($this->postArray['Depth']);
412  $this->forumPost->setParentId($this->postArray['ParentId']);
413  $this->forumPost->setThread($this->forumThread);
414  $this->forumPost->setThreadId($this->lastHandledThreadId);
415  $this->forumPost->setForumId($this->lastHandledForumId);
416  $this->forumPost->setCreateDate($this->postArray['CreateDate']);
417  $this->forumPost->setChangeDate($this->postArray['UpdateDate']);
418 
419  $usr_data = $this->getUserIdAndAlias(
420  $this->postArray['UserId'], $this->postArray['Alias']
421  );
422  $update_usr_data = $this->getUserIdAndAlias(
423  $this->postArray['UpdateUserId']
424  );
425 
426  $this->forumPost->setUserId($usr_data['usr_id']);
427  $this->forumPost->setUserAlias($usr_data['usr_alias']);
428  $this->forumPost->setUpdateUserId($update_usr_data['usr_id']);
429 
430  $this->forumPost->insert();
431 
432  if($this->mapping['pos'][$this->postArray['ParentId']])
433  {
434  $parentId = $this->mapping['pos'][$this->postArray['ParentId']];
435  }
436  else
437  {
438  $parentId = 0;
439  }
440 
441  $postTreeNodeId = $ilDB->nextId('frm_posts_tree');
442  $ilDB->insert('frm_posts_tree', array(
443  'fpt_pk' => array('integer', $postTreeNodeId),
444  'thr_fk' => array('integer', $this->lastHandledThreadId),
445  'pos_fk' => array('integer', $this->forumPost->getId()),
446  'parent_pos' => array('integer', $parentId),
447  'lft' => array('integer', $this->postArray['Lft']),
448  'rgt' => array('integer', $this->postArray['Rgt']),
449  'depth' => array('integer', $this->postArray['Depth']),
450  'fpt_date' => array('timestamp', date('Y-m-d H:i:s'))
451  ));
452 
453  $this->mapping['pos'][$this->postArray['Id']] = $this->forumPost->getId();
454  $this->lastHandledPostId = $this->forumPost->getId();
455 
456  unset($this->postArray);
457  }
458 
459  break;
460 
461  case 'Content':
462  $x['content'] = $this->cdata;
463  break;
464 
465  /* case 'ContentThumbnail':
466  $x['content_thumb'] = $this->cdata;
467  break;*/
468 
469  case 'Attachment':
470  $filedata = new ilFileDataForum($this->forum->getId(), $this->lastHandledPostId);
471 
472  $importPath = $this->contentArray['content'];
473 
474  if( strlen($importPath) )
475  {
476  $importPath = $this->getImportDirectory().'/'.$importPath;
477 
478  $newFilename = preg_replace("/^\d+_\d+(_.*)/ims", $this->forum->getId()."_".$this->lastHandledPostId."$1", basename($importPath));
479  $path = $filedata->getForumPath();
480  $newPath = $path.'/'.$newFilename;
481  @copy($importPath, $newPath);
482  }
483 
484  /* $importPathThumbnail = $this->content_thumbArray['content_thumb'];
485 
486  if( strlen($importPathThumbnail) )
487  {
488  $importPathThumbnail = $this->getImportDirectory().'/'.$importPathThumbnail;
489 
490  $newThumbFilename = preg_replace("/^\d+_\d+(_.*)/ims", $this->forum->getId()."_".$this->lastHandledPostId."$1", basename($importPathThumbnail));
491  $thumbPath = $filedata->getThumbPath();
492  $newThumbnailPath = $thumbPath.'/'.$newThumbFilename;
493  @copy($importPathThumbnail, $newThumbnailPath);
494  }*/
495 
496  break;
497  }
498 
499  $this->cdata = '';
500 
501  return;
502  }
503 
504  private function getIdAndAliasArray($imp_usr_id, $param = 'import')
505  {
506  global $ilDB;
507 
508  $select = 'SELECT od.obj_id, ud.login
509  FROM object_data od
510  INNER JOIN usr_data ud
511  ON od.obj_id = ud.usr_id';
512 
513  if($param == 'import')
514  {
515  $where = ' WHERE od.import_id = '.$ilDB->quote(
516  'il_'.$this->import_install_id.'_usr_'.$imp_usr_id, 'text'
517  );
518  }
519 
520  if($param == 'user')
521  {
522  $where = ' WHERE ud.usr_id = '.$ilDB->quote(
523  $imp_usr_id, 'integer'
524  );
525  }
526 
527  $query = $ilDB->query($select.$where);
528 
529  while( $res = $ilDB->fetchAssoc($query) )
530  {
531  break;
532  }
533 
534  if($res)
535  {
536  return array(
537  'usr_id' => $res['obj_id'],
538  'usr_alias' => $res['login']
539  );
540  }
541  else
542  {
543  return false;
544  }
545  }
546 
547  private function getAnonymousArray()
548  {
549  return array(
550  'usr_id' => $this->aobject->getId(),
551  'usr_alias' => $this->aobject->getLogin()
552  );
553  }
554 
555 
556  private function getUserIdAndAlias($imp_usr_id, $imp_usr_alias = '')
557  {
558  global $ilDB;
559 
560  if( (int)$imp_usr_id > 0 )
561  {
562  $newUsrId = -1;
563 
564  if( $this->import_install_id != IL_INST_ID && IL_INST_ID > 0 )
565  {
566  // Different installations
567  if( $this->user_id_mapping[$imp_usr_id] )
568  {
569  return $this->user_id_mapping[$imp_usr_id];
570  }
571  else
572  {
573  $res = $this->getIdAndAliasArray($imp_usr_id, 'import');
574 
575  if($res)
576  {
577  $this->user_id_mapping[$imp_usr_id] = $res;
578 
579  return $res;
580  }
581  else
582  {
583  $return_value = $this->getAnonymousArray();
584  $this->user_id_mapping[$imp_usr_id] = $return_value;
585 
586  return $return_value;
587  }
588  }
589  }
590  else if( $this->import_install_id == IL_INST_ID && IL_INST_ID == 0 )
591  {
592  // Eventually different installations. We cannot determine it.
593  if( $this->user_id_mapping[$imp_usr_id] )
594  {
595  return $this->user_id_mapping[$imp_usr_id];
596  }
597  else
598  {
599  $res = $this->getIdAndAliasArray($imp_usr_id, 'import');
600 
601  if($res)
602  {
603  $this->user_id_mapping[$imp_usr_id] = $res;
604 
605  return $res;
606  }
607  else
608  {
609  // Same installation
610  if( $this->user_id_mapping[$imp_usr_id] )
611  {
612  return $this->user_id_mapping[$imp_usr_id];
613  }
614  else
615  {
616  $res = $this->getIdAndAliasArray($imp_usr_id, 'user');
617 
618  if($res)
619  {
620  $this->user_id_mapping[$imp_usr_id] = $res;
621 
622  return $res;
623  }
624  else
625  {
626  $return_value = $this->getAnonymousArray();
627  $this->user_id_mapping[$imp_usr_id] = $return_value;
628 
629  return $return_value;
630  }
631  }
632  }
633  }
634  }
635  else
636  {
637  // Same installation
638  if( $this->user_id_mapping[$imp_usr_id] )
639  {
640  return $this->user_id_mapping[$imp_usr_id];
641  }
642  else
643  {
644  $res = $this->getIdAndAliasArray($imp_usr_id, 'user');
645 
646  if($res)
647  {
648  $this->user_id_mapping[$imp_usr_id] = $res;
649 
650  return $res;
651  }
652  else
653  {
654  $return_value = $this->getAnonymousArray();
655  $this->user_id_mapping[$imp_usr_id] = $return_value;
656 
657  return $return_value;
658  }
659  }
660  }
661  }
662  else
663  {
664  return array(
665  'usr_id' => $imp_usr_id,
666  'usr_alias' => $imp_usr_alias
667  );
668  }
669  }
670 
671  public function setImportInstallId($id)
672  {
673  $this->import_install_id = $id;
674  }
675 
676  private function getNewForumPk()
677  {
678  global $ilDB;
679 
680  $query = "SELECT top_pk FROM frm_data
681  WHERE top_frm_fk = ".$ilDB->quote(
682  $this->forum->getId(), 'integer');
683  $res = $ilDB->query($query);
684  $data = $ilDB->fetchAssoc($res);
685 
686  return $data['top_pk'];
687  }
688 
695  public function handlerCharacterData($a_xml_parser,$a_data)
696  {
697  if($a_data != "\n")
698  {
699  // Replace multiple tabs with one space
700  $a_data = preg_replace("/\t+/"," ",$a_data);
701 
702  $this->cdata .= $a_data;
703  }
704  }
705 
712  public function start()
713  {
714  $this->startParsing();
715  return $this->result > 0;
716  }
717 }
718 ?>