ILIAS  Release_4_2_x_branch Revision 61807
 All Data Structures Namespaces Files Functions Variables Groups Pages
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_ECONTENT = 'econtents';
39  const TYPE_EXPORTED = 'exported';
40 
41  const ADMIN_RESET = 'reset';
42  const ADMIN_RESET_ALL = 'reset_all';
43 
44  protected $log;
45  protected $db;
46 
47  protected $events = array();
48  protected $econtent_ids = array();
49 
55  public function __construct($a_server_id)
56  {
57  global $ilLog,$ilDB;
58 
59  include_once('Services/WebServices/ECS/classes/class.ilECSSetting.php');
60  include_once('Services/WebServices/ECS/classes/class.ilECSReaderException.php');
61 
62  $this->settings = ilECSSetting::getInstanceByServerId($a_server_id);
63  $this->log = $ilLog;
64  $this->db = $ilDB;
65 
66  $this->read();
67  }
68 
76  public static function handleImportReset(ilECSSetting $server)
77  {
78  global $ilLog;
79 
80  include_once('Services/WebServices/ECS/classes/class.ilECSConnector.php');
81  include_once('Services/WebServices/ECS/classes/class.ilECSConnectorException.php');
82 
83  try
84  {
85  include_once('./Services/WebServices/ECS/classes/class.ilECSEContentReader.php');
86  include_once('./Services/WebServices/ECS/classes/class.ilECSEventQueueReader.php');
87  include_once('./Services/WebServices/ECS/classes/class.ilECSImport.php');
88  include_once('./Services/WebServices/ECS/classes/class.ilECSExport.php');
89 
90  $event_queue = new ilECSEventQueueReader($server->getServerId());
91  $event_queue->deleteAllEContentEvents();
92 
93  $reader = new ilECSEContentReader($server->getServerId());
94  $list = $reader->readResourceList();
95  //$all_content = $reader->getEContent();
96 
97  $imported = ilECSImport::_getAllImportedLinks($server->getServerId());
98 
99  if(count($list))
100  {
101  foreach($list->getLinkIds() as $link_id)
102  {
103  if(!isset($imported[$link_id]))
104  {
105  // Add create event for not imported econtent
106  $event_queue->add(
108  $link_id,
110  );
111  }
112  else
113  {
114  // Add update event for already existing events
115  $event_queue->add(
117  $link_id,
119  );
120  }
121 
122  if(isset($imported[$link_id]))
123  {
124  unset($imported[$link_id]);
125  }
126  }
127  }
128  if(is_array($imported))
129  {
130  // Delete event for deprecated econtent
131  foreach($imported as $econtent_id => $null)
132  {
133  $event_queue->add(ilECSEventQueueReader::TYPE_ECONTENT,
134  $econtent_id,
136  );
137  }
138  }
139  }
140  catch(ilECSConnectorException $e1)
141  {
142  $ilLog->write('Cannot connect to ECS server: '.$e1->getMessage());
143  throw $e1;
144  }
145  catch(ilException $e2)
146  {
147  $ilLog->write('Update failed: '.$e2->getMessage());
148  throw $e2;
149  }
150  return true;
151  }
152 
161  public static function handleExportReset(ilECSSetting $server)
162  {
163  include_once('./Services/WebServices/ECS/classes/class.ilECSExport.php');
164 
165  // Delete all export events
166  $queue = new ilECSEventQueueReader($server->getServerId());
167  $queue->deleteAllExportedEvents();
168 
169  // Read all local export info
170  $local_econtent_ids = ilECSExport::_getAllEContentIds($server->getServerId());
171 
172  // Read remote list
173  try
174  {
175  include_once './Services/WebServices/ECS/classes/class.ilECSEContentReader.php';
176  $reader = new ilECSEContentReader($server->getServerId());
177  $list = $reader->readResourceList(ilECSEContentReader::SENDER_ONLY);
178  }
179  catch(ilECSConnectorException $e)
180  {
181  $GLOBALS['ilLog']->write(__METHOD__.': Connect failed '.$e->getMessage());
182  throw $e;
183  }
184  catch(ilECSReaderException $e)
185  {
186  $GLOBALS['ilLog']->write(__METHOD__.': Connect failed '.$e->getMessage());
187  throw $e;
188  }
189 
190  $remote_econtent_ids = array();
191  if(count($list))
192  {
193  $remote_econtent_ids = $list->getLinkIds();
194  }
195 
196  // Delete all deprecated local export info
197  foreach($local_econtent_ids as $econtent_id => $obj_id)
198  {
199  if(!in_array($econtent_id, $remote_econtent_ids))
200  {
201  ilECSExport::_deleteEContentIds($server->getServerId(),array($econtent_id));
202  }
203  }
204 
205  // Delete all with deprecated remote info
206  foreach($remote_econtent_ids as $econtent_id)
207  {
208  if(!isset($local_econtent_ids[$econtent_id]))
209  {
210  ilECSExport::_deleteEContentIds($server->getServerId(),array($econtent_id));
211  }
212  }
213  return true;
214  }
215 
216 
221  public function getServer()
222  {
223  return $this->settings;
224  }
225 
226 
233  public function getEvents()
234  {
235  return $this->events ? $this->events : array();
236  }
237 
243  public function deleteAll()
244  {
245  global $ilDB;
246 
247  $query = "DELETE FROM ecs_events ".
248  'WHERE server_id = '.$ilDB->quote($this->getServer()->getServerId(),'integer');
249  $res = $ilDB->manipulate($query);
250  return true;
251  }
252 
258  public function deleteAllEContentEvents()
259  {
260  global $ilDB;
261 
262  $query = "DELETE FROM ecs_events ".
263  "WHERE type = ".$this->db->quote(self::TYPE_ECONTENT,'text').' '.
264  'AND server_id = '.$ilDB->quote($this->getServer()->getServerId(),'integer');
265  $res = $ilDB->manipulate($query);
266  return true;
267  }
268 
274  public function deleteAllExportedEvents()
275  {
276  global $ilDB;
277 
278  $query = "DELETE FROM ecs_events ".
279  "WHERE type = ".$this->db->quote(self::TYPE_EXPORTED,'text').' '.
280  'AND server_id = '.$ilDB->quote($this->getServer()->getServerId(),'integer');
281  $res = $ilDB->manipulate($query);
282  return true;
283  }
284 
291  public function refresh()
292  {
293  try {
294  include_once('Services/WebServices/ECS/classes/class.ilECSConnector.php');
295  include_once('Services/WebServices/ECS/classes/class.ilECSConnectorException.php');
296 
297  $connector = new ilECSConnector($this->getServer());
298  while(true)
299  {
300  $res = $connector->readEventFifo(false);
301 
302  if(!count($res->getResult()))
303  {
304  return true;
305  }
306 
307  foreach($res->getResult() as $result)
308  {
309  include_once './Services/WebServices/ECS/classes/class.ilECSEvent.php';
310  $event = new ilECSEvent($result);
311 
312  $GLOBALS['ilLog']->write(__METHOD__.' ---------------------------- Handling new event ');
313  $GLOBALS['ilLog']->write(__METHOD__.print_r($event,true));
314  $GLOBALS['ilLog']->write(__METHOD__.' ---------------------------- Done! ');
315 
316  // Fill command queue
317  $this->writeEventToDB($event);
318  }
319  // Delete from fifo
320  $connector->readEventFifo(true);
321  }
322  }
323  catch(ilECSConnectorException $e)
324  {
325  $GLOBALS['ilLog']->write(__METHOD__.': Cannot read event fifo. Aborting');
326  }
327  }
328 
334  public static function deleteServer($a_server_id)
335  {
336  global $ilDB;
337 
338  $query = 'DELETE FROM ecs_events '.
339  'WHERE server_id = '.$ilDB->quote($a_server_id,'integer');
340  $ilDB->manipulate($query);
341  }
342 
346  private function writeEventToDB(ilECSEvent $ev)
347  {
348  global $ilDB;
349 
350  $query = "SELECT * FROM ecs_events ".
351  "WHERE type = ".$ilDB->quote(self::TYPE_ECONTENT,'integer')." ".
352  "AND id = ".$ilDB->quote($ev->getRessourceId(),'integer')." ".
353  'AND server_id = '.$ilDB->quote($this->getServer()->getServerId(),'integer');
354  $res = $ilDB->query($query);
355 
356  $event_id = 0;
357  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
358  {
359  $event_id = $row->event_id;
360  }
361 
362  $GLOBALS['ilLog']->write(__METHOD__.': Handling new event '.$ev->getStatus().' for econtent '.$ev->getRessourceId());
363 
364  if(!$event_id)
365  {
366  // No previous entry exists => perform insert
367  $query = "INSERT ecs_events (event_id,type,id,op,server_id) ".
368  "VALUES( ".
369  $ilDB->quote($ilDB->nextId('ecs_events'),'integer').','.
370  $ilDB->quote(self::TYPE_ECONTENT,'text').', '.
371  $ilDB->quote($ev->getRessourceId(),'integer').', '.
372  $ilDB->quote($ev->getStatus(),'text').', '.
373  $ilDB->quote($this->getServer()->getServerId(),'integer').' '.
374  ')';
375  $ilDB->manipulate($query);
376  return true;
377  }
378  // Do update
379  $do_update = false;
380  switch($ev->getStatus())
381  {
382  case ilECSEvent::CREATED:
383  // Do update, although impossible
384  $do_update = true;
385  break;
386 
388  $do_update = true;
389  break;
390 
391  case ilECSEvent::UPDATED:
392  // Do nothing. Old status is ok.
393  break;
394  }
395 
396  if(!$do_update)
397  {
398  return true;
399  }
400  $query = "UPDATE ecs_events ".
401  "SET op = ".$ilDB->quote($ev->getStatus(),'text')." ".
402  "WHERE event_id = ".$ilDB->quote($event_id,'integer').' '.
403  'AND server_id = '.$ilDB->quote($this->getServer()->getServerId(),'integer');
404  $ilDB->manipulate($query);
405  return true;
406  }
407 
414  public function shift()
415  {
416  $event = array_shift($this->events);
417  if($event == null)
418  {
419  return array();
420  }
421  else
422  {
423  $this->delete($event['event_id']);
424  return $event;
425  }
426  }
427 
428 
434  public function add($a_type,$a_id,$a_op)
435  {
436  global $ilDB;
437 
438  $next_id = $ilDB->nextId('ecs_events');
439  $query = "INSERT INTO ecs_events (event_id,type,id,op,server_id) ".
440  "VALUES (".
441  $ilDB->quote($next_id,'integer').", ".
442  $this->db->quote($a_type,'text').", ".
443  $this->db->quote($a_id,'integer').", ".
444  $this->db->quote($a_op,'text').", ".
445  $ilDB->quote($this->getServer()->getServerId(),'integer').' '.
446  ")";
447  $res = $ilDB->manipulate($query);
448 
449  $new_event['event_id'] = $next_id;
450  $new_event['type'] = $a_type;
451  $new_event['id'] = $a_id;
452  $new_event['op'] = $a_op;
453 
454  $this->events[] = $new_event;
455  $this->econtent_ids[$a_id] = $a_id;
456  return true;
457  }
458 
465  private function update($a_type,$a_id,$a_operation)
466  {
467  global $ilDB;
468 
469  $query = "UPDATE ecs_events ".
470  "SET op = ".$this->db->quote($a_operation,'text')." ".
471  "WHERE type = ".$this->db->quote($a_type,'text')." ".
472  "AND id = ".$this->db->quote($a_id,'integer')." ".
473  'AND server_id = '.$ilDB->quote($this->getServer()->getServerId(),'integer');
474  $res = $ilDB->manipulate($query);
475  }
476 
483  private function delete($a_event_id)
484  {
485  global $ilDB;
486 
487  $query = "DELETE FROM ecs_events ".
488  "WHERE event_id = ".$this->db->quote($a_event_id,'integer')." ".
489  'AND server_id = '.$ilDB->quote($this->getServer()->getServerId(),'integer');
490  $res = $ilDB->manipulate($query);
491  unset($this->econtent_ids[$a_event_id]);
492  return true;
493  }
494 
499  public function read()
500  {
501  global $ilDB;
502 
503  $query = "SELECT * FROM ecs_events ORDER BY event_id ".
504  'AND server_id = '.$ilDB->quote($this->getServer()->getServerId(),'integer');
505  $res = $this->db->query($query);
506  $counter = 0;
507  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
508  {
509  $this->events[$counter]['event_id'] = $row->event_id;
510  $this->events[$counter]['type'] = $row->type;
511  $this->events[$counter]['id'] = $row->id;
512  $this->events[$counter]['op'] = $row->op;
513 
514  $this->econtent_ids[$row->event_id] = $row->event_id;
515  ++$counter;
516  }
517  return true;
518  }
519 
520 
521 }
522 ?>