|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Conner\Tagging; |
|
4
|
|
|
|
|
5
|
|
|
use Conner\Tagging\Model\Tag; |
|
6
|
|
|
use Illuminate\Support\Str; |
|
7
|
|
|
use Log; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Utility functions to help with various tagging functionality. |
|
11
|
|
|
*/ |
|
12
|
|
|
class TaggingUtility |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* Converts input into array |
|
16
|
|
|
* |
|
17
|
|
|
* @param string|array $tagNames |
|
18
|
|
|
* @return array |
|
19
|
|
|
*/ |
|
20
|
|
|
public static function makeTagArray($tagNames) |
|
21
|
|
|
{ |
|
22
|
|
|
if (is_array($tagNames) && count($tagNames) == 1) { |
|
23
|
|
|
$tagNames = reset($tagNames); |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
if (is_string($tagNames)) { |
|
27
|
|
|
$tagNames = explode(',', $tagNames); |
|
28
|
|
|
} elseif (! is_array($tagNames)) { |
|
29
|
|
|
$tagNames = [null]; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
$tagNames = array_map('trim', $tagNames); |
|
33
|
|
|
|
|
34
|
|
|
return array_values($tagNames); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
public static function displayize($string) |
|
38
|
|
|
{ |
|
39
|
|
|
$displayer = config('tagging.displayer'); |
|
40
|
|
|
$displayer = empty($displayer) ? '\Illuminate\Support\Str::title' : $displayer; |
|
41
|
|
|
|
|
42
|
|
|
return call_user_func($displayer, $string); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
public static function normalize($string) |
|
46
|
|
|
{ |
|
47
|
|
|
$normalizer = config('tagging.normalizer'); |
|
48
|
|
|
|
|
49
|
|
|
if (is_string($normalizer) && Str::contains($normalizer, 'Conner\Tagging\Util')) { |
|
50
|
|
|
$normalizer = '\Conner\Tagging\TaggingUtility::slug'; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
$normalizer = $normalizer ?: self::class.'::slug'; |
|
54
|
|
|
|
|
55
|
|
|
return call_user_func($normalizer, $string); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Create normalize string slug. |
|
60
|
|
|
* |
|
61
|
|
|
* Although supported, transliteration is discouraged because |
|
62
|
|
|
* 1) most web browsers support UTF-8 characters in URLs |
|
63
|
|
|
* 2) transliteration causes a loss of information |
|
64
|
|
|
* |
|
65
|
|
|
* @param string $str |
|
66
|
|
|
* @return string |
|
67
|
|
|
*/ |
|
68
|
|
|
public static function slug($str) |
|
69
|
|
|
{ |
|
70
|
|
|
// Make sure string is in UTF-8 and strip invalid UTF-8 characters |
|
71
|
|
|
$str = mb_convert_encoding((string) $str, 'UTF-8'); |
|
72
|
|
|
|
|
73
|
|
|
$options = [ |
|
74
|
|
|
'delimiter' => config('tagging.delimiter', '-'), |
|
75
|
|
|
'limit' => '255', |
|
76
|
|
|
'lowercase' => true, |
|
77
|
|
|
'replacements' => [], |
|
78
|
|
|
'transliterate' => true, |
|
79
|
|
|
]; |
|
80
|
|
|
|
|
81
|
|
|
$char_map = [ |
|
82
|
|
|
// Latin |
|
83
|
|
|
'À' => 'A', 'Á' => 'A', 'Â' => 'A', 'Ã' => 'A', 'Ä' => 'A', 'Å' => 'A', 'Æ' => 'AE', 'Ç' => 'C', |
|
84
|
|
|
'È' => 'E', 'É' => 'E', 'Ê' => 'E', 'Ë' => 'E', 'Ì' => 'I', 'Í' => 'I', 'Î' => 'I', 'Ï' => 'I', |
|
85
|
|
|
'Ð' => 'D', 'Ñ' => 'N', 'Ò' => 'O', 'Ó' => 'O', 'Ô' => 'O', 'Õ' => 'O', 'Ö' => 'O', 'Ő' => 'O', |
|
86
|
|
|
'Ø' => 'O', 'Ù' => 'U', 'Ú' => 'U', 'Û' => 'U', 'Ü' => 'U', 'Ű' => 'U', 'Ý' => 'Y', 'Þ' => 'TH', |
|
87
|
|
|
'ß' => 'ss', |
|
88
|
|
|
'à' => 'a', 'á' => 'a', 'â' => 'a', 'ã' => 'a', 'ä' => 'a', 'å' => 'a', 'æ' => 'ae', 'ç' => 'c', |
|
89
|
|
|
'è' => 'e', 'é' => 'e', 'ê' => 'e', 'ë' => 'e', 'ì' => 'i', 'í' => 'i', 'î' => 'i', 'ï' => 'i', |
|
90
|
|
|
'ð' => 'd', 'ñ' => 'n', 'ò' => 'o', 'ó' => 'o', 'ô' => 'o', 'õ' => 'o', 'ö' => 'o', 'ő' => 'o', |
|
91
|
|
|
'ø' => 'o', 'ù' => 'u', 'ú' => 'u', 'û' => 'u', 'ü' => 'u', 'ű' => 'u', 'ý' => 'y', 'þ' => 'th', |
|
92
|
|
|
'ÿ' => 'y', |
|
93
|
|
|
|
|
94
|
|
|
// Latin symbols |
|
95
|
|
|
'©' => '(c)', |
|
96
|
|
|
|
|
97
|
|
|
// Greek |
|
98
|
|
|
'Α' => 'A', 'Β' => 'B', 'Γ' => 'G', 'Δ' => 'D', 'Ε' => 'E', 'Ζ' => 'Z', 'Η' => 'H', 'Θ' => '8', |
|
99
|
|
|
'Ι' => 'I', 'Κ' => 'K', 'Λ' => 'L', 'Μ' => 'M', 'Ν' => 'N', 'Ξ' => '3', 'Ο' => 'O', 'Π' => 'P', |
|
100
|
|
|
'Ρ' => 'R', 'Σ' => 'S', 'Τ' => 'T', 'Υ' => 'Y', 'Φ' => 'F', 'Χ' => 'X', 'Ψ' => 'PS', 'Ω' => 'W', |
|
101
|
|
|
'Ά' => 'A', 'Έ' => 'E', 'Ί' => 'I', 'Ό' => 'O', 'Ύ' => 'Y', 'Ή' => 'H', 'Ώ' => 'W', 'Ϊ' => 'I', |
|
102
|
|
|
'Ϋ' => 'Y', |
|
103
|
|
|
'α' => 'a', 'β' => 'b', 'γ' => 'g', 'δ' => 'd', 'ε' => 'e', 'ζ' => 'z', 'η' => 'h', 'θ' => '8', |
|
104
|
|
|
'ι' => 'i', 'κ' => 'k', 'λ' => 'l', 'μ' => 'm', 'ν' => 'n', 'ξ' => '3', 'ο' => 'o', 'π' => 'p', |
|
105
|
|
|
'ρ' => 'r', 'σ' => 's', 'τ' => 't', 'υ' => 'y', 'φ' => 'f', 'χ' => 'x', 'ψ' => 'ps', 'ω' => 'w', |
|
106
|
|
|
'ά' => 'a', 'έ' => 'e', 'ί' => 'i', 'ό' => 'o', 'ύ' => 'y', 'ή' => 'h', 'ώ' => 'w', 'ς' => 's', |
|
107
|
|
|
'ϊ' => 'i', 'ΰ' => 'y', 'ϋ' => 'y', 'ΐ' => 'i', |
|
108
|
|
|
|
|
109
|
|
|
// Turkish |
|
110
|
|
|
'Ş' => 'S', 'İ' => 'I', 'Ğ' => 'G', |
|
111
|
|
|
'ş' => 's', 'ı' => 'i', 'ğ' => 'g', |
|
112
|
|
|
|
|
113
|
|
|
// Russian |
|
114
|
|
|
'А' => 'A', 'Б' => 'B', 'В' => 'V', 'Г' => 'G', 'Д' => 'D', 'Е' => 'E', 'Ё' => 'Yo', 'Ж' => 'Zh', |
|
115
|
|
|
'З' => 'Z', 'И' => 'I', 'Й' => 'J', 'К' => 'K', 'Л' => 'L', 'М' => 'M', 'Н' => 'N', 'О' => 'O', |
|
116
|
|
|
'П' => 'P', 'Р' => 'R', 'С' => 'S', 'Т' => 'T', 'У' => 'U', 'Ф' => 'F', 'Х' => 'H', 'Ц' => 'C', |
|
117
|
|
|
'Ч' => 'Ch', 'Ш' => 'Sh', 'Щ' => 'Sh', 'Ъ' => '', 'Ы' => 'Y', 'Ь' => '', 'Э' => 'E', 'Ю' => 'Yu', |
|
118
|
|
|
'Я' => 'Ya', |
|
119
|
|
|
'а' => 'a', 'б' => 'b', 'в' => 'v', 'г' => 'g', 'д' => 'd', 'е' => 'e', 'ё' => 'yo', 'ж' => 'zh', |
|
120
|
|
|
'з' => 'z', 'и' => 'i', 'й' => 'j', 'к' => 'k', 'л' => 'l', 'м' => 'm', 'н' => 'n', 'о' => 'o', |
|
121
|
|
|
'п' => 'p', 'р' => 'r', 'с' => 's', 'т' => 't', 'у' => 'u', 'ф' => 'f', 'х' => 'h', 'ц' => 'c', |
|
122
|
|
|
'ч' => 'ch', 'ш' => 'sh', 'щ' => 'sh', 'ъ' => '', 'ы' => 'y', 'ь' => '', 'э' => 'e', 'ю' => 'yu', |
|
123
|
|
|
'я' => 'ya', |
|
124
|
|
|
|
|
125
|
|
|
// Ukrainian |
|
126
|
|
|
'Є' => 'Ye', 'І' => 'I', 'Ї' => 'Yi', 'Ґ' => 'G', |
|
127
|
|
|
'є' => 'ye', 'і' => 'i', 'ї' => 'yi', 'ґ' => 'g', |
|
128
|
|
|
|
|
129
|
|
|
// Czech |
|
130
|
|
|
'Č' => 'C', 'Ď' => 'D', 'Ě' => 'E', 'Ň' => 'N', 'Ř' => 'R', 'Š' => 'S', 'Ť' => 'T', 'Ů' => 'U', |
|
131
|
|
|
'Ž' => 'Z', |
|
132
|
|
|
'č' => 'c', 'ď' => 'd', 'ě' => 'e', 'ň' => 'n', 'ř' => 'r', 'š' => 's', 'ť' => 't', 'ů' => 'u', |
|
133
|
|
|
'ž' => 'z', |
|
134
|
|
|
|
|
135
|
|
|
// Polish |
|
136
|
|
|
'Ą' => 'A', 'Ć' => 'C', 'Ę' => 'e', 'Ł' => 'L', 'Ń' => 'N', 'Ś' => 'S', 'Ź' => 'Z', |
|
137
|
|
|
'Ż' => 'Z', |
|
138
|
|
|
'ą' => 'a', 'ć' => 'c', 'ę' => 'e', 'ł' => 'l', 'ń' => 'n', 'ś' => 's', 'ź' => 'z', |
|
139
|
|
|
'ż' => 'z', |
|
140
|
|
|
|
|
141
|
|
|
// Latvian |
|
142
|
|
|
'Ā' => 'A', 'Ē' => 'E', 'Ģ' => 'G', 'Ī' => 'i', 'Ķ' => 'k', 'Ļ' => 'L', 'Ņ' => 'N', 'Ū' => 'u', |
|
143
|
|
|
'ā' => 'a', 'ē' => 'e', 'ģ' => 'g', 'ī' => 'i', 'ķ' => 'k', 'ļ' => 'l', 'ņ' => 'n', 'ū' => 'u', |
|
144
|
|
|
|
|
145
|
|
|
//Romanian |
|
146
|
|
|
'Ă' => 'A', 'ă' => 'a', 'Ș' => 'S', 'ș' => 's', 'Ț' => 'T', 'ț' => 't', |
|
147
|
|
|
|
|
148
|
|
|
//Vietnamese |
|
149
|
|
|
'ả' => 'a', 'Ả' => 'A', 'ạ' => 'a', 'Ạ' => 'A', 'ắ' => 'a', 'Ắ' => 'A', 'ằ' => 'a', 'Ằ' => 'A', |
|
150
|
|
|
'ẳ' => 'a', 'Ẳ' => 'A', 'ẵ' => 'a', 'Ẵ' => 'A', 'ặ' => 'a', 'Ặ' => 'A', 'ẩ' => 'a', 'Ẩ' => 'A', |
|
151
|
|
|
'Ấ' => 'A', 'ấ' => 'a', 'Ầ' => 'A', 'ầ' => 'a', 'Ơ' => 'O', 'ơ' => 'o', 'Đ' => 'D', 'đ' => 'd', |
|
152
|
|
|
'ẫ' => 'a', 'Ẫ' => 'A', 'ậ' => 'a', 'Ậ' => 'A', 'ẻ' => 'e', 'Ẻ' => 'E', 'ẽ' => 'e', 'Ẽ' => 'E', |
|
153
|
|
|
'ẹ' => 'e', 'Ẹ' => 'E', 'ế' => 'e', 'Ế' => 'E', 'ề' => 'e', 'Ề' => 'E', 'ể' => 'e', 'Ể' => 'E', |
|
154
|
|
|
'ễ' => 'e', 'Ễ' => 'E', 'ệ' => 'e', 'Ệ' => 'E', 'ỉ' => 'i', 'Ỉ' => 'I', 'ĩ' => 'i', 'Ĩ' => 'I', |
|
155
|
|
|
'ị' => 'i', 'Ị' => 'I', 'ỏ' => 'o', 'Ỏ' => 'O', 'ọ' => 'o', 'Ọ' => 'O', 'ố' => 'o', 'Ố' => 'O', |
|
156
|
|
|
'ồ' => 'o', 'Ồ' => 'O', 'ổ' => 'o', 'Ổ' => 'O', 'ỗ' => 'o', 'Ỗ' => 'O', 'ộ' => 'o', 'Ộ' => 'O', |
|
157
|
|
|
'ớ' => 'o', 'Ớ' => 'O', 'ờ' => 'o', 'Ờ' => 'O', 'ở' => 'o', 'Ở' => 'O', 'ỡ' => 'o', 'Ỡ' => 'O', |
|
158
|
|
|
'ợ' => 'o', 'Ợ' => 'O', 'ủ' => 'u', 'Ủ' => 'U', 'ũ' => 'u', 'Ũ' => 'U', 'ụ' => 'u', 'Ụ' => 'U', |
|
159
|
|
|
'ư' => 'u', 'Ư' => 'U', 'ứ' => 'u', 'Ứ' => 'U', 'ừ' => 'u', 'Ừ' => 'U', 'ử' => 'u', 'Ử' => 'U', |
|
160
|
|
|
'ữ' => 'u', 'Ữ' => 'U', 'ự' => 'u', 'Ự' => 'U', 'ỳ' => 'y', 'Ỳ' => 'Y', 'ỷ' => 'y', 'Ỷ' => 'Y', |
|
161
|
|
|
'ỹ' => 'y', 'Ỹ' => 'Y', 'ỵ' => 'y', 'Ỵ' => 'Y', |
|
162
|
|
|
|
|
163
|
|
|
//Kurdish |
|
164
|
|
|
'ا' => 'a', 'ب' => 'b', 'ج' => 'c', 'د' => 'd', 'ێ' => 'e', 'ف' => 'f', 'گ' => 'g', 'ژ' => 'j', |
|
165
|
|
|
'ک' => 'k', 'ل' => 'l', 'م' => 'm', 'ن' => 'n', 'ۆ' => 'o', 'پ' => 'p', 'ق' => 'q', 'ر' => 'r', |
|
166
|
|
|
'س' => 's', 'ت' => 't', 'ڤ' => 'v', 'وو' => 'u', 'و' => 'w', 'خ' => 'x', 'ی' => 'y', 'ز' => 'z', |
|
167
|
|
|
'ڕ' => 'rr', 'ە' => 'e', 'ح' => 'hh', 'ع' => '', 'ش' => 'sh', 'غ' => 'gh', 'ك' => 'k', 'ڵ' => 'll', |
|
168
|
|
|
'چ' => 'ch', 'ھ' => 'h', 'ئ' => '', 'ه' => 'e', 'ه' => 'h', 'ص' => 's', 'ي' => 'y', 'ة' => 'e', |
|
169
|
|
|
'ط' => 't', 'ذ' => 'z', 'ؤ' => 'u', 'ظ' => 'dh', 'ض' => 'dh', 'ث' => 's', 'أ' => 'a', 'إ' => 'i', |
|
170
|
|
|
'ى' => 'y', 'ء' => 'u', |
|
171
|
|
|
]; |
|
172
|
|
|
|
|
173
|
|
|
// Make custom replacements |
|
174
|
|
|
$str = preg_replace(array_keys($options['replacements']), $options['replacements'], $str); |
|
175
|
|
|
|
|
176
|
|
|
// Transliterate characters to ASCII |
|
177
|
|
|
if ($options['transliterate']) { |
|
178
|
|
|
$str = str_replace(array_keys($char_map), $char_map, $str); |
|
179
|
|
|
} |
|
180
|
|
|
// Replace non-alphanumeric characters with our delimiter |
|
181
|
|
|
$str = preg_replace('/[^\p{L}\p{Nd}]+/u', $options['delimiter'], $str); |
|
182
|
|
|
|
|
183
|
|
|
// Remove duplicate delimiters |
|
184
|
|
|
$str = preg_replace('/('.preg_quote($options['delimiter'], '/').'){2,}/', '$1', $str); |
|
185
|
|
|
|
|
186
|
|
|
// Truncate slug to max. characters |
|
187
|
|
|
$str = mb_substr($str, 0, ($options['limit'] ? $options['limit'] : mb_strlen($str, 'UTF-8')), 'UTF-8'); |
|
188
|
|
|
|
|
189
|
|
|
// Remove delimiter from ends |
|
190
|
|
|
$str = trim($str, $options['delimiter']); |
|
191
|
|
|
|
|
192
|
|
|
return $options['lowercase'] ? mb_strtolower($str, 'UTF-8') : $str; |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
/** |
|
196
|
|
|
* Private! Please do not call this function directly, just let the Tag library use it. |
|
197
|
|
|
* Increment count of tag by one. This function will create tag record if it does not exist. |
|
198
|
|
|
* |
|
199
|
|
|
* @param string $tagString |
|
200
|
|
|
* @param string $tagSlug |
|
201
|
|
|
* @param int $count |
|
202
|
|
|
*/ |
|
203
|
|
|
public static function incrementCount($tagString, $tagSlug, $count, $locale) |
|
204
|
|
|
{ |
|
205
|
|
|
if ($count <= 0) { |
|
206
|
|
|
return; |
|
207
|
|
|
} |
|
208
|
|
|
$model = static::tagModelString(); |
|
209
|
|
|
|
|
210
|
|
|
/** @var Tag $model|$tag */ |
|
211
|
|
|
$tag = $model::where('slug', '=', $tagSlug)->first(); |
|
212
|
|
|
|
|
213
|
|
|
if (! $tag) { |
|
214
|
|
|
$tag = new $model; |
|
215
|
|
|
$tag->name = $tagString; |
|
216
|
|
|
$tag->slug = $tagSlug; |
|
217
|
|
|
$tag->suggest = false; |
|
218
|
|
|
$tag->locale = $locale; |
|
219
|
|
|
$tag->save(); |
|
220
|
|
|
} |
|
221
|
|
|
|
|
222
|
|
|
$tag->count = $tag->count + $count; |
|
223
|
|
|
$tag->save(); |
|
224
|
|
|
} |
|
225
|
|
|
|
|
226
|
|
|
/** |
|
227
|
|
|
* Private! Please do not call this function directly, let the Tag library use it. |
|
228
|
|
|
* Decrement count of tag by one. This function will create tag record if it does not exist. |
|
229
|
|
|
*/ |
|
230
|
|
|
public static function decrementCount($tagSlug, $count) |
|
231
|
|
|
{ |
|
232
|
|
|
if ($count <= 0) { |
|
233
|
|
|
return; |
|
234
|
|
|
} |
|
235
|
|
|
|
|
236
|
|
|
/** @var Tag $model */ |
|
237
|
|
|
$model = static::tagModelString(); |
|
238
|
|
|
|
|
239
|
|
|
$tag = $model::where('slug', '=', $tagSlug)->first(); |
|
240
|
|
|
|
|
241
|
|
|
if ($tag) { |
|
242
|
|
|
$tag->count = $tag->count - $count; |
|
243
|
|
|
if ($tag->count < 0) { |
|
244
|
|
|
$tag->count = 0; |
|
245
|
|
|
Log::warning("The '.$model.' count for `$tag->name` was a negative number. This probably means your data got corrupted. Please assess your code and report an issue if you find one."); |
|
246
|
|
|
} |
|
247
|
|
|
$tag->save(); |
|
248
|
|
|
} |
|
249
|
|
|
} |
|
250
|
|
|
|
|
251
|
|
|
/** |
|
252
|
|
|
* Look at the tags table and delete any tags that are no longer in use by any taggable database rows. |
|
253
|
|
|
* Does not delete tags where 'suggest' is true |
|
254
|
|
|
* |
|
255
|
|
|
* @return int |
|
256
|
|
|
*/ |
|
257
|
|
|
public static function deleteUnusedTags() |
|
258
|
|
|
{ |
|
259
|
|
|
/** @var Tag $model */ |
|
260
|
|
|
$model = static::tagModelString(); |
|
261
|
|
|
|
|
262
|
|
|
return $model::deleteUnused(); |
|
263
|
|
|
} |
|
264
|
|
|
|
|
265
|
|
|
/** |
|
266
|
|
|
* @return string |
|
267
|
|
|
*/ |
|
268
|
|
|
public static function tagModelString() |
|
269
|
|
|
{ |
|
270
|
|
|
return config('tagging.tag_model', '\Conner\Tagging\Model\Tag'); |
|
271
|
|
|
} |
|
272
|
|
|
|
|
273
|
|
|
/** |
|
274
|
|
|
* @return string |
|
275
|
|
|
*/ |
|
276
|
|
|
public static function taggedModelString() |
|
277
|
|
|
{ |
|
278
|
|
|
return config('tagging.tagged_model', '\Conner\Tagging\Model\Tagged'); |
|
279
|
|
|
} |
|
280
|
|
|
|
|
281
|
|
|
/** |
|
282
|
|
|
* @return string |
|
283
|
|
|
*/ |
|
284
|
|
|
public static function tagGroupModelString() |
|
285
|
|
|
{ |
|
286
|
|
|
return config('tagging.tag_group_model', '\Conner\Tagging\Model\TagGroup'); |
|
287
|
|
|
} |
|
288
|
|
|
} |
|
289
|
|
|
|