From: Matthias Schmidt Date: Sat, 23 May 2015 07:25:51 +0000 (+0200) Subject: Add name attribute for cronjobs X-Git-Tag: 3.0.0_Beta_1~2336^2~2 X-Git-Url: https://git.stricted.de/?a=commitdiff_plain;h=22c4b08146f3a12ed5e54400d57598939d2023cb;p=GitHub%2FWoltLab%2FWCF.git Add name attribute for cronjobs --- diff --git a/CHANGELOG.md b/CHANGELOG.md index 9a1ba55aec..9fc7e2caa2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ ### 2.2.0 Alpha 1 (XXXX-YY-ZZ) * `options` support for cronjobs. +* `name` attribute for cronjob PIP (`cronjobName` for cronjob objects). * `permissions` and `options` support for event listeners. * `name` attribute for event listener PIP (`listenerName` for event listener objects). * `permissions` and `options` support for template listeners. diff --git a/com.woltlab.wcf/cronjob.xml b/com.woltlab.wcf/cronjob.xml index 00a2771b30..ef8c93d744 100644 --- a/com.woltlab.wcf/cronjob.xml +++ b/com.woltlab.wcf/cronjob.xml @@ -1,6 +1,185 @@ + + + + + 0 + 2 + */2 + * + * + 1 + 1 + + + + + + + 0 + 3 + 1 + * + * + 1 + 1 + + + + + + + 0 + 1 + * + * + * + 1 + 1 + + + + + + + 0 + * + * + * + * + 1 + 1 + + + + + + + */30 + * + * + * + * + 1 + 1 + + + + + + + 0 + 2 + * + * + * + 1 + 1 + + + + + + + */5 + * + * + * + * + 1 + 1 + 1 + + + + + + + 0 + 0 + * + * + * + 1 + 1 + 1 + + + + + + + */30 + * + * + * + * + 1 + 1 + 1 + + + + + + + 0 + 1 + * + * + * + 1 + 1 + 1 + + + + + + + 0 + 1 + * + * + * + 1 + 1 + 1 + + + + + + + */30 + * + * + * + * + 1 + 1 + 1 + + + + + + + 0 + 1 + * + * + * + 1 + 1 + 1 + + + + + @@ -176,5 +355,6 @@ 1 1 - - \ No newline at end of file + + + diff --git a/wcfsetup/install/files/lib/data/cronjob/Cronjob.class.php b/wcfsetup/install/files/lib/data/cronjob/Cronjob.class.php index 0a270a9cd0..d5b7f382fc 100644 --- a/wcfsetup/install/files/lib/data/cronjob/Cronjob.class.php +++ b/wcfsetup/install/files/lib/data/cronjob/Cronjob.class.php @@ -47,6 +47,13 @@ class Cronjob extends DatabaseObject { */ const MAX_FAIL_COUNT = 3; + /** + * prefix of automatically created cronjob names + * @var string + * @deprecated will be removed once cronjob names are mandatory + */ + const AUTOMATIC_NAME_PREFIX = 'com.woltlab.wcf.cronjob'; + /** * Returns timestamp of next execution. * diff --git a/wcfsetup/install/files/lib/data/cronjob/CronjobEditor.class.php b/wcfsetup/install/files/lib/data/cronjob/CronjobEditor.class.php index ce3ca8842e..0ef999242e 100644 --- a/wcfsetup/install/files/lib/data/cronjob/CronjobEditor.class.php +++ b/wcfsetup/install/files/lib/data/cronjob/CronjobEditor.class.php @@ -42,68 +42,110 @@ class CronjobEditor extends DatabaseObjectEditor implements IEditableCachedObjec // save cronjob description if (!empty($descriptions)) { - // set default value - if (isset($descriptions[''])) { - $defaultValue = $descriptions['']; - } - else if (isset($descriptions['en'])) { - // fallback to English - $defaultValue = $descriptions['en']; - } - else if (isset($descriptions[WCF::getLanguage()->getFixedLanguageCode()])) { - // fallback to the language of the current user - $defaultValue = $descriptions[WCF::getLanguage()->getFixedLanguageCode()]; - } - else { - // fallback to first description - $defaultValue = reset($descriptions); - } - - // fetch data directly from database during framework installation - if (!PACKAGE_ID) { - $sql = "SELECT * - FROM wcf".WCF_N."_language_category - WHERE languageCategory = ?"; - $statement = WCF::getDB()->prepareStatement($sql); - $statement->execute(array('wcf.acp.cronjob')); - $languageCategory = $statement->fetchObject('wcf\data\language\category\LanguageCategory'); - - $languages = new LanguageList(); - $languages->readObjects(); - } - else { - $languages = LanguageFactory::getInstance()->getLanguages(); - $languageCategory = LanguageFactory::getInstance()->getCategory('wcf.acp.cronjob'); - } - - $sql = "INSERT INTO wcf".WCF_N."_language_item - (languageID, languageItem, languageItemValue, languageCategoryID, packageID) - VALUES (?, ?, ?, ?, ?)"; + $cronjobEditor = new self($cronjob); + $cronjobEditor->saveDescriptions($descriptions, false); + } + + return $cronjob; + } + + /** + * Saves the descriptions of the cronjob in language items. + * + * @param array $descriptions + * @param boolean $deleteOldDescriptions + */ + protected function saveDescriptions(array $descriptions, $deleteOldDescriptions = true) { + // set default value + if (isset($descriptions[''])) { + $defaultValue = $descriptions['']; + } + else if (isset($descriptions['en'])) { + // fallback to English + $defaultValue = $descriptions['en']; + } + else if (isset($descriptions[WCF::getLanguage()->getFixedLanguageCode()])) { + // fallback to the language of the current user + $defaultValue = $descriptions[WCF::getLanguage()->getFixedLanguageCode()]; + } + else { + // fallback to first description + $defaultValue = reset($descriptions); + } + + // fetch data directly from database during framework installation + if (!PACKAGE_ID) { + $sql = "SELECT * + FROM wcf".WCF_N."_language_category + WHERE languageCategory = ?"; $statement = WCF::getDB()->prepareStatement($sql); + $statement->execute(array('wcf.acp.cronjob')); + $languageCategory = $statement->fetchObject('wcf\data\language\category\LanguageCategory'); - foreach ($languages as $language) { - $value = $defaultValue; - if (isset($descriptions[$language->languageCode])) { - $value = $descriptions[$language->languageCode]; - } - - $statement->execute(array( - $language->languageID, - 'wcf.acp.cronjob.description.cronjob'.$cronjob->cronjobID, - $value, - $languageCategory->languageCategoryID, - $cronjob->packageID - )); + $languages = new LanguageList(); + $languages->readObjects(); + } + else { + $languages = LanguageFactory::getInstance()->getLanguages(); + $languageCategory = LanguageFactory::getInstance()->getCategory('wcf.acp.cronjob'); + } + + // delete old descriptions first + if ($deleteOldDescriptions) { + $sql = "DELETE FROM wcf".WCF_N."_language_item + WHERE languageItem = ?"; + $statement = WCF::getDB()->prepareStatement($sql); + $statement->execute([ 'wcf.acp.cronjob.description.cronjob'.$this->cronjobID ]); + } + + // save new descriptions + $sql = "INSERT INTO wcf".WCF_N."_language_item + (languageID, languageItem, languageItemValue, languageCategoryID, packageID) + VALUES (?, ?, ?, ?, ?)"; + $statement = WCF::getDB()->prepareStatement($sql); + + foreach ($languages as $language) { + $value = $defaultValue; + if (isset($descriptions[$language->languageCode])) { + $value = $descriptions[$language->languageCode]; } - // update cronjob - $cronjobEditor = new CronjobEditor($cronjob); - $cronjobEditor->update(array( - 'description' => 'wcf.acp.cronjob.description.cronjob'.$cronjob->cronjobID + $statement->execute(array( + $language->languageID, + 'wcf.acp.cronjob.description.cronjob'.$this->cronjobID, + $value, + $languageCategory->languageCategoryID, + $this->packageID )); } - return $cronjob; + // update cronjob + $this->update(array( + 'description' => 'wcf.acp.cronjob.description.cronjob'.$this->cronjobID + )); + } + + /** + * @see \wcf\data\IEditableObject::update() + */ + public function update(array $parameters = array()) { + $descriptions = array(); + if (isset($parameters['description']) && is_array($parameters['description'])) { + if (count($parameters['description']) > 1) { + $descriptions = $parameters['description']; + $parameters['description'] = ''; + } + else { + $parameters['description'] = reset($parameters['description']); + } + } + + parent::update($parameters); + + // save cronjob description + if (!empty($descriptions)) { + $this->saveDescriptions($descriptions); + } } /** diff --git a/wcfsetup/install/files/lib/system/package/plugin/CronjobPackageInstallationPlugin.class.php b/wcfsetup/install/files/lib/system/package/plugin/CronjobPackageInstallationPlugin.class.php index f8545ac681..70bf865a5c 100644 --- a/wcfsetup/install/files/lib/system/package/plugin/CronjobPackageInstallationPlugin.class.php +++ b/wcfsetup/install/files/lib/system/package/plugin/CronjobPackageInstallationPlugin.class.php @@ -1,5 +1,7 @@ tableName." WHERE className = ? AND packageID = ?"; + $legacyStatement = WCF::getDB()->prepareStatement($sql); + + $sql = "DELETE FROM wcf".WCF_N."_".$this->tableName." + WHERE cronjobName = ? + AND packageID = ?"; $statement = WCF::getDB()->prepareStatement($sql); + foreach ($items as $item) { - $statement->execute(array( - $item['elements']['classname'], - $this->installation->getPackageID() - )); + if (!isset($item['attributes']['name'])) { + $legacyStatement->execute(array( + $item['elements']['classname'], + $this->installation->getPackageID() + )); + } + else { + $statement->execute(array( + $item['attributes']['name'], + $this->installation->getPackageID() + )); + } } } @@ -59,6 +75,7 @@ class CronjobPackageInstallationPlugin extends AbstractXMLPackageInstallationPlu 'canBeDisabled' => (isset($data['elements']['canbedisabled'])) ? intval($data['elements']['canbedisabled']) : 1, 'canBeEdited' => (isset($data['elements']['canbeedited'])) ? intval($data['elements']['canbeedited']) : 1, 'className' => (isset($data['elements']['classname'])) ? $data['elements']['classname'] : '', + 'cronjobName' => (isset($data['attributes']['name']) ? $data['attributes']['name'] : ''), 'description' => (isset($data['elements']['description'])) ? $data['elements']['description'] : '', 'isDisabled' => (isset($data['elements']['isdisabled'])) ? intval($data['elements']['isdisabled']) : 0, 'options' => (isset($data['elements']['options'])) ? $data['elements']['options'] : '', @@ -77,11 +94,50 @@ class CronjobPackageInstallationPlugin extends AbstractXMLPackageInstallationPlu CronjobUtil::validate($data['startMinute'], $data['startHour'], $data['startDom'], $data['startMonth'], $data['startDow']); } + /** + * @see \wcf\system\package\plugin\AbstractXMLPackageInstallationPlugin::import() + */ + protected function import(array $row, array $data) { + // if a cronjob is updated without a name given, keep the old automatically + // assigned name + if (!empty($row) && !$data['cronjobName']) { + unset($data['cronjobName']); + } + + $cronjob = parent::import($row, $data); + + // update event listener name + if (!$cronjob->cronjobName) { + $cronjobEditor = new CronjobEditor($cronjob); + $cronjobEditor->update(array( + 'cronjobName' => Cronjob::AUTOMATIC_NAME_PREFIX.$cronjob->cronjobID + )); + + $cronjob = new Cronjob($cronjob->listenerID); + } + + return $cronjob; + } + /** * @see \wcf\system\package\plugin\AbstractXMLPackageInstallationPlugin::findExistingItem() */ protected function findExistingItem(array $data) { - return null; + if (!$data['cronjobName']) return null; + + $sql = "SELECT * + FROM wcf".WCF_N."_".$this->tableName." + WHERE packageID = ? + AND cronjobName = ?"; + $parameters = array( + $this->installation->getPackageID(), + $data['cronjobName'] + ); + + return array( + 'sql' => $sql, + 'parameters' => $parameters + ); } /** diff --git a/wcfsetup/setup/db/install.sql b/wcfsetup/setup/db/install.sql index 91525f3daf..ebd39536f5 100644 --- a/wcfsetup/setup/db/install.sql +++ b/wcfsetup/setup/db/install.sql @@ -308,6 +308,7 @@ CREATE TABLE wcf1_cronjob ( cronjobID INT(10) NOT NULL AUTO_INCREMENT PRIMARY KEY, className varchar(255) NOT NULL DEFAULT '', packageID INT(10) NOT NULL, + cronjobName VARCHAR(255) NOT NULL, description varchar(255) NOT NULL DEFAULT '', startMinute varchar(255) NOT NULL DEFAULT '*', startHour varchar(255) NOT NULL DEFAULT '*', @@ -322,7 +323,9 @@ CREATE TABLE wcf1_cronjob ( canBeDisabled TINYINT(1) NOT NULL DEFAULT 1, state TINYINT(1) NOT NULL DEFAULT 0, failCount TINYINT(1) NOT NULL DEFAULT 0, - options TEXT + options TEXT, + + UNIQUE KEY cronjobName (cronjobName, packageID) ); DROP TABLE IF EXISTS wcf1_cronjob_log;