BxDolTranscoderQuery.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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 updateObjectModificationTime($iTime = 0)
  66. {
  67. $sQuery = $this->prepare("UPDATE `sys_objects_transcoder` SET `ts` = ? WHERE `object` = ?", $iTime ? $iTime : time(), $this->_aObject['object']);
  68. return $this->res($sQuery);
  69. }
  70. public function updateAccessTime($mixedHandler)
  71. {
  72. $iTime = time();
  73. $sQuery = $this->prepare("UPDATE {$this->_sTableFiles} SET `atime` = ? WHERE `transcoder_object` = ? AND `handler` = ?", $iTime, $this->_aObject['object'], $mixedHandler);
  74. return $this->res($sQuery);
  75. }
  76. public function deleteFileTraces($iFileId)
  77. {
  78. $sQuery = $this->prepare("DELETE FROM {$this->_sTableFiles} WHERE `transcoder_object` = ? AND `file_id` = ?", $this->_aObject['object'], $iFileId);
  79. return $this->res($sQuery);
  80. }
  81. public function getFilesForPruning ()
  82. {
  83. if (!$this->_aObject['atime_tracking'] || !$this->_aObject['atime_pruning'])
  84. continue;
  85. $sQuery = $this->prepare("SELECT * FROM {$this->_sTableFiles} WHERE `transcoder_object` = ? AND `atime` != 0 AND `atime` < ?", $this->_aObject['object'], time() - $this->_aObject['atime_pruning']);
  86. return $this->getAll($sQuery);
  87. }
  88. public function registerHandlers ()
  89. {
  90. if (!$this->registerHandler ('getAlertHandlerNameLocal', 'alert_response_file_delete_local', $this->_aObject['object'], $this->_aObject['storage_object']))
  91. return false;
  92. // add handler for original storage engine
  93. if ('Storage' == $this->_aObject['source_type']) // if original storage is "Storage", not "Folder"
  94. if (!$this->registerHandler ('getAlertHandlerNameOrig', 'alert_response_file_delete_orig', $this->_aObject['object'], $this->_aObject['source_params']['object']))
  95. return false;
  96. return true;
  97. }
  98. public function unregisterHandlers ()
  99. {
  100. if (!$this->unregisterHandler('getAlertHandlerNameLocal', $this->_aObject['storage_object']))
  101. return false;
  102. // remove handler for original storage engine
  103. if ('Storage' == $this->_aObject['source_type']) // if original storage is "Storage", not "Folder"
  104. if (!$this->unregisterHandler('getAlertHandlerNameOrig', $this->_aObject['source_params']['object']))
  105. return false;
  106. return true;
  107. }
  108. static public function getForDeletionFromQueue ($sServer, $iLimit = 10)
  109. {
  110. $oDb = BxDolDb::getInstance();
  111. // delete items which are stuck in processing status
  112. $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);
  113. $oDb->query($sQuery);
  114. // get files which are subject to delete: with status 'delete' and expired 'failed' items
  115. $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);
  116. return $oDb->getAll($sQuery);
  117. }
  118. static public function getCompletedFromQueue ($iLimit = 10)
  119. {
  120. $oDb = BxDolDb::getInstance();
  121. $sQuery = $oDb->prepare("SELECT * FROM `" . self::TABLE_QUEUE . "` WHERE `status` = 'complete' ORDER BY `added` ASC LIMIT ?", $iLimit);
  122. return $oDb->getAll($sQuery);
  123. }
  124. static public function getNextInQueue ($sServer, $iLimit = 5)
  125. {
  126. $oDb = BxDolDb::getInstance();
  127. $sQuery = $oDb->prepare("SELECT COUNT(*) FROM `" . self::TABLE_QUEUE . "` WHERE `server` = ? AND `status` = 'processing'", $sServer);
  128. if ($oDb->getOne($sQuery))
  129. return array();
  130. $sQuery = $oDb->prepare("SELECT * FROM `" . self::TABLE_QUEUE . "` WHERE `status` = 'pending' ORDER BY `added` ASC LIMIT ?", $iLimit);
  131. return $oDb->getAll($sQuery);
  132. }
  133. public function getQueueTable ()
  134. {
  135. return $this->_sTableQueue;
  136. }
  137. public function deleteFromQueue ($mixedId)
  138. {
  139. if (!$this->_sTableQueue)
  140. return false;
  141. $sQuery = $this->prepare("DELETE FROM `" . $this->_sTableQueue . "` WHERE `transcoder_object` = ? AND `file_id_source` = ?", $this->_aObject['object'], $mixedId);
  142. return $this->query($sQuery);
  143. }
  144. public function getFromQueue ($mixedId)
  145. {
  146. if (!$this->_sTableQueue)
  147. return false;
  148. $sQuery = $this->prepare("SELECT * FROM `" . $this->_sTableQueue . "` WHERE `transcoder_object` = ? AND `file_id_source` = ?", $this->_aObject['object'], $mixedId);
  149. return $this->getRow($sQuery);
  150. }
  151. public function addToQueue ($mixedId, $sFileUrl, $iProfileId)
  152. {
  153. if (!$this->_sTableQueue)
  154. return false;
  155. $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());
  156. return $this->query($sQuery);
  157. }
  158. public function updateQueueStatus ($mixedId, $sStatus, $sLog = '', $sServer = '', $mixedIdResult = '', $sFileUrlResult = '', $sFileExtResult = '')
  159. {
  160. if (!$this->_sTableQueue)
  161. return false;
  162. $sQueryVals = $this->prepare("`status` = ?, changed = ? WHERE `transcoder_object` = ? AND `file_id_source` = ?", $sStatus, time(), $this->_aObject['object'], $mixedId);
  163. $sQueryServer = $sServer ? $this->prepare(" `server` = ?, ", $sServer) : '';
  164. $sQueryLog = $sLog ? $this->prepare(" `log` = ?, ", $sLog) : '';
  165. $sQueryResultFile = $mixedIdResult && $sFileUrlResult ? $this->prepare(" `file_id_result` = ?, `file_url_result` = ?, `file_ext_result` = ?, ", $mixedIdResult, $sFileUrlResult, $sFileExtResult) : '';
  166. $sQueryAll = "UPDATE `" . $this->_sTableQueue . "` SET " . $sQueryServer . $sQueryLog . $sQueryResultFile . $sQueryVals;
  167. return $this->query($sQueryAll);
  168. }
  169. protected function registerHandler ($sHandlerNameFunc, $sServiceFunc, $sObject, $sUnit)
  170. {
  171. $sHandlerName = $this->$sHandlerNameFunc ();
  172. $iHandlerId = $this->getAlertHandlerId ($sHandlerName);
  173. if ($iHandlerId) // if handler already exists, do nothing
  174. return true;
  175. $sServiceCall = serialize(array(
  176. 'module' => 'system',
  177. 'method' => $sServiceFunc,
  178. 'params' => array($sObject),
  179. 'class' => 'TemplImageServices'
  180. ));
  181. $sQuery = $this->prepare("INSERT INTO `sys_alerts_handlers` SET `name` = ?, `service_call` = ?", $sHandlerName, $sServiceCall);
  182. if (!$this->query($sQuery))
  183. return false;
  184. $iHandlerId = $this->lastId();
  185. $sQuery = $this->prepare("INSERT INTO `sys_alerts` SET `unit` = ?, `action` = ?, `handler_id` = ?", $sUnit, 'file_deleted', $iHandlerId);
  186. if (!$this->query($sQuery)) {
  187. $this->unregisterHandlers ();
  188. return false;
  189. }
  190. return true;
  191. }
  192. protected function unregisterHandler ($sHandlerNameFunc, $sUnit)
  193. {
  194. $sHandlerName = $this->$sHandlerNameFunc ();
  195. $iHandlerId = $this->getAlertHandlerId ($sHandlerName);
  196. if (!$iHandlerId) // if handler is alrady missing, do nothing
  197. return true;
  198. $sQuery = $this->prepare("DELETE FROM `sys_alerts` WHERE `unit` = ? AND `action` = ? AND `handler_id` = ?", $sUnit, 'file_deleted', $iHandlerId);
  199. if (!$this->query($sQuery))
  200. return false;
  201. $sQuery = $this->prepare("DELETE FROM `sys_alerts_handlers` WHERE `id` = ?", $iHandlerId);
  202. return $this->query($sQuery);
  203. }
  204. protected function getAlertHandlerNameLocal ()
  205. {
  206. return $this->_sHandlerPrefix . 'local_file_delete_' . $this->_aObject['object'];
  207. }
  208. protected function getAlertHandlerNameOrig ()
  209. {
  210. return $this->_sHandlerPrefix . 'orig_file_delete_' . $this->_aObject['object'];
  211. }
  212. protected function getAlertHandlerId ($sHandlerName)
  213. {
  214. $sQuery = $this->prepare("SELECT `id` FROM `sys_alerts_handlers` WHERE `name` = ?", $sHandlerName);
  215. return $this->getOne($sQuery);
  216. }
  217. public function getTranscodedFileData($mixedHandler)
  218. {
  219. $sQuery = $this->prepare("SELECT `data` FROM {$this->_sTableFiles} WHERE `transcoder_object` = ? AND `handler` = ?", $this->_aObject['object'], $mixedHandler);
  220. $s = $this->getOne($sQuery);
  221. if (!$s)
  222. return false;
  223. return unserialize($s);
  224. }
  225. public function updateTranscodedFileData($mixedHandler, $mixedData)
  226. {
  227. $sData = $mixedData ? serialize($mixedData) : '';
  228. $sQuery = $this->prepare("UPDATE {$this->_sTableFiles} SET `data` = ? WHERE `transcoder_object` = ? AND `handler` = ?", $sData, $this->_aObject['object'], $mixedHandler);
  229. return $this->res($sQuery);
  230. }
  231. }
  232. /** @} */