/
URLArrayObject.php
189 lines (166 loc) · 5.17 KB
/
URLArrayObject.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
<?php
/**
* This is an helper object to StaticPagesQueue to hold an array of urls with
* priorites to be recached.
*
* If the StaticPagesQueue::is_realtime is false this class will call
* StaticPagesQueue::push_urls_to_db when in __destructs.
*
*/
class URLArrayObject extends ArrayObject {
private $shutDownRegistered = false;
/**
* Adds metadata into the URL.
*
* @param $url string
* @param $obj DataObject to inject
*
* @return string transformed URL.
*/
public function addObject($url, DataObject $dataObject) {
$updatedUrl = HTTP::setGetVar('_ID', $dataObject->ID, $url, '&');
$updatedUrl = HTTP::setGetVar('_ClassName', $dataObject->ClassName, $updatedUrl, '&');
// Hack: fix the HTTP::setGetVar removing leading slash from the URL if BaseURL is used.
if (strpos($url, '/')===0) return '/' . $updatedUrl;
else return $updatedUrl;
}
/**
* Adds metadata into all URLs in the array.
*
* @param $urls array of url => priority
* @param $obj DataObject to inject
*
* @return array array of transformed URLs.
*/
public function addObjects($urls, DataObject $dataObject) {
$processedUrls = array();
foreach ($urls as $url=>$priority) {
$url = $this->addObject($url, $dataObject);
$processedUrls[$url] = $priority;
}
return $processedUrls;
}
/**
* Extracts the metadata from the queued structure.
*
* @param $url string
*
* @return DataObject represented by the URL.
*/
public function getObject($url) {
$urlParts = @parse_url($url);
if (isset($urlParts['query'])) parse_str($urlParts['query'], $getParameters);
else $getParameters = array();
$obj = null;
if(isset($getParameters['_ID']) && isset($getParameters['_ClassName'])) {
$id = $getParameters['_ID'];
$className = $getParameters['_ClassName'];
$obj = DataObject::get($className)->byID($id);
}
return $obj;
}
/**
* Adds urls to the queue after injecting the objects' metadata.
*
* @param $urls array associative array of url => priority
* @param $dataObject DataObject object to associate the urls with
*/
public function addUrlsOnBehalf(array $urls, DataObject $dataObject) {
return $this->addUrls($this->addObjects($urls, $dataObject));
}
/**
* The format of the urls should be array( 'URLSegment' => '50')
*
* @param array $urls
*/
public function addUrls(array $urls) {
if(!$urls) {
return;
}
$urlsAlreadyProcessed = array(); //array to filter out any duplicates
foreach ($urls as $URLSegment=>$priority) {
if(is_numeric($URLSegment) && is_string($priority)) { //case when we have a non-associative flat array
$URLSegment = $priority;
$priority = 50;
}
//only add URLs of a certain length and only add URLs not already added
if (!empty($URLSegment) &&
strlen($URLSegment) > 0 &&
!isset($urlsAlreadyProcessed[$URLSegment])) {
//check to make sure this page isn't excluded from the cache
if (!$this->excludeFromCache($URLSegment)) {
$this->append(array($priority, $URLSegment));
}
$urlsAlreadyProcessed[$URLSegment] = true; //set as already processed
}
}
// Insert into the database directly instead of waiting to destruct time
if (Config::inst()->get('StaticPagesQueue', 'realtime')) {
$this->insertIntoDB();
} else {
//don't register a shutdown twice within a single object
if (!$this->shutDownRegistered) {
register_shutdown_function(array($this,'destructMethod'));
$this->shutDownRegistered = true;
}
}
}
protected function excludeFromCache($url) {
$candidatePage = $this->getObject($url);
if (empty($candidatePage)) {
$cleanUrl = $this->removeQueryStringFromUrl($url);
$candidatePage = SiteTree::get_by_link($cleanUrl);
}
return !empty($candidatePage) && !empty($candidatePage->excludeFromCache);
}
private function removeQueryStringFromUrl($url) {
return strtok($url, '?');
}
/**
* When this class is getting garbage collected, trigger the insert of all
* urls into the database
*
*/
public function destructMethod() {
if (!Config::inst()->get('StaticPagesQueue', 'realtime')) {
$this->insertIntoDB();
}
}
/**
* This method will insert all URLs that exists in this object into the
* database by calling the StaticPagesQueue
*
* @return type
*/
public function insertIntoDB() {
$arraycopy = $this->getArrayCopy();
usort($arraycopy, array($this, 'sortOnPriority'));
foreach ($arraycopy as $array) {
StaticPagesQueue::add_to_queue($array[0], $array[1], $array[2]);
}
StaticPagesQueue::push_urls_to_db();
$this->exchangeArray(array());
}
/**
* Sorts the array on priority, from highest to lowest
*
* @param array $a
* @param array $b
* @return int - signed
*/
protected function sortOnPriority($a, $b) {
if ($a[0] == $b[0]) {
return 0;
}
return ($a[0] > $b[0]) ? -1 : 1;
}
// removes the injected _ID and _ClassName get parameters
public static function sanitize_url($url) {
list($urlPart, $query) = array_pad(explode('?', $url), 2, '');
parse_str($query, $getVars);
unset($getVars['_ID'], $getVars['_ClassName']);
$sanitizedQuery = http_build_query($getVars);
$sanitizedQuery = ($sanitizedQuery !== '') ? '?' . $sanitizedQuery : '';
return $urlPart . $sanitizedQuery;
}
}