/
QubitSlug.php
131 lines (109 loc) · 3.47 KB
/
QubitSlug.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
<?php
/*
* This file is part of the Access to Memory (AtoM) software.
*
* Access to Memory (AtoM) is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Access to Memory (AtoM) is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Access to Memory (AtoM). If not, see <http://www.gnu.org/licenses/>.
*/
class QubitSlug extends BaseSlug
{
const
SLUG_BASIS_TITLE = 0,
SLUG_BASIS_REFERENCE_CODE = 1;
public static function random($length = 12)
{
$separator = '-';
// Adapted from http://stackoverflow.com/questions/5615490/random-code-generator/5615957#5615957
$alphabet = '23456789abcdefghkmnpqrstwxyz';
$alphabetSize = strlen($alphabet);
$blockLength = 4;
$numBlocks = ceil($length / $blockLength);
$slug = '';
for ($i = 0; $i < $numBlocks; $i++)
{
for ($j = 0; $j < $blockLength; $j++)
{
$slug .= $alphabet[mt_rand(0, $alphabetSize - 1)];
}
if ($i != $numBlocks - 1)
{
$slug .= $separator;
}
}
return $slug;
}
/**
* Slugify a specified string
*
* @param string $slug The string we want to slugify
*
* @param bool $dropArticles Whether or not to drop English articles from the slug.
* We can disable this when we generate slugs by identifier.
*/
public static function slugify($slug, $dropArticles = true)
{
// Handle exotic characters gracefully.
// TRANSLIT doesn't work in musl's iconv, see #9855.
if ((false !== $result = iconv('utf-8', 'ascii//TRANSLIT', $slug)) || (false !== $result = iconv('utf-8', 'ascii', $slug)))
{
$slug = $result;
}
$slug = strtolower($slug);
// Remove apostrophes
$slug = preg_replace('/\'/', '', $slug);
// Allow only digits, letters, and dashes. Replace sequences of other
// characters with dash
$slug = preg_replace('/[^0-9a-z]+/', '-', $slug);
$slug = "-$slug-";
// Drop (English) articles
if ($dropArticles)
{
$slug = str_replace('-a-', '-', $slug);
$slug = str_replace('-an-', '-', $slug);
$slug = str_replace('-the-', '-', $slug);
}
$slug = trim($slug, '-');
return $slug;
}
public static function getUnique($connection = null)
{
if (!isset($connection))
{
$connection = QubitTransactionFilter::getConnection(QubitObject::DATABASE_NAME);
}
// Try a max of 10 times before giving up (avoid infinite loops when
// possible slugs exhausted)
for ($i = 0; $i < 10; $i++)
{
$slug = self::random();
$statement = $connection->prepare('
SELECT COUNT(*)
FROM '.QubitSlug::TABLE_NAME.'
WHERE '.QubitSlug::SLUG.' = ?;');
$statement->execute(array($slug));
if (0 == $statement->fetchColumn(0))
{
return $slug;
}
}
}
public static function getByObjectId($id, array $options = array())
{
$criteria = new Criteria;
$criteria->add(QubitSlug::OBJECT_ID, $id);
if (1 == count($query = self::get($criteria, $options)))
{
return $query[0];
}
}
}