From 2820fdc89b6095e50e60020dcd0d72a2e7fa2e7b Mon Sep 17 00:00:00 2001 From: epriestley Date: Fri, 2 Aug 2013 06:11:25 -0700 Subject: [PATCH] Add PHIDs to Herald Rules Summary: Ref T2769. Precursor to various Herald-related modernizations. Test Plan: Ran migration; loaded Herald via web. Reviewers: btrahan Reviewed By: btrahan CC: aran Maniphest Tasks: T2769 Differential Revision: https://secure.phabricator.com/D6648 --- resources/sql/patches/20130802.heraldphid.sql | 2 + .../sql/patches/20130802.heraldphids.php | 24 ++++++++++ .../sql/patches/20130802.heraldphidukey.sql | 2 + src/__phutil_library_map__.php | 2 + .../herald/phid/HeraldPHIDTypeRule.php | 45 +++++++++++++++++++ .../herald/query/HeraldRuleQuery.php | 38 ++++++++++++++++ .../herald/storage/HeraldRule.php | 10 +++++ .../patch/PhabricatorBuiltinPatchList.php | 12 +++++ 8 files changed, 135 insertions(+) create mode 100644 resources/sql/patches/20130802.heraldphid.sql create mode 100644 resources/sql/patches/20130802.heraldphids.php create mode 100644 resources/sql/patches/20130802.heraldphidukey.sql create mode 100644 src/applications/herald/phid/HeraldPHIDTypeRule.php diff --git a/resources/sql/patches/20130802.heraldphid.sql b/resources/sql/patches/20130802.heraldphid.sql new file mode 100644 index 0000000000..1450afd277 --- /dev/null +++ b/resources/sql/patches/20130802.heraldphid.sql @@ -0,0 +1,2 @@ +ALTER TABLE {$NAMESPACE}_herald.herald_rule + ADD phid VARCHAR(64) NOT NULL COLLATE utf8_bin; diff --git a/resources/sql/patches/20130802.heraldphids.php b/resources/sql/patches/20130802.heraldphids.php new file mode 100644 index 0000000000..52f9de2729 --- /dev/null +++ b/resources/sql/patches/20130802.heraldphids.php @@ -0,0 +1,24 @@ +establishConnection('w'); + +echo "Assigning PHIDs to Herald Rules...\n"; + +foreach (new LiskMigrationIterator(new HeraldRule()) as $rule) { + $id = $rule->getID(); + echo "Rule {$id}.\n"; + + if ($rule->getPHID()) { + continue; + } + + queryfx( + $conn_w, + 'UPDATE %T SET phid = %s WHERE id = %d', + $table->getTableName(), + PhabricatorPHID::generateNewPHID(HeraldPHIDTypeRule::TYPECONST), + $rule->getID()); +} + +echo "Done.\n"; diff --git a/resources/sql/patches/20130802.heraldphidukey.sql b/resources/sql/patches/20130802.heraldphidukey.sql new file mode 100644 index 0000000000..7b6f20a641 --- /dev/null +++ b/resources/sql/patches/20130802.heraldphidukey.sql @@ -0,0 +1,2 @@ +ALTER TABLE {$NAMESPACE}_herald.herald_rule + ADD UNIQUE KEY (phid); diff --git a/src/__phutil_library_map__.php b/src/__phutil_library_map__.php index 12c02c4af9..55329a3aaf 100644 --- a/src/__phutil_library_map__.php +++ b/src/__phutil_library_map__.php @@ -617,6 +617,7 @@ phutil_register_library_map(array( 'HeraldNewController' => 'applications/herald/controller/HeraldNewController.php', 'HeraldObjectAdapter' => 'applications/herald/adapter/HeraldObjectAdapter.php', 'HeraldObjectTranscript' => 'applications/herald/storage/transcript/HeraldObjectTranscript.php', + 'HeraldPHIDTypeRule' => 'applications/herald/phid/HeraldPHIDTypeRule.php', 'HeraldRecursiveConditionsException' => 'applications/herald/engine/engine/HeraldRecursiveConditionsException.php', 'HeraldRepetitionPolicyConfig' => 'applications/herald/config/HeraldRepetitionPolicyConfig.php', 'HeraldRule' => 'applications/herald/storage/HeraldRule.php', @@ -2621,6 +2622,7 @@ phutil_register_library_map(array( 'HeraldInvalidConditionException' => 'Exception', 'HeraldInvalidFieldException' => 'Exception', 'HeraldNewController' => 'HeraldController', + 'HeraldPHIDTypeRule' => 'PhabricatorPHIDType', 'HeraldRecursiveConditionsException' => 'Exception', 'HeraldRule' => 'HeraldDAO', 'HeraldRuleController' => 'HeraldController', diff --git a/src/applications/herald/phid/HeraldPHIDTypeRule.php b/src/applications/herald/phid/HeraldPHIDTypeRule.php new file mode 100644 index 0000000000..9e517cf61d --- /dev/null +++ b/src/applications/herald/phid/HeraldPHIDTypeRule.php @@ -0,0 +1,45 @@ +setViewer($query->getViewer()) + ->withPHIDs($phids) + ->execute(); + } + + public function loadHandles( + PhabricatorHandleQuery $query, + array $handles, + array $objects) { + + foreach ($handles as $phid => $handle) { + $rule = $objects[$phid]; + + $id = $rule->getID(); + $name = $rule->getName(); + + $handle->setName($name); + $handle->setURI("/herald/rule/{$id}/"); + } + } + +} diff --git a/src/applications/herald/query/HeraldRuleQuery.php b/src/applications/herald/query/HeraldRuleQuery.php index 1335ebc98a..e1fed7f1bf 100644 --- a/src/applications/herald/query/HeraldRuleQuery.php +++ b/src/applications/herald/query/HeraldRuleQuery.php @@ -2,10 +2,34 @@ final class HeraldRuleQuery extends PhabricatorOffsetPagedQuery { + private $ids; + private $phids; private $authorPHIDs; private $ruleTypes; private $contentTypes; + // TODO: Remove when this becomes policy-aware. + private $viewer; + + public function setViewer($viewer) { + $this->viewer = $viewer; + return $this; + } + + public function getViewer() { + return $this->viewer; + } + + public function withIDs(array $ids) { + $this->ids = $ids; + return $this; + } + + public function withPHIDs(array $phids) { + $this->phids = $phids; + return $this; + } + public function withAuthorPHIDs(array $author_phids) { $this->authorPHIDs = $author_phids; return $this; @@ -43,6 +67,20 @@ final class HeraldRuleQuery extends PhabricatorOffsetPagedQuery { private function buildWhereClause($conn_r) { $where = array(); + if ($this->ids) { + $where[] = qsprintf( + $conn_r, + 'rule.id IN (%Ld)', + $this->ids); + } + + if ($this->phids) { + $where[] = qsprintf( + $conn_r, + 'rule.phid IN (%Ls)', + $this->phids); + } + if ($this->authorPHIDs) { $where[] = qsprintf( $conn_r, diff --git a/src/applications/herald/storage/HeraldRule.php b/src/applications/herald/storage/HeraldRule.php index 30436849e4..8a1e018c94 100644 --- a/src/applications/herald/storage/HeraldRule.php +++ b/src/applications/herald/storage/HeraldRule.php @@ -19,6 +19,16 @@ final class HeraldRule extends HeraldDAO { private $conditions; private $actions; + public function getConfiguration() { + return array( + self::CONFIG_AUX_PHID => true, + ) + parent::getConfiguration(); + } + + public function generatePHID() { + return PhabricatorPHID::generateNewPHID(HeraldPHIDTypeRule::TYPECONST); + } + public static function loadAllByContentTypeWithFullData( $content_type, $object_phid) { diff --git a/src/infrastructure/storage/patch/PhabricatorBuiltinPatchList.php b/src/infrastructure/storage/patch/PhabricatorBuiltinPatchList.php index 2b7e12d301..5b987b3aec 100644 --- a/src/infrastructure/storage/patch/PhabricatorBuiltinPatchList.php +++ b/src/infrastructure/storage/patch/PhabricatorBuiltinPatchList.php @@ -1519,6 +1519,18 @@ final class PhabricatorBuiltinPatchList extends PhabricatorSQLPatchList { 'type' => 'php', 'name' => $this->getPatchPath('20130805.pastemailkeypop.php'), ), + '20130802.heraldphid.sql' => array( + 'type' => 'sql', + 'name' => $this->getPatchPath('20130802.heraldphid.sql'), + ), + '20130802.heraldphids.php' => array( + 'type' => 'php', + 'name' => $this->getPatchPath('20130802.heraldphids.php'), + ), + '20130802.heraldphidukey.sql' => array( + 'type' => 'sql', + 'name' => $this->getPatchPath('20130802.heraldphidukey.sql'), + ), ); } }