Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/phabricator
Path: blob/master/src/applications/macro/xaction/PhabricatorMacroNameTransaction.php
12241 views
1
<?php
2
3
final class PhabricatorMacroNameTransaction
4
extends PhabricatorMacroTransactionType {
5
6
const TRANSACTIONTYPE = 'macro:name';
7
8
public function generateOldValue($object) {
9
return $object->getName();
10
}
11
12
public function applyInternalEffects($object, $value) {
13
$object->setName($value);
14
}
15
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());
22
}
23
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());
31
}
32
33
public function validateTransactions($object, array $xactions) {
34
$errors = array();
35
$viewer = $this->getActor();
36
37
if ($this->isEmptyTextTransaction($object->getName(), $xactions)) {
38
$errors[] = $this->newRequiredError(
39
pht('Macros must have a name.'));
40
return $errors;
41
}
42
43
$max_length = $object->getColumnMaximumByteLength('name');
44
foreach ($xactions as $xaction) {
45
$old_value = $this->generateOldValue($object);
46
$new_value = $xaction->getNewValue();
47
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)));
53
}
54
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);
65
}
66
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();
73
74
if ($macro) {
75
$errors[] = $this->newInvalidError(
76
pht('Macro "%s" already exists.', $new_value));
77
}
78
}
79
80
}
81
82
return $errors;
83
}
84
85
public static function isValidMacroName($name) {
86
if (preg_match('/^[:_-]+\z/', $name)) {
87
return false;
88
}
89
90
// Accept trivial macro names.
91
if (preg_match('/^[a-z0-9:_-]{3,}\z/', $name)) {
92
return true;
93
}
94
95
// Reject names with fewer than 3 glyphs.
96
$length = phutil_utf8v_combined($name);
97
if (count($length) < 3) {
98
return false;
99
}
100
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;
106
}
107
108
if (preg_match('/^[^a-z0-9:_-]/', $character)) {
109
return false;
110
}
111
}
112
113
return true;
114
}
115
116
}
117
118