ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
class.ilECSEventQueueReader.php
Go to the documentation of this file.
1 <?php
2 /*
3  +-----------------------------------------------------------------------------+
4  | ILIAS open source |
5  +-----------------------------------------------------------------------------+
6  | Copyright (c) 1998-2006 ILIAS open source, University of Cologne |
7  | |
8  | This program is free software; you can redistribute it and/or |
9  | modify it under the terms of the GNU General Public License |
10  | as published by the Free Software Foundation; either version 2 |
11  | of the License, or (at your option) any later version. |
12  | |
13  | This program is distributed in the hope that it will be useful, |
14  | but WITHOUT ANY WARRANTY; without even the implied warranty of |
15  | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16  | GNU General Public License for more details. |
17  | |
18  | You should have received a copy of the GNU General Public License |
19  | along with this program; if not, write to the Free Software |
20  | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
21  +-----------------------------------------------------------------------------+
22 */
23 
24 include_once './Services/WebServices/ECS/classes/class.ilECSEvent.php';
25 
26 
37 {
38  const TYPE_EXPORTED = 'exported';
39  const TYPE_DIRECTORY_TREES = 'directory_trees';
40  const TYPE_CMS_COURSES = 'courses';
41  const TYPE_CMS_COURSE_MEMBERS = 'course_members';
42  const TYPE_REMOTE_COURSE = 'rcrs';
43  const TYPE_REMOTE_CATEGORY = 'rcat';
44  const TYPE_REMOTE_FILE = 'rfil';
45  const TYPE_REMOTE_GLOSSARY = 'rglo';
46  const TYPE_REMOTE_GROUP = 'rgrp';
48  const TYPE_REMOTE_WIKI = 'rwik';
49  const TYPE_REMOTE_TEST = 'rtst';
50  const TYPE_COURSE_URLS = 'course_urls';
51  const TYPE_ENROLMENT_STATUS = 'member_status';
52 
53  protected $log;
54  protected $db;
55 
56  protected $events = array();
57  protected $econtent_ids = array();
58 
64  public function __construct($a_server_id)
65  {
66  global $ilLog,$ilDB;
67 
68  include_once('Services/WebServices/ECS/classes/class.ilECSSetting.php');
69 
70  $this->settings = ilECSSetting::getInstanceByServerId($a_server_id);
71  $this->log = $ilLog;
72  $this->db = $ilDB;
73 
74  $this->read();
75  }
76 
83  protected static function getEventTypeFromObjectType($a_obj_type)
84  {
85  // currently they are the same for all resource types
86  return $a_obj_type;
87  }
88 
94  protected static function getAllEContentTypes()
95  {
96  return array(self::TYPE_REMOTE_COURSE, self::TYPE_REMOTE_CATEGORY,
97  self::TYPE_REMOTE_FILE, self::TYPE_REMOTE_GLOSSARY, self::TYPE_REMOTE_GROUP,
98  self::TYPE_REMOTE_LEARNING_MODULE, self::TYPE_REMOTE_WIKI, self::TYPE_REMOTE_TEST);
99  }
100 
109  protected static function getAllResourceIds(ilECSSetting $server, array $a_types, $a_sender_only = false)
110  {
111  include_once 'Services/WebServices/ECS/classes/class.ilRemoteObjectBase.php';
112  $list = array();
113  foreach($a_types as $type)
114  {
116  if($robj)
117  {
118  $list[$type] = $robj->getAllResourceIds($server, $a_sender_only);
119  }
120  }
121 
122  return $list;
123  }
124 
132  public static function handleImportReset(ilECSSetting $server)
133  {
134  global $ilLog;
135 
136  include_once('Services/WebServices/ECS/classes/class.ilECSConnector.php');
137  include_once('Services/WebServices/ECS/classes/class.ilECSConnectorException.php');
138 
139  try
140  {
141  include_once('./Services/WebServices/ECS/classes/class.ilECSEventQueueReader.php');
142  include_once('./Services/WebServices/ECS/classes/class.ilECSImport.php');
143  include_once('./Services/WebServices/ECS/classes/class.ilECSExport.php');
144 
145  $types = self::getAllEContentTypes();
146 
147  $event_queue = new ilECSEventQueueReader($server->getServerId());
148  $event_queue->deleteAllEContentEvents($types);
149 
150  $list = self::getAllResourceIds($server, $types);
152 
153  $GLOBALS['ilLog']->write(__METHOD__.': Imported = '.print_r($imported,true));
154  $GLOBALS['ilLog']->write(__METHOD__.': List = '.print_r($list,true));
155 
156  foreach($list as $resource_type => $link_ids)
157  {
158  if(!in_array($resource_type, ilECSUtils::getPossibleRemoteTypes()))
159  {
160  $GLOBALS['ilLog']->write(__METHOD__.': Ignoring resource type '. $resource_type);
161  continue;
162  }
163 
164 
165  foreach((array) $link_ids as $link_id)
166  {
167  if(!isset($imported[$link_id]))
168  {
169  // Add create event for not imported econtent
170  $event_queue->add(
171  $resource_type,
172  $link_id,
174  );
175  }
176  else
177  {
178  // Add update event for already existing events
179  $event_queue->add(
180  $resource_type,
181  $link_id,
183  );
184  }
185 
186  if(isset($imported[$link_id]))
187  {
188  unset($imported[$link_id]);
189  }
190  }
191  }
192 
193  if(is_array($imported))
194  {
195  // Delete event for deprecated econtent
196  include_once 'Services/WebServices/ECS/classes/class.ilECSObjectSettings.php';
197  foreach($imported as $econtent_id => $obj_id)
198  {
199  $type = self::getEventTypeFromObjectType(ilObject::_lookupType($obj_id));
200  if($type)
201  {
202  $event_queue->add($type,
203  $econtent_id,
205  );
206  }
207  }
208  }
209  }
210  catch(ilECSConnectorException $e1)
211  {
212  $ilLog->write('Cannot connect to ECS server: '.$e1->getMessage());
213  throw $e1;
214  }
215  catch(ilException $e2)
216  {
217  $ilLog->write('Update failed: '.$e2->getMessage());
218  throw $e2;
219  }
220  return true;
221  }
222 
231  public static function handleExportReset(ilECSSetting $server)
232  {
233  include_once('./Services/WebServices/ECS/classes/class.ilECSExport.php');
234 
235  // Delete all export events
236  $queue = new ilECSEventQueueReader($server->getServerId());
237  $queue->deleteAllExportedEvents();
238 
239  // Read all local export info
240  $local_econtent_ids = ilECSExport::_getAllEContentIds($server->getServerId());
241 
242  $types = self::getAllEContentTypes();
243  $list = self::getAllResourceIds($server, $types, true);
244 
245 
246  // merge in one array
247  $all_remote_ids = array();
248  foreach($list as $resource_type => $remote_ids)
249  {
250  $all_remote_ids = array_merge($all_remote_ids,(array) $remote_ids);
251  }
252  $all_remote_ids = array_unique($all_remote_ids);
253 
254  $GLOBALS['ilLog']->write(__METHOD__.': Resources = ' . print_r($all_remote_ids,true));
255  $GLOBALS['ilLog']->write(__METHOD__.': Local = ' . print_r($local_econtent_ids,true));
256  foreach($local_econtent_ids as $local_econtent_id => $local_obj_id)
257  {
258  if(!in_array($local_econtent_id, $all_remote_ids))
259  {
260  // Delete this deprecated info
261  $GLOBALS['ilLog']->write(__METHOD__.': Deleting deprecated econtent id '. $local_econtent_id);
262  ilECSExport::_deleteEContentIds($server->getServerId(),array($local_econtent_id));
263 
264  }
265  }
266  return true;
267  }
268 
269 
274  public function getServer()
275  {
276  return $this->settings;
277  }
278 
279 
286  public function getEvents()
287  {
288  return $this->events ? $this->events : array();
289  }
290 
296  public function deleteAll()
297  {
298  global $ilDB;
299 
300  $query = "DELETE FROM ecs_events ".
301  'WHERE server_id = '.$ilDB->quote($this->getServer()->getServerId(),'integer');
302  $res = $ilDB->manipulate($query);
303  return true;
304  }
305 
312  public function deleteAllEContentEvents(array $a_types)
313  {
314  global $ilDB;
315 
316  $query = "DELETE FROM ecs_events ".
317  "WHERE ".$this->db->in("type", $a_types, "", "text").' '.
318  'AND server_id = '.$ilDB->quote($this->getServer()->getServerId(),'integer');
319  $res = $ilDB->manipulate($query);
320  return true;
321  }
322 
328  protected function deleteAllExportedEvents()
329  {
330  global $ilDB;
331 
332  $query = "DELETE FROM ecs_events ".
333  "WHERE type = ".$this->db->quote(self::TYPE_EXPORTED,'text').' '.
334  'AND server_id = '.$ilDB->quote($this->getServer()->getServerId(),'integer');
335  $res = $ilDB->manipulate($query);
336  return true;
337  }
338 
345  public function refresh()
346  {
347  try {
348  include_once('Services/WebServices/ECS/classes/class.ilECSConnector.php');
349  include_once('Services/WebServices/ECS/classes/class.ilECSConnectorException.php');
350 
351  $connector = new ilECSConnector($this->getServer());
352  while(true)
353  {
354  $res = $connector->readEventFifo(false);
355 
356  if(!count($res->getResult()))
357  {
358  return true;
359  }
360 
361  foreach($res->getResult() as $result)
362  {
363  include_once './Services/WebServices/ECS/classes/class.ilECSEvent.php';
364  $event = new ilECSEvent($result);
365 
366  $GLOBALS['ilLog']->write(__METHOD__.' ---------------------------- Handling new event ');
367  $GLOBALS['ilLog']->write(__METHOD__.print_r($event,true));
368  $GLOBALS['ilLog']->write(__METHOD__.' ---------------------------- Done! ');
369 
370  // Fill command queue
371  $this->writeEventToDB($event);
372  }
373  // Delete from fifo
374  $connector->readEventFifo(true);
375  }
376 
377  }
378  catch(ilECSConnectorException $e)
379  {
380  $GLOBALS['ilLog']->write(__METHOD__.': Cannot read event fifo. Aborting');
381  }
382  }
383 
389  public static function deleteServer($a_server_id)
390  {
391  global $ilDB;
392 
393  $query = 'DELETE FROM ecs_events '.
394  'WHERE server_id = '.$ilDB->quote($a_server_id,'integer');
395  $ilDB->manipulate($query);
396  }
397 
401  private function writeEventToDB(ilECSEvent $ev)
402  {
403  global $ilDB;
404 
405  // this should probably be moved elsewhere
406  switch($ev->getRessourceType())
407  {
408  case 'directory_trees':
409  $type = self::TYPE_DIRECTORY_TREES;
410  break;
411 
412  case 'course_members':
413  $type = self::TYPE_CMS_COURSE_MEMBERS;
414  break;
415 
416  case 'courses':
417  $type = self::TYPE_CMS_COURSES;
418  break;
419 
420  case 'courselinks':
421  $type = self::TYPE_REMOTE_COURSE;
422  break;
423 
424  case 'categories':
425  $type = self::TYPE_REMOTE_CATEGORY;
426  break;
427 
428  case 'files':
429  $type = self::TYPE_REMOTE_FILE;
430  break;
431 
432  case 'glossaries':
433  $type = self::TYPE_REMOTE_GLOSSARY;
434  break;
435 
436  case 'groups':
437  $type = self::TYPE_REMOTE_GROUP;
438  break;
439 
440  case 'learningmodules':
441  $type = self::TYPE_REMOTE_LEARNING_MODULE;
442  break;
443 
444  case 'wikis':
445  $type = self::TYPE_REMOTE_WIKI;
446  break;
447 
448  case 'tests':
449  $type = self::TYPE_REMOTE_TEST;
450  break;
451 
452  case 'course_urls':
453  $type = self::TYPE_COURSE_URLS;
454  break;
455 
456  case 'member_status':
457  $type = self::TYPE_ENROLMENT_STATUS;
458  break;
459 
460  default:
461  // write custom event type
462  $type = $ev->getRessourceType();
463  break;
464  }
465 
466  $query = "SELECT * FROM ecs_events ".
467  "WHERE type = ".$ilDB->quote($type,'integer')." ".
468  "AND id = ".$ilDB->quote($ev->getRessourceId(),'integer')." ".
469  'AND server_id = '.$ilDB->quote($this->getServer()->getServerId(),'integer');
470  $res = $ilDB->query($query);
471 
472  $event_id = 0;
473  while($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT))
474  {
475  $event_id = $row->event_id;
476  }
477 
478  if(!$event_id)
479  {
480  // No previous entry exists => perform insert
481  $query = "INSERT ecs_events (event_id,type,id,op,server_id) ".
482  "VALUES( ".
483  $ilDB->quote($ilDB->nextId('ecs_events'),'integer').','.
484  $ilDB->quote($type,'text').', '.
485  $ilDB->quote($ev->getRessourceId(),'integer').', '.
486  $ilDB->quote($ev->getStatus(),'text').', '.
487  $ilDB->quote($this->getServer()->getServerId(),'integer').' '.
488  ')';
489  $ilDB->manipulate($query);
490  return true;
491  }
492  // Do update
493  $do_update = false;
494  switch($ev->getStatus())
495  {
496  case ilECSEvent::CREATED:
497  // Do update, although impossible
498  $do_update = true;
499  break;
500 
502  $do_update = true;
503  break;
504 
505  case ilECSEvent::UPDATED:
506  // Do nothing. Old status is ok.
507  break;
508  }
509 
510  if(!$do_update)
511  {
512  return true;
513  }
514  $query = "UPDATE ecs_events ".
515  "SET op = ".$ilDB->quote($ev->getStatus(),'text')." ".
516  "WHERE event_id = ".$ilDB->quote($event_id,'integer').' '.
517  'AND type = '.$ilDB->quote($type).' '.
518  'AND server_id = '.$ilDB->quote($this->getServer()->getServerId(),'integer');
519  $ilDB->manipulate($query);
520  return true;
521  }
522 
529  public function shift()
530  {
531  $event = array_shift($this->events);
532  if($event == null)
533  {
534  return array();
535  }
536  else
537  {
538 
539  #$this->delete($event['event_id']);
540  return $event;
541  }
542  }
543 
544 
550  public function add($a_type,$a_id,$a_op)
551  {
552  global $ilDB;
553 
554  $next_id = $ilDB->nextId('ecs_events');
555  $query = "INSERT INTO ecs_events (event_id,type,id,op,server_id) ".
556  "VALUES (".
557  $ilDB->quote($next_id,'integer').", ".
558  $this->db->quote($a_type,'text').", ".
559  $this->db->quote($a_id,'integer').", ".
560  $this->db->quote($a_op,'text').", ".
561  $ilDB->quote($this->getServer()->getServerId(),'integer').' '.
562  ")";
563  $res = $ilDB->manipulate($query);
564 
565  $new_event['event_id'] = $next_id;
566  $new_event['type'] = $a_type;
567  $new_event['id'] = $a_id;
568  $new_event['op'] = $a_op;
569 
570  $this->events[] = $new_event;
571  $this->econtent_ids[$a_id] = $a_id;
572  return true;
573  }
574 
581  private function update($a_type,$a_id,$a_operation)
582  {
583  global $ilDB;
584 
585  $query = "UPDATE ecs_events ".
586  "SET op = ".$this->db->quote($a_operation,'text')." ".
587  "WHERE type = ".$this->db->quote($a_type,'text')." ".
588  "AND id = ".$this->db->quote($a_id,'integer')." ".
589  'AND server_id = '.$ilDB->quote($this->getServer()->getServerId(),'integer');
590  $res = $ilDB->manipulate($query);
591  }
592 
599  public function delete($a_event_id)
600  {
601  global $ilDB;
602 
603  $query = "DELETE FROM ecs_events ".
604  "WHERE event_id = ".$this->db->quote($a_event_id,'integer')." ".
605  'AND server_id = '.$ilDB->quote($this->getServer()->getServerId(),'integer');
606  $res = $ilDB->manipulate($query);
607  unset($this->econtent_ids[$a_event_id]);
608  return true;
609  }
610 
615  public function read()
616  {
617  global $ilDB;
618 
619  $query = "SELECT * FROM ecs_events ".
620  'WHERE server_id = '.$ilDB->quote($this->getServer()->getServerId(),'integer').' '.
621  'ORDER BY event_id';
622 
623  $res = $this->db->query($query);
624  $counter = 0;
625  while($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT))
626  {
627  $this->events[$counter]['event_id'] = $row->event_id;
628  $this->events[$counter]['type'] = $row->type;
629  $this->events[$counter]['id'] = $row->id;
630  $this->events[$counter]['op'] = $row->op;
631 
632  $this->econtent_ids[$row->event_id] = $row->event_id;
633  ++$counter;
634  }
635  return true;
636  }
637 
638  public static function deleteByServerId($a_server_id)
639  {
640  global $ilDB;
641 
642  $query = 'DELETE FROM ecs_events'.
643  ' WHERE server_id = '.$ilDB->quote($a_server_id,'integer');
644  $ilDB->manipulate($query);
645  return true;
646  }
647 }
648 ?>
static getAllImportedRemoteObjects($a_server_id)
get all imported links
static getEventTypeFromObjectType($a_obj_type)
Convert object type to event type.
static _getAllEContentIds($a_server_id)
get all exported econtent ids per server
static getPossibleRemoteTypes($a_with_captions=false)
Get all possible remote object types.
Base class for ILIAS Exception handling.
static getInstanceByServerId($a_server_id)
Get singleton instance per server.
static handleImportReset(ilECSSetting $server)
Reread all imported econtent.
update($a_type, $a_id, $a_operation)
update one entry
$result
static deleteServer($a_server_id)
Delete by server id ilDB $ilDB.
getStatus()
get title
getServerId()
Get current server id.
static _deleteEContentIds($a_server_id, $a_ids)
Delete econtent ids for server.
$GLOBALS['loaded']
Global hash that tracks already loaded includes.
getRessourceType()
Get ressource type.
deleteAllExportedEvents()
Delete all exported events.
static getInstanceByEventType($a_type)
Get instance by ilECSEvent(QueueReader) type.
$counter
static getAllEContentTypes()
All available content types.
deleteAllEContentEvents(array $a_types)
Delete all econtents.
$a_type
Definition: workflow.php:93
getRessourceId()
Get ressource id.
writeEventToDB(ilECSEvent $ev)
Write event to db.
refresh()
Fetch events from fifo Using fifo public.
Reads ECS events and stores them in the database.
static deleteByServerId($a_server_id)
Create styles array
The data for the language used.
static _lookupType($a_id, $a_reference=false)
lookup object type
shift()
get and delete the first event entry
$server
settings()
Definition: settings.php:2
__construct($a_server_id)
Constructor.
global $ilDB
static handleExportReset(ilECSSetting $server)
Handle export reset.
static getAllResourceIds(ilECSSetting $server, array $a_types, $a_sender_only=false)
Get all resource ids by resource type.
add($a_type, $a_id, $a_op)
add