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