summaryrefslogtreecommitdiff
path: root/Vote.php
diff options
context:
space:
mode:
authorMartin Fischer <martin@push-f.com>2022-10-08 08:30:37 +0200
committerMartin Fischer <martin@push-f.com>2022-10-08 09:14:25 +0200
commit3cd1539ad81868242b17b4d163d105483dc84983 (patch)
tree55d739304049f0f257889a12583134908e8c18c1 /Vote.php
parentb5fdd1fd93ff9bd8e52fa8418c1e10417548d556 (diff)
refactor: separate vote parsing from vote rendering
Diffstat (limited to 'Vote.php')
-rw-r--r--Vote.php56
1 files changed, 37 insertions, 19 deletions
diff --git a/Vote.php b/Vote.php
index ada160a..63d8fde 100644
--- a/Vote.php
+++ b/Vote.php
@@ -63,29 +63,48 @@ class VoteHooks {
return formatError($msg('vote-error-missing-newline'));
}
- $votes = [];
+ // Parse the passed text into $lines.
+ // Sidenote: We parse and render in two steps because we need both:
+ // * lookahead (gray out votes that are superseded), as well as
+ // * lookbehind (prefix "I changed my mind" for superseding votes).
+ $lines = [];
- $voteText = '';
foreach (explode("\n", $input) as $line) {
if (str_starts_with($line, '* ')) {
$line = substr($line, 2);
- [$date, $userTitle, $vote, $comment] = self::parseVote($line);
- $voteText .= '* ';
+ $vote = self::parseVote($line);
- if (!$date) {
- $addErrorCategory();
- $voteText .= formatError($msg('vote-error-invalid-date') . ':') . " $line\n";
+ if (!$vote['date']) {
+ array_push($lines, ['error' => $msg('vote-error-invalid-date'), 'line' => $line]);
continue;
}
- if (!in_array($vote, self::$options)) {
- $addErrorCategory();
- $voteText .= formatError($msg('vote-error-invalid-vote', '(YES, NO, ABSTAIN)') . ':') . " $line\n";
+ if (!in_array($vote['vote'], self::$options)) {
+ array_push($lines, ['error' => $msg('vote-error-invalid-vote', '(YES, NO, ABSTAIN)'), 'line' => $line]);
continue;
}
- $user = $userTitle->getText();
- $userLink = $parser->getLinkRenderer()->makeLink($userTitle, $user);
+ array_push($lines, $vote);
+ continue;
+ }
+ array_push($lines, ['raw' => $line . "\n"]);
+ }
+
+ // Render the parsed $lines into $voteText.
+ $voteText = '';
+ $votes = [];
+
+ foreach ($lines as $line) {
+ if (array_key_exists('raw', $line)) {
+ $voteText .= $line['raw'];
+ } elseif (array_key_exists('error', $line)) {
+ $addErrorCategory();
+ $voteText .= '* ' . formatError($line['error'] . ':') . " " . $line['line'] . "\n";
+ } else {
+ $user = $line['user'];
+ $vote = $line['vote'];
+
+ $voteText .= '* ';
if (array_key_exists($user, $votes)) {
if ($votes[$user] == $vote) {
$voteText .= "(" . $msg('vote-repeated') . ') ';
@@ -95,14 +114,13 @@ class VoteHooks {
}
$votes[$user] = $vote;
$voteText .= $msg('vote-option-' . $vote) . ' ';
- if ($comment) {
- $voteText .= $parser->recursiveTagParse($comment);
+ if ($line['comment']) {
+ $voteText .= $parser->recursiveTagParse($line['comment']);
}
- $voteText .= " --$userLink ". $date->format('H:i, j F Y') . "\n";
- continue;
- }
- $voteText .= $line . "\n";
+ $userLink = $parser->getLinkRenderer()->makeLink($line['userTitle'], $user);
+ $voteText .= " --$userLink ". $line['date']->format('H:i, j F Y') . "\n";
+ }
}
$voteCounts = ['yes' => 0, 'no' => 0, 'abstain' => 0];
@@ -143,7 +161,7 @@ class VoteHooks {
$user = Title::newFromText('User:' . rtrim(array_shift($parts), ':'));
$vote = strtolower(array_shift($parts));
$comment = array_shift($parts);
- return [$date, $user, $vote, $comment];
+ return ['date' => $date, 'userTitle' => $user, 'user' => $user->getText(), 'vote' => $vote, 'comment' => $comment];
}
private static function votingFormHtml($parser, $votes) {