ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
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  // Manipulate user object
289  // changed smeyer 28.7.16: the session id is not manipulated
290  // anymore. Instead the user is passwd ilObjForum::update()
291  $this->forum->setTitle($this->forumArray["Title"]);
292  $this->forum->setDescription($this->forumArray["Description"]);
293  $this->forum->update($update_forum_array['usr_id']);
294 
295  // create frm_settings
296  $newObjProp = ilForumProperties::getInstance($this->forum->getId());
297  $newObjProp->setDefaultView((int)$this->forumArray['DefaultView']);
298  $newObjProp->setAnonymisation((int)$this->forumArray['Pseudonyms']);
299  $newObjProp->setStatisticsStatus((int)$this->forumArray['Statistics']);
300  $newObjProp->setIsThreadRatingEnabled((int)$this->forumArray['ThreadRatings']);
301  $newObjProp->setPostActivation((int)$this->forumArray['PostingActivation']);
302  $newObjProp->setPresetSubject((int)$this->forumArray['PresetSubject']);
303  $newObjProp->setAddReSubject((int)$this->forumArray['PresetRe']);
304  $newObjProp->setNotificationType($this->forumArray['NotificationType'] ? $this->forumArray['NotificationType'] : 'all_users');
305  $newObjProp->setAdminForceNoti((int)$this->forumArray['ForceNotification']);
306  $newObjProp->setUserToggleNoti((int)$this->forumArray['ToggleNotification']);
307  $newObjProp->setFileUploadAllowed((int)$this->forumArray['FileUpload']);
308  $newObjProp->setThreadSorting($this->forumArray['Sorting']);
309  $newObjProp->setMarkModeratorPosts($this->forumArray['MarkModeratorPosts']);
310  $newObjProp->update();
311 
312  $id = $this->getNewForumPk();
313  $this->forum_obj_id = $newObjProp->getObjId();
314  $this->mapping['frm'][$this->forumArray['Id']] = $id;
315  $this->lastHandledForumId = $id;
316 
317  unset($this->forumArray);
318  }
319 
320  break;
321 
322  case 'Thread':
323  $update_str = "$this->lastHandledForumId#$this->lastHandledThreadId#$this->lastHandledPostId";
324  $ilDB->manipulateF(
325  "UPDATE frm_threads
326  SET thr_last_post = %s
327  WHERE thr_pk = %s",
328  array('text', 'integer'),
329  array($update_str, $this->lastHandledThreadId)
330  );
331  break;
332 
333  case 'Subject':
334  $x['Subject'] = $this->cdata;
335  break;
336 
337  case 'Alias':
338  $x['Alias'] = $this->cdata;
339  break;
340 
341  case 'Sticky':
342  $x['Sticky'] = $this->cdata;
343  break;
344 
345  case 'Sorting':
346  $x['Sorting'] = $this->cdata;
347  break;
348 
349  case 'MarkModeratorPosts':
350  $x['MarkModeratorPosts'] = $this->cdata;
351  break;
352 
353  case 'Closed':
354  $x['Closed'] = $this->cdata;
355 
356  if($this->entity == 'thread' && $this->threadArray)
357  {
358  require_once 'Modules/Forum/classes/class.ilForumTopic.php';
359 
360  $this->forumThread = new ilForumTopic();
361  $this->forumThread->setId($this->threadArray['Id']);
362  $this->forumThread->setForumId( $this->lastHandledForumId );
363  $this->forumThread->setSubject($this->threadArray['Subject']);
364  $this->forumThread->setSticky($this->threadArray['Sticky']);
365  $this->forumThread->setClosed($this->threadArray['Closed']);
366  $this->forumThread->setCreateDate($this->threadArray['CreateDate']);
367  $this->forumThread->setChangeDate($this->threadArray['UpdateDate']);
368  $this->forumThread->setImportName($this->threadArray['ImportName']);
369 
370  $usr_data = $this->getUserIdAndAlias(
371  $this->threadArray['UserId'], $this->threadArray['Alias']
372  );
373 
374  $this->forumThread->setDisplayUserId($usr_data['usr_id']);
375  $this->forumThread->setUserAlias($usr_data['usr_alias']);
376 
377  if(version_compare($this->getSchemaVersion(), '4.5.0', '<='))
378  {
379  $this->threadArray['AuthorId'] = $this->threadArray['UserId'];
380  }
381 
382  $author_id_data = $this->getUserIdAndAlias(
383  $this->threadArray['AuthorId']
384  );
385  $this->forumThread->setThrAuthorId((int)$author_id_data['usr_id']);
386 
387  $this->forumThread->insert();
388 
389  $this->mapping['thr'][$this->threadArray['Id']] = $this->forumThread->getId();
390  $this->lastHandledThreadId = $this->forumThread->getId();
391 
392  unset($this->threadArray);
393  }
394 
395  break;
396 
397  case 'Post':
398  break;
399 
400  case 'Censorship':
401  $x['Censorship'] = $this->cdata;
402  break;
403 
404  case 'CensorshipMessage':
405  $x['CensorshipMessage'] = $this->cdata;
406  break;
407 
408  case 'Notification':
409  $x['Notification'] = $this->cdata;
410  break;
411 
412  case 'ImportName':
413  $x['ImportName'] = $this->cdata;
414  break;
415 
416  case 'Status':
417  $x['Status'] = $this->cdata;
418  break;
419 
420  case 'Message':
421  $x['Message'] = $this->cdata;
422  break;
423 
424  case 'Lft':
425  $x['Lft'] = $this->cdata;
426  break;
427 
428  case 'Rgt':
429  $x['Rgt'] = $this->cdata;
430  break;
431 
432  case 'Depth':
433  $x['Depth'] = $this->cdata;
434  break;
435 
436  case 'ParentId':
437  $x['ParentId'] = $this->cdata;
438 
439  if($this->entity == 'post' && $this->postArray)
440  {
441  require_once 'Modules/Forum/classes/class.ilForumPost.php';
442 
443  $this->forumPost = new ilForumPost();
444  $this->forumPost->setId($this->postArray['Id']);
445  $this->forumPost->setCensorship($this->postArray['Censorship']);
446  $this->forumPost->setCensorshipComment($this->postArray['CensorshipMessage']);
447  $this->forumPost->setNotification($this->postArray['Notification']);
448  $this->forumPost->setImportName($this->postArray['ImportName']);
449  $this->forumPost->setStatus($this->postArray['Status']);
450  $this->forumPost->setMessage($this->postArray['Message']);
451  $this->forumPost->setSubject($this->postArray['Subject']);
452  $this->forumPost->setLft($this->postArray['Lft']);
453  $this->forumPost->setRgt($this->postArray['Rgt']);
454  $this->forumPost->setDepth($this->postArray['Depth']);
455  $this->forumPost->setParentId($this->postArray['ParentId']);
456  $this->forumPost->setThread($this->forumThread);
457  $this->forumPost->setThreadId($this->lastHandledThreadId);
458  $this->forumPost->setForumId($this->lastHandledForumId);
459  $this->forumPost->setCreateDate($this->postArray['CreateDate']);
460  $this->forumPost->setChangeDate($this->postArray['UpdateDate']);
461 
462  $usr_data = $this->getUserIdAndAlias(
463  $this->postArray['UserId'], $this->postArray['Alias']
464  );
465  $update_usr_data = $this->getUserIdAndAlias(
466  $this->postArray['UpdateUserId']
467  );
468  $this->forumPost->setDisplayUserId($usr_data['usr_id']);
469  $this->forumPost->setUserAlias($usr_data['usr_alias']);
470  $this->forumPost->setUpdateUserId($update_usr_data['usr_id']);
471 
472  if(version_compare($this->getSchemaVersion(), '4.5.0', '<='))
473  {
474  $this->postArray['AuthorId'] = $this->postArray['UserId'];
475  }
476  $author_id_data = $this->getUserIdAndAlias(
477  $this->postArray['AuthorId']
478  );
479  $this->forumPost->setPosAuthorId((int)$author_id_data['usr_id']);
480 
481  if($this->postArray['isAuthorModerator'] === 'NULL')
482  {
483  $this->forumPost->setIsAuthorModerator(NULL);
484  }
485  else
486  {
487  $this->forumPost->setIsAuthorModerator((int)$this->postArray['isAuthorModerator']);
488  }
489 
490  $this->forumPost->insert();
491 
492  if($this->mapping['pos'][$this->postArray['ParentId']])
493  {
494  $parentId = $this->mapping['pos'][$this->postArray['ParentId']];
495  }
496  else
497  {
498  $parentId = 0;
499  }
500 
501  $postTreeNodeId = $ilDB->nextId('frm_posts_tree');
502  $ilDB->insert('frm_posts_tree', array(
503  'fpt_pk' => array('integer', $postTreeNodeId),
504  'thr_fk' => array('integer', $this->lastHandledThreadId),
505  'pos_fk' => array('integer', $this->forumPost->getId()),
506  'parent_pos' => array('integer', $parentId),
507  'lft' => array('integer', $this->postArray['Lft']),
508  'rgt' => array('integer', $this->postArray['Rgt']),
509  'depth' => array('integer', $this->postArray['Depth']),
510  'fpt_date' => array('timestamp', date('Y-m-d H:i:s'))
511  ));
512 
513  $this->mapping['pos'][$this->postArray['Id']] = $this->forumPost->getId();
514  $this->lastHandledPostId = $this->forumPost->getId();
515 
516  $media_objects_found = false;
517  foreach($this->mediaObjects as $mob_attr)
518  {
519  $importfile = $this->getImportDirectory().'/'.$mob_attr['uri'];
520  if(file_exists($importfile))
521  {
522  $mob = ilObjMediaObject::_saveTempFileAsMediaObject(basename($importfile), $importfile, false);
523  ilObjMediaObject::_saveUsage($mob->getId(), "frm:html", $this->forumPost->getId());
524 
525  $this->forumPost->setMessage(
526  str_replace(
527  array(
528  "src=\"" . $mob_attr["label"] . "\"",
529  "src=\"" . preg_replace("/(il)_[\d]+_(mob)_([\d]+)/", "$1_0_$2_$3", $mob_attr["label"]) . "\""
530  ),
531  "src=\"" . "il_" . IL_INST_ID . "_mob_" . $mob->getId() . "\"",
532  $this->forumPost->getMessage()
533  )
534  );
535  $media_objects_found = true;
536  }
537  }
538 
539  if($media_objects_found)
540  {
541  $this->forumPost->update();
542  }
543 
544  unset($this->postArray);
545  }
546 
547  break;
548 
549  case 'Content':
550  $x['content'] = $this->cdata;
551  break;
552 
553  case 'Attachment':
554  $filedata = new ilFileDataForum($this->forum->getId(), $this->lastHandledPostId);
555 
556  $importPath = $this->contentArray['content'];
557 
558  if( strlen($importPath) )
559  {
560  $importPath = $this->getImportDirectory().'/'.$importPath;
561 
562  $newFilename = preg_replace("/^\d+_\d+(_.*)/ims", $this->forum->getId()."_".$this->lastHandledPostId."$1", basename($importPath));
563  $path = $filedata->getForumPath();
564  $newPath = $path.'/'.$newFilename;
565  @copy($importPath, $newPath);
566  }
567  break;
568  }
569 
570  $this->cdata = '';
571 
572  return;
573  }
574 
575  private function getIdAndAliasArray($imp_usr_id, $param = 'import')
576  {
577  global $ilDB;
578 
579  $select = 'SELECT od.obj_id, ud.login
580  FROM object_data od
581  INNER JOIN usr_data ud
582  ON od.obj_id = ud.usr_id';
583 
584  if($param == 'import')
585  {
586  $where = ' WHERE od.import_id = '.$ilDB->quote(
587  'il_'.$this->import_install_id.'_usr_'.$imp_usr_id, 'text'
588  );
589  }
590 
591  if($param == 'user')
592  {
593  $where = ' WHERE ud.usr_id = '.$ilDB->quote(
594  $imp_usr_id, 'integer'
595  );
596  }
597 
598  $query = $ilDB->query($select.$where);
599 
600  while( $res = $ilDB->fetchAssoc($query) )
601  {
602  break;
603  }
604 
605  if($res)
606  {
607  return array(
608  'usr_id' => $res['obj_id'],
609  'usr_alias' => $res['login']
610  );
611  }
612  else
613  {
614  return false;
615  }
616  }
617 
618  private function getAnonymousArray()
619  {
620  return array(
621  'usr_id' => $this->aobject->getId(),
622  'usr_alias' => $this->aobject->getLogin()
623  );
624  }
625 
626 
627  private function getUserIdAndAlias($imp_usr_id, $imp_usr_alias = '')
628  {
629  global $ilDB;
630 
631  if( (int)$imp_usr_id > 0 )
632  {
633  $newUsrId = -1;
634 
635  if( $this->import_install_id != IL_INST_ID && IL_INST_ID > 0 )
636  {
637  // Different installations
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, 'import');
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  else if( $this->import_install_id == IL_INST_ID && IL_INST_ID == 0 )
662  {
663  // Eventually different installations. We cannot determine it.
664  if( $this->user_id_mapping[$imp_usr_id] )
665  {
666  return $this->user_id_mapping[$imp_usr_id];
667  }
668  else
669  {
670  $res = $this->getIdAndAliasArray($imp_usr_id, 'import');
671 
672  if($res)
673  {
674  $this->user_id_mapping[$imp_usr_id] = $res;
675 
676  return $res;
677  }
678  else
679  {
680  // Same installation
681  if( $this->user_id_mapping[$imp_usr_id] )
682  {
683  return $this->user_id_mapping[$imp_usr_id];
684  }
685  else
686  {
687  $res = $this->getIdAndAliasArray($imp_usr_id, 'user');
688 
689  if($res)
690  {
691  $this->user_id_mapping[$imp_usr_id] = $res;
692 
693  return $res;
694  }
695  else
696  {
697  $return_value = $this->getAnonymousArray();
698  $this->user_id_mapping[$imp_usr_id] = $return_value;
699 
700  return $return_value;
701  }
702  }
703  }
704  }
705  }
706  else
707  {
708  // Same installation
709  if( $this->user_id_mapping[$imp_usr_id] )
710  {
711  return $this->user_id_mapping[$imp_usr_id];
712  }
713  else
714  {
715  $res = $this->getIdAndAliasArray($imp_usr_id, 'user');
716 
717  if($res)
718  {
719  $this->user_id_mapping[$imp_usr_id] = $res;
720 
721  return $res;
722  }
723  else
724  {
725  $return_value = $this->getAnonymousArray();
726  $this->user_id_mapping[$imp_usr_id] = $return_value;
727 
728  return $return_value;
729  }
730  }
731  }
732  }
733  else
734  {
735  return array(
736  'usr_id' => $imp_usr_id,
737  'usr_alias' => $imp_usr_alias
738  );
739  }
740  }
741 
742  public function setImportInstallId($id)
743  {
744  $this->import_install_id = $id;
745  }
746 
747  private function getNewForumPk()
748  {
749  global $ilDB;
750 
751  $query = "SELECT top_pk FROM frm_data
752  WHERE top_frm_fk = ".$ilDB->quote(
753  $this->forum->getId(), 'integer');
754  $res = $ilDB->query($query);
755  $data = $ilDB->fetchAssoc($res);
756 
757  return $data['top_pk'];
758  }
759 
766  public function handlerCharacterData($a_xml_parser,$a_data)
767  {
768  if($a_data != "\n")
769  {
770  // Replace multiple tabs with one space
771  $a_data = preg_replace("/\t+/"," ",$a_data);
772 
773  $this->cdata .= $a_data;
774  }
775  }
776 
783  public function start()
784  {
785  $this->startParsing();
786  return $this->result > 0;
787  }
788 }
789 ?>
$path
Definition: aliased.php:25
handlerBeginTag($a_xml_parser, $a_name, $a_attribs)
handler for begin of element
getImportDirectory()
Get import directory.
$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)
handlerCharacterData($a_xml_parser, $a_data)
handler for character data
static _saveTempFileAsMediaObject($name, $tmp_name, $upload=TRUE)
Create new media object and update page in db and return new media object.
date( 'd-M-Y', $objPHPExcel->getProperties() ->getCreated())
setHandlers($a_xml_parser)
set event handlers
start()
starts parsing an changes object by side effect.
$ilUser
Definition: imgupload.php:18
Create styles array
The data for the language used.
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)
static _saveUsage($a_mob_id, $a_type, $a_id, $a_usage_hist_nr=0, $a_lang="-")
Save usage of mob within another container (e.g.