Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix deprecated stuff #13

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
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
104 changes: 67 additions & 37 deletions extension.json
Original file line number Diff line number Diff line change
@@ -1,37 +1,67 @@
{
"manifest_version": 1,
"name": "MediaWiki Election System",
"descriptionmsg": "election-desc",
"author": ["Jacob G. (jvvg)", "apple502j"],
"url": "https://github.com/InternationalScratchWiki/mediawiki-elections",
"version": "0.1",
"license-name": "GPL-2.0-or-later",
"MessagesDirs": {
"mediawiki-elections": [
"i18n"
]
},
"AutoloadClasses": {
"SpecialVote": "src/SpecialVote.php",
"SpecialElectionResults": "src/SpecialElectionResults.php",
"ElectionHooks": "src/ElectionHooks.php",
"ElectionVoteLoader": "src/ElectionVoteRepository.php",
"ElectionVoteRepository": "src/ElectionVoteRepository.php"
},
"SpecialPages": {
"Vote": "SpecialVote",
"ElectionResults": "SpecialElectionResults"
},
"Hooks": {
"LoadExtensionSchemaUpdates": "ElectionHooks::onLoadExtensionSchemaUpdates"
},
"ResourceModules": {
},
"config": {
"ElectionActive": false,
"ElectionId": "00000000-0000-0000-0000-000000000000",
"ElectionCandidates": [],
"ElectionMinRegistrationDate": 0,
"ElectionCountMethod": "borda"
}
}
{
"manifest_version": 2,
"name": "MediaWiki Election System",
"descriptionmsg": "election-desc",
"author": [
"Jacob G. (jvvg)",
"apple502j"
],
"url": "https://github.com/InternationalScratchWiki/mediawiki-elections",
"version": "0.1",
"license-name": "GPL-2.0-or-later",
"MessagesDirs": {
"mediawiki-elections": [
"i18n"
]
},
"AutoloadClasses": {
"SpecialVote": "src/SpecialVote.php",
"SpecialElectionResults": "src/SpecialElectionResults.php",
"ElectionHooks": "src/ElectionHooks.php",
"ElectionVoteLoader": "src/ElectionVoteRepository.php",
"ElectionVoteRepository": "src/ElectionVoteRepository.php"
},
"SpecialPages": {
"Vote": {
"class": "SpecialVote",
"services": [
"DBLoadBalancer"
]
},
"ElectionResults": {
"class": "SpecialElectionResults",
"services": [
"DBLoadBalancer"
]
}
},
"Hooks": {
"LoadExtensionSchemaUpdates": "main"
},
"HookHandlers": {
"main": {
"class": "ElectionHooks"
}
},
"ResourceModules": [],
"config": {
"ElectionActive": {
"value": false
},
"ElectionId": {
"value": "00000000-0000-0000-0000-000000000000"
},
"ElectionCandidates": {
"value": []
},
"ElectionMinRegistrationDate": {
"value": 0
},
"ElectionCountMethod": {
"value": "borda"
}
},
"requires": {
"MediaWiki": ">= 1.35.0"
}
}
7 changes: 5 additions & 2 deletions src/ElectionHooks.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
<?php
class ElectionHooks {
public static function onLoadExtensionSchemaUpdates( DatabaseUpdater $updater ) {

use MediaWiki\Installer\Hook\LoadExtensionSchemaUpdatesHook;

class ElectionHooks implements LoadExtensionSchemaUpdatesHook {
public function onLoadExtensionSchemaUpdates($updater) {
$updater->addExtensionTable('election_votes', __DIR__ . '/../sql/election_votes.sql');
$updater->addExtensionTable('election_voters', __DIR__ . '/../sql/election_voters.sql');
}
Expand Down
16 changes: 5 additions & 11 deletions src/ElectionVoteRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,8 @@ class ElectionVoteLoader {
protected $db;
protected $electionId;

function __construct(string $method, string $electionId, ?IDatabase $db = null) {
if ($db === null) {
$this->db = wfGetDB(DB_REPLICA);
} else {
$this->db = $db;
}
function __construct(string $method, string $electionId, IDatabase $db) {
$this->db = $db;

$this->electionId = $electionId;
}
Expand Down Expand Up @@ -66,8 +62,8 @@ function getResults(array $candidates) : array {
}

class ElectionVoteRepository extends ElectionVoteLoader {
function __construct($method, $electionId) {
parent::__construct($method, $electionId, wfGetDB(DB_MASTER));
function __construct($method, $electionId, IDatabase $db) {
parent::__construct($method, $electionId, $db);
}

static function getEligibilityError(User $user) : ?string {
Expand All @@ -81,7 +77,7 @@ static function getEligibilityError(User $user) : ?string {
return 'blocked';
}

if (wfTimestamp(TS_UNIX, $user->getRegistration()) > $wgElectionMinRegistrationDate) {
if ($wgElectionMinRegistrationDate && wfTimestamp(TS_UNIX, $user->getRegistration()) > $wgElectionMinRegistrationDate) {
return 'age';
}

Expand Down Expand Up @@ -132,8 +128,6 @@ function validateVotes(array $votes) : ?string {
}

function addVotes(User $user, array $votes) : ?string {
global $wgElectionActive, $wgElectionMinRegistrationDate;

$this->db->startAtomic(__METHOD__);

try {
Expand Down
23 changes: 16 additions & 7 deletions src/SpecialElectionResults.php
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
<?php

use Wikimedia\Rdbms\ILoadBalancer;

class SpecialElectionResults extends SpecialPage {
function __construct() {
private ILoadBalancer $loadBalancer;

function __construct(ILoadBalancer $loadBalancer) {
parent::__construct('ElectionResults', 'viewelectionresults');
$this->loadBalancer = $loadBalancer;
}

function execute($par) {
global $wgElectionActive, $wgElectionId, $wgElectionCandidates, $wgElectionCountMethod;
global $wgElectionId, $wgElectionCandidates, $wgElectionCountMethod;

$output = $this->getOutput();
$output->setPageTitle($this->msg('election-viewelectionresults-title')->escaped());

$this->checkPermissions();

$voteLoader = new ElectionVoteLoader(__METHOD__, $wgElectionId);
$voteLoader = new ElectionVoteLoader(__METHOD__, $wgElectionId, $this->loadBalancer->getConnection(DB_REPLICA));
$electionResults = $voteLoader->getResults($wgElectionCandidates);
$results = $electionResults['results'];
$voteCounts = $electionResults['voteCounts'];
Expand All @@ -29,7 +35,8 @@ function execute($par) {
$output->addHTML(Html::openElement('tr'));

$output->addHTML(Html::element(
'th', ['scope' => 'col'],
'th',
['scope' => 'col'],
$this->msg('election-viewelectionresults-candidate')->text()
));
switch ($wgElectionCountMethod) {
Expand All @@ -41,7 +48,8 @@ function execute($par) {
$resultKey = 'score';
}
$output->addHTML(Html::element(
'th', ['scope' => 'col'],
'th',
['scope' => 'col'],
$this->msg('election-viewelectionresults-' . $resultKey)->text()
));

Expand All @@ -66,11 +74,12 @@ function execute($par) {
$key = 'election-viewelectionresults-score-display';
}
$output->addHTML(Html::element('td', [], $this->msg($key)->params(
$score, $voteCount
$score,
$voteCount
)));

$output->addHTML(Html::closeElement('tr'));
}
$output->addHTML(Html::closeElement('table'));
}
}
}
66 changes: 49 additions & 17 deletions src/SpecialVote.php
Original file line number Diff line number Diff line change
@@ -1,16 +1,35 @@
<?php

use Wikimedia\Rdbms\ILoadBalancer;

class SpecialVote extends SpecialPage {
function __construct() {
private ILoadBalancer $loadBalancer;

function __construct(ILoadBalancer $loadBalancer) {
parent::__construct('Vote', 'vote');
$this->loadBalancer = $loadBalancer;
}

function setCSRFToken() {
$session = $this->getRequest()->getSession();
$session->persist();
$csrftoken = $session->getToken();
$session->save();
return $csrftoken;
}

function isCSRF($csrftoken) {
$session = $this->getRequest()->getSession();
return !$session->getToken()->match($csrftoken);
}

function handleVoteSubmission() {
global $wgElectionCandidates, $wgElectionId;
global $wgElectionId;

$request = $this->getRequest();
$output = $this->getOutput();

if ( !$this->getUser()->matchEditToken( $request->getVal( 'token' ) ) ) {
if ($this->isCSRF($request->getText('token'))) {
$output->addWikiMsg('sessionfailure');
$output->addReturnTo($this->getPageTitle());
return;
Expand All @@ -23,7 +42,11 @@ function handleVoteSubmission() {
$output->addReturnTo($this->getPageTitle());
return;
}
$voteRepo = new ElectionVoteRepository(__METHOD__, $wgElectionId);
$voteRepo = new ElectionVoteRepository(
__METHOD__,
$wgElectionId,
$this->loadBalancer->getConnection(defined('DB_PRIMARY') ? DB_PRIMARY : DB_MASTER)
);
$message = $voteRepo->addVotes($this->getUser(), $votes);

if ($message) {
Expand Down Expand Up @@ -87,11 +110,13 @@ function showVoteForm() {
$output->addHTML(Html::openElement('tr'));
$output->addHTML(Html::element('th', ['scope' => 'row']));
$output->addHTML(Html::element(
'th', ['scope' => 'col'],
'th',
['scope' => 'col'],
$this->msg('election-vote-yes')->text()
));
$output->addHTML(Html::element(
'th', ['scope' => 'col'],
'th',
['scope' => 'col'],
$this->msg('election-vote-no')->text()
));
$output->addHTML(Html::closeElement('tr'));
Expand All @@ -100,13 +125,15 @@ function showVoteForm() {
$output->addHTML(Html::openElement('tr'));
$output->addHTML(Html::element('th', ['scope' => 'row'], $candidateName));
$output->addHTML(Html::rawElement(
'td', ['style' => 'text-align: center'],
'td',
['style' => 'text-align: center'],
Html::radio('candidateRank[' . $candidateId . ']', false, [
'value' => 1
])
));
$output->addHTML(Html::rawElement(
'td', ['style' => 'text-align: center'],
'td',
['style' => 'text-align: center'],
Html::radio('candidateRank[' . $candidateId . ']', false, [
'value' => 0
])
Expand Down Expand Up @@ -136,7 +163,8 @@ function showVoteForm() {
$output->addHTML(Html::element('th', ['scope' => 'row'], $candidateName));
for ($rankIdx = 1; $rankIdx <= $numCandidates; $rankIdx++) {
$output->addHTML(Html::rawElement(
'td', ['style' => 'text-align: center'],
'td',
['style' => 'text-align: center'],
Html::radio('candidateRank[' . $candidateId . ']', false, [
'value' => $rankIdx
])
Expand All @@ -147,7 +175,7 @@ function showVoteForm() {
}
$output->addHTML(Html::closeElement('table'));

$output->addHTML(Html::hidden('token', $this->getUser()->getEditToken()));
$output->addHTML(Html::hidden('token', $this->setCSRFToken()));

$output->addHTML(Html::element('input', [
'type' => 'submit',
Expand All @@ -158,26 +186,30 @@ function showVoteForm() {
}

function showVotePage() {
global $wgElectionActive, $wgElectionId, $wgElectionMinRegistrationDate;
global $wgElectionId;

$eligibilityError = ElectionVoteRepository::getEligibilityError($this->getUser());
if ($eligibilityError) {
switch ($eligibilityError) {
case 'inactive':
$this->showElectionInactivePage(); return;
$this->showElectionInactivePage();
return;
case 'blocked':
$this->showBlockedPage(); return;
$this->showBlockedPage();
return;
case 'age':
$this->showUserTooNewPage(); return;
$this->showUserTooNewPage();
return;
default:
assert(false);
}
return;
}

$voteLoader = new ElectionVoteLoader(__METHOD__, $wgElectionId);
$voteLoader = new ElectionVoteLoader(__METHOD__, $wgElectionId, $this->loadBalancer->getConnection(DB_REPLICA));
if ($voteLoader->hasUserVoted($this->getUser())) {
$this->showAlreadyVotedPage(); return;
$this->showAlreadyVotedPage();
return;
}

$this->showVoteForm();
Expand All @@ -195,4 +227,4 @@ function execute($par) {
$this->showVotePage();
}
}
}
}