-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWikiRatingRestClient.php
155 lines (140 loc) · 4.58 KB
/
WikiRatingRestClient.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
<?php
/**
* WikiRatingRestClient is a static class to consume the WikiRating RESTful API
*
* @author Alessandro Tundo <alessandrotundo94@gmail.com>
* @access public
*/
class WikiRatingRestClient{
/**
* Use WikiRating RESTful API to get the latest revision rating information
* @param string $title the page title
* @return string the JSON response
* @access public
*/
public static function getLatestRevisionRating($title){
global $wgLanguageCode, $wgWikiRatingRestApi;
$pageTitle = Title::newFromTextThrow($title);
$wikiPage = WikiPage::factory($pageTitle);
$pageId = $wikiPage->getId();
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_FRESH_CONNECT => 1,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_FORBID_REUSE => 1,
CURLOPT_URL => "$wgWikiRatingRestApi/$wgLanguageCode/pages/$pageId"
));
$response = curl_exec($curl);
curl_close($curl);
return $response;
}
/**
* Use WikiRating RESTful API to get rating information
* requested revision
* @param string $title the page title
* @param string $revisionId the revision id
* @return string the JSON response
* @access public
*/
public static function getRevisionRatingById($title, $revisionId = null){
global $wgLanguageCode, $wgWikiRatingRestApi;
$pageTitle = Title::newFromTextThrow($title);
$wikiPage = WikiPage::factory($pageTitle);
$pageId = $wikiPage->getId();
if ($revisionId == null) {
$revisionId = $wikiPage->getLatest();
}
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_FRESH_CONNECT => 1,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_FORBID_REUSE => 1,
CURLOPT_URL => "$wgWikiRatingRestApi/$wgLanguageCode/pages/$pageId/revisions/$revisionId"
));
$response = curl_exec($curl);
curl_close($curl);
return $response;
}
/**
* Use WikiRating RESTful API to get the all revision rating information
* @param string $title the page title
* @return string the JSON response
* @access public
*/
public static function getAllRevisionsRating($title){
global $wgLanguageCode, $wgWikiRatingRestApi;
$pageTitle = Title::newFromTextThrow($title);
$wikiPage = WikiPage::factory($pageTitle);
$pageId = $wikiPage->getId();
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_FRESH_CONNECT => 1,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_FORBID_REUSE => 1,
CURLOPT_URL => "$wgWikiRatingRestApi/$wgLanguageCode/pages/$pageId/revisions"
));
$response = curl_exec($curl);
curl_close($curl);
return $response;
}
/**
* Use WikiRating RESTful API to get all the votes of the requested revision
* @param string $title the page title
* @param string $revisionId the revision id
* @return string the JSON response
* @access public
*/
public static function getVotes($title, $revisionId = null){
global $wgLanguageCode, $wgWikiRatingRestApi;
$pageTitle = Title::newFromTextThrow($title);
$wikiPage = WikiPage::factory($pageTitle);
$pageId = $wikiPage->getId();
if ($revisionId == null) {
$revisionId = $wikiPage->getLatest();
}
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_FRESH_CONNECT => 1,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_FORBID_REUSE => 1,
CURLOPT_URL => "$wgWikiRatingRestApi/$wgLanguageCode/pages/$pageId/revisions/$revisionId/votes"
));
$response = curl_exec($curl);
curl_close($curl);
return $response;
}
/**
* Use WikiRating RESTful API to vote the requested revision
* @param string $title the page title
* @param int $userId the user id
* @param double $vote the vote value
* @param int $revisionId the revision id
* @return string the JSON response
* @access public
*/
public static function vote($title, $userId, $vote, $revisionId = null){
global $wgLanguageCode, $wgWikiRatingRestApi;
$pageTitle = Title::newFromTextThrow($title);
$wikiPage = WikiPage::factory($pageTitle);
$pageId = $wikiPage->getId();
if ($revisionId == null) {
$revisionId = $wikiPage->getLatest();
}
$params = [
'userId' => $userId,
'vote' => $vote
];
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_FRESH_CONNECT => 1,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_FORBID_REUSE => 1,
CURLOPT_URL => "$wgWikiRatingRestApi/$wgLanguageCode/pages/$pageId/revisions/$revisionId/votes",
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => http_build_query($params),
));
$response = curl_exec($curl);
curl_close($curl);
return $response;
}
}