Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 15 additions & 7 deletions cron/delete_invalid.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,13 @@
$ext = new Extension_cachelite;

while (true) {
$next = Symphony::Database()->fetch(
"SELECT * FROM `tbl_cachelite_invalid` LIMIT 1"
);
$next = Symphony::Database()
->select(['*'])
->from('tbl_cachelite_invalid')
->limit(1)
->execute()
->rows();

if (empty($next)) {
break;
}
Expand All @@ -49,10 +53,14 @@
sleep(1);
echo '. Done.' . PHP_EOL;
}
Symphony::Database()->delete(
'tbl_cachelite_invalid',
" `section_id` = $section_id AND `entry_id` = $entry_id"
);

Symphony::Database()
->delete('tbl_cachelite_invalid')
->where(['section_id' => $section_id])
->where(['entry_id' => $entry_id])
->execute()
->success();

sleep(1);
}

Expand Down
195 changes: 132 additions & 63 deletions extension.driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,7 @@ public function interceptPage($context)
$modified = $this->_cacheLite->lastModified();
$modified_gmt = gmdate('r', $modified);
if ($_SERVER['HTTP_IF_MODIFIED_SINCE'] == $modified_gmt ||
str_replace('"', NULL, stripslashes($_SERVER['HTTP_IF_NONE_MATCH'])) == $this->computeEtag()){
str_replace('"', null, stripslashes($_SERVER['HTTP_IF_NONE_MATCH'])) == $this->computeEtag()){
Page::renderStatusCode(Page::HTTP_NOT_MODIFIED);
exit();
}
Expand Down Expand Up @@ -492,11 +492,13 @@ public function entryEdit($context)
public function entryDelete($context)
{
if (Symphony::Configuration()->get('backend-delegates', 'cachelite') == 'no') return;
$ids = !is_array($context['entry_id']) ? array($context['entry_id']) : $context['entry_id'];

// flush by Entry ID
foreach ($ids as $id) {
$this->clearPagesByStrategy($id, 'entry');
$id = $context['entry_id'];
if (!is_array($id)) {
$id = array($id);
}
foreach ($id as $i) {
$this->clearPagesByStrategy($i, 'entry');
}
}

Expand Down Expand Up @@ -665,10 +667,14 @@ public function getPagesByContent($id, $type)
try {
$col = $type == 'entry' ? 'entry_id' : 'section_id';
$id = General::intval($id);
return Symphony::Database()->fetch(
"SELECT DISTINCT `page` FROM `tbl_cachelite_references`
WHERE `$col` = $id"
);

return Symphony::Database()
->select(['page'])
->distinct()
->from('tbl_cachelite_references')
->where([$col => $id])
->execute()
->rows();
} catch (Exception $ex) {
Symphony::Log()->pushExceptionToLog($ex);
}
Expand All @@ -680,10 +686,14 @@ public function isInvalidByContent($id, $type)
try {
$col = $type == 'entry' ? 'entry_id' : 'section_id';
$id = General::intval($id);
return !empty(Symphony::Database()->fetch(
"SELECT DISTINCT `$col` FROM `tbl_cachelite_invalid`
WHERE `$col` = $id"
));

return Symphony::Database()
->select([$col])
->distinct()
->from('tbl_cachelite_invalid')
->where([$col => $id])
->execute()
->rows();
} catch (Exception $ex) {
Symphony::Log()->pushExceptionToLog($ex);
}
Expand All @@ -693,10 +703,11 @@ public function isInvalidByContent($id, $type)
public function deletePageReferences($url)
{
try {
$url = Symphony::Database()->cleanValue($url);
return Symphony::Database()->query(
"DELETE FROM `tbl_cachelite_references` WHERE `page` = '$url'"
);
return Symphony::Database()
->delete('tbl_cachelite_references')
->where(['page' => $url])
->execute()
->success();
} catch (Exception $ex) {
Symphony::Log()->pushExceptionToLog($ex);
}
Expand All @@ -705,28 +716,36 @@ public function deletePageReferences($url)

protected function saveReferences($reference, $url, $ids)
{
try {
$now = DateTimeObj::get('Y-m-d H:i:s');
$values = array();
foreach ($ids as $id) {
$now = DateTimeObj::get('Y-m-d H:i:s');
$values = array();
$result = true;
foreach ($ids as $id) {
try {
$id = General::intval($id);
$values[] = "('$url', $id, '$now')";

$thisResult = Symphony::Database()
->insert('tbl_cachelite_references')
->values([
'page' => $url,
$reference => $id,
'timestamp' => $now,
])
->updateOnDuplicateKey()
->execute()
->success();
if (!$thisResult) {
$result = false;
}
} catch (Exception $ex) {
$result = false;
Symphony::Log()->pushExceptionToLog($ex);
}
$values = implode(',', $values);
return Symphony::Database()->query(
"INSERT INTO `tbl_cachelite_references` (`page`, `$reference`, `timestamp`)
VALUES $values
ON DUPLICATE KEY UPDATE `timestamp` = '$now'"
);
} catch (Exception $ex) {
Symphony::Log()->pushExceptionToLog($ex);
}
return false;
return $result;
}

protected function savePageReferences($url, array $sections, array $entries)
{
$url = Symphony::Database()->cleanValue($url);
// Create sections rows
$sections = $this->saveReferences('section_id', $url, $sections);
// Create entries rows
Expand All @@ -738,12 +757,14 @@ protected function saveInvalid($id, $type)
{
$col = $type == 'entry' ? 'entry_id' : 'section_id';
try {
$id = General::intval(Symphony::Database()->cleanValue($id));
return Symphony::Database()->query(
"INSERT INTO `tbl_cachelite_invalid` (`$col`)
VALUES ($id)
ON DUPLICATE KEY UPDATE `$col` = `$col`"
);
return Symphony::Database()
->insert('tbl_cachelite_invalid')
->values([
$col => $id,
])
->updateOnDuplicateKey()
->execute()
->success();
} catch (Exception $ex) {
Symphony::Log()->pushExceptionToLog($ex);
throw $ex;
Expand All @@ -754,52 +775,100 @@ protected function saveInvalid($id, $type)
protected function createPageTable()
{
// Create extension table
return Symphony::Database()->query("
CREATE TABLE `tbl_cachelite_references` (
`page` char(128) NOT NULL,
`section_id` int(11) NOT NULL default 0,
`entry_id` int(11) NOT NULL default 0,
`timestamp` datetime NOT NULL /*!50600 default CURRENT_TIMESTAMP */,
PRIMARY KEY (`page`, `section_id`, `entry_id`),
KEY `page` (`page`),
KEY `section_page` (`page`, `section_id`),
KEY `entry_page` (`page`, `entry_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
");
return Symphony::Database()
->create('tbl_cachelite_references')
->ifNotExists()
->fields([
'page' => 'char(128)',
'section_id' => [
'type' => 'int(11)',
'default' => 0,
],
'entry_id' => [
'type' => 'int(11)',
'default' => 0,
],
'timestamp' => 'datetime',
])
->keys([
'page_section_entry' => [
'type' => 'primary',
'cols' => ['page', 'section_id', 'entry_id'],
],
'page' => 'key',
'section_page' => [
'type' => 'key',
'cols' => ['page', 'section_id'],
],
'entry_page' => [
'type' => 'key',
'cols' => ['page', 'entry_id'],
],
])
->execute()
->success();
}

protected function createInvalidTable()
{
// Create extension table
return Symphony::Database()->query("
CREATE TABLE `tbl_cachelite_invalid` (
`section_id` int(11) NOT NULL default 0,
`entry_id` int(11) NOT NULL default 0,
PRIMARY KEY (`section_id`, `entry_id`),
KEY `section_id` (`section_id`),
KEY `entry_id` (`entry_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
");
return Symphony::Database()
->create('tbl_cachelite_invalid')
->ifNotExists()
->fields([
'section_id' => [
'type' => 'int(11)',
'default' => 0,
],
'entry_id' => [
'type' => 'int(11)',
'default' => 0,
],
])
->keys([
'section_entry' => [
'type' => 'primary',
'cols' => ['section_id', 'entry_id'],
],
'section_id' => 'key',
'entry_id' => 'key',
])
->execute()
->success();
}

public function dropPageTable()
{
return Symphony::Database()->query("DROP TABLE IF EXISTS `tbl_cachelite_references`");
return Symphony::Database()
->drop('tbl_cachelite_references')
->ifExists()
->execute()
->success();
}

public function dropInvalidTable()
{
return Symphony::Database()->query("DROP TABLE IF EXISTS `tbl_cachelite_invalid`");
return Symphony::Database()
->drop('tbl_cachelite_invalid')
->ifExists()
->execute()
->success();
}

public function optimizePageTable()
{
return Symphony::Database()->query("OPTIMIZE TABLE `tbl_cachelite_references`");
return Symphony::Database()
->optimize('tbl_cachelite_references')
->execute()
->success();
}

public function optimizeInvalidTable()
{
return Symphony::Database()->query("OPTIMIZE TABLE `tbl_cachelite_invalid`");
return Symphony::Database()
->optimize('tbl_cachelite_invalid')
->execute()
->success();
}

/*-------------------------------------------------------------------------
Expand Down
5 changes: 4 additions & 1 deletion extension.meta.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
</author>
</authors>
<releases>
<release version="3.0.0" date="TBA" min="4.0.0" max="4.x.x" php-min="5.6.x" php-max="7.x.x">
- Update for Symphony 4.x
</release>
<release version="2.1.2" date="2018-08-29" min="2.6.x" max="2.x.x">
- Fix crash on deleting entries
</release>
Expand Down Expand Up @@ -108,4 +111,4 @@
- Added support for flushing the cache based on entry/section I
</release>
</releases>
</extension>
</extension>
Loading