wCMF  3.6
 All Classes Namespaces Files Functions Variables Groups Pages
class.PersistentIterator.php
Go to the documentation of this file.
1 <?php
2 /**
3  * wCMF - wemove Content Management Framework
4  * Copyright (C) 2005-2014 wemove digital solutions GmbH
5  *
6  * Licensed under the terms of any of the following licenses
7  * at your choice:
8  *
9  * - GNU Lesser General Public License (LGPL)
10  * http://www.gnu.org/licenses/lgpl.html
11  * - Eclipse Public License (EPL)
12  * http://www.eclipse.org/org/documents/epl-v10.php
13  *
14  * See the license.txt file distributed with this work for
15  * additional information.
16  *
17  * $Id: class.PersistentIterator.php 1462 2014-02-04 23:52:27Z iherwig $
18  */
19 require_once(BASE."wcmf/lib/persistence/class.PersistenceFacade.php");
20 require_once(BASE."wcmf/lib/util/class.SessionData.php");
21 
22 /**
23  * @class PersistentIterator
24  * @ingroup Model
25  * @brief PersistentIterator is used to iterate over a tree/list build of oids
26  * using a Depth-First-Algorithm. To persist its state use the PersistentIterator::save() method,
27  * to restore its state use the static PersistentIterator::load() method, which returns the loaded instance.
28  * States are identified by an unique id, which is provided after saving.
29  * PersistentIterator implements the 'Iterator Pattern'.
30  *
31  * @author ingo herwig <ingo@wemove.com>
32  */
34 {
35  var $_end; // indicates if the iteration is ended
36  var $_oidList; // the list of oids to process
37  var $_allList; // the list of all seen object ids
38  var $_currentOID; // the oid the iterator points to
39  var $_currentDepth; // the depth in the tree of the oid the iterator points to
40  /**
41  * Constructor.
42  * @param oid The oid to start from.
43  */
44  function PersistentIterator($oid)
45  {
46  $this->_end = false;
47  $this->_oidList = array();
48  $this->_allList = array();
49  $this->_currentOID = $oid;
50  $this->_currentDepth = 0;
51  }
52  /**
53  * Save the iterator state to the session
54  * @return A unique id to provide for load, see PersistentIterator::load()
55  */
56  function save()
57  {
58  $session = &SessionData::getInstance();
59 
60  $uid = md5(uniqid(""));
61  $state = array('end' => $this->_end, 'oidList' => $this->_oidList, 'allList' => $this->_allList, 'currentOID' => $this->_currentOID,
62  'currentDepth' => $this->_currentDepth);
63  $session->set('PersistentIterator.'.$uid, $state);
64  return $uid;
65  }
66  /**
67  * Load an iterator state from the session
68  * @note static method
69  * @param uid The unique id returned from the save method, see PersistentIterator::save()
70  * @return A reference to an PersistentIterator instance holding the saved state or null if unique id is not found
71  */
72  function &load($uid)
73  {
74  // get state from session
75  $session = &SessionData::getInstance();
76  $state = $session->get('PersistentIterator.'.$uid);
77  if ($state == null) {
78  return null;
79  }
80  // create instance
81  $instance = new PersistentIterator($state['currentOID']);
82  $instance->_end = $state['end'];
83  $instance->_oidList = $state['oidList'];
84  $instance->_allList = $state['allList'];
85  $instance->_currentDepth = $state['currentDepth'];
86  return $instance;
87  }
88  /**
89  * Proceed to next oid.
90  * @return A reference to the Iterator.
91  */
92  function &proceed()
93  {
94  $persistenceFacade = &PersistenceFacade::getInstance();
95  $node = &$persistenceFacade->load($this->_currentOID, BUILDDEPTH_SINGLE);
96 
97  $childOIDs = $node->getProperty('childoids');
98  $this->addToSeenList($childOIDs, ++$this->_currentDepth);
99 
100  if (sizeOf($this->_oidList) != 0) {
101  list($this->_currentOID, $this->_currentDepth) = array_pop($this->_oidList);
102  }
103  else {
104  $this->_end = true;
105  }
106  return $this;
107  }
108  /**
109  * Get the current oid.
110  * @return The current oid.
111  */
112  function getCurrentOID()
113  {
114  return $this->_currentOID;
115  }
116  /**
117  * Get the current depth.
118  * @return The current depth.
119  */
120  function getCurrentDepth()
121  {
122  return $this->_currentDepth;
123  }
124  /**
125  * Find out whether iteration is finished.
126  * @return 'True' if iteration is finished, 'False' alternatively.
127  */
128  function isEnd()
129  {
130  return $this->_end;
131  }
132  /**
133  * Reset the iterator to given oid.
134  * @param oid The oid of the object to start from.
135  */
136  function reset($oid)
137  {
138  $this->_end = false;
139  $this->_oidList= array();
140  $this->_allList = array();
141  $this->_currentOID = $oid;
142  $this->_currentDepth = 0;
143  }
144  /**
145  * Add oids to the internal processed oid list.
146  * @attention Internal use only.
147  * @param oidList An array of oids.
148  * @param depth The depth of the oids in the tree.
149  */
150  function addToSeenList($oidList, $depth)
151  {
152  for ($i=sizeOf($oidList)-1;$i>=0;$i--)
153  {
154  if (!in_array($oidList[$i], $this->_allList)) {
155  array_push($this->_oidList, array($oidList[$i], $depth));
156  array_push($this->_allList, $oidList[$i]);
157  }
158  }
159  }
160  /**
161  */
162  function dumpOIDList()
163  {
164  $str = '';
165  for ($i=0; $i<sizeOf($this->_oidList); $i++)
166  {
167  $str .= $this->_oidList[$i][0].",";
168  }
169  return $str;
170  }
171 }
172 ?>
addToSeenList($oidList, $depth)
PersistentIterator is used to iterate over a tree/list build of oids using a Depth-First-Algorithm. To persist its state use the PersistentIterator::save() method, to restore its state use the static PersistentIterator::load() method, which returns the loaded instance. States are identified by an unique id, which is provided after saving. PersistentIterator implements the 'Iterator Pattern'.
const BUILDDEPTH_SINGLE