Path: blob/master/src/applications/herald/management/HeraldRuleManagementWorkflow.php
12256 views
<?php12final class HeraldRuleManagementWorkflow3extends HeraldManagementWorkflow {45protected function didConstruct() {6$this7->setName('rule')8->setExamples('**rule** --rule __rule__ --disable')9->setSynopsis(10pht(11'Modify a rule, bypassing policies. This workflow can disable '.12'problematic personal rules.'))13->setArguments(14array(15array(16'name' => 'rule',17'param' => 'rule',18'help' => pht('Apply changes to this rule.'),19),20array(21'name' => 'disable',22'help' => pht('Disable the rule.'),23),24array(25'name' => 'enable',26'help' => pht('Enable the rule.'),27),28));29}3031public function execute(PhutilArgumentParser $args) {32$viewer = $this->getViewer();3334$rule_name = $args->getArg('rule');35if (!strlen($rule_name)) {36throw new PhutilArgumentUsageException(37pht('Specify a rule to edit with "--rule <id|monogram>".'));38}3940if (preg_match('/^H\d+/', $rule_name)) {41$rule_id = substr($rule_name, 1);42} else {43$rule_id = $rule_name;44}4546$rule = id(new HeraldRuleQuery())47->setViewer($viewer)48->withIDs(array($rule_id))49->executeOne();50if (!$rule) {51throw new PhutilArgumentUsageException(52pht(53'Unable to load Herald rule with ID or monogram "%s".',54$rule_name));55}5657$is_disable = $args->getArg('disable');58$is_enable = $args->getArg('enable');5960$xactions = array();6162if ($is_disable && $is_enable) {63throw new PhutilArgumentUsageException(64pht(65'Specify "--enable" or "--disable", but not both.'));66} else if ($is_disable || $is_enable) {67$xactions[] = $rule->getApplicationTransactionTemplate()68->setTransactionType(HeraldRuleDisableTransaction::TRANSACTIONTYPE)69->setNewValue($is_disable);70}7172if (!$xactions) {73throw new PhutilArgumentUsageException(74pht(75'Use flags to specify at least one edit to apply to the '.76'rule (for example, use "--disable" to disable a rule).'));77}7879$herald_phid = id(new PhabricatorHeraldApplication())->getPHID();8081$editor = $rule->getApplicationTransactionEditor()82->setActor($viewer)83->setActingAsPHID($herald_phid)84->setContentSource($this->newContentSource())85->setContinueOnMissingFields(true)86->setContinueOnNoEffect(true);8788echo tsprintf(89"%s\n",90pht(91'Applying changes to %s: %s...',92$rule->getMonogram(),93$rule->getName()));9495$editor->applyTransactions($rule, $xactions);9697echo tsprintf(98"%s\n",99pht('Done.'));100101102return 0;103}104105}106107108