ILIAS  release_5-0 Revision 5.0.0-1144-gc4397b1f870
All Data Structures Namespaces Files Functions Variables Modules 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 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 'UpdateUserId':
255  $x['UpdateUserId'] = $this->cdata;
256  break;
257 
258  case 'AuthorId':
259  $x['AuthorId'] = $this->cdata;
260  break;
261  case 'isAuthorModerator':
262  $x['isAuthorModerator'] = $this->cdata;
263  break;
264 
265  case 'UserId':
266  $x['UserId'] = $this->cdata;
267  if( $this->entity == 'forum' && $this->forumArray )
268  {
270  // createSettings accesses superglobal $_GET array, which can cause problems
271  // with public_notifications of block settings
272  $this->forum->createSettings();
273 
274  $forum_array = $this->getUserIdAndAlias(
275  $this->forumArray['UserId'], ''
276  );
277 
278  $this->frm_last_mapped_top_usr_id = $forum_array['usr_id'];
279 
280  $update_forum_array = $this->getUserIdAndAlias(
281  $this->forumArray['UpdateUserId'], ''
282  );
283  // Store old user id
284  $oldUsrId = $_SESSION["AccountId"];
285  // Manipulate user object
286  $_SESSION["AccountId"] = $update_forum_array['usr_id'];
287  $this->forum->setTitle($this->forumArray["Title"]);
288  $this->forum->setDescription($this->forumArray["Description"]);
289  $this->forum->update();
290  // Restore old user id
291  $_SESSION["AccountId"] = $oldUsrId;
292 
293  // create frm_settings
294  $newObjProp = ilForumProperties::getInstance($this->forum->getId());
295  $newObjProp->setDefaultView((int)$this->forumArray['DefaultView']);
296  $newObjProp->setAnonymisation((int)$this->forumArray['Pseudonyms']);
297  $newObjProp->setStatisticsStatus((int)$this->forumArray['Statistics']);
298  $newObjProp->setIsThreadRatingEnabled((int)$this->forumArray['ThreadRatings']);
299  $newObjProp->setPostActivation((int)$this->forumArray['PostingActivation']);
300  $newObjProp->setPresetSubject((int)$this->forumArray['PresetSubject']);
301  $newObjProp->setAddReSubject((int)$this->forumArray['PresetRe']);
302  $newObjProp->setNotificationType($this->forumArray['NotificationType'] ? $this->forumArray['NotificationType'] : 'all_users');
303  $newObjProp->setAdminForceNoti((int)$this->forumArray['ForceNotification']);
304  $newObjProp->setUserToggleNoti((int)$this->forumArray['ToggleNotification']);
305  $newObjProp->update();
306 
307  $id = $this->getNewForumPk();
308  $this->forum_obj_id = $newObjProp->getObjId();
309  $this->mapping['frm'][$this->forumArray['Id']] = $id;
310  $this->lastHandledForumId = $id;
311 
312  unset($this->forumArray);
313  }
314 
315  break;
316 
317  case 'Thread':
318  $update_str = "$this->lastHandledForumId#$this->lastHandledThreadId#$this->lastHandledPostId";
319  $ilDB->manipulateF(
320  "UPDATE frm_threads
321  SET thr_last_post = %s
322  WHERE thr_pk = %s",
323  array('text', 'integer'),
324  array($update_str, $this->lastHandledThreadId)
325  );
326  break;
327 
328  case 'Subject':
329  $x['Subject'] = $this->cdata;
330  break;
331 
332  case 'Alias':
333  $x['Alias'] = $this->cdata;
334  break;
335 
336  case 'Sticky':
337  $x['Sticky'] = $this->cdata;
338  break;
339 
340  case 'Closed':
341  $x['Closed'] = $this->cdata;
342 
343  if($this->entity == 'thread' && $this->threadArray)
344  {
345  require_once 'Modules/Forum/classes/class.ilForumTopic.php';
346 
347  $this->forumThread = new ilForumTopic();
348  $this->forumThread->setId($this->threadArray['Id']);
349  $this->forumThread->setForumId( $this->lastHandledForumId );
350  $this->forumThread->setSubject($this->threadArray['Subject']);
351  $this->forumThread->setSticky($this->threadArray['Sticky']);
352  $this->forumThread->setClosed($this->threadArray['Closed']);
353  $this->forumThread->setCreateDate($this->threadArray['CreateDate']);
354  $this->forumThread->setChangeDate($this->threadArray['UpdateDate']);
355  $this->forumThread->setImportName($this->threadArray['ImportName']);
356 
357  $usr_data = $this->getUserIdAndAlias(
358  $this->threadArray['UserId'], $this->threadArray['Alias']
359  );
360 
361  $this->forumThread->setDisplayUserId($usr_data['usr_id']);
362  $this->forumThread->setUserAlias($usr_data['usr_alias']);
363 
364  if(version_compare($this->getSchemaVersion(), '4.5.0', '<='))
365  {
366  $this->threadArray['AuthorId'] = $this->threadArray['UserId'];
367  }
368 
369  $author_id_data = $this->getUserIdAndAlias(
370  $this->threadArray['AuthorId']
371  );
372  $this->forumThread->setThrAuthorId((int)$author_id_data['usr_id']);
373 
374  $this->forumThread->insert();
375 
376  $this->mapping['thr'][$this->threadArray['Id']] = $this->forumThread->getId();
377  $this->lastHandledThreadId = $this->forumThread->getId();
378 
379  unset($this->threadArray);
380  }
381 
382  break;
383 
384  case 'Post':
385  break;
386 
387  case 'Censorship':
388  $x['Censorship'] = $this->cdata;
389  break;
390 
391  case 'CensorshipMessage':
392  $x['CensorshipMessage'] = $this->cdata;
393  break;
394 
395  case 'Notification':
396  $x['Notification'] = $this->cdata;
397  break;
398 
399  case 'ImportName':
400  $x['ImportName'] = $this->cdata;
401  break;
402 
403  case 'Status':
404  $x['Status'] = $this->cdata;
405  break;
406 
407  case 'Message':
408  $x['Message'] = $this->cdata;
409  break;
410 
411  case 'Lft':
412  $x['Lft'] = $this->cdata;
413  break;
414 
415  case 'Rgt':
416  $x['Rgt'] = $this->cdata;
417  break;
418 
419  case 'Depth':
420  $x['Depth'] = $this->cdata;
421  break;
422 
423  case 'ParentId':
424  $x['ParentId'] = $this->cdata;
425 
426  if($this->entity == 'post' && $this->postArray)
427  {
428  require_once 'Modules/Forum/classes/class.ilForumPost.php';
429 
430  $this->forumPost = new ilForumPost();
431  $this->forumPost->setId($this->postArray['Id']);
432  $this->forumPost->setCensorship($this->postArray['Censorship']);
433  $this->forumPost->setCensorshipComment($this->postArray['CensorshipMessage']);
434  $this->forumPost->setNotification($this->postArray['Notification']);
435  $this->forumPost->setImportName($this->postArray['ImportName']);
436  $this->forumPost->setStatus($this->postArray['Status']);
437  $this->forumPost->setMessage($this->postArray['Message']);
438  $this->forumPost->setSubject($this->postArray['Subject']);
439  $this->forumPost->setLft($this->postArray['Lft']);
440  $this->forumPost->setRgt($this->postArray['Rgt']);
441  $this->forumPost->setDepth($this->postArray['Depth']);
442  $this->forumPost->setParentId($this->postArray['ParentId']);
443  $this->forumPost->setThread($this->forumThread);
444  $this->forumPost->setThreadId($this->lastHandledThreadId);
445  $this->forumPost->setForumId($this->lastHandledForumId);
446  $this->forumPost->setCreateDate($this->postArray['CreateDate']);
447  $this->forumPost->setChangeDate($this->postArray['UpdateDate']);
448 
449  $usr_data = $this->getUserIdAndAlias(
450  $this->postArray['UserId'], $this->postArray['Alias']
451  );
452  $update_usr_data = $this->getUserIdAndAlias(
453  $this->postArray['UpdateUserId']
454  );
455  $this->forumPost->setDisplayUserId($usr_data['usr_id']);
456  $this->forumPost->setUserAlias($usr_data['usr_alias']);
457  $this->forumPost->setUpdateUserId($update_usr_data['usr_id']);
458 
459  if(version_compare($this->getSchemaVersion(), '4.5.0', '<='))
460  {
461  $this->postArray['AuthorId'] = $this->postArray['UserId'];
462  }
463  $author_id_data = $this->getUserIdAndAlias(
464  $this->postArray['AuthorId']
465  );
466  $this->forumPost->setPosAuthorId((int)$author_id_data['usr_id']);
467 
468  if($this->postArray['isAuthorModerator'] === 'NULL')
469  {
470  $this->forumPost->setIsAuthorModerator(NULL);
471  }
472  else
473  {
474  $this->forumPost->setIsAuthorModerator((int)$this->postArray['isAuthorModerator']);
475  }
476 
477  $this->forumPost->insert();
478 
479  if($this->mapping['pos'][$this->postArray['ParentId']])
480  {
481  $parentId = $this->mapping['pos'][$this->postArray['ParentId']];
482  }
483  else
484  {
485  $parentId = 0;
486  }
487 
488  $postTreeNodeId = $ilDB->nextId('frm_posts_tree');
489  $ilDB->insert('frm_posts_tree', array(
490  'fpt_pk' => array('integer', $postTreeNodeId),
491  'thr_fk' => array('integer', $this->lastHandledThreadId),
492  'pos_fk' => array('integer', $this->forumPost->getId()),
493  'parent_pos' => array('integer', $parentId),
494  'lft' => array('integer', $this->postArray['Lft']),
495  'rgt' => array('integer', $this->postArray['Rgt']),
496  'depth' => array('integer', $this->postArray['Depth']),
497  'fpt_date' => array('timestamp', date('Y-m-d H:i:s'))
498  ));
499 
500  $this->mapping['pos'][$this->postArray['Id']] = $this->forumPost->getId();
501  $this->lastHandledPostId = $this->forumPost->getId();
502 
503  $media_objects_found = false;
504  foreach($this->mediaObjects as $mob_attr)
505  {
506  $importfile = $this->getImportDirectory().'/'.$mob_attr['uri'];
507  if(file_exists($importfile))
508  {
509  $mob = ilObjMediaObject::_saveTempFileAsMediaObject(basename($importfile), $importfile, false);
510  ilObjMediaObject::_saveUsage($mob->getId(), "frm:html", $this->forumPost->getId());
511 
512  $this->forumPost->setMessage(
513  str_replace(
514  array(
515  "src=\"" . $mob_attr["label"] . "\"",
516  "src=\"" . preg_replace("/(il)_[\d]+_(mob)_([\d]+)/", "$1_0_$2_$3", $mob_attr["label"]) . "\""
517  ),
518  "src=\"" . "il_" . IL_INST_ID . "_mob_" . $mob->getId() . "\"",
519  $this->forumPost->getMessage()
520  )
521  );
522  $media_objects_found = true;
523  }
524  }
525 
526  if($media_objects_found)
527  {
528  $this->forumPost->update();
529  }
530 
531  unset($this->postArray);
532  }
533 
534  break;
535 
536  case 'Content':
537  $x['content'] = $this->cdata;
538  break;
539 
540  case 'Attachment':
541  $filedata = new ilFileDataForum($this->forum->getId(), $this->lastHandledPostId);
542 
543  $importPath = $this->contentArray['content'];
544 
545  if( strlen($importPath) )
546  {
547  $importPath = $this->getImportDirectory().'/'.$importPath;
548 
549  $newFilename = preg_replace("/^\d+_\d+(_.*)/ims", $this->forum->getId()."_".$this->lastHandledPostId."$1", basename($importPath));
550  $path = $filedata->getForumPath();
551  $newPath = $path.'/'.$newFilename;
552  @copy($importPath, $newPath);
553  }
554  break;
555  }
556 
557  $this->cdata = '';
558 
559  return;
560  }
561 
562  private function getIdAndAliasArray($imp_usr_id, $param = 'import')
563  {
564  global $ilDB;
565 
566  $select = 'SELECT od.obj_id, ud.login
567  FROM object_data od
568  INNER JOIN usr_data ud
569  ON od.obj_id = ud.usr_id';
570 
571  if($param == 'import')
572  {
573  $where = ' WHERE od.import_id = '.$ilDB->quote(
574  'il_'.$this->import_install_id.'_usr_'.$imp_usr_id, 'text'
575  );
576  }
577 
578  if($param == 'user')
579  {
580  $where = ' WHERE ud.usr_id = '.$ilDB->quote(
581  $imp_usr_id, 'integer'
582  );
583  }
584 
585  $query = $ilDB->query($select.$where);
586 
587  while( $res = $ilDB->fetchAssoc($query) )
588  {
589  break;
590  }
591 
592  if($res)
593  {
594  return array(
595  'usr_id' => $res['obj_id'],
596  'usr_alias' => $res['login']
597  );
598  }
599  else
600  {
601  return false;
602  }
603  }
604 
605  private function getAnonymousArray()
606  {
607  return array(
608  'usr_id' => $this->aobject->getId(),
609  'usr_alias' => $this->aobject->getLogin()
610  );
611  }
612 
613 
614  private function getUserIdAndAlias($imp_usr_id, $imp_usr_alias = '')
615  {
616  global $ilDB;
617 
618  if( (int)$imp_usr_id > 0 )
619  {
620  $newUsrId = -1;
621 
622  if( $this->import_install_id != IL_INST_ID && IL_INST_ID > 0 )
623  {
624  // Different installations
625  if( $this->user_id_mapping[$imp_usr_id] )
626  {
627  return $this->user_id_mapping[$imp_usr_id];
628  }
629  else
630  {
631  $res = $this->getIdAndAliasArray($imp_usr_id, 'import');
632 
633  if($res)
634  {
635  $this->user_id_mapping[$imp_usr_id] = $res;
636 
637  return $res;
638  }
639  else
640  {
641  $return_value = $this->getAnonymousArray();
642  $this->user_id_mapping[$imp_usr_id] = $return_value;
643 
644  return $return_value;
645  }
646  }
647  }
648  else if( $this->import_install_id == IL_INST_ID && IL_INST_ID == 0 )
649  {
650  // Eventually different installations. We cannot determine it.
651  if( $this->user_id_mapping[$imp_usr_id] )
652  {
653  return $this->user_id_mapping[$imp_usr_id];
654  }
655  else
656  {
657  $res = $this->getIdAndAliasArray($imp_usr_id, 'import');
658 
659  if($res)
660  {
661  $this->user_id_mapping[$imp_usr_id] = $res;
662 
663  return $res;
664  }
665  else
666  {
667  // Same installation
668  if( $this->user_id_mapping[$imp_usr_id] )
669  {
670  return $this->user_id_mapping[$imp_usr_id];
671  }
672  else
673  {
674  $res = $this->getIdAndAliasArray($imp_usr_id, 'user');
675 
676  if($res)
677  {
678  $this->user_id_mapping[$imp_usr_id] = $res;
679 
680  return $res;
681  }
682  else
683  {
684  $return_value = $this->getAnonymousArray();
685  $this->user_id_mapping[$imp_usr_id] = $return_value;
686 
687  return $return_value;
688  }
689  }
690  }
691  }
692  }
693  else
694  {
695  // Same installation
696  if( $this->user_id_mapping[$imp_usr_id] )
697  {
698  return $this->user_id_mapping[$imp_usr_id];
699  }
700  else
701  {
702  $res = $this->getIdAndAliasArray($imp_usr_id, 'user');
703 
704  if($res)
705  {
706  $this->user_id_mapping[$imp_usr_id] = $res;
707 
708  return $res;
709  }
710  else
711  {
712  $return_value = $this->getAnonymousArray();
713  $this->user_id_mapping[$imp_usr_id] = $return_value;
714 
715  return $return_value;
716  }
717  }
718  }
719  }
720  else
721  {
722  return array(
723  'usr_id' => $imp_usr_id,
724  'usr_alias' => $imp_usr_alias
725  );
726  }
727  }
728 
729  public function setImportInstallId($id)
730  {
731  $this->import_install_id = $id;
732  }
733 
734  private function getNewForumPk()
735  {
736  global $ilDB;
737 
738  $query = "SELECT top_pk FROM frm_data
739  WHERE top_frm_fk = ".$ilDB->quote(
740  $this->forum->getId(), 'integer');
741  $res = $ilDB->query($query);
742  $data = $ilDB->fetchAssoc($res);
743 
744  return $data['top_pk'];
745  }
746 
753  public function handlerCharacterData($a_xml_parser,$a_data)
754  {
755  if($a_data != "\n")
756  {
757  // Replace multiple tabs with one space
758  $a_data = preg_replace("/\t+/"," ",$a_data);
759 
760  $this->cdata .= $a_data;
761  }
762  }
763 
770  public function start()
771  {
772  $this->startParsing();
773  return $this->result > 0;
774  }
775 }
776 ?>
< a tabindex="-1" style="border-style: none;" href="#" title="Refresh Image" onclick="document.getElementById('siimage').src = './securimage_show.php?sid=' + Math.random(); this.blur(); return false">< img src="./images/refresh.png" alt="Reload Image" height="32" width="32" onclick="this.blur()" align="bottom" border="0"/></a >< br/>< strong > Enter Code *if($_SERVER['REQUEST_METHOD']=='POST' &&@ $_POST['do']=='contact') $_SESSION['ctform']['success']
handlerBeginTag($a_xml_parser, $a_name, $a_attribs)
handler for begin of element
getImportDirectory()
Get import directory.
startParsing()
stores xml data in array
getUserIdAndAlias($imp_usr_id, $imp_usr_alias='')
& _saveTempFileAsMediaObject($name, $tmp_name, $upload=TRUE)
Create new media object and update page in db and return new media object.
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
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)