From b051dd59f866c67106b29db0855d7696d557c8ec Mon Sep 17 00:00:00 2001 From: Olivier PEREZ Date: Sat, 28 Feb 2015 19:18:59 +0100 Subject: [PATCH] Implement purge in 2 steps First: Block the poll (no more modification) Second: Delete the poll 60 days after the expiration date (configurable) --- admin/purge.php | 56 +++++++++++++++++++ adminstuds.php | 2 + app/classes/Framadate/FramaDB.php | 2 +- .../Framadate/Services/AdminPollService.php | 2 +- .../Framadate/Services/PurgeService.php | 2 +- app/inc/config.template.php | 3 + studs.php | 2 + tpl/admin/index.tpl | 3 + tpl/admin/purge.tpl | 13 +++++ tpl/part/comments.tpl | 4 +- tpl/part/poll_info.tpl | 48 +++++++++------- tpl/part/vote_table_classic.tpl | 6 +- tpl/part/vote_table_date.tpl | 8 +-- tpl/studs.tpl | 14 +++-- 14 files changed, 128 insertions(+), 37 deletions(-) create mode 100644 admin/purge.php create mode 100644 tpl/admin/purge.tpl diff --git a/admin/purge.php b/admin/purge.php new file mode 100644 index 0000000..b97dcb2 --- /dev/null +++ b/admin/purge.php @@ -0,0 +1,56 @@ + ['regexp' => NAME_REGEX]]); + +/* PAGE */ +/* ---- */ + +if ($action === 'purge' && $securityService->checkCsrf('admin', $_POST['csrf'])) { + $count = $purgeService->purgeOldPolls(); + $message = _('Purged:') . ' ' . $count; +} + +// Assign data to template +$smarty->assign('message', $message); +$smarty->assign('crsf', $securityService->getToken('admin')); + +$smarty->display('admin/purge.tpl'); \ No newline at end of file diff --git a/adminstuds.php b/adminstuds.php index 2fb5ac9..f8501dd 100644 --- a/adminstuds.php +++ b/adminstuds.php @@ -364,6 +364,8 @@ $smarty->assign('poll_id', $poll_id); $smarty->assign('admin_poll_id', $admin_poll_id); $smarty->assign('poll', $poll); $smarty->assign('title', _('Poll') . ' - ' . $poll->title); +$smarty->assign('expired', strtotime($poll->end_date) < time()); +$smarty->assign('deletion_date', $poll->end_date + PURGE_DELAY * 86400); $smarty->assign('slots', $poll->format === 'D' ? $pollService->splitSlots($slots) : $slots); $smarty->assign('votes', $pollService->splitVotes($votes)); $smarty->assign('best_choices', $pollService->computeBestChoices($votes)); diff --git a/app/classes/Framadate/FramaDB.php b/app/classes/Framadate/FramaDB.php index 78b6680..6b7c4c3 100644 --- a/app/classes/Framadate/FramaDB.php +++ b/app/classes/Framadate/FramaDB.php @@ -270,7 +270,7 @@ class FramaDB { * @return array Array of old polls */ public function findOldPolls() { - $prepared = $this->prepare('SELECT * FROM `' . Utils::table('poll') . '` WHERE end_date < NOW() AND end_date != 0 LIMIT 20'); + $prepared = $this->prepare('SELECT * FROM `' . Utils::table('poll') . '` WHERE DATE_ADD(`end_date`, INTERVAL ' . PURGE_DELAY . ' DAY) < NOW() AND `end_date` != 0 LIMIT 20'); $prepared->execute([]); return $prepared->fetchAll(); diff --git a/app/classes/Framadate/Services/AdminPollService.php b/app/classes/Framadate/Services/AdminPollService.php index a346de2..70699bb 100644 --- a/app/classes/Framadate/Services/AdminPollService.php +++ b/app/classes/Framadate/Services/AdminPollService.php @@ -23,7 +23,7 @@ class AdminPollService { function updatePoll($poll) { global $config; - if ($poll->end_date <= strtotime($poll->creation_date) + (86400 * $config['default_poll_duration'])) { + if ($poll->end_date > $poll->creation_date && $poll->end_date <= strtotime($poll->creation_date) + (86400 * $config['default_poll_duration'])) { return $this->connect->updatePoll($poll); } else { return false; diff --git a/app/classes/Framadate/Services/PurgeService.php b/app/classes/Framadate/Services/PurgeService.php index c9fd7d3..fc05365 100644 --- a/app/classes/Framadate/Services/PurgeService.php +++ b/app/classes/Framadate/Services/PurgeService.php @@ -38,7 +38,7 @@ class PurgeService { } } - return false; + return $count; } /** diff --git a/app/inc/config.template.php b/app/inc/config.template.php index c840737..0c49704 100644 --- a/app/inc/config.template.php +++ b/app/inc/config.template.php @@ -70,6 +70,9 @@ const USE_REMOTE_USER = true; // Path to the log file const LOG_FILE = 'admin/stdout.log'; +// Days (after expiration date) before purge a poll +const PURGE_DELAY = 60; + // Config $config = [ /* general config */ diff --git a/studs.php b/studs.php index d0c5798..ddfc2b3 100644 --- a/studs.php +++ b/studs.php @@ -194,6 +194,8 @@ $comments = $pollService->allCommentsByPollId($poll_id); $smarty->assign('poll_id', $poll_id); $smarty->assign('poll', $poll); $smarty->assign('title', _('Poll') . ' - ' . $poll->title); +$smarty->assign('expired', $poll->end_date < time()); +$smarty->assign('deletion_date', $poll->end_date + PURGE_DELAY * 86400); $smarty->assign('slots', $poll->format === 'D' ? $pollService->splitSlots($slots) : $slots); $smarty->assign('votes', $pollService->splitVotes($votes)); $smarty->assign('best_choices', $pollService->computeBestChoices($votes)); diff --git a/tpl/admin/index.tpl b/tpl/admin/index.tpl index 26841f3..a2453d3 100644 --- a/tpl/admin/index.tpl +++ b/tpl/admin/index.tpl @@ -8,6 +8,9 @@

{_('Migration')}

+
+

{_('Purge')}

+
{if $logsAreReadable}

{_('Logs')}

diff --git a/tpl/admin/purge.tpl b/tpl/admin/purge.tpl new file mode 100644 index 0000000..d36553d --- /dev/null +++ b/tpl/admin/purge.tpl @@ -0,0 +1,13 @@ +{extends 'admin/admin_page.tpl'} + +{block 'admin_main'} + {if $message} + + {/if} +
+ +
+ +
+
+{/block} \ No newline at end of file diff --git a/tpl/part/comments.tpl b/tpl/part/comments.tpl index 87a8be6..5923cc9 100644 --- a/tpl/part/comments.tpl +++ b/tpl/part/comments.tpl @@ -7,7 +7,7 @@

{_("Comments of polled people")}

{foreach $comments as $comment}
- {if $admin} + {if $admin && !$expired} {/if} {$comment->name|html}  @@ -17,7 +17,7 @@ {/if} {* Add comment form *} - {if $active} + {if $active && !$expired}
{_("Add a comment to the poll")} diff --git a/tpl/part/poll_info.tpl b/tpl/part/poll_info.tpl index 7c1b220..360e09a 100644 --- a/tpl/part/poll_info.tpl +++ b/tpl/part/poll_info.tpl @@ -4,8 +4,8 @@
-

{$poll->title|html}{if $admin} {/if}

- {if $admin} +

{$poll->title|html}{if $admin && !$expired} {/if}

+ {if $admin && !$expired}