From: Matthias Schmidt Date: Mon, 25 Jul 2011 15:17:07 +0000 (+0200) Subject: Create CleanupHandler file X-Git-Tag: 2.0.0_Beta_1~1961^2~1^2~1 X-Git-Url: https://git.stricted.de/?a=commitdiff_plain;h=f3ddfd7844518f5514907a46d95ab40aaf8d6163;p=GitHub%2FWoltLab%2FWCF.git Create CleanupHandler file --- diff --git a/wcfsetup/install/files/lib/system/cleanup/CleanupHandler.class.php b/wcfsetup/install/files/lib/system/cleanup/CleanupHandler.class.php new file mode 100644 index 0000000000..2534b8c4ab --- /dev/null +++ b/wcfsetup/install/files/lib/system/cleanup/CleanupHandler.class.php @@ -0,0 +1,172 @@ + + * @package com.woltlab.wcf + * @subpackage system.cleanup + * @category Community Framework + */ +class CleanupHandler { + /** + * unique instance of CleanupHandler + * + * @var CleanupHandler + */ + protected static $instance = null; + + /** + * cleanup adapter cache + * + * @var array + */ + protected $cache = null; + + /** + * Initializes cleanup handler. + */ + protected function __construct() { + $this->loadCache(); + } + + /** + * Prevents creating an additional instance. + */ + protected function __clone() {} + + /** + * Loads cleanup adapter cache. + */ + protected function loadCache() { + CacheHandler::getInstance()->addResource( + 'cleanupAdapter-'.PACKAGE_ID, + WCF_DIR.'cache/cache.cleanupAdapter.php', + 'wcf\system\cache\builder\CacheBuilderCleanupAdapter' + ); + + $this->cache = CacheHandler::getInstance()->get('cleanupAdapter'); + } + + /** + * Prepares adapter execution + */ + public function execute() { + // remove all logged items older than 24 hours + $sql = "DELETE FROM wcf".WCF_N."_cleanup_log + WHERE deleteTime < ?"; + $statement = WCF::getDB()->prepareStatement($sql); + $statement->execute(array( + (TIME_NOW - 86400) + )); + + // loop through all available adapters + foreach ($this->cache['adapters'] as $objectType => $adapters) { + // determine if there are any items for current object type + $conditions = new PreparedStatementConditionBuilder(); + $conditions->add("objectType = ?", array($objectType)); + $conditions->add("packageID IN (?)", array($this->cache['objectTypes'][$objectType])); + + $sql = "SELECT objectID + FROM wcf".WCF_N."_cleanup_log + ".$conditions; + $statement = WCF::getDB()->prepareStatement($sql); + $statement->execute($conditions->getParameters()); + + $objectIDs = array(); + while ($row = $statement->fetchArray()) { + $objectIDs[] = $row['objectID']; + } + + if (count($objectIDs)) { + $this->executeAdapters($adapters, $objectIDs); + } + } + } + + /** + * Executes specific cleanup adapters. + * + * @param array $adapters + * @param array $objectIDs + */ + protected function executeAdapters(array $adapters, array $objectIDs) { + $sql = "UPDATE wcf".WCF_N."_cleanup_listener + SET lastUpdateTime = ? + WHERE listenerID = ?"; + $statement = WCF::getDB()->prepareStatement($sql); + + foreach ($adapters as $adapterData) { + // validate class + if (!class_exists($adapterData['className'])) { + throw new SystemException("unable to find class '".$adapterData['className']."'", 11001); + } + + // validate interface + if (!(ClassUtil::isInstanceOf($adapterData['className'], 'wcf\system\cleanup\ICleanupAdapter'))) { + throw new SystemException("class '".$adapterData['className']."' does not implement the interface 'wcf\system\cleanup\ICleanupAdapter'", 11010); + } + + $adapter = new $adapterData['className'](); + $adapter->execute($objectIDs); + + // update last time of execution + $statement->execute(array(TIME_NOW, $adapterData['listenerID'])); + } + } + + /** + * Returns an unique instance of CleanupHandler. + * + * @return CleanupHandler + */ + public static function getInstance() { + if (self::$instance === null) { + // call loadInstance event + EventHandler::getInstance()->fireAction(__CLASS__, 'loadInstance'); + + if (self::$instance === null) { + self::$instance = new CleanupHandler(); + } + } + + return self::$instance; + } + + /** + * Registers deleted objects. + * + * @param string $objectType + * @param array $objectIDs + * @param integer $packageID + */ + public static function registerObjects($objectType, array $objectIDs, $packageID) { + $objectIDs = ArrayUtil::toIntegerArray($objectIDs); + $packageID = intval($packageID); + + // insert items + $sql = "INSERT INTO wcf".WCF_N."_cleanup_log + (packageID, objectType, objectID, deleteTime) + VALUES (?, ?, ?, ?)"; + $statement = WCF::getDB()->prepareStatement($sql); + + foreach ($objectIDs as $objectID) { + $statement->execute(array( + $packageID, + $objectType, + $objectID, + TIME_NOW + )); + } + } +}