BxDolTranscoderQuery.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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. define('BX_DOL_QUEUE_PRUNE_FAILED', 604800); ///< timeout in seconds when 'failed' items are deleted
  10. define('BX_DOL_QUEUE_PRUNE_PROCESSING', 86400); ///< timeout in seconds when 'processing' items are deleted, in case when something went wrong during processing
  11. /**
  12. * @see BxDolTranscoder
  13. */
  14. class BxDolTranscoderQuery extends BxDolDb
  15. {
  16. const TABLE_QUEUE = 'sys_transcoder_queue';
  17. protected $_aObject;
  18. protected $_sTableQueue;
  19. protected $_sTableFiles;
  20. protected $_sTableFilters;
  21. protected $_sHandlerPrefix;
  22. public function __construct($aObject, $bUseQueue = false)
  23. {
  24. parent::__construct();
  25. $this->_aObject = $aObject;
  26. $this->_sTableQueue = $bUseQueue ? self::TABLE_QUEUE : '';
  27. $this->_sTableFiles = '`sys_transcoder_images_files`';
  28. $this->_sTableFilters = '`sys_transcoder_filters`';
  29. $this->_sHandlerPrefix = 'sys_image_transcoder_';
  30. }
  31. static public function getTranscoderObject ($sObject)
  32. {
  33. $oDb = BxDolDb::getInstance();
  34. $sQuery = $oDb->prepare("SELECT * FROM `sys_objects_transcoder` WHERE `object` = ?", $sObject);
  35. return $oDb->getRow($sQuery);
  36. }
  37. static public function getTranscoderObjects ()
  38. {
  39. $oDb = BxDolDb::getInstance();
  40. $sQuery = $oDb->prepare("SELECT * FROM `sys_objects_transcoder`");
  41. return $oDb->getAll($sQuery);
  42. }
  43. public function getTranscoderFilters ()
  44. {
  45. $sQuery = $this->prepare("SELECT * FROM {$this->_sTableFilters} WHERE `transcoder_object` = ? ORDER BY `order` ASC", $this->_aObject['object']);
  46. return $this->getAll($sQuery);
  47. }
  48. public function updateHandler ($iFileId, $mixedHandler)
  49. {
  50. $sUpdateATime = '';
  51. if ($this->_aObject['atime_tracking']) {
  52. $iTime = time();
  53. $sUpdateATime = $this->prepare(", `atime` = ?", $iTime);
  54. }
  55. $sQuery = $this->prepare("INSERT INTO {$this->_sTableFiles} SET `transcoder_object` = ?, `file_id` = ?, `handler` = ? $sUpdateATime
  56. ON DUPLICATE KEY UPDATE `file_id` = ? $sUpdateATime", $this->_aObject['object'], $iFileId, $mixedHandler, $iFileId);
  57. return $this->res($sQuery);
  58. }
  59. public function getFileIdByHandler ($mixedHandler)
  60. {
  61. $sQuery = $this->prepare("SELECT `file_id` FROM {$this->_sTableFiles} WHERE `transcoder_object` = ? AND `handler` = ?", $this->_aObject['object'], $mixedHandler);
  62. return $this->getOne($sQuery);
  63. }
  64. public function updateObjectModificationTime($iTime = 0)
  65. {
  66. $sQuery = $this->prepare("UPDATE `sys_objects_transcoder` SET `ts` = ? WHERE `object` = ?", $iTime ? $iTime : time(), $this->_aObject['object']);
  67. return $this->res($sQuery);
  68. }
  69. public function updateAccessTime($mixedHandler)
  70. {
  71. $iTime = time();
  72. $sQuery = $this->prepare("UPDATE {$this->_sTableFiles} SET `atime` = ? WHERE `transcoder_object` = ? AND `handler` = ?", $iTime, $this->_aObject['object'], $mixedHandler);
  73. return $this->res($sQuery);
  74. }
  75. public function deleteFileTraces($iFileId)
  76. {
  77. $sQuery = $this->prepare("DELETE FROM {$this->_sTableFiles} WHERE `transcoder_object` = ? AND `file_id` = ?", $this->_aObject['object'], $iFileId);
  78. return $this->res($sQuery);
  79. }
  80. public function getFilesForPruning ()
  81. {
  82. if (!$this->_aObject['atime_tracking'] || !$this->_aObject['atime_pruning'])
  83. continue;
  84. $sQuery = $this->prepare("SELECT * FROM {$this->_sTableFiles} WHERE `transcoder_object` = ? AND `atime` != 0 AND `atime` < ?", $this->_aObject['object'], time() - $this->_aObject['atime_pruning']);
  85. return $this->getAll($sQuery);
  86. }
  87. public function registerHandlers ()
  88. {
  89. if (!$this->registerHandler ('getAlertHandlerNameLocal', 'alert_response_file_delete_local', $this->_aObject['object'], $this->_aObject['storage_object']))
  90. return false;
  91. // add handler for original storage engine
  92. if ('Storage' == $this->_aObject['source_type']) // if original storage is "Storage", not "Folder"
  93. if (!$this->registerHandler ('getAlertHandlerNameOrig', 'alert_response_file_delete_orig', $this->_aObject['object'], $this->_aObject['source_params']['object']))
  94. return false;
  95. return true;
  96. }
  97. public function unregisterHandlers ()
  98. {
  99. if (!$this->unregisterHandler('getAlertHandlerNameLocal', $this->_aObject['storage_object']))
  100. return false;
  101. // remove handler for original storage engine
  102. if ('Storage' == $this->_aObject['source_type']) // if original storage is "Storage", not "Folder"
  103. if (!$this->unregisterHandler('getAlertHandlerNameOrig', $this->_aObject['source_params']['object']))
  104. return false;
  105. return true;
  106. }
  107. static public function getForDeletionFromQueue ($sServer, $iLimit = 10)
  108. {
  109. $oDb = BxDolDb::getInstance();
  110. // delete items which are stuck in processing status
  111. $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);
  112. $oDb->query($sQuery);
  113. // get files which are subject to delete: with status 'delete' and expired 'failed' items
  114. $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);
  115. return $oDb->getAll($sQuery);
  116. }
  117. static public function getCompletedFromQueue ($iLimit = 10)
  118. {
  119. $oDb = BxDolDb::getInstance();
  120. $sQuery = $oDb->prepare("SELECT * FROM `" . self::TABLE_QUEUE . "` WHERE `status` = 'complete' ORDER BY `added` ASC LIMIT ?", $iLimit);
  121. return $oDb->getAll($sQuery);
  122. }
  123. static public function getNextInQueue ($sServer, $iLimit = 5)
  124. {
  125. $oDb = BxDolDb::getInstance();
  126. $sQuery = $oDb->prepare("SELECT COUNT(*) FROM `" . self::TABLE_QUEUE . "` WHERE `server` = ? AND `status` = 'processing'", $sServer);
  127. if ($oDb->getOne($sQuery))
  128. return array();
  129. $sQuery = $oDb->prepare("SELECT * FROM `" . self::TABLE_QUEUE . "` WHERE `status` = 'pending' ORDER BY `added` ASC LIMIT ?", $iLimit);
  130. return $oDb->getAll($sQuery);
  131. }
  132. public function getQueueTable ()
  133. {
  134. return $this->_sTableQueue;
  135. }
  136. public function deleteFromQueue ($mixedId)
  137. {
  138. if (!$this->_sTableQueue)
  139. return false;
  140. $sQuery = $this->prepare("DELETE FROM `" . $this->_sTableQueue . "` WHERE `transcoder_object` = ? AND `file_id_source` = ?", $this->_aObject['object'], $mixedId);
  141. return $this->query($sQuery);
  142. }
  143. public function getFromQueue ($mixedId)
  144. {
  145. if (!$this->_sTableQueue)
  146. return false;
  147. $sQuery = $this->prepare("SELECT * FROM `" . $this->_sTableQueue . "` WHERE `transcoder_object` = ? AND `file_id_source` = ?", $this->_aObject['object'], $mixedId);
  148. return $this->getRow($sQuery);
  149. }
  150. public function addToQueue ($mixedId, $sFileUrl, $iProfileId)
  151. {
  152. if (!$this->_sTableQueue)
  153. return false;
  154. $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());
  155. return $this->query($sQuery);
  156. }
  157. public function updateQueueStatus ($mixedId, $sStatus, $sLog = '', $sServer = '', $mixedIdResult = '', $sFileUrlResult = '', $sFileExtResult = '')
  158. {
  159. if (!$this->_sTableQueue)
  160. return false;
  161. $sQueryVals = $this->prepare("`status` = ?, changed = ? WHERE `transcoder_object` = ? AND `file_id_source` = ?", $sStatus, time(), $this->_aObject['object'], $mixedId);
  162. $sQueryServer = $sServer ? $this->prepare(" `server` = ?, ", $sServer) : '';
  163. $sQueryLog = $sLog ? $this->prepare(" `log` = ?, ", $sLog) : '';
  164. $sQueryResultFile = $mixedIdResult && $sFileUrlResult ? $this->prepare(" `file_id_result` = ?, `file_url_result` = ?, `file_ext_result` = ?, ", $mixedIdResult, $sFileUrlResult, $sFileExtResult) : '';
  165. $sQueryAll = "UPDATE `" . $this->_sTableQueue . "` SET " . $sQueryServer . $sQueryLog . $sQueryResultFile . $sQueryVals;
  166. return $this->query($sQueryAll);
  167. }
  168. protected function registerHandler ($sHandlerNameFunc, $sServiceFunc, $sObject, $sUnit)
  169. {
  170. $sHandlerName = $this->$sHandlerNameFunc ();
  171. $iHandlerId = $this->getAlertHandlerId ($sHandlerName);
  172. if ($iHandlerId) // if handler already exists, do nothing
  173. return true;
  174. $sServiceCall = serialize(array(
  175. 'module' => 'system',
  176. 'method' => $sServiceFunc,
  177. 'params' => array($sObject),
  178. 'class' => 'TemplImageServices'
  179. ));
  180. $sQuery = $this->prepare("INSERT INTO `sys_alerts_handlers` SET `name` = ?, `service_call` = ?", $sHandlerName, $sServiceCall);
  181. if (!$this->query($sQuery))
  182. return false;
  183. $iHandlerId = $this->lastId();
  184. $sQuery = $this->prepare("INSERT INTO `sys_alerts` SET `unit` = ?, `action` = ?, `handler_id` = ?", $sUnit, 'file_deleted', $iHandlerId);
  185. if (!$this->query($sQuery)) {
  186. $this->unregisterHandlers ();
  187. return false;
  188. }
  189. return true;
  190. }
  191. protected function unregisterHandler ($sHandlerNameFunc, $sUnit)
  192. {
  193. $sHandlerName = $this->$sHandlerNameFunc ();
  194. $iHandlerId = $this->getAlertHandlerId ($sHandlerName);
  195. if (!$iHandlerId) // if handler is alrady missing, do nothing
  196. return true;
  197. $sQuery = $this->prepare("DELETE FROM `sys_alerts` WHERE `unit` = ? AND `action` = ? AND `handler_id` = ?", $sUnit, 'file_deleted', $iHandlerId);
  198. if (!$this->query($sQuery))
  199. return false;
  200. $sQuery = $this->prepare("DELETE FROM `sys_alerts_handlers` WHERE `id` = ?", $iHandlerId);
  201. return $this->query($sQuery);
  202. }
  203. protected function getAlertHandlerNameLocal ()
  204. {
  205. return $this->_sHandlerPrefix . 'local_file_delete_' . $this->_aObject['object'];
  206. }
  207. protected function getAlertHandlerNameOrig ()
  208. {
  209. return $this->_sHandlerPrefix . 'orig_file_delete_' . $this->_aObject['object'];
  210. }
  211. protected function getAlertHandlerId ($sHandlerName)
  212. {
  213. $sQuery = $this->prepare("SELECT `id` FROM `sys_alerts_handlers` WHERE `name` = ?", $sHandlerName);
  214. return $this->getOne($sQuery);
  215. }
  216. public function getTranscodedFileData($mixedHandler)
  217. {
  218. $sQuery = $this->prepare("SELECT `data` FROM {$this->_sTableFiles} WHERE `transcoder_object` = ? AND `handler` = ?", $this->_aObject['object'], $mixedHandler);
  219. $s = $this->getOne($sQuery);
  220. if (!$s)
  221. return false;
  222. return unserialize($s);
  223. }
  224. public function updateTranscodedFileData($mixedHandler, $mixedData)
  225. {
  226. $sData = $mixedData ? serialize($mixedData) : '';
  227. $sQuery = $this->prepare("UPDATE {$this->_sTableFiles} SET `data` = ? WHERE `transcoder_object` = ? AND `handler` = ?", $sData, $this->_aObject['object'], $mixedHandler);
  228. return $this->res($sQuery);
  229. }
  230. }
  231. /** @} */