AJAX_Locking
[ class tree: AJAX_Locking ] [ index: AJAX_Locking ] [ all elements ]

Source for file MDB2.php

Documentation is available at MDB2.php

  1. <?php
  2. require_once('Driver.php');
  3. require_once('MDB2.php');
  4.  
  5. /**
  6. * A driver that use MDB2 as data store
  7. */
  8. class AJAX_Locking_Driver_MDB2 extends AJAX_Locking_Driver
  9. {
  10. /**
  11. * Instance of a MDB2 connection
  12. *
  13. * @var MBD2 connection
  14. */
  15. var $mdb2;
  16.  
  17. /**
  18. * Default table and columns names
  19. *
  20. * @var array
  21. */
  22. var $names = array(
  23. 'table' => 'ajax_locks',
  24. 'columns' => array(
  25. 'type' => 'type',
  26. 'id' => 'id',
  27. 'user' => '[user]',
  28. 'time' => '[time]'
  29. )
  30. );
  31.  
  32. /**
  33. * Constructor
  34. *
  35. * @param mixed $dsn DSN for MDB2 connection
  36. * @param array $names names of table and its columns where store data
  37. * @param int $timeout session timeout
  38. * @return AJAX_Locking_Driver
  39. */
  40. function AJAX_Locking_Driver_MDB2($dsn = false, $names = false, $timeout = false)
  41. {
  42. parent::AJAX_Locking_Driver($timeout);
  43.  
  44. $this->mdb2 =& MDB2::factory($dsn);
  45. if (PEAR::isError($this->mdb2)) {
  46. die($this->mdb2->getMessage());
  47. }
  48.  
  49. // eventually override default names of table and columns
  50. if ($names && is_array($names)) {
  51. $names['columns'] = array_merge($this->names['columns'], $names['columns']);
  52. $this->names = array_merge($this->names, $names);
  53. }
  54. }
  55.  
  56. /**
  57. * Locks an object
  58. *
  59. * @param mixed $user id of the user who wants to lock
  60. * @param string $type type/classname of the object
  61. * @param mixed $id id of the object
  62. * @return boolean true if the unlock was successfull, false otherwise
  63. */
  64. function lock($user, $type, $id)
  65. {
  66. $result = false;
  67. $this->mdb2->beginTransaction();
  68.  
  69. $sql = $this->_getStatusSQL($type, $id);
  70. $rs = $this->mdb2->query($sql);
  71. if (PEAR::isError($rs)) {
  72. die($rs->getMessage());
  73. }
  74. $row = $rs->fetchRow();
  75. if (!$row) {
  76. // object is unlocked: lock it
  77. $sql = $this->_getLockSQL($user, $type, $id);
  78. $affected =& $this->mdb2->exec($sql);
  79. if (PEAR::isError($affected)) {
  80. die($affected->getMessage());
  81. }
  82. $result = true;
  83. } else {
  84. // object is locked: check if lock expired
  85. $time = $row[3];
  86. if (time() - $time > $this->timeout) {
  87. // lock expired: remove it and lock
  88. $sql = $this->_getUnlockSQL($type, $id);
  89. $this->mdb2->exec($sql);
  90. $sql = $this->_getLockSQL($user, $type, $id);
  91. $this->mdb2->exec($sql);
  92. $result = true;
  93. } else {
  94. // object properly locked by (another?) user
  95. $result = ($owner == $user);
  96. }
  97. }
  98.  
  99. $this->mdb2->commit();
  100.  
  101. return $result;
  102. }
  103.  
  104. /**
  105. * Unlocks an object
  106. *
  107. * @param mixed $user id of the user who wants to unlock
  108. * @param string $type type/classname of the object
  109. * @param mixed $id id of the object
  110. * @return boolean true if the unlock was successfull, false otherwise
  111. */
  112. function unlock($user, $type, $id)
  113. {
  114. $result = false;
  115. $this->mdb2->beginTransaction();
  116.  
  117. $sql = $this->_getStatusSQL($type, $id);
  118. $rs = $this->mdb2->query($sql);
  119. if (PEAR::isError($rs)) {
  120. die($rs->getMessage());
  121. }
  122. $row = $rs->fetchRow();
  123. if (!$row) {
  124. // object was not locked
  125. $result = false;
  126. } else {
  127. // check if user is owner of the lock
  128. $owner = $row[2];
  129. if ($user == $owner) {
  130. // unlock it
  131. $sql = $this->_getUnlockSQL($type, $id);
  132. $this->mdb2->exec($sql);
  133. $result = true;
  134. } else {
  135. // user cannot unlock it
  136. $result = false;
  137. }
  138. }
  139.  
  140. $this->mdb2->commit();
  141. return $result;
  142. }
  143.  
  144. /**
  145. * Returns the status of the object (lock or unlocked)
  146. *
  147. * @param mixed $user id of the user who wants to know the object's status
  148. * @param string $type type/classname of the object
  149. * @param mixed $id id of the object
  150. * @return string the status and the owner of the object
  151. */
  152. function status($user, $type, $id)
  153. {
  154. $result = "~";
  155. $this->mdb2->beginTransaction();
  156.  
  157. $sql = $this->_getStatusSQL($type, $id);
  158. $rs = $this->mdb2->query($sql);
  159. if (PEAR::isError($rs)) {
  160. die($rs->getMessage());
  161. }
  162. $row = $rs->fetchRow();
  163. if (!$row) {
  164. // object is unlocked
  165. $result = AJAX_LOCKING_UNLOCKED . '~' . 'noboby';
  166. } else {
  167. // object is locked:
  168. // 1. check if current user is owner of the lock
  169. // 2. check if lock expired
  170. $owner = $row[2]; $time = $row[3];
  171. $timeout = (time() - $time > $this->timeout);
  172. if ($owner == $user) {
  173. if ($timeout) {
  174. // lock expired: remove it and inform it's ower
  175. $sql = $this->_getUnlockSQL($type, $id);
  176. $this->mdb2->exec($sql);
  177. $result = AJAX_LOCKING_TIMEOUT . '~' . $owner;
  178. } else {
  179. $result = AJAX_LOCKING_OWNED . '~' . $owner;
  180. }
  181. } else {
  182. if ($timeout) {
  183. // lock expired: remove it
  184. $sql = $this->_getUnlockSQL($type, $id);
  185. $this->mdb2->exec($sql);
  186. $result = AJAX_LOCKING_UNLOCKED . '~' . 'nobody';
  187. } else {
  188. $result = AJAX_LOCKING_LOCKED . '~' . $owner;
  189. }
  190. }
  191. }
  192.  
  193. $this->mdb2->commit();
  194. return $result;
  195. }
  196.  
  197. /**
  198. * Returns the list of all active locks for administration purpose
  199. *
  200. * @return array of locks (owner, type, id), false if not implemented
  201. */
  202. function getLocks()
  203. {
  204. $locks = array();
  205.  
  206. $sql = $this->_getListSQL($type, $id);
  207. $rs = $this->mdb2->query($sql);
  208. if (PEAR::isError($rs)) {
  209. die($rs->getMessage());
  210. }
  211. while (($row = $rs->fetchRow())) {
  212. list($owner, $type, $id, $time) = $row;
  213. $timeout = (time() - $time > $this->timeout);
  214. if (!$timeout) {
  215. $locks[] = array(
  216. 'owner' => $owner,
  217. 'type' => $type,
  218. 'id' => $id
  219. );
  220. }
  221. }
  222.  
  223. return $locks;
  224. }
  225.  
  226. /**
  227. * Administrately delete lock
  228. *
  229. * @param string $type
  230. * @param mixed $id
  231. *
  232. * @return true if the lock is deleted successfully, false otherwise
  233. */
  234. function delete($type, $id)
  235. {
  236. $sql = $this->_getUnlockSQL($type, $id);
  237. return $this->mdb2->exec($sql);
  238. }
  239.  
  240.  
  241. function _getStatusSQL($type, $id)
  242. {
  243. $sql = sprintf("select * from %s where %s = %s and %s = %s",
  244. $this->names['table'],
  245. $this->names['columns']['type'], $this->mdb2->quote($type),
  246. $this->names['columns']['id'], $this->mdb2->quote($id));
  247. return $sql;
  248. }
  249.  
  250. function _getLockSQL($user, $type, $id)
  251. {
  252. $timestamp = time();
  253. $sql = sprintf("insert into %s (%s, %s, %s, %s) values(%s, %s, %s, %s)",
  254. $this->names['table'],
  255. $this->names['columns']['type'], $this->names['columns']['id'], $this->names['columns']['user'], $this->names['columns']['time'],
  256. $this->mdb2->quote($type),
  257. $this->mdb2->quote($id),
  258. $this->mdb2->quote($user),
  259. $timestamp);
  260. return $sql;
  261. }
  262.  
  263. function _getUnlockSQL($type, $id)
  264. {
  265. $sql = sprintf("delete from %s where %s = %s and %s = %s",
  266. $this->names['table'],
  267. $this->names['columns']['type'], $this->mdb2->quote($type),
  268. $this->names['columns']['id'], $this->mdb2->quote($id));
  269. return $sql;
  270. }
  271.  
  272. function _getListSQL($type, $id)
  273. {
  274. $sql = sprintf("select * from %s",
  275. $this->names['table']);
  276. return $sql;
  277. }
  278.  
  279. }
  280. ?>

Documentation generated on Tue, 13 Feb 2007 21:42:45 +0100 by phpDocumentor 1.3.0RC3