-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathApiLikes.php
62 lines (53 loc) · 1.33 KB
/
ApiLikes.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
<?php
/**
* Author: Denisov Denis
* Date: 14.08.13
* Time: 19:51
*/
if (!defined('MEDIAWIKI')) {
die("This is not a valid entry point.\n");
}
class ApiLikes extends ApiBase
{
protected function getAllowedParams()
{
return array(
'pageId' => null,
'userId' => null,
);
}
private function tableName()
{
return 'likes';
}
public function execute()
{
wfSetupSession();
$pageId = $userId = null;
extract($this->extractRequestParams());
if (!$pageId or !$userId) {
$this->getResult()->addValue(null, 'error', 'Variables is not defined.');
}
$dbr = wfGetDB(DB_SLAVE);
$res = $dbr->select(
$this->tableName(),
array('page_id', 'user_id'),
array(
'page_id ' => $pageId,
'user_id ' => $userId,
),
__METHOD__
);
$isLiked = $res->numRows() == 0 ? false : true ;
$action = $isLiked ? 'delete' : 'insert';
$res = $dbr->$action(
$this->tableName(),
array(
'page_id ' => $pageId,
'user_id ' => $userId,
),
__METHOD__
);
$this->getResult()->addValue(null, 'success', 'Like has been saved');
}
}