BxDolTranscoderQuery.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) BoonEx Pty Limited - http://www.boonex.com/
  4. * CC-BY License - http://creativecommons.org/licenses/by/3.0/
  5. *
  6. * @defgroup DolphinCore Dolphin Core
  7. * @{
  8. */
  9. bx_import('BxDolDb');
  10. define('BX_DOL_QUEUE_PRUNE_FAILED', 604800); ///< timeout in seconds when 'failed' items are deleted
  11. define('BX_DOL_QUEUE_PRUNE_PROCESSING', 86400); ///< timeout in seconds when 'processing' items are deleted, in case when something went wrong during processing
  12. /**
  13. * @see BxDolTranscoder
  14. */
  15. class BxDolTranscoderQuery extends BxDolDb
  16. {
  17. const TABLE_QUEUE = 'sys_transcoder_queue';
  18. protected $_aObject;
  19. protected $_sTableQueue;
  20. protected $_sTableFiles;
  21. protected $_sTableFilters;
  22. protected $_sHandlerPrefix;
  23. public function __construct($aObject, $bUseQueue = false)
  24. {
  25. parent::__construct();
  26. $this->_aObject = $aObject;
  27. $this->_sTableQueue = $bUseQueue ? self::TABLE_QUEUE : '';
  28. $this->_sTableFiles = '`sys_transcoder_images_files`';
  29. $this->_sTableFilters = '`sys_transcoder_filters`';
  30. $this->_sHandlerPrefix = 'sys_image_transcoder_';
  31. }
  32. static public function getTranscoderObject ($sObject)
  33. {
  34. $oDb = BxDolDb::getInstance();
  35. $sQuery = $oDb->prepare("SELECT * FROM `sys_objects_transcoder` WHERE `object` = ?", $sObject);
  36. return $oDb->getRow($sQuery);
  37. }
  38. static public function getTranscoderObjects ()
  39. {
  40. $oDb = BxDolDb::getInstance();
  41. $sQuery = $oDb->prepare("SELECT * FROM `sys_objects_transcoder`");
  42. return $oDb->getAll($sQuery);
  43. }
  44. public function getTranscoderFilters ()
  45. {
  46. $sQuery = $this->prepare("SELECT * FROM {$this->_sTableFilters} WHERE `transcoder_object` = ? ORDER BY `order` ASC", $this->_aObject['object']);
  47. return $this->getAll($sQuery);
  48. }
  49. public function updateHandler ($iFileId, $mixedHandler)
  50. {
  51. $sUpdateATime = '';
  52. if ($this->_aObject['atime_tracking']) {
  53. $iTime = time();
  54. $sUpdateATime = $this->prepare(", `atime` = ?", $iTime);
  55. }
  56. $sQuery = $this->prepare("INSERT INTO {$this->_sTableFiles} SET `transcoder_object` = ?, `file_id` = ?, `handler` = ? $sUpdateATime
  57. ON DUPLICATE KEY UPDATE `file_id` = ? $sUpdateATime", $this->_aObject['object'], $iFileId, $mixedHandler, $iFileId);
  58. return $this->res($sQuery);
  59. }
  60. public function getFileIdByHandler ($mixedHandler)
  61. {
  62. $sQuery = $this->prepare("SELECT `file_id` FROM {$this->_sTableFiles} WHERE `transcoder_object` = ? AND `handler` = ?", $this->_aObject['object'], $mixedHandler);
  63. return $this->getOne($sQuery);
  64. }
  65. public function updateAccessTime($mixedHandler)
  66. {
  67. $iTime = time();
  68. $sQuery = $this->prepare("UPDATE {$this->_sTableFiles} SET `atime` = ? WHERE `transcoder_object` = ? AND `handler` = ?", $iTime, $this->_aObject['object'], $mixedHandler);
  69. return $this->res($sQuery);
  70. }
  71. public function deleteFileTraces($iFileId)
  72. {
  73. $sQuery = $this->prepare("DELETE FROM {$this->_sTableFiles} WHERE `transcoder_object` = ? AND `file_id` = ?", $this->_aObject['object'], $iFileId);
  74. return $this->res($sQuery);
  75. }
  76. public function getFilesForPruning ()
  77. {
  78. if (!$this->_aObject['atime_tracking'] || !$this->_aObject['atime_pruning'])
  79. continue;
  80. $sQuery = $this->prepare("SELECT * FROM {$this->_sTableFiles} WHERE `transcoder_object` = ? AND `atime` != 0 AND `atime` < ?", $this->_aObject['object'], time() - $this->_aObject['atime_pruning']);
  81. return $this->getAll($sQuery);
  82. }
  83. public function registerHandlers ()
  84. {
  85. if (!$this->registerHandler ('getAlertHandlerNameLocal', 'alert_response_file_delete_local', $this->_aObject['object'], $this->_aObject['storage_object']))
  86. return false;
  87. // add handler for original storage engine
  88. if ('Storage' == $this->_aObject['source_type']) // if original storage is "Storage", not "Folder"
  89. if (!$this->registerHandler ('getAlertHandlerNameOrig', 'alert_response_file_delete_orig', $this->_aObject['object'], $this->_aObject['source_params']['object']))
  90. return false;
  91. return true;
  92. }
  93. public function unregisterHandlers ()
  94. {
  95. if (!$this->unregisterHandler('getAlertHandlerNameLocal', $this->_aObject['storage_object']))
  96. return false;
  97. // remove handler for original storage engine
  98. if ('Storage' == $this->_aObject['source_type']) // if original storage is "Storage", not "Folder"
  99. if (!$this->unregisterHandler('getAlertHandlerNameOrig', $this->_aObject['source_params']['object']))
  100. return false;
  101. return true;
  102. }
  103. static public function queuePruning ()
  104. {
  105. $oDb = BxDolDb::getInstance();
  106. $sQuery = $oDb->prepare("DELETE FROM `" . self::TABLE_QUEUE . "` WHERE `status` = ? AND `changed` < ? - ?", BX_DOL_QUEUE_PROCESSING, time(), BX_DOL_QUEUE_PRUNE_PROCESSING);
  107. $oDb->query($sQuery);
  108. $sQuery = $oDb->prepare("DELETE FROM `" . self::TABLE_QUEUE . "` WHERE `status` = ? AND `changed` < ? - ?", BX_DOL_QUEUE_FAILED, time(), BX_DOL_QUEUE_PRUNE_FAILED);
  109. return $oDb->query($sQuery);
  110. }
  111. static public function getNextInQueue ($sServer, $iLimit = 5)
  112. {
  113. $oDb = BxDolDb::getInstance();
  114. $sQuery = $oDb->prepare("SELECT COUNT(*) FROM `" . self::TABLE_QUEUE . "` WHERE `server` = ? AND `status` = 'processing'", $sServer);
  115. if ($oDb->getOne($sQuery))
  116. return array();
  117. $sQuery = $oDb->prepare("SELECT * FROM `" . self::TABLE_QUEUE . "` WHERE `status` = 'pending' ORDER BY `added` ASC LIMIT ?", $iLimit);
  118. return $oDb->getAll($sQuery);
  119. }
  120. public function getQueueTable ()
  121. {
  122. return $this->_sTableQueue;
  123. }
  124. public function deleteFromQueue ($mixedId)
  125. {
  126. if (!$this->_sTableQueue)
  127. return false;
  128. $sQuery = $this->prepare("DELETE FROM `" . $this->_sTableQueue . "` WHERE `transcoder_object` = ? AND `file_id_source` = ?", $this->_aObject['object'], $mixedId);
  129. return $this->query($sQuery);
  130. }
  131. public function getFromQueue ($mixedId)
  132. {
  133. if (!$this->_sTableQueue)
  134. return false;
  135. $sQuery = $this->prepare("SELECT * FROM `" . $this->_sTableQueue . "` WHERE `transcoder_object` = ? AND `file_id_source` = ?", $this->_aObject['object'], $mixedId);
  136. return $this->getRow($sQuery);
  137. }
  138. public function addToQueue ($mixedId, $sFileUrl, $iProfileId)
  139. {
  140. if (!$this->_sTableQueue)
  141. return false;
  142. $sQuery = $this->prepare("INSERT INTO `" . $this->_sTableQueue . "` SET `transcoder_object` = ?, `profile_id` = ?, `file_url_source` = ?, `file_id_source` = ?, `status` = ?, `added` = ?, changed = ?", $this->_aObject['object'], $iProfileId, $sFileUrl, $mixedId, BX_DOL_QUEUE_PENDING, time(), time());
  143. return $this->query($sQuery);
  144. }
  145. public function updateQueueStatus ($mixedId, $sStatus, $sLog = '', $sServer = '')
  146. {
  147. if (!$this->_sTableQueue)
  148. return false;
  149. $sQueryVals = $this->prepare("`status` = ?, changed = ? WHERE `transcoder_object` = ? AND `file_id_source` = ?", $sStatus, time(), $this->_aObject['object'], $mixedId);
  150. $sQueryServer = $sServer ? $this->prepare(" `server` = ?, ", $sServer) : '';
  151. $sQueryLog = $sLog ? $this->prepare(" `log` = ?, ", $sLog) : '';
  152. $sQueryAll = "UPDATE `" . $this->_sTableQueue . "` SET " . $sQueryServer . $sQueryLog . $sQueryVals;
  153. return $this->query($sQueryAll);
  154. }
  155. protected function registerHandler ($sHandlerNameFunc, $sServiceFunc, $sObject, $sUnit)
  156. {
  157. $sHandlerName = $this->$sHandlerNameFunc ();
  158. $iHandlerId = $this->getAlertHandlerId ($sHandlerName);
  159. if ($iHandlerId) // if handler already exists, do nothing
  160. return true;
  161. $sServiceCall = serialize(array(
  162. 'module' => 'system',
  163. 'method' => $sServiceFunc,
  164. 'params' => array($sObject),
  165. 'class' => 'TemplImageServices'
  166. ));
  167. $sQuery = $this->prepare("INSERT INTO `sys_alerts_handlers` SET `name` = ?, `service_call` = ?", $sHandlerName, $sServiceCall);
  168. if (!$this->query($sQuery))
  169. return false;
  170. $iHandlerId = $this->lastId();
  171. $sQuery = $this->prepare("INSERT INTO `sys_alerts` SET `unit` = ?, `action` = ?, `handler_id` = ?", $sUnit, 'file_deleted', $iHandlerId);
  172. if (!$this->query($sQuery)) {
  173. $this->unregisterHandlers ();
  174. return false;
  175. }
  176. return true;
  177. }
  178. protected function unregisterHandler ($sHandlerNameFunc, $sUnit)
  179. {
  180. $sHandlerName = $this->$sHandlerNameFunc ();
  181. $iHandlerId = $this->getAlertHandlerId ($sHandlerName);
  182. if (!$iHandlerId) // if handler is alrady missing, do nothing
  183. return true;
  184. $sQuery = $this->prepare("DELETE FROM `sys_alerts` WHERE `unit` = ? AND `action` = ? AND `handler_id` = ?", $sUnit, 'file_deleted', $iHandlerId);
  185. if (!$this->query($sQuery))
  186. return false;
  187. $sQuery = $this->prepare("DELETE FROM `sys_alerts_handlers` WHERE `id` = ?", $iHandlerId);
  188. return $this->query($sQuery);
  189. }
  190. protected function getAlertHandlerNameLocal ()
  191. {
  192. return $this->_sHandlerPrefix . 'local_file_delete_' . $this->_aObject['object'];
  193. }
  194. protected function getAlertHandlerNameOrig ()
  195. {
  196. return $this->_sHandlerPrefix . 'orig_file_delete_' . $this->_aObject['object'];
  197. }
  198. protected function getAlertHandlerId ($sHandlerName)
  199. {
  200. $sQuery = $this->prepare("SELECT `id` FROM `sys_alerts_handlers` WHERE `name` = ?", $sHandlerName);
  201. return $this->getOne($sQuery);
  202. }
  203. }
  204. /** @} */