ILIAS  release_5-1 Revision 5.0.0-5477-g43f3e3fab5f
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 include_once "./Services/MediaObjects/classes/class.ilObjMediaObject.php";
7 
9 {
16  private $forum;
17  private $entity;
18  private $mapping = array(
19  'frm' => array(),
20  'thr' => array(),
21  'pos' => array()
22  );
23  private $import_install_id = null;
24  private $user_id_mapping = array();
25  protected $mediaObjects = array();
26 
30  protected $schema_version = null;
31 
39  public function __construct($forum, $a_xml_data)
40  {
41  parent::__construct();
42  $this->forum = $forum;
43  $this->setXMLContent('<?xml version="1.0" encoding="utf-8"?>'.$a_xml_data);
44  $this->aobject = new ilObjUser(ANONYMOUS_USER_ID);
45  }
46 
52  public function setImportDirectory($a_val)
53  {
54  $this->importDirectory = $a_val;
55  }
56 
62  public function getImportDirectory()
63  {
64  return $this->importDirectory;
65  }
66 
70  public function getSchemaVersion()
71  {
72  return $this->schema_version;
73  }
74 
79  {
80  $this->schema_version = $schema_version;
81  }
82 
89  public function setHandlers($a_xml_parser)
90  {
91  xml_set_object($a_xml_parser, $this);
92  xml_set_element_handler($a_xml_parser, 'handlerBeginTag', 'handlerEndTag');
93  xml_set_character_data_handler($a_xml_parser, 'handlerCharacterData');
94  }
95 
103  public function handlerBeginTag($a_xml_parser, $a_name, $a_attribs)
104  {
105  switch($a_name)
106  {
107  case 'Forum':
108  $this->entity = 'forum';
109  $this->forumArray = array();
110  break;
111 
112  case 'Thread':
113  $this->entity = 'thread';
114  $this->threadArray = array();
115  break;
116 
117  case 'Post':
118  $this->mediaObjects = array();
119  $this->entity = 'post';
120  $this->postArray = array();
121  break;
122 
123  case 'Content':
124  $this->entity = 'content';
125  $this->contentArray = array();
126  break;
127 
128  case 'MediaObject':
129  $this->mediaObjects[] = $a_attribs;
130  break;
131  }
132  }
133 
140  public function handlerEndTag($a_xml_parser, $a_name)
141  {
142  global $ilDB, $ilUser;
143 
144  $this->cdata = trim($this->cdata);
145  $arrayname = strtolower($this->entity).'Array';
146  $x = &$this->$arrayname;
147 
148  switch($a_name)
149  {
150  case 'Forum':
151  $query_num_posts = "SELECT COUNT(pos_pk) cnt
152  FROM frm_posts
153  WHERE pos_top_fk = ".$ilDB->quote(
154  $this->lastHandledForumId, 'integer');
155 
156  $res_pos = $ilDB->query($query_num_posts);
157  $data_pos = $ilDB->fetchAssoc($res_pos);
158  $num_posts = $data_pos['cnt'];
159 
160  $query_num_threads = "SELECT COUNT(thr_pk) cnt
161  FROM frm_threads
162  WHERE thr_top_fk = ".$ilDB->quote(
163  $this->lastHandledForumId, 'integer');
164 
165  $res_thr = $ilDB->query($query_num_threads);
166  $data_thr = $ilDB->fetchAssoc($res_thr);
167  $num_threads = $data_thr['cnt'];
168 
169  $update_str = "$this->lastHandledForumId#$this->lastHandledThreadId#$this->lastHandledPostId";
170  $ilDB->manipulateF(
171  "UPDATE frm_data
172  SET top_last_post = %s,
173  top_num_posts = %s,
174  top_num_threads = %s,
175  top_usr_id = %s
176  WHERE top_frm_fk = %s",
177  array('text', 'integer', 'integer', 'integer', 'integer'),
178  array($update_str, $num_posts, $num_threads, $this->frm_last_mapped_top_usr_id, $this->forum_obj_id)
179  );
180  break;
181 
182  case 'Id':
183  $x['Id'] = $this->cdata;
184  break;
185 
186  case 'ObjId':
187  $x['ObjId'] = $this->cdata;
188  break;
189 
190  case 'Title':
191  $x['Title'] = $this->cdata;
192  break;
193 
194  case 'Description':
195  $x['Description'] = $this->cdata;
196  break;
197 
198  case 'DefaultView':
199  $x['DefaultView'] = $this->cdata;
200  break;
201 
202  case 'Pseudonyms':
203  $x['Pseudonyms'] = $this->cdata;
204  break;
205 
206  case 'Statistics':
207  $x['Statistics'] = $this->cdata;
208  break;
209 
210  case 'ThreadRatings':
211  $x['ThreadRatings'] = $this->cdata;
212  break;
213 
214  case 'PostingActivation':
215  $x['PostingActivation'] = $this->cdata;
216  break;
217 
218  case 'PresetSubject':
219  $x['PresetSubject'] = $this->cdata;
220  break;
221 
222  case 'PresetRe':
223  $x['PresetRe'] = $this->cdata;
224  break;
225 
226  case 'NotificationType':
227  $x['NotificationType'] = $this->cdata;
228  break;
229 
230  case 'ForceNotification':
231  $x['ForceNotification'] = $this->cdata;
232  break;
233 
234  case 'ToggleNotification':
235  $x['ToggleNotification'] = $this->cdata;
236  break;
237 
238  case 'LastPost':
239  $x['LastPost'] = $this->cdata;
240  break;
241 
242  case 'Moderator':
243  $x['Moderator'] = $this->cdata;
244  break;
245 
246  case 'CreateDate':
247  $x['CreateDate'] = $this->cdata;
248  break;
249 
250  case 'UpdateDate':
251  $x['UpdateDate'] = $this->cdata;
252  break;
253 
254  case 'FileUpload':
255  $x['FileUpload'] = $this->cdata;
256  break;
257 
258  case 'UpdateUserId':
259  $x['UpdateUserId'] = $this->cdata;
260  break;
261 
262  case 'AuthorId':
263  $x['AuthorId'] = $this->cdata;
264  break;
265  case 'isAuthorModerator':
266  $x['isAuthorModerator'] = $this->cdata;
267  break;
268 
269  case 'UserId':
270  $x['UserId'] = $this->cdata;
271  if( $this->entity == 'forum' && $this->forumArray )
272  {
274  // createSettings accesses superglobal $_GET array, which can cause problems
275  // with public_notifications of block settings
276  $this->forum->createSettings();
277 
278  $forum_array = $this->getUserIdAndAlias(
279  $this->forumArray['UserId'], ''
280  );
281 
282  $this->frm_last_mapped_top_usr_id = $forum_array['usr_id'];
283 
284  $update_forum_array = $this->getUserIdAndAlias(
285  $this->forumArray['UpdateUserId'], ''
286  );
287  // Store old user id
288  $oldUsrId = $_SESSION["AccountId"];
289  // Manipulate user object
290  $_SESSION["AccountId"] = $update_forum_array['usr_id'];
291  $this->forum->setTitle($this->forumArray["Title"]);
292  $this->forum->setDescription($this->forumArray["Description"]);
293  $this->forum->update();
294  // Restore old user id
295  $_SESSION["AccountId"] = $oldUsrId;
296 
297  // create frm_settings
298  $newObjProp = ilForumProperties::getInstance($this->forum->getId());
299  $newObjProp->setDefaultView((int)$this->forumArray['DefaultView']);
300  $newObjProp->setAnonymisation((int)$this->forumArray['Pseudonyms']);
301  $newObjProp->setStatisticsStatus((int)$this->forumArray['Statistics']);
302  $newObjProp->setIsThreadRatingEnabled((int)$this->forumArray['ThreadRatings']);
303  $newObjProp->setPostActivation((int)$this->forumArray['PostingActivation']);
304  $newObjProp->setPresetSubject((int)$this->forumArray['PresetSubject']);
305  $newObjProp->setAddReSubject((int)$this->forumArray['PresetRe']);
306  $newObjProp->setNotificationType($this->forumArray['NotificationType'] ? $this->forumArray['NotificationType'] : 'all_users');
307  $newObjProp->setAdminForceNoti((int)$this->forumArray['ForceNotification']);
308  $newObjProp->setUserToggleNoti((int)$this->forumArray['ToggleNotification']);
309  $newObjProp->setFileUploadAllowed((int)$this->forumArray['FileUpload']);
310  $newObjProp->setThreadSorting($this->forumArray['Sorting']);
311  $newObjProp->setMarkModeratorPosts($this->forumArray['MarkModeratorPosts']);
312  $newObjProp->update();
313 
314  $id = $this->getNewForumPk();
315  $this->forum_obj_id = $newObjProp->getObjId();
316  $this->mapping['frm'][$this->forumArray['Id']] = $id;
317  $this->lastHandledForumId = $id;
318 
319  unset($this->forumArray);
320  }
321 
322  break;
323 
324  case 'Thread':
325  $update_str = "$this->lastHandledForumId#$this->lastHandledThreadId#$this->lastHandledPostId";
326  $ilDB->manipulateF(
327  "UPDATE frm_threads
328  SET thr_last_post = %s
329  WHERE thr_pk = %s",
330  array('text', 'integer'),
331  array($update_str, $this->lastHandledThreadId)
332  );
333  break;
334 
335  case 'Subject':
336  $x['Subject'] = $this->cdata;
337  break;
338 
339  case 'Alias':
340  $x['Alias'] = $this->cdata;
341  break;
342 
343  case 'Sticky':
344  $x['Sticky'] = $this->cdata;
345  break;
346 
347  case 'Sorting':
348  $x['Sorting'] = $this->cdata;
349  break;
350 
351  case 'MarkModeratorPosts':
352  $x['MarkModeratorPosts'] = $this->cdata;
353  break;
354 
355  case 'Closed':
356  $x['Closed'] = $this->cdata;
357 
358  if($this->entity == 'thread' && $this->threadArray)
359  {
360  require_once 'Modules/Forum/classes/class.ilForumTopic.php';
361 
362  $this->forumThread = new ilForumTopic();
363  $this->forumThread->setId($this->threadArray['Id']);
364  $this->forumThread->setForumId( $this->lastHandledForumId );
365  $this->forumThread->setSubject($this->threadArray['Subject']);
366  $this->forumThread->setSticky($this->threadArray['Sticky']);
367  $this->forumThread->setClosed($this->threadArray['Closed']);
368  $this->forumThread->setCreateDate($this->threadArray['CreateDate']);
369  $this->forumThread->setChangeDate($this->threadArray['UpdateDate']);
370  $this->forumThread->setImportName($this->threadArray['ImportName']);
371 
372  $usr_data = $this->getUserIdAndAlias(
373  $this->threadArray['UserId'], $this->threadArray['Alias']
374  );
375 
376  $this->forumThread->setDisplayUserId($usr_data['usr_id']);
377  $this->forumThread->setUserAlias($usr_data['usr_alias']);
378 
379  if(version_compare($this->getSchemaVersion(), '4.5.0', '<='))
380  {
381  $this->threadArray['AuthorId'] = $this->threadArray['UserId'];
382  }
383 
384  $author_id_data = $this->getUserIdAndAlias(
385  $this->threadArray['AuthorId']
386  );
387  $this->forumThread->setThrAuthorId((int)$author_id_data['usr_id']);
388 
389  $this->forumThread->insert();
390 
391  $this->mapping['thr'][$this->threadArray['Id']] = $this->forumThread->getId();
392  $this->lastHandledThreadId = $this->forumThread->getId();
393 
394  unset($this->threadArray);
395  }
396 
397  break;
398 
399  case 'Post':
400  break;
401 
402  case 'Censorship':
403  $x['Censorship'] = $this->cdata;
404  break;
405 
406  case 'CensorshipMessage':
407  $x['CensorshipMessage'] = $this->cdata;
408  break;
409 
410  case 'Notification':
411  $x['Notification'] = $this->cdata;
412  break;
413 
414  case 'ImportName':
415  $x['ImportName'] = $this->cdata;
416  break;
417 
418  case 'Status':
419  $x['Status'] = $this->cdata;
420  break;
421 
422  case 'Message':
423  $x['Message'] = $this->cdata;
424  break;
425 
426  case 'Lft':
427  $x['Lft'] = $this->cdata;
428  break;
429 
430  case 'Rgt':
431  $x['Rgt'] = $this->cdata;
432  break;
433 
434  case 'Depth':
435  $x['Depth'] = $this->cdata;
436  break;
437 
438  case 'ParentId':
439  $x['ParentId'] = $this->cdata;
440 
441  if($this->entity == 'post' && $this->postArray)
442  {
443  require_once 'Modules/Forum/classes/class.ilForumPost.php';
444 
445  $this->forumPost = new ilForumPost();
446  $this->forumPost->setId($this->postArray['Id']);
447  $this->forumPost->setCensorship($this->postArray['Censorship']);
448  $this->forumPost->setCensorshipComment($this->postArray['CensorshipMessage']);
449  $this->forumPost->setNotification($this->postArray['Notification']);
450  $this->forumPost->setImportName($this->postArray['ImportName']);
451  $this->forumPost->setStatus($this->postArray['Status']);
452  $this->forumPost->setMessage($this->postArray['Message']);
453  $this->forumPost->setSubject($this->postArray['Subject']);
454  $this->forumPost->setLft($this->postArray['Lft']);
455  $this->forumPost->setRgt($this->postArray['Rgt']);
456  $this->forumPost->setDepth($this->postArray['Depth']);
457  $this->forumPost->setParentId($this->postArray['ParentId']);
458  $this->forumPost->setThread($this->forumThread);
459  $this->forumPost->setThreadId($this->lastHandledThreadId);
460  $this->forumPost->setForumId($this->lastHandledForumId);
461  $this->forumPost->setCreateDate($this->postArray['CreateDate']);
462  $this->forumPost->setChangeDate($this->postArray['UpdateDate']);
463 
464  $usr_data = $this->getUserIdAndAlias(
465  $this->postArray['UserId'], $this->postArray['Alias']
466  );
467  $update_usr_data = $this->getUserIdAndAlias(
468  $this->postArray['UpdateUserId']
469  );
470  $this->forumPost->setDisplayUserId($usr_data['usr_id']);
471  $this->forumPost->setUserAlias($usr_data['usr_alias']);
472  $this->forumPost->setUpdateUserId($update_usr_data['usr_id']);
473 
474  if(version_compare($this->getSchemaVersion(), '4.5.0', '<='))
475  {
476  $this->postArray['AuthorId'] = $this->postArray['UserId'];
477  }
478  $author_id_data = $this->getUserIdAndAlias(
479  $this->postArray['AuthorId']
480  );
481  $this->forumPost->setPosAuthorId((int)$author_id_data['usr_id']);
482 
483  if($this->postArray['isAuthorModerator'] === 'NULL')
484  {
485  $this->forumPost->setIsAuthorModerator(NULL);
486  }
487  else
488  {
489  $this->forumPost->setIsAuthorModerator((int)$this->postArray['isAuthorModerator']);
490  }
491 
492  $this->forumPost->insert();
493 
494  if($this->mapping['pos'][$this->postArray['ParentId']])
495  {
496  $parentId = $this->mapping['pos'][$this->postArray['ParentId']];
497  }
498  else
499  {
500  $parentId = 0;
501  }
502 
503  $postTreeNodeId = $ilDB->nextId('frm_posts_tree');
504  $ilDB->insert('frm_posts_tree', array(
505  'fpt_pk' => array('integer', $postTreeNodeId),
506  'thr_fk' => array('integer', $this->lastHandledThreadId),
507  'pos_fk' => array('integer', $this->forumPost->getId()),
508  'parent_pos' => array('integer', $parentId),
509  'lft' => array('integer', $this->postArray['Lft']),
510  'rgt' => array('integer', $this->postArray['Rgt']),
511  'depth' => array('integer', $this->postArray['Depth']),
512  'fpt_date' => array('timestamp', date('Y-m-d H:i:s'))
513  ));
514 
515  $this->mapping['pos'][$this->postArray['Id']] = $this->forumPost->getId();
516  $this->lastHandledPostId = $this->forumPost->getId();
517 
518  $media_objects_found = false;
519  foreach($this->mediaObjects as $mob_attr)
520  {
521  $importfile = $this->getImportDirectory().'/'.$mob_attr['uri'];
522  if(file_exists($importfile))
523  {
524  $mob = ilObjMediaObject::_saveTempFileAsMediaObject(basename($importfile), $importfile, false);
525  ilObjMediaObject::_saveUsage($mob->getId(), "frm:html", $this->forumPost->getId());
526 
527  $this->forumPost->setMessage(
528  str_replace(
529  array(
530  "src=\"" . $mob_attr["label"] . "\"",
531  "src=\"" . preg_replace("/(il)_[\d]+_(mob)_([\d]+)/", "$1_0_$2_$3", $mob_attr["label"]) . "\""
532  ),
533  "src=\"" . "il_" . IL_INST_ID . "_mob_" . $mob->getId() . "\"",
534  $this->forumPost->getMessage()
535  )
536  );
537  $media_objects_found = true;
538  }
539  }
540 
541  if($media_objects_found)
542  {
543  $this->forumPost->update();
544  }
545 
546  unset($this->postArray);
547  }
548 
549  break;
550 
551  case 'Content':
552  $x['content'] = $this->cdata;
553  break;
554 
555  case 'Attachment':
556  $filedata = new ilFileDataForum($this->forum->getId(), $this->lastHandledPostId);
557 
558  $importPath = $this->contentArray['content'];
559 
560  if( strlen($importPath) )
561  {
562  $importPath = $this->getImportDirectory().'/'.$importPath;
563 
564  $newFilename = preg_replace("/^\d+_\d+(_.*)/ims", $this->forum->getId()."_".$this->lastHandledPostId."$1", basename($importPath));
565  $path = $filedata->getForumPath();
566  $newPath = $path.'/'.$newFilename;
567  @copy($importPath, $newPath);
568  }
569  break;
570  }
571 
572  $this->cdata = '';
573 
574  return;
575  }
576 
577  private function getIdAndAliasArray($imp_usr_id, $param = 'import')
578  {
579  global $ilDB;
580 
581  $select = 'SELECT od.obj_id, ud.login
582  FROM object_data od
583  INNER JOIN usr_data ud
584  ON od.obj_id = ud.usr_id';
585 
586  if($param == 'import')
587  {
588  $where = ' WHERE od.import_id = '.$ilDB->quote(
589  'il_'.$this->import_install_id.'_usr_'.$imp_usr_id, 'text'
590  );
591  }
592 
593  if($param == 'user')
594  {
595  $where = ' WHERE ud.usr_id = '.$ilDB->quote(
596  $imp_usr_id, 'integer'
597  );
598  }
599 
600  $query = $ilDB->query($select.$where);
601 
602  while( $res = $ilDB->fetchAssoc($query) )
603  {
604  break;
605  }
606 
607  if($res)
608  {
609  return array(
610  'usr_id' => $res['obj_id'],
611  'usr_alias' => $res['login']
612  );
613  }
614  else
615  {
616  return false;
617  }
618  }
619 
620  private function getAnonymousArray()
621  {
622  return array(
623  'usr_id' => $this->aobject->getId(),
624  'usr_alias' => $this->aobject->getLogin()
625  );
626  }
627 
628 
629  private function getUserIdAndAlias($imp_usr_id, $imp_usr_alias = '')
630  {
631  global $ilDB;
632 
633  if( (int)$imp_usr_id > 0 )
634  {
635  $newUsrId = -1;
636 
637  if( $this->import_install_id != IL_INST_ID && IL_INST_ID > 0 )
638  {
639  // Different installations
640  if( $this->user_id_mapping[$imp_usr_id] )
641  {
642  return $this->user_id_mapping[$imp_usr_id];
643  }
644  else
645  {
646  $res = $this->getIdAndAliasArray($imp_usr_id, 'import');
647 
648  if($res)
649  {
650  $this->user_id_mapping[$imp_usr_id] = $res;
651 
652  return $res;
653  }
654  else
655  {
656  $return_value = $this->getAnonymousArray();
657  $this->user_id_mapping[$imp_usr_id] = $return_value;
658 
659  return $return_value;
660  }
661  }
662  }
663  else if( $this->import_install_id == IL_INST_ID && IL_INST_ID == 0 )
664  {
665  // Eventually different installations. We cannot determine it.
666  if( $this->user_id_mapping[$imp_usr_id] )
667  {
668  return $this->user_id_mapping[$imp_usr_id];
669  }
670  else
671  {
672  $res = $this->getIdAndAliasArray($imp_usr_id, 'import');
673 
674  if($res)
675  {
676  $this->user_id_mapping[$imp_usr_id] = $res;
677 
678  return $res;
679  }
680  else
681  {
682  // Same installation
683  if( $this->user_id_mapping[$imp_usr_id] )
684  {
685  return $this->user_id_mapping[$imp_usr_id];
686  }
687  else
688  {
689  $res = $this->getIdAndAliasArray($imp_usr_id, 'user');
690 
691  if($res)
692  {
693  $this->user_id_mapping[$imp_usr_id] = $res;
694 
695  return $res;
696  }
697  else
698  {
699  $return_value = $this->getAnonymousArray();
700  $this->user_id_mapping[$imp_usr_id] = $return_value;
701 
702  return $return_value;
703  }
704  }
705  }
706  }
707  }
708  else
709  {
710  // Same installation
711  if( $this->user_id_mapping[$imp_usr_id] )
712  {
713  return $this->user_id_mapping[$imp_usr_id];
714  }
715  else
716  {
717  $res = $this->getIdAndAliasArray($imp_usr_id, 'user');
718 
719  if($res)
720  {
721  $this->user_id_mapping[$imp_usr_id] = $res;
722 
723  return $res;
724  }
725  else
726  {
727  $return_value = $this->getAnonymousArray();
728  $this->user_id_mapping[$imp_usr_id] = $return_value;
729 
730  return $return_value;
731  }
732  }
733  }
734  }
735  else
736  {
737  return array(
738  'usr_id' => $imp_usr_id,
739  'usr_alias' => $imp_usr_alias
740  );
741  }
742  }
743 
744  public function setImportInstallId($id)
745  {
746  $this->import_install_id = $id;
747  }
748 
749  private function getNewForumPk()
750  {
751  global $ilDB;
752 
753  $query = "SELECT top_pk FROM frm_data
754  WHERE top_frm_fk = ".$ilDB->quote(
755  $this->forum->getId(), 'integer');
756  $res = $ilDB->query($query);
757  $data = $ilDB->fetchAssoc($res);
758 
759  return $data['top_pk'];
760  }
761 
768  public function handlerCharacterData($a_xml_parser,$a_data)
769  {
770  if($a_data != "\n")
771  {
772  // Replace multiple tabs with one space
773  $a_data = preg_replace("/\t+/"," ",$a_data);
774 
775  $this->cdata .= $a_data;
776  }
777  }
778 
785  public function start()
786  {
787  $this->startParsing();
788  return $this->result > 0;
789  }
790 }
791 ?>
handlerBeginTag($a_xml_parser, $a_name, $a_attribs)
handler for begin of element
getImportDirectory()
Get import directory.
$_SESSION["AccountId"]
$x
Definition: example_009.php:98
startParsing()
stores xml data in array
getUserIdAndAlias($imp_usr_id, $imp_usr_alias='')
handlerEndTag($a_xml_parser, $a_name)
handler for end of element
Base class for sax-based expat parsing extended classes need to overwrite the method setHandlers and ...
getIdAndAliasArray($imp_usr_id, $param='import')
__construct($forum, $a_xml_data)
Constructor.
static getInstance($a_obj_id=0)
_saveUsage($a_mob_id, $a_type, $a_id, $a_usage_hist_nr=0, $a_lang="-")
Save usage of mob within another container (e.g.
handlerCharacterData($a_xml_parser, $a_data)
handler for character data
$data
static _saveTempFileAsMediaObject($name, $tmp_name, $upload=TRUE)
Create new media object and update page in db and return new media object.
setHandlers($a_xml_parser)
set event handlers
start()
starts parsing an changes object by side effect.
global $ilUser
Definition: imgupload.php:15
$path
Definition: index.php:22
This class handles all operations on files for the forum object.
global $ilDB
setSchemaVersion($schema_version)
setImportDirectory($a_val)
Set import directory.
setXMLContent($a_xml_content)