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