-
Notifications
You must be signed in to change notification settings - Fork 0
/
Generator.php
85 lines (75 loc) · 2.98 KB
/
Generator.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
<?php
class Generator {
/**
* An instance of Zend_Db_Adapter.
*/
protected $_db;
/**
* You should initialize $_db here. The example is if you had an _init()
* method for Zend_Registry::set('dbAdapter') in your Bootstrap.php file.
*
* @access public
* @return void
*/
public function __construct()
{
//$this->_db = Zend_Registry::get('dbAdapter');
}
/**
* Generates a uristub of maximum specified length.
*
* @access protected
* @param string $tableName The name of the table to query
* @param string $tableField The table column to check
* @param string $uristub The initial input string (page title) to clean
* @param int $length The maximum allowable length for the clean url
* @param mixed $iteration The current iteration, when duplicates found
* @return string
*/
protected function generateUristub($tableName, $tableField, $uristub, $length = 30, $iteration = NULL)
{
// begin uristub generation on first iteration
if (is_null($iteration)) {
// set the locale, just once
setlocale(LC_ALL, 'en_US.UTF8');
// clean the uristub
$uristub = iconv('UTF-8', 'ASCII//TRANSLIT', $uristub);
$uristub = preg_replace("/[^a-zA-Z0-9\/_|+ -]/", '', $uristub);
$uristub = preg_replace("/[\/_|+ -]+/", '-', $uristub);
$uristub = strtolower(trim($uristub, '-'));
// ensure uristub is less than length
if (strlen($uristub) > $length) {
// get char at chopped position
$char = $uristub[$length-1];
// quick chop (leave room for 9 iterations)
$uristub = substr($uristub, 0, $length - 1);
// if we chopped mid word
if ($char != '-') {
$pos = strrpos($uristub, '-');
if ($pos !== FALSE) {
$uristub = substr($uristub, 0, $pos);
}
}
}
}
// if we have an iteration, add to the uristub
$uristubToCheck = !empty($iteration) ? $uristub . $iteration : $uristub;
// check if the uristub exists
$sql = sprintf('SELECT 1 FROM `%s` WHERE `%s` = %s',
$tableName,
$tableField,
$this->_db->quote($uristubToCheck));
try {
$result = $this->_db->query($sql)->fetch();
if (!$result) {
return $uristubToCheck;
}
} catch (Exception $e) {
// fatal
die($e->getMessage());
}
// increment iteration before trying again
$iteration = (is_null($iteration)) ? 1 : ++$iteration;
return $this->generateUristub($tableName, $tableField, $uristub, $length, $iteration);
}
}