|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This class responsibility is twofold: |
|
4
|
|
|
* 1) Holding the data for a prioritized queue of URLs that needs to be static cached |
|
5
|
|
|
* 2) Interaction with that queue |
|
6
|
|
|
* |
|
7
|
|
|
* @TODO: would be good to refactor this queue to hold not only URLSegment, but also ClassName and ID of the |
|
8
|
|
|
* associated object (or any other metadata). This would allow FilesystemPublisher::publishPages and others |
|
9
|
|
|
* to stop having to smuggle the metadata within the URL (see URLArrayData::get_object). |
|
10
|
|
|
* |
|
11
|
|
|
*/ |
|
12
|
|
|
class StaticPagesQueue extends DataObject { |
|
|
|
|
|
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* |
|
16
|
|
|
* @var array |
|
17
|
|
|
*/ |
|
18
|
|
|
public static $create_table_options = array( |
|
19
|
|
|
'MySQLDatabase' => 'ENGINE=InnoDB' |
|
20
|
|
|
); |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* |
|
24
|
|
|
* @var array |
|
25
|
|
|
*/ |
|
26
|
|
|
public static $db = array( |
|
27
|
|
|
'Priority' => 'Int', |
|
28
|
|
|
'URLSegment' => 'Varchar(255)', |
|
29
|
|
|
'Freshness' => "Enum('stale, regenerating, error', 'stale')" |
|
30
|
|
|
); |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* |
|
34
|
|
|
* @var array |
|
35
|
|
|
*/ |
|
36
|
|
|
public static $defaults = array( |
|
37
|
|
|
"Priority" => 3 |
|
38
|
|
|
); |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* |
|
42
|
|
|
* @var array |
|
43
|
|
|
*/ |
|
44
|
|
|
public static $default_sort = "\"Priority\""; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Sets database indexes |
|
48
|
|
|
* |
|
49
|
|
|
* @var array |
|
50
|
|
|
*/ |
|
51
|
|
|
public static $indexes = array( |
|
52
|
|
|
'freshness_priority_created' => '(Freshness, Priority, Created)', |
|
53
|
|
|
); |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* |
|
57
|
|
|
* @var boolean |
|
58
|
|
|
*/ |
|
59
|
|
|
private static $use_mysql_locks = true; |
|
|
|
|
|
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* |
|
63
|
|
|
* @var boolean |
|
64
|
|
|
*/ |
|
65
|
|
|
private static $realtime = false; |
|
|
|
|
|
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* |
|
69
|
|
|
* @var int |
|
70
|
|
|
*/ |
|
71
|
|
|
protected static $minutes_until_force_regeneration = 1; |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* |
|
75
|
|
|
* @var array |
|
76
|
|
|
*/ |
|
77
|
|
|
protected static $insert_statements = array(); |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* |
|
81
|
|
|
* @var array |
|
82
|
|
|
*/ |
|
83
|
|
|
protected static $urls = array(); |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* |
|
87
|
|
|
* @return bool |
|
|
|
|
|
|
88
|
|
|
*/ |
|
89
|
|
|
public static function is_realtime() { |
|
90
|
|
|
return Config::inst()->get('StaticPagesQueue','realtime'); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* |
|
95
|
|
|
* @param type $priority |
|
96
|
|
|
* @param type $URLSegment |
|
97
|
|
|
* @return type |
|
|
|
|
|
|
98
|
|
|
*/ |
|
99
|
|
|
public static function add_to_queue($priority, $URLSegment) { |
|
100
|
|
|
$now = date("Y-m-d H:i:s"); |
|
101
|
|
|
self::$insert_statements[$URLSegment] = '(\''.$now.'\',\''.$now.'\', \''.Convert::raw2sql($priority).'\',\''.Convert::raw2sql($URLSegment).'\')'; |
|
102
|
|
|
self::$urls[md5($URLSegment)] = $URLSegment; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* This will push all the currently cached insert statements to be pushed |
|
107
|
|
|
* into the database |
|
108
|
|
|
* |
|
109
|
|
|
* @return void |
|
110
|
|
|
*/ |
|
111
|
|
|
public static function push_urls_to_db() { |
|
112
|
|
|
foreach(self::$insert_statements as $stmt) { |
|
113
|
|
|
$insertSQL = 'INSERT INTO "StaticPagesQueue" ("Created", "LastEdited", "Priority", "URLSegment") VALUES ' . $stmt; |
|
114
|
|
|
DB::query($insertSQL); |
|
115
|
|
|
} |
|
116
|
|
|
self::remove_old_cache(self::$urls); |
|
117
|
|
|
// Flush the cache so DataObject::get works correctly |
|
118
|
|
|
if(!empty(self::$insert_statements) && DB::affectedRows()) { |
|
119
|
|
|
singleton(__CLASS__)->flushCache(); |
|
120
|
|
|
} |
|
121
|
|
|
self::$insert_statements = array(); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* Remove an object by the url |
|
126
|
|
|
* |
|
127
|
|
|
* @param string $URLSegment |
|
128
|
|
|
* @return bool - if there was an queue item removed |
|
129
|
|
|
* |
|
130
|
|
|
*/ |
|
131
|
|
|
public static function delete_by_link($URLSegment) { |
|
132
|
|
|
$object = self::get_by_link($URLSegment); |
|
133
|
|
|
if(!$object) return false; |
|
134
|
|
|
|
|
135
|
|
|
$object->delete(); |
|
136
|
|
|
unset($object); |
|
137
|
|
|
return true; |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* Update the queue with the information that this url renders an error somehow |
|
142
|
|
|
* |
|
143
|
|
|
* @param string $url |
|
144
|
|
|
*/ |
|
145
|
|
|
public static function has_error( $url ) { |
|
146
|
|
|
if(!$url) return; |
|
147
|
|
|
|
|
148
|
|
|
$existingObject = self::get_by_link($url); |
|
149
|
|
|
$existingObject->Freshness = 'error'; |
|
150
|
|
|
$existingObject->write(); |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* Returns a single queue object according to a particular priority and freshness measure. |
|
155
|
|
|
* This method removes any duplicates and makes the object as "regenerating", so other calls to this method |
|
156
|
|
|
* don't grab the same object. |
|
157
|
|
|
* If we are using MySQLDatabase with InnoDB, we do row-level locking when updating the dataobject to allow for |
|
158
|
|
|
* distributed cache rebuilds |
|
159
|
|
|
* @static |
|
160
|
|
|
* @param $freshness |
|
161
|
|
|
* @param $sortOrder |
|
162
|
|
|
*/ |
|
163
|
|
|
protected static function get_queue_object($freshness, $interval = null, $sortOrder = array('Priority'=>'DESC', 'ID'=>'ASC')) { |
|
|
|
|
|
|
164
|
|
|
$className = __CLASS__; |
|
165
|
|
|
$queueObject = null; |
|
166
|
|
|
$filterQuery = array("Freshness" => $freshness); |
|
167
|
|
|
if ($interval) $filterQuery["LastEdited:LessThan"] = $interval; |
|
168
|
|
|
|
|
169
|
|
|
$query = self::get(); |
|
170
|
|
|
if ($query->Count() > 0) { |
|
171
|
|
|
$offset = 0; |
|
172
|
|
|
$filteredQuery = $query->filter($filterQuery)->sort($sortOrder); |
|
173
|
|
|
|
|
174
|
|
|
if ($filteredQuery->Count() > 0) { |
|
175
|
|
|
if (self::config()->use_mysql_locks && DB::getConn() instanceof MySQLDatabase) { //locking currently only works on MySQL |
|
|
|
|
|
|
176
|
|
|
|
|
177
|
|
|
do { |
|
178
|
|
|
$queueObject = $filteredQuery->limit(1, $offset)->first(); //get first item |
|
179
|
|
|
|
|
180
|
|
|
if ($queueObject) $lockName = md5($queueObject->URLSegment . $className); |
|
181
|
|
|
//try to locking the item's URL, keep trying new URLs until we find one that is free to lock |
|
182
|
|
|
$offset++; |
|
183
|
|
|
} while($queueObject && !LockMySQL::isFreeToLock($lockName)); |
|
|
|
|
|
|
184
|
|
|
|
|
185
|
|
|
if ($queueObject) { |
|
186
|
|
|
$lockSuccess = LockMySQL::getLock($lockName); //acquire a lock with the URL of the queue item we have just fetched |
|
187
|
|
|
if ($lockSuccess) { |
|
188
|
|
|
self::remove_duplicates($queueObject->ID); //remove any duplicates |
|
189
|
|
|
self::mark_as_regenerating($queueObject); //mark as regenerating so nothing else grabs it |
|
190
|
|
|
LockMySQL::releaseLock($lockName); //return the object and release the lock |
|
191
|
|
|
} |
|
192
|
|
|
} |
|
193
|
|
|
} else { |
|
194
|
|
|
$queueObject = $filteredQuery->first(); |
|
195
|
|
|
self::remove_duplicates($queueObject->ID); |
|
196
|
|
|
self::mark_as_regenerating($queueObject); |
|
197
|
|
|
} |
|
198
|
|
|
} |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
return $queueObject; //return the object or null |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
|
|
/** |
|
205
|
|
|
* Finds the next most prioritized url that needs recaching |
|
206
|
|
|
* |
|
207
|
|
|
* @return string |
|
208
|
|
|
*/ |
|
209
|
|
|
public static function get_next_url() { |
|
210
|
|
|
$object = self::get_queue_object('stale'); |
|
211
|
|
|
if($object) return $object->URLSegment; |
|
212
|
|
|
|
|
213
|
|
|
$interval = date('Y-m-d H:i:s', strtotime('-'.self::$minutes_until_force_regeneration.' minutes')); |
|
214
|
|
|
|
|
215
|
|
|
// Find URLs that has been stuck in regeneration |
|
216
|
|
|
$object = self::get_queue_object('regenerating', $interval); |
|
217
|
|
|
if($object) return $object->URLSegment; |
|
218
|
|
|
|
|
219
|
|
|
// Find URLs that is erronous and might work now (flush issues etc) |
|
220
|
|
|
$object = self::get_queue_object('error', $interval); |
|
221
|
|
|
if($object) return $object->URLSegment; |
|
222
|
|
|
|
|
223
|
|
|
return ''; |
|
224
|
|
|
} |
|
225
|
|
|
|
|
226
|
|
|
/** |
|
227
|
|
|
* Removes the .html fresh copy of the cache. |
|
228
|
|
|
* Keeps the *.stale.html copy in place, |
|
229
|
|
|
* in order to notify the user of the stale content. |
|
230
|
|
|
* |
|
231
|
|
|
* @param array $URLSegments |
|
232
|
|
|
*/ |
|
233
|
|
|
protected static function remove_old_cache( array $URLSegments ) { |
|
234
|
|
|
$publisher = singleton('SiteTree')->getExtensionInstance('FilesystemPublisher'); |
|
235
|
|
|
if ($publisher) { |
|
236
|
|
|
$paths = $publisher->urlsToPaths($URLSegments); |
|
237
|
|
|
foreach($paths as $absolutePath) { |
|
238
|
|
|
|
|
239
|
|
|
if(!file_exists($publisher->getDestDir().'/'.$absolutePath)) { |
|
240
|
|
|
continue; |
|
241
|
|
|
} |
|
242
|
|
|
|
|
243
|
|
|
unlink($publisher->getDestDir().'/'.$absolutePath); |
|
244
|
|
|
} |
|
245
|
|
|
} |
|
246
|
|
|
} |
|
247
|
|
|
|
|
248
|
|
|
/** |
|
249
|
|
|
* Mark this current StaticPagesQueue as a work in progress |
|
250
|
|
|
* |
|
251
|
|
|
* @param StaticPagesQueue $object |
|
252
|
|
|
*/ |
|
253
|
|
|
protected static function mark_as_regenerating(StaticPagesQueue $object) { |
|
254
|
|
|
$now = date('Y-m-d H:i:s'); |
|
255
|
|
|
DB::query('UPDATE "StaticPagesQueue" SET "LastEdited" = \''.$now.'\', "Freshness"=\'regenerating\' WHERE "ID" = '.$object->ID); |
|
256
|
|
|
singleton(__CLASS__)->flushCache(); |
|
257
|
|
|
} |
|
258
|
|
|
|
|
259
|
|
|
/** |
|
260
|
|
|
* Removes all duplicates that has the same URLSegment as $ID |
|
261
|
|
|
* |
|
262
|
|
|
* @param int $ID - ID of the object whose duplicates we want to remove |
|
263
|
|
|
* @return void |
|
|
|
|
|
|
264
|
|
|
*/ |
|
265
|
|
|
static function remove_duplicates( $ID ) { |
|
|
|
|
|
|
266
|
|
|
$obj = DataObject::get_by_id('StaticPagesQueue', $ID); |
|
267
|
|
|
if(!$obj) return 0; |
|
268
|
|
|
DB::query( |
|
269
|
|
|
sprintf('DELETE FROM "StaticPagesQueue" WHERE "URLSegment" = \'%s\' AND "ID" != %d', $obj->URLSegment, (int)$ID) |
|
270
|
|
|
); |
|
271
|
|
|
} |
|
272
|
|
|
|
|
273
|
|
|
/** |
|
274
|
|
|
* |
|
275
|
|
|
* @param string $url |
|
276
|
|
|
* @param bool $onlyStale - Get only stale entries |
|
|
|
|
|
|
277
|
|
|
* @return DataObject || false - The first item matching the query |
|
|
|
|
|
|
278
|
|
|
*/ |
|
279
|
|
|
protected static function get_by_link($url) { |
|
280
|
|
|
$filter = '"URLSegment" = \''.Convert::raw2sql($url).'\''; |
|
281
|
|
|
$res = DB::query('SELECT * FROM "StaticPagesQueue" WHERE '.$filter.' LIMIT 1;'); |
|
282
|
|
|
if(!$res->numRecords()){ |
|
283
|
|
|
return false; |
|
284
|
|
|
} |
|
285
|
|
|
return new StaticPagesQueue($res->first()); |
|
286
|
|
|
} |
|
287
|
|
|
} |
|
288
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.