| 1 |
<?php
|
| 2 |
|
| 3 |
/*
|
| 4 |
|
| 5 |
A wiki ID consists of a list of wiki names and namespaces
|
| 6 |
|
| 7 |
*/
|
| 8 |
|
| 9 |
class WikiId {
|
| 10 |
|
| 11 |
var $data;
|
| 12 |
|
| 13 |
function WikiId($data = null) {
|
| 14 |
if ($data == null)
|
| 15 |
$this->data = array();
|
| 16 |
|
| 17 |
else if (is_array($data))
|
| 18 |
$this->data = $data;
|
| 19 |
|
| 20 |
else
|
| 21 |
$this->data = $this->_parseString($data);
|
| 22 |
}
|
| 23 |
|
| 24 |
|
| 25 |
/* parses a wiki id and returns a WikiId object */
|
| 26 |
function _parseString($str) {
|
| 27 |
$list = explode('/', $str);
|
| 28 |
$data = array();
|
| 29 |
|
| 30 |
foreach ($list as $name) {
|
| 31 |
$data[] = $this->_encodeName($name);
|
| 32 |
}
|
| 33 |
|
| 34 |
return $data;
|
| 35 |
}
|
| 36 |
|
| 37 |
|
| 38 |
/* encodes a wiki name */
|
| 39 |
function _encodeName($name) {
|
| 40 |
return ucfirst(preg_replace('/ /','_', trim($this->_fixUTF8($name))));
|
| 41 |
}
|
| 42 |
|
| 43 |
/* decodes a wiki name*/
|
| 44 |
function _decodeName($name) {
|
| 45 |
return preg_replace('/_/', ' ', $name);
|
| 46 |
}
|
| 47 |
|
| 48 |
/* detects non UTF8 encoding and encodes the string to UTF8 if nessecary. */
|
| 49 |
function _fixUTF8($str) {
|
| 50 |
if (!preg_match('/[\x80-\xff]/', $str))
|
| 51 |
return $str;
|
| 52 |
|
| 53 |
if (preg_match('/^([\x00-\x7f]|[\xc0-\xdf][\x80-\xbf]|[\xe0-\xef][\x80-\xbf]{2}|'.
|
| 54 |
'[\xf0-\xf7][\x80-\xbf]{3})+$/', $str))
|
| 55 |
return $str;
|
| 56 |
|
| 57 |
return utf8_encode($str);
|
| 58 |
}
|
| 59 |
|
| 60 |
/* returns the string representation of the object (readable with parse method) */
|
| 61 |
function toString() {
|
| 62 |
return implode('/', $this->data);
|
| 63 |
}
|
| 64 |
|
| 65 |
function toTitleString() {
|
| 66 |
$titles = array();
|
| 67 |
foreach ($this->data as $name)
|
| 68 |
$titles[] = $this->_decodeName($name);
|
| 69 |
|
| 70 |
return implode('/', $titles);
|
| 71 |
}
|
| 72 |
|
| 73 |
function toURLString() {
|
| 74 |
return implode('/', $this->data);
|
| 75 |
}
|
| 76 |
|
| 77 |
function identifier() {
|
| 78 |
return implode('/', $this->data);
|
| 79 |
}
|
| 80 |
|
| 81 |
function name() {
|
| 82 |
$e = end($this->data);
|
| 83 |
return $e;
|
| 84 |
}
|
| 85 |
|
| 86 |
function displayName() {
|
| 87 |
$e = end($this->data);
|
| 88 |
return $this->_decodeName($e);
|
| 89 |
}
|
| 90 |
|
| 91 |
function parent() {
|
| 92 |
$new = $this->data;
|
| 93 |
array_pop($new);
|
| 94 |
return new WikiId($new);
|
| 95 |
}
|
| 96 |
|
| 97 |
function data() {
|
| 98 |
return $this->data;
|
| 99 |
}
|
| 100 |
|
| 101 |
function isNull() {
|
| 102 |
return empty($this->data);
|
| 103 |
}
|
| 104 |
|
| 105 |
function isValid() {
|
| 106 |
// data should of cause be an array...
|
| 107 |
if (!is_array($this->data))
|
| 108 |
return false;
|
| 109 |
|
| 110 |
foreach ($this->data as $name) {
|
| 111 |
// name may not be empty
|
| 112 |
if ($name['name'] == '')
|
| 113 |
return false;
|
| 114 |
}
|
| 115 |
|
| 116 |
return true;
|
| 117 |
}
|
| 118 |
|
| 119 |
}
|