Correct Aphlict websocket URI construction after PHP8 compatibility changes
[phabricator.git] / src / applications / macro / xaction / PhabricatorMacroNameTransaction.php
blobf55de443bfcafd8a591b1f7141a6a432f134914e
1 <?php
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() {
17 return pht(
18 '%s renamed this macro from %s to %s.',
19 $this->renderAuthor(),
20 $this->renderOldValue(),
21 $this->renderNewValue());
24 public function getTitleForFeed() {
25 return pht(
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) {
34 $errors = array();
35 $viewer = $this->getActor();
37 if ($this->isEmptyTextTransaction($object->getName(), $xactions)) {
38 $errors[] = $this->newRequiredError(
39 pht('Macros must have a name.'));
40 return $errors;
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(
59 pht(
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.',
63 $new_value),
64 $xaction);
67 // Check name is unique when updating / creating
68 if ($old_value != $new_value) {
69 $macro = id(new PhabricatorMacroQuery())
70 ->setViewer($viewer)
71 ->withNames(array($new_value))
72 ->executeOne();
74 if ($macro) {
75 $errors[] = $this->newInvalidError(
76 pht('Macro "%s" already exists.', $new_value));
82 return $errors;
85 public static function isValidMacroName($name) {
86 if (preg_match('/^[:_-]+\z/', $name)) {
87 return false;
90 // Accept trivial macro names.
91 if (preg_match('/^[a-z0-9:_-]{3,}\z/', $name)) {
92 return true;
95 // Reject names with fewer than 3 glyphs.
96 $length = phutil_utf8v_combined($name);
97 if (count($length) < 3) {
98 return false;
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) {
105 continue;
108 if (preg_match('/^[^a-z0-9:_-]/', $character)) {
109 return false;
113 return true;