forked from splitbrain/dokuwiki-plugin-data
-
Notifications
You must be signed in to change notification settings - Fork 0
/
helper.php
536 lines (488 loc) · 17.8 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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
<?php
/**
* @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
* @author Andreas Gohr <andi@splitbrain.org>
*/
// must be run within Dokuwiki
if(!defined('DOKU_INC')) die();
if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
require_once(DOKU_PLUGIN.'syntax.php');
require_once(DOKU_INC.'inc/infoutils.php');
/**
* This is the base class for all syntax classes, providing some general stuff
*/
class helper_plugin_data extends DokuWiki_Plugin {
/**
* @var helper_plugin_sqlite initialized via _getDb()
*/
protected $db = null;
/**
* @var array stores the alias definitions
*/
protected $aliases = null;
/**
* @var array stores custom key localizations
*/
protected $locs = array();
/**
* Constructor
*
* Loads custom translations
*/
public function __construct(){
$this->loadLocalizedLabels();
}
private function loadLocalizedLabels() {
$lang = array();
$path = DOKU_CONF.'/lang/en/data-plugin.php';
if(file_exists($path)) include($path);
$path = DOKU_CONF.'/lang/'.$this->determineLang().'/data-plugin.php';
if(file_exists($path)) include($path);
foreach ($lang as $key => $val) {
$lang[utf8_strtolower($key)] = $val;
}
$this->locs = $lang;
}
protected function determineLang() {
/** @var helper_plugin_translation $trans */
$trans = plugin_load('helper','translation');
if ($trans) {
$value = $trans->getLangPart(getID());
if ($value) return $value;
}
global $conf;
return $conf['lang'];
}
/**
* Simple function to check if the database is ready to use
*/
public function ready(){
return (bool) $this->_getDB();
}
/**
* @return helper_plugin_sqlite|false load the sqlite helper
*/
function _getDB(){
if ($this->db === null) {
$this->db = plugin_load('helper', 'sqlite');
if ($this->db === null) {
msg('The data plugin needs the sqlite plugin', -1);
return false;
}
if(!$this->db->init('data',dirname(__FILE__).'/db/')){
$this->db = null;
return false;
}
$this->db->create_function('DATARESOLVE',array($this,'_resolveData'),2);
}
return $this->db;
}
/**
* Makes sure the given data fits with the given type
*/
function _cleanData($value, $type){
$value = trim($value);
if(!$value AND $value!=='0') return '';
if (is_array($type)) {
if (isset($type['enum']) &&
!preg_match('/(^|,\s*)' . preg_quote_cb($value) . '($|\s*,)/', $type['enum'])) {
return '';
}
$type = $type['type'];
}
switch($type){
case 'dt':
if(preg_match('/^(\d\d\d\d)-(\d\d?)-(\d\d?)$/',$value,$m)){
return sprintf('%d-%02d-%02d',$m[1],$m[2],$m[3]);
}
if ($value === '%now%') {
return $value;
}
return '';
case 'url':
if(!preg_match('!^[a-z]+://!i',$value)) $value='http://'.$value;
return $value;
case 'mail':
$email = '';
$name = '';
$part = '';
$parts = preg_split('/\s+/',$value);
do{
$part = array_shift($parts);
if(!$email && mail_isvalid($part)){
$email = strtolower($part);
continue;
}
$name .= $part.' ';
}while($part);
return trim($email.' '.$name);
case 'page': case 'nspage':
return cleanID($value);
default:
return $value;
}
}
/**
* Add pre and postfixs to the given value
*
* $type may be an column array with pre and postfixes
*/
function _addPrePostFixes($type, $val, $pre='', $post='') {
if (is_array($type)) {
if (isset($type['prefix'])) $pre = $type['prefix'];
if (isset($type['postfix'])) $post = $type['postfix'];
}
$val = $pre.$val.$post;
$val = $this->replacePlaceholders($val);
return $val;
}
/**
* Resolve a value according to its column settings
*
* This function is registered as a SQL function named DATARESOLVE
*/
function _resolveData($value, $colname){
// resolve pre and postfixes
$column = $this->_column($colname);
$value = $this->_addPrePostFixes($column['type'], $value);
// for pages, resolve title
$type = $column['type'];
if(is_array($type)) $type = $type['type'];
if($type == 'title' || ($type == 'page' && useHeading('content'))){
$id = $value;
if($type == 'title'){
list($id,) = explode('|',$value,2);
}
//DATARESOLVE is only used with the 'LIKE' comparator, so concatenate the different strings is fine.
$value .= ' ' . p_get_first_heading($id);
}
return $value;
}
/**
* Return XHTML formated data, depending on column type
*
* @param $column
* @param $value
* @param $R Doku_Renderer_xhtml
* @return string
*/
function _formatData($column, $value, &$R){
global $conf;
$vals = explode("\n",$value);
$outs = array();
foreach($vals as $val){
$val = trim($val);
if($val=='') continue;
$type = $column['type'];
if (is_array($type)) $type = $type['type'];
switch($type){
case 'page':
$val = $this->_addPrePostFixes($column['type'], $val);
$outs[] = $R->internallink($val,null,null,true);
break;
case 'title':
case 'pageid':
list($id,$title) = explode('|',$val,2);
$id = $this->_addPrePostFixes($column['type'], $id);
$outs[] = $R->internallink($id,$title,null,true);
break;
case 'nspage':
// no prefix/postfix here
$val = ':'.$column['key'].":$val";
$outs[] = $R->internallink($val,null,null,true);
break;
case 'mail':
list($id,$title) = explode(' ',$val,2);
$id = $this->_addPrePostFixes($column['type'], $id);
$id = obfuscate(hsc($id));
if(!$title){
$title = $id;
}else{
$title = hsc($title);
}
if($conf['mailguard'] == 'visible') $id = rawurlencode($id);
$outs[] = '<a href="mailto:'.$id.'" class="mail" title="'.$id.'">'.$title.'</a>';
break;
case 'url':
$val = $this->_addPrePostFixes($column['type'], $val);
$outs[] = $this->external_link($val,false,'urlextern');
break;
case 'tag':
// per default use keyname as target page, but prefix on aliases
if(!is_array($column['type'])){
$target = $column['key'].':';
}else{
$target = $this->_addPrePostFixes($column['type'],'');
}
$outs[] = '<a href="'.wl(str_replace('/',':',cleanID($target)), $this->_getTagUrlparam($column, $val)).
'" title="'.sprintf($this->getLang('tagfilter'),hsc($val)).
'" class="wikilink1">'.hsc($val).'</a>';
break;
case 'timestamp':
$outs[] = dformat($val);
break;
case 'wiki':
global $ID;
$oldid = $ID;
list($ID,$data) = explode('|',$val,2);
$data = $this->_addPrePostFixes($column['type'], $data);
// Trim document_{start,end}, p_{open,close}
$ins = array_slice(p_get_instructions($data), 2, -2);
$outs[] = p_render('xhtml', $ins, $byref_ignore);
$ID = $oldid;
break;
default:
$val = $this->_addPrePostFixes($column['type'], $val);
if(substr($type,0,3) == 'img'){
$sz = (int) substr($type,3);
if(!$sz) $sz = 40;
$title = $column['key'].': '.basename(str_replace(':','/',$val));
$outs[] = '<a href="'.ml($val).'" class="media" rel="lightbox"><img src="'.ml($val,"w=$sz").'" alt="'.hsc($title).'" title="'.hsc($title).'" width="'.$sz.'" /></a>';
}else{
$outs[] = hsc($val);
}
}
}
return join(', ',$outs);
}
/**
* Split a column name into its parts
*
* @param string $col column name
* @returns array with key, type, ismulti, title, opt
*/
function _column($col){
preg_match('/^([^_]*)(?:_(.*))?((?<!s)|s)$/', $col, $matches);
$column = array(
'colname' => $col,
'multi' => ($matches[3] === 's'),
'key' => utf8_strtolower($matches[1]),
'origkey' => $matches[1], //similar to key, but stores upper case
'title' => $matches[1],
'type' => utf8_strtolower($matches[2])
);
// fix title for special columns
static $specials = array('%title%' => array('page', 'title'),
'%pageid%' => array('title', 'page'),
'%class%' => array('class'),
'%lastmod%' => array('lastmod','timestamp'));
if (isset($specials[$column['title']])) {
$s = $specials[$column['title']];
$column['title'] = $this->getLang($s[0]);
if($column['type'] === '' && isset($s[1])) {
$column['type'] = $s[1];
}
}
// check if the type is some alias
$aliases = $this->_aliases();
if(isset($aliases[$column['type']])){
$column['origtype'] = $column['type'];
$column['type'] = $aliases[$column['type']];
}
// use custom localization for keys
if(isset($this->locs[$column['key']])){
$column['title'] = $this->locs[$column['key']];
}
return $column;
}
/**
* Load defined type aliases
*/
function _aliases(){
if(!is_null($this->aliases)) return $this->aliases;
$sqlite = $this->_getDB();
if(!$sqlite) return array();
$this->aliases = array();
$res = $sqlite->query("SELECT * FROM aliases");
$rows = $sqlite->res2arr($res);
foreach($rows as $row){
$name = $row['name'];
unset($row['name']);
$this->aliases[$name] = array_filter(array_map('trim', $row));
if (!isset($this->aliases[$name]['type'])) $this->aliases[$name]['type'] = '';
}
return $this->aliases;
}
/**
* Parse a filter line into an array
*
* @param $filterline
* @return mixed - array on success, false on error
*/
function _parse_filter($filterline){
//split filterline on comparator
if(preg_match('/^(.*?)([\*=<>!~]{1,2})(.*)$/',$filterline,$matches)){
$column = $this->_column(trim($matches[1]));
$com = $matches[2];
$aliasses = array('<>' => '!=', '=!' => '!=', '~!' => '!~',
'==' => '=', '~=' => '~', '=~' => '~');
if (isset($aliasses[$com])) {
$com = $aliasses[$com];
} elseif (!preg_match('/(!?[=~])|([<>]=?)|(\*~)/', $com)) {
msg('Failed to parse comparison "'.hsc($com).'"',-1);
return false;
}
$val = trim($matches[3]);
if($com == '~~') {
$com = 'IN(';
}
if(strpos($com, '~') !== false) {
if ($com === '*~') {
$val = '*' . $val . '*';
$com = '~';
}
$val = str_replace('*','%',$val);
if ($com == '!~'){
$com = 'NOT LIKE';
} else {
$com = 'LIKE';
}
} else {
// Clean if there are no asterisks I could kill
$val = $this->_cleanData($val, $column['type']);
}
$sqlite = $this->_getDB();
if(!$sqlite) return false;
$val = $sqlite->escape_string($val); //pre escape
if($com == 'IN(') {
$val = explode(',',$val);
$val = array_map('trim', $val);
$val = implode("','", $val);
}
return array('key' => $column['key'],
'value' => $val,
'compare' => $com,
'colname' => $column['colname'],
'type' => $column['type']
);
}
msg('Failed to parse filter "'.hsc($filterline).'"',-1);
return false;
}
/**
* Replace placeholders in sql
*/
function _replacePlaceholdersInSQL(&$data){
global $USERINFO;
// allow current user name in filter:
$data['sql'] = str_replace('%user%', $_SERVER['REMOTE_USER'], $data['sql']);
$data['sql'] = str_replace('%groups%', implode("','", (array)$USERINFO['grps']), $data['sql']);
// allow current date in filter:
$data['sql'] = str_replace('%now%', dformat(null, '%Y-%m-%d'),$data['sql']);
// language filter
$data['sql'] = $this->makeTranslationReplacement($data['sql']);
}
public function makeTranslationReplacement($data) {
global $conf;
global $ID;
$patterns[] = '%lang%';
if (isset($conf['lang_before_translation'])) {
$values[] = $conf['lang_before_translation'];
} else {
$values[] = $conf['lang'];
}
// if translation plugin available, get current translation (empty for default lang)
$patterns[] = '%trans%';
/** @var helper_plugin_translation $trans */
$trans = plugin_load('helper','translation');
if($trans) {
$local = $trans->getLangPart($ID);
if ($local === '') {
$local = $conf['lang'];
}
$values[] = $local;
}
else $values[] = '';
return str_replace($patterns, $values, $data);
}
/**
* Get filters given in the request via GET or POST
*/
function _get_filters(){
$flt = array();
$filters = array();
if(!isset($_REQUEST['dataflt'])){
$flt = array();
}elseif(!is_array($_REQUEST['dataflt'])){
$flt = (array) $_REQUEST['dataflt'];
}else{
$flt = $_REQUEST['dataflt'];
}
foreach($flt as $key => $line){
// we also take the column and filtertype in the key:
if(!is_numeric($key)) $line = $key.$line;
$f = $this->_parse_filter($line);
if(is_array($f)){
$f['logic'] = 'AND';
$filters[] = $f;
}
}
return $filters;
}
/**
* prepare an array to be passed through buildURLparams()
*/
function _a2ua($name,$array){
$urlarray = array();
foreach((array) $array as $key => $val){
$urlarray[$name.'['.$key.']'] = $val;
}
return $urlarray;
}
/**
* get current URL parameters
*/
function _get_current_param($returnURLparams=true){
$cur_params = array();
if(isset($_REQUEST['dataflt'])){
$cur_params = $this->_a2ua('dataflt', $_REQUEST['dataflt']);
}
if (isset($_REQUEST['datasrt'])) {
$cur_params['datasrt'] = $_REQUEST['datasrt'];
}
if (isset($_REQUEST['dataofs'])) {
$cur_params['dataofs'] = $_REQUEST['dataofs'];
}
//combine key and value
if(!$returnURLparams){
$flat_param=array();
foreach($cur_params as $key => $val){
$flat_param[]=$key.$val;
}
$cur_params=$flat_param;
}
return $cur_params;
}
/**
* Get url parameters, remove all filters for given column and add filter for desired tag
* @param array $column
* @param string $tag
* @return array of url parameters
*/
function _getTagUrlparam($column, $tag) {
$param = array();
if(isset($_REQUEST['dataflt'])) {
$param = (array) $_REQUEST['dataflt'];
//remove all filters equal to column
foreach($param as $key => $flt) {
if(!is_numeric($key)) $flt = $key.$flt;
$filter = $this->_parse_filter($flt);
if($filter['key'] == $column['key']) {
unset($param[$key]);
}
}
}
$param[] = $column['key']."_=$tag";
$param = $this->_a2ua('dataflt', $param);
if(isset($_REQUEST['datasrt'])) {
$param['datasrt'] = $_REQUEST['datasrt'];
}
if(isset($_REQUEST['dataofs'])) {
$param['dataofs'] = $_REQUEST['dataofs'];
}
return $param;
}
private function replacePlaceholders($value) {
return $this->makeTranslationReplacement($value);
}
}