3 final class PhabricatorMacroNameTransaction
4 extends PhabricatorMacroTransactionType
{
6 const TRANSACTIONTYPE
= 'macro:name';
8 public function generateOldValue($object) {
9 return $object->getName();
12 public function applyInternalEffects($object, $value) {
13 $object->setName($value);
16 public function getTitle() {
18 '%s renamed this macro from %s to %s.',
19 $this->renderAuthor(),
20 $this->renderOldValue(),
21 $this->renderNewValue());
24 public function getTitleForFeed() {
26 '%s renamed %s from %s to %s.',
27 $this->renderAuthor(),
28 $this->renderObject(),
29 $this->renderOldValue(),
30 $this->renderNewValue());
33 public function validateTransactions($object, array $xactions) {
35 $viewer = $this->getActor();
37 if ($this->isEmptyTextTransaction($object->getName(), $xactions)) {
38 $errors[] = $this->newRequiredError(
39 pht('Macros must have a name.'));
43 $max_length = $object->getColumnMaximumByteLength('name');
44 foreach ($xactions as $xaction) {
45 $old_value = $this->generateOldValue($object);
46 $new_value = $xaction->getNewValue();
48 $new_length = strlen($new_value);
49 if ($new_length > $max_length) {
50 $errors[] = $this->newInvalidError(
51 pht('The name can be no longer than %s characters.',
52 new PhutilNumber($max_length)));
55 if (!self
::isValidMacroName($new_value)) {
56 // This says "emoji", but the actual rule we implement is "all other
57 // unicode characters are also fine".
58 $errors[] = $this->newInvalidError(
60 'Macro name "%s" be: at least three characters long; and contain '.
61 'only lowercase letters, digits, hyphens, colons, underscores, '.
62 'and emoji; and not be composed entirely of latin symbols.',
67 // Check name is unique when updating / creating
68 if ($old_value != $new_value) {
69 $macro = id(new PhabricatorMacroQuery())
71 ->withNames(array($new_value))
75 $errors[] = $this->newInvalidError(
76 pht('Macro "%s" already exists.', $new_value));
85 public static function isValidMacroName($name) {
86 if (preg_match('/^[:_-]+\z/', $name)) {
90 // Accept trivial macro names.
91 if (preg_match('/^[a-z0-9:_-]{3,}\z/', $name)) {
95 // Reject names with fewer than 3 glyphs.
96 $length = phutil_utf8v_combined($name);
97 if (count($length) < 3) {
101 // Check character-by-character for any symbols that we don't want.
102 $characters = phutil_utf8v($name);
103 foreach ($characters as $character) {
104 if (ord($character[0]) > 0x7F) {
108 if (preg_match('/^[^a-z0-9:_-]/', $character)) {