Source for file HarmoniEntryIterator.class.php

Documentation is available at HarmoniEntryIterator.class.php

  1. <?php
  2. /**
  3. * @since 3/1/06
  4. * @package harmoni.osid_v2.logging
  5. *
  6. * @copyright Copyright &copy; 2005, Middlebury College
  7. * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License (GPL)
  8. *
  9. * @version $Id: HarmoniEntryIterator.class.php,v 1.6 2007/09/04 20:25:43 adamfranco Exp $
  10. */
  11.  
  12. require_once(OKI2."/osid/logging/EntryIterator.php");
  13. require_once(dirname(__FILE__)."/HarmoniEntry.class.php");
  14.  
  15. /**
  16. * EntryIterator provides access to these objects sequentially, one at a time.
  17. * The purpose of all Iterators is to to offer a way for OSID methods to
  18. * return multiple values of a common type and not use an array. Returning an
  19. * array may not be appropriate if the number of values returned is large or
  20. * is fetched remotely. Iterators do not allow access to values by index,
  21. * rather you must access values in sequence. Similarly, there is no way to go
  22. * backwards through the sequence unless you place the values in a data
  23. * structure, such as an array, that allows for access by index.
  24. *
  25. * @since 3/1/06
  26. * @package harmoni.osid_v2.logging
  27. *
  28. * @copyright Copyright &copy; 2005, Middlebury College
  29. * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License (GPL)
  30. *
  31. * @version $Id: HarmoniEntryIterator.class.php,v 1.6 2007/09/04 20:25:43 adamfranco Exp $
  32. */
  33. class HarmoniEntryIterator
  34. extends EntryIterator
  35. {
  36. /**
  37. * Constructor
  38. *
  39. * @param string $logName
  40. * @param object Type $formatType
  41. * @param object Type $priorityType
  42. * @return object
  43. * @access public
  44. * @since 3/1/06
  45. */
  46. function HarmoniEntryIterator ( $logName, $formatType, $priorityType, $dbIndex ) {
  47. $this->_logName = $logName;
  48. $this->_formatType =$formatType;
  49. $this->_priorityType =$priorityType;
  50. $this->_dbIndex = $dbIndex;
  51. $this->_current = 0;
  52. $this->_currentRow = 0;
  53. $this->_numPerLoad = 50;
  54. $this->_entries = array();
  55. $this->_entryIds = array();
  56. $this->loadCount();
  57. }
  58. /**
  59. * Return true if there is an additional Entry
  60. * Description_IteratorHasNext2]
  61. *
  62. * @return boolean
  63. *
  64. * @throws object LoggingException An exception with one of the
  65. * following messages defined in org.osid.logging.LoggingException
  66. * may be thrown: {@link }
  67. * org.osid.logging.LoggingException#UNIMPLEMENTED UNIMPLEMENTED},
  68. * {@link org.osid.logging.LoggingException#OPERATION_FAILED}
  69. * OPERATION_FAILED}, {@link }
  70. * org.osid.logging.LoggingException#CONFIGURATION_ERROR
  71. * CONFIGURATION_ERROR}, {@link }
  72. * org.osid.logging.LoggingException#PERMISSION_DENIED
  73. * PERMISSION_DENIED}
  74. *
  75. * @access public
  76. */
  77. function hasNextEntry () {
  78. return $this->hasNext();
  79. }
  80.  
  81. /**
  82. * Return the next Entry.
  83. *
  84. * @return object Entry
  85. *
  86. * @throws object LoggingException An exception with one of the
  87. * following messages defined in org.osid.logging.LoggingException
  88. * may be thrown: {@link }
  89. * org.osid.logging.LoggingException#UNIMPLEMENTED UNIMPLEMENTED},
  90. * {@link org.osid.logging.LoggingException#OPERATION_FAILED}
  91. * OPERATION_FAILED}, {@link }
  92. * org.osid.logging.LoggingException#CONFIGURATION_ERROR
  93. * CONFIGURATION_ERROR}, {@link }
  94. * org.osid.logging.LoggingException#PERMISSION_DENIED
  95. * PERMISSION_DENIED}, {@link }
  96. * org.osid.logging.LoggingException#NO_MORE_ITERATOR_ELEMENTS
  97. * NO_MORE_ITERATOR_ELEMENTS}
  98. *
  99. * @access public
  100. */
  101. function nextEntry () {
  102. return $this->next();
  103. }
  104. /**
  105. * Return true if there is an additional Entry
  106. * Description_IteratorHasNext2]
  107. *
  108. * @return boolean
  109. *
  110. * @throws object LoggingException An exception with one of the
  111. * following messages defined in org.osid.logging.LoggingException
  112. * may be thrown: {@link }
  113. * org.osid.logging.LoggingException#UNIMPLEMENTED UNIMPLEMENTED},
  114. * {@link org.osid.logging.LoggingException#OPERATION_FAILED}
  115. * OPERATION_FAILED}, {@link }
  116. * org.osid.logging.LoggingException#CONFIGURATION_ERROR
  117. * CONFIGURATION_ERROR}, {@link }
  118. * org.osid.logging.LoggingException#PERMISSION_DENIED
  119. * PERMISSION_DENIED}
  120. *
  121. * @access public
  122. */
  123. function hasNext () {
  124. return ($this->_current < $this->_count);
  125. }
  126.  
  127. /**
  128. * Return the next Entry.
  129. *
  130. * @return object Entry
  131. *
  132. * @throws object LoggingException An exception with one of the
  133. * following messages defined in org.osid.logging.LoggingException
  134. * may be thrown: {@link }
  135. * org.osid.logging.LoggingException#UNIMPLEMENTED UNIMPLEMENTED},
  136. * {@link org.osid.logging.LoggingException#OPERATION_FAILED}
  137. * OPERATION_FAILED}, {@link }
  138. * org.osid.logging.LoggingException#CONFIGURATION_ERROR
  139. * CONFIGURATION_ERROR}, {@link }
  140. * org.osid.logging.LoggingException#PERMISSION_DENIED
  141. * PERMISSION_DENIED}, {@link }
  142. * org.osid.logging.LoggingException#NO_MORE_ITERATOR_ELEMENTS
  143. * NO_MORE_ITERATOR_ELEMENTS}
  144. *
  145. * @access public
  146. */
  147. function next () {
  148. if (!$this->hasNext())
  149. throwError(new Error(SharedException::NO_MORE_ITERATOR_ELEMENTS(),
  150. get_class($this), true));
  151. if (!isset($this->_entries[$this->_current]))
  152. $this->loadNext();
  153. $entry =$this->_entries[$this->_current];
  154. $this->_current++;
  155. return $entry;
  156. }
  157. /**
  158. * Skips the next item in the iterator
  159. *
  160. * @return void
  161. * @public
  162. */
  163. function skipNext() {
  164. // if (!$this->hasNext())
  165. // throwError(new Error(SharedException::NO_MORE_ITERATOR_ELEMENTS(),
  166. // get_class($this), true));
  167. //
  168. // $this->_current++;
  169.  
  170. $this->next();
  171. }
  172. /**
  173. * Gives the number of items in the iterator
  174. *
  175. * @return int
  176. * @public
  177. */
  178. function count () {
  179. return $this->_count;
  180. }
  181. /**
  182. * Load the number of results
  183. *
  184. * @return void
  185. * @access public
  186. * @since 3/9/06
  187. */
  188. function loadCount () {
  189. // load the count
  190. $dbc = Services::getService("DatabaseManager");
  191. $query = new SelectQuery;
  192. $query->addTable("log_entry");
  193. $query->addTable("log_type", INNER_JOIN, "log_entry.fk_format_type = format_type.id", "format_type");
  194. $query->addTable("log_type", INNER_JOIN, "log_entry.fk_priority_type = priority_type.id", "priority_type");
  195. $query->addColumn("id", "entry_id", "log_entry");
  196. $query->setDistinct(true);
  197. $this->addWhereClauses($query);
  198. // Debug::printQuery($query);
  199. $results =$dbc->query($query, $this->_dbIndex);
  200. $this->_count = $results->getNumberOfRows();
  201. $results->free();
  202. }
  203. /**
  204. * Load the next bunch of items
  205. *
  206. * @return void
  207. * @access public
  208. * @since 3/1/06
  209. */
  210. function loadNext () {
  211. $dbc = Services::getService("DatabaseManager");
  212.  
  213. // get the list of the next set of Ids
  214. $query = new SelectQuery;
  215. $query->addTable("log_entry");
  216. $query->addTable("log_type", INNER_JOIN, "log_entry.fk_format_type = format_type.id", "format_type");
  217. $query->addTable("log_type", INNER_JOIN, "log_entry.fk_priority_type = priority_type.id", "priority_type");
  218. $query->addColumn("id", "entry_id", "log_entry");
  219. $query->setDistinct(true);
  220. $query->addOrderBy("timestamp", DESCENDING);
  221. $this->addWhereClauses($query);
  222. $query->limitNumberOfRows($this->_numPerLoad);
  223. if ($this->_currentRow)
  224. $query->startFromRow($this->_currentRow + 1);
  225. // debug::printQuery($query);
  226. $results =$dbc->query($query, $this->_dbIndex);
  227. $nextIds = array();
  228. while ($results->hasNext()) {
  229. $row = $results->next();
  230. $nextIds[] = "'".addslashes($row['entry_id'])."'";
  231. }
  232. // Load the rows for the next set of Ids
  233. $query =$this->getBaseQuery();
  234. $query->addWhere("log_entry.id IN (".implode(", ", $nextIds).")");
  235. $this->addColumnsAndOrder($query);
  236. // debug::printQuery($query);
  237. $results =$dbc->query($query, $this->_dbIndex);
  238. $i = $this->_current;
  239. $currentEntryId = null;
  240. $timestamp = null;
  241. $category = null;
  242. $description = null;
  243. $backtrace = '';
  244. $agents = array();
  245. $nodes = array();
  246. $rowsTraversed = 0;
  247. while ($results->hasNext()) {
  248. $row = $results->next();
  249. // Create the entry if we have all of the data for it.
  250. if ($currentEntryId && $currentEntryId != $row["id"]) {
  251. // printpre("Creating Entry: ".$currentEntryId." ".$timestamp." -- ".($i+1)." of ".$this->_count);
  252. $this->_entries[$i] = new HarmoniEntry($dbc->fromDBDate($timestamp, $this->_dbIndex),
  253. $category,
  254. $description,
  255. $backtrace,
  256. array_unique($agents),
  257. array_unique($nodes),
  258. $this->_formatType,
  259. $this->_priorityType);
  260. $i++;
  261. $currentEntryId = null;
  262. $timestamp = null;
  263. $category = null;
  264. $description = null;
  265. $backtrace = '';
  266. $agents = array();
  267. $nodes = array();
  268. $this->_currentRow = $this->_currentRow + $rowsTraversed;
  269. $rowsTraversed = 0;
  270. }
  271. $currentEntryId = $row["id"];
  272. $timestamp = $row["timestamp"];
  273. $category = $row["category"];
  274. $description = $row["description"];
  275. $backtrace = $row["backtrace"];
  276. $agents[] = $row["agent_id"];
  277. $nodes[] = $row["node_id"];
  278. // printpre($currentEntryId." ".$timestamp." ".$this->_currentRow);
  279. $rowsTraversed++;
  280. }
  281. $results->free();
  282. // get the last entry if we are at the end of the iterator
  283. if ($currentEntryId && $i == ($this->_count - 1)) {
  284. // printpre("Creating Entry: ".$currentEntryId." ".$timestamp." -- ".($i+1)." of ".$this->_count);
  285. $this->_entries[$i] = new HarmoniEntry($dbc->fromDBDate($timestamp, $this->_dbIndex),
  286. $category,
  287. $description,
  288. $backtrace,
  289. array_unique($agents),
  290. array_unique($nodes),
  291. $this->_formatType,
  292. $this->_priorityType);
  293. }
  294. }
  295. /**
  296. * Answer the basic query
  297. *
  298. * @return object SelectQuery
  299. * @access public
  300. * @since 3/9/06
  301. */
  302. function getBaseQuery () {
  303. $query = new SelectQuery;
  304. $query->addTable("log_entry");
  305. $query->addTable("log_type", INNER_JOIN, "log_entry.fk_format_type = format_type.id", "format_type");
  306. $query->addTable("log_type", INNER_JOIN, "log_entry.fk_priority_type = priority_type.id", "priority_type");
  307. $query->addTable("log_agent", LEFT_JOIN, "log_entry.id = log_agent.fk_entry");
  308. $query->addTable("log_node", LEFT_JOIN, "log_entry.id = log_node.fk_entry");
  309. return $query;
  310. }
  311. /**
  312. * Add columns, orders, and limits to our query
  313. *
  314. * @param object SelectQuery $query
  315. * @return void
  316. * @access public
  317. * @since 3/9/06
  318. */
  319. function addColumnsAndOrder ( $query ) {
  320. $query->addOrderBy("timestamp", DESCENDING);
  321. $query->addOrderBy("id", ASCENDING);
  322. $query->addColumn("id", "id", "log_entry");
  323. $query->addColumn("timestamp", "timestamp", "log_entry");
  324. $query->addColumn("category", "category", "log_entry");
  325. $query->addColumn("description", "description", "log_entry");
  326. $query->addColumn("backtrace", "backtrace", "log_entry");
  327. $query->addColumn("fk_agent", "agent_id", "log_agent");
  328. $query->addColumn("fk_node", "node_id", "log_node");
  329. }
  330. /**
  331. * Add where clauses to the query
  332. *
  333. * @param object SelectQuery $query
  334. * @return void
  335. * @access public
  336. * @since 3/9/06
  337. */
  338. function addWhereClauses ( $query ) {
  339. $query->addWhere("log_name = '".addslashes($this->_logName)."'");
  340. $query->addWhere("format_type.domain = '".addslashes($this->_formatType->getDomain())."'");
  341. $query->addWhere("format_type.authority = '".addslashes($this->_formatType->getAuthority())."'");
  342. $query->addWhere("format_type.keyword = '".addslashes($this->_formatType->getKeyword())."'");
  343. if ($this->_priorityType) {
  344. $query->addWhere("priority_type.domain = '".addslashes($this->_priorityType->getDomain())."'");
  345. $query->addWhere("priority_type.authority = '".addslashes($this->_priorityType->getAuthority())."'");
  346. $query->addWhere("priority_type.keyword = '".addslashes($this->_priorityType->getKeyword())."'");
  347. }
  348. }
  349. }
  350.  
  351. ?>

Documentation generated on Wed, 19 Sep 2007 10:23:53 -0400 by phpDocumentor 1.3.0RC3