-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathhelper.php
164 lines (137 loc) · 4.9 KB
/
helper.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
156
157
158
159
160
161
162
163
164
<?php
/**
* DokuWiki Plugin rating (Helper Component)
*
* @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
* @author Andreas Gohr <[email protected]>
*/
// must be run within Dokuwiki
if(!defined('DOKU_INC')) die();
class helper_plugin_rating extends DokuWiki_Plugin {
/** @var helper_plugin_sqlite */
protected $sqlite = null;
/**
* initializes the DB connection
*
* @return helper_plugin_sqlite|null
*/
public function getDBHelper() {
if(!is_null($this->sqlite)) return $this->sqlite;
$this->sqlite = plugin_load('helper', 'sqlite');
if(!$this->sqlite) {
msg('The rating plugin requires the sqlite plugin', -1);
$this->sqlite = null;
return null;
}
$ok = $this->sqlite->init('rating', __DIR__ . '/db');
if(!$ok) {
msg('rating plugin sqlite initialization failed', -1);
$this->sqlite = null;
return null;
}
return $this->sqlite;
}
/**
* Current user identifier
*
* @return string
*/
public function userID() {
if(isset($_SERVER['REMOTE_USER'])) return $_SERVER['REMOTE_USER'];
return clientIP(true);
}
/**
* Display the rating tool in a template
*
* @param bool $inner used for AJAX updates
*/
public function tpl($inner = false) {
global $ID;
$sqlite = $this->getDBHelper();
if(!$sqlite) return;
$sql = "SELECT sum(value) FROM ratings WHERE page = ?";
$res = $sqlite->query($sql, $ID);
$current = (int) $sqlite->res2single($res);
$sqlite->res_close($res);
$sql = "SELECT value FROM ratings WHERE page = ? AND rater = ?";
$res = $sqlite->query($sql, $ID, $this->userID());
$self = (int) $sqlite->res2single($res);
$sqlite->res_close($res);
if(!$inner) echo '<div class="plugin_rating">';
echo '<span class="intro">' . $this->getLang('intro') . '</span>';
$class = ($self == -1) ? 'act' : '';
echo '<a href="' . wl($ID, array('rating' => -1)) . '" class="plugin_rating_down ' . $class . ' plugin_feedback" data-rating="-1">-1</a>';
echo '<span class="current">' . $current . '</span>';
$class = ($self == 1) ? 'act' : '';
echo '<a href="' . wl($ID, array('rating' => +1)) . '" class="plugin_rating_up ' . $class . '" data-rating="1">+1</a>';
if(!$inner) echo '</div>';
}
/**
* Removes all entries for a page
*
* @param $page
*/
public function remove($page) {
$sqlite = $this->getDBHelper();
if(!$sqlite) return;
$sql = "DELETE FROM ratings WHERE page = ?";
$res = $sqlite->query($sql, $page);
$sqlite->res_close($res);
}
/**
* Get the best voted pages
*
* @param int $num
* @return array
*/
public function best($lang, $startdate, $num = 10) {
$sqlite = $this->getDBHelper();
if(!$sqlite) return array();
$sqlbegin = "SELECT sum(value) as val, page, lang FROM ratings ";
$sqlend = "GROUP BY page ORDER BY sum(value) DESC LIMIT ?";
if ($lang === null && $startdate === null){
$sql = $sqlbegin . $sqlend;
$res = $sqlite->query($sql, $num);
} else if ($lang !== null && $startdate === null) {
$sql = $sqlbegin . "WHERE lang = ? " . $sqlend;
$res = $sqlite->query($sql, $lang, $num);
} else if ($lang === null && $startdate !== null){
$sql = $sqlbegin . "WHERE date >= ? " . $sqlend;
$res = $sqlite->query($sql, $startdate, $num);
} else {
$sql = $sqlbegin . "WHERE lang = ? AND date >= ? " . $sqlend;
$res = $sqlite->query($sql, $lang, $startdate, $num);
}
$list = $sqlite->res2arr($res);
$sqlite->res_close($res);
return $list;
}
/**
* Store a rating
*
* @param int $rate either -1 or +1
* @param string $page page to rate
*/
public function rate($rate, $page) {
if($rate < -1) $rate = -1;
if($rate > 1) $rate = 1;
$sqlite = $this->getDBHelper();
if(!$sqlite) return;
// ignore any bot accesses
if(!class_exists('Jaybizzle\CrawlerDetect\CrawlerDetect')){
require (__DIR__ . '/CrawlerDetect.php');
}
$CrawlerDetect = new Jaybizzle\CrawlerDetect\CrawlerDetect();
if($CrawlerDetect->isCrawler()) return;
$translation = plugin_load('helper', 'translation');
if (!$translation) {
$lang = '';
} else {
$lang = $translation->getLangPart($page);
}
$date = date('Y-m-d');
$sql = "INSERT OR REPLACE INTO ratings (page, rater, lang, date, value) VALUES (?, ?, ?, ?, ?)";
$sqlite->query($sql, $page, $this->userID(), $lang, $date, $rate);
}
}
// vim:ts=4:sw=4:et: