BxDolTranscoderQuery.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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 TridentCore Trident 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 getForDeletionFromQueue ($sServer, $iLimit = 10)
  104. {
  105. $oDb = BxDolDb::getInstance();
  106. // delete items which are stuck in processing status
  107. $sQuery = $oDb->prepare("DELETE FROM `" . self::TABLE_QUEUE . "` WHERE `server` = ? AND `status` = ? AND `changed` < ? - ?", $sServer, BX_DOL_QUEUE_PROCESSING, time(), BX_DOL_QUEUE_PRUNE_PROCESSING);
  108. $oDb->query($sQuery);
  109. // get files which are subject to delete: with status 'delete' and expired 'failed' items
  110. $sQuery = $oDb->prepare("SELECT * FROM `" . self::TABLE_QUEUE . "` WHERE `server` = ? AND ((`status` = ? AND `changed` < ? - ?) OR `status` = ?)", $sServer, BX_DOL_QUEUE_FAILED, time(), BX_DOL_QUEUE_PRUNE_FAILED, BX_DOL_QUEUE_DELETE);
  111. return $oDb->getAll($sQuery);
  112. }
  113. static public function getCompletedFromQueue ($iLimit = 10)
  114. {
  115. $oDb = BxDolDb::getInstance();
  116. $sQuery = $oDb->prepare("SELECT * FROM `" . self::TABLE_QUEUE . "` WHERE `status` = 'complete' ORDER BY `added` ASC LIMIT ?", $iLimit);
  117. return $oDb->getAll($sQuery);
  118. }
  119. static public function getNextInQueue ($sServer, $iLimit = 5)
  120. {
  121. $oDb = BxDolDb::getInstance();
  122. $sQuery = $oDb->prepare("SELECT COUNT(*) FROM `" . self::TABLE_QUEUE . "` WHERE `server` = ? AND `status` = 'processing'", $sServer);
  123. if ($oDb->getOne($sQuery))
  124. return array();
  125. $sQuery = $oDb->prepare("SELECT * FROM `" . self::TABLE_QUEUE . "` WHERE `status` = 'pending' ORDER BY `added` ASC LIMIT ?", $iLimit);
  126. return $oDb->getAll($sQuery);
  127. }
  128. public function getQueueTable ()
  129. {
  130. return $this->_sTableQueue;
  131. }
  132. public function deleteFromQueue ($mixedId)
  133. {
  134. if (!$this->_sTableQueue)
  135. return false;
  136. $sQuery = $this->prepare("DELETE FROM `" . $this->_sTableQueue . "` WHERE `transcoder_object` = ? AND `file_id_source` = ?", $this->_aObject['object'], $mixedId);
  137. return $this->query($sQuery);
  138. }
  139. public function getFromQueue ($mixedId)
  140. {
  141. if (!$this->_sTableQueue)
  142. return false;
  143. $sQuery = $this->prepare("SELECT * FROM `" . $this->_sTableQueue . "` WHERE `transcoder_object` = ? AND `file_id_source` = ?", $this->_aObject['object'], $mixedId);
  144. return $this->getRow($sQuery);
  145. }
  146. public function addToQueue ($mixedId, $sFileUrl, $iProfileId)
  147. {
  148. if (!$this->_sTableQueue)
  149. return false;
  150. $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());
  151. return $this->query($sQuery);
  152. }
  153. public function updateQueueStatus ($mixedId, $sStatus, $sLog = '', $sServer = '', $mixedIdResult = '', $sFileUrlResult = '', $sFileExtResult = '')
  154. {
  155. if (!$this->_sTableQueue)
  156. return false;
  157. $sQueryVals = $this->prepare("`status` = ?, changed = ? WHERE `transcoder_object` = ? AND `file_id_source` = ?", $sStatus, time(), $this->_aObject['object'], $mixedId);
  158. $sQueryServer = $sServer ? $this->prepare(" `server` = ?, ", $sServer) : '';
  159. $sQueryLog = $sLog ? $this->prepare(" `log` = ?, ", $sLog) : '';
  160. $sQueryResultFile = $mixedIdResult && $sFileUrlResult ? $this->prepare(" `file_id_result` = ?, `file_url_result` = ?, `file_ext_result` = ?, ", $mixedIdResult, $sFileUrlResult, $sFileExtResult) : '';
  161. $sQueryAll = "UPDATE `" . $this->_sTableQueue . "` SET " . $sQueryServer . $sQueryLog . $sQueryResultFile . $sQueryVals;
  162. return $this->query($sQueryAll);
  163. }
  164. protected function registerHandler ($sHandlerNameFunc, $sServiceFunc, $sObject, $sUnit)
  165. {
  166. $sHandlerName = $this->$sHandlerNameFunc ();
  167. $iHandlerId = $this->getAlertHandlerId ($sHandlerName);
  168. if ($iHandlerId) // if handler already exists, do nothing
  169. return true;
  170. $sServiceCall = serialize(array(
  171. 'module' => 'system',
  172. 'method' => $sServiceFunc,
  173. 'params' => array($sObject),
  174. 'class' => 'TemplImageServices'
  175. ));
  176. $sQuery = $this->prepare("INSERT INTO `sys_alerts_handlers` SET `name` = ?, `service_call` = ?", $sHandlerName, $sServiceCall);
  177. if (!$this->query($sQuery))
  178. return false;
  179. $iHandlerId = $this->lastId();
  180. $sQuery = $this->prepare("INSERT INTO `sys_alerts` SET `unit` = ?, `action` = ?, `handler_id` = ?", $sUnit, 'file_deleted', $iHandlerId);
  181. if (!$this->query($sQuery)) {
  182. $this->unregisterHandlers ();
  183. return false;
  184. }
  185. return true;
  186. }
  187. protected function unregisterHandler ($sHandlerNameFunc, $sUnit)
  188. {
  189. $sHandlerName = $this->$sHandlerNameFunc ();
  190. $iHandlerId = $this->getAlertHandlerId ($sHandlerName);
  191. if (!$iHandlerId) // if handler is alrady missing, do nothing
  192. return true;
  193. $sQuery = $this->prepare("DELETE FROM `sys_alerts` WHERE `unit` = ? AND `action` = ? AND `handler_id` = ?", $sUnit, 'file_deleted', $iHandlerId);
  194. if (!$this->query($sQuery))
  195. return false;
  196. $sQuery = $this->prepare("DELETE FROM `sys_alerts_handlers` WHERE `id` = ?", $iHandlerId);
  197. return $this->query($sQuery);
  198. }
  199. protected function getAlertHandlerNameLocal ()
  200. {
  201. return $this->_sHandlerPrefix . 'local_file_delete_' . $this->_aObject['object'];
  202. }
  203. protected function getAlertHandlerNameOrig ()
  204. {
  205. return $this->_sHandlerPrefix . 'orig_file_delete_' . $this->_aObject['object'];
  206. }
  207. protected function getAlertHandlerId ($sHandlerName)
  208. {
  209. $sQuery = $this->prepare("SELECT `id` FROM `sys_alerts_handlers` WHERE `name` = ?", $sHandlerName);
  210. return $this->getOne($sQuery);
  211. }
  212. }
  213. /** @} */