/
meta.class.php
720 lines (590 loc) · 23.2 KB
/
meta.class.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
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
<?php
/**
* properties Class and generic property classes
*
*
* @author Stefan Leutenmayr <stefan.leutenmayr@freenet.de>
* @praise Chris Smith <chris@jalakai.co.uk>
* @praise Ben Coburn <btcoburn@silicodon.net>
*/
if(!defined('CM_KEYMARKER')) define('CM_KEYMARKER','____');
if (!class_exists('properties')) {
/**
* Class properties
*/
class properties {
var $_loaded = false; // set to true after configuration files are loaded
var $_metatype = array(); // holds metadata describing the properties
/** @var property[] */
var $property = array(); // array of property objects
var $_plugin_list = null;
/**
* constructor
*
* @param string $datafile path to config metadata file
*/
public function __construct() {
$meta = array();
$this->_metatype = array_merge($meta, $this->get_metatype());
$this->retrieve_properties();
}
/**
* Retrieve and stores properties in property[] attribute
*/
public function retrieve_properties() {
global $conf;
if (!$this->_loaded) {
$metavalue = $this->get_metadata_value();
$keys = array_merge(array_keys($this->_metatype),array_keys($metavalue));
$keys = array_unique($keys);
$param = null;
foreach ($keys as $key) {
if (isset($this->_metatype[$key])) {
$class = $this->_metatype[$key][0];
if($class && class_exists('property_'.$class)){
$class = 'property_'.$class;
} else {
if($class != '') {
$this->property[$key] = new property_no_class($key,$param);
}
$class = 'property';
}
$param = $this->_metatype[$key];
array_shift($param);
} else {
$class = 'property_undefined';
$param = null;
}
$this->property[$key] = new $class($key,$param);
$this->property[$key]->initialize($metavalue[$key]);
}
$this->_loaded = true;
}
}
/**
* Returns array of plugin names
*
* @return array plugin names
*/
function get_plugin_list() {
if (is_null($this->_plugin_list)) {
$list = plugin_list('',false);
$this->_plugin_list = $list;
}
return $this->_plugin_list;
}
/**
* load metadata for page metadata
*
* @return array metadata of properties
*/
function get_metatype(){
$file = '/conf/metatypes.php';
$class = '/conf/meta.class.php';
$metatype = array();
foreach ($this->get_plugin_list() as $plugin) {
$plugin_dir = plugin_directory($plugin);
if (file_exists(DOKU_PLUGIN.$plugin_dir.$file)){
$meta = array();
@include(DOKU_PLUGIN.$plugin_dir.$file);
@include(DOKU_PLUGIN.$plugin_dir.$class);
foreach ($meta as $key => $value){
$metatype[$key] = $value;
}
}
}
return $metatype;
}
/**
* Load values of metadata
*
* @return array default properties
*/
function get_metadata_value(){
global $ID;
$metadata = array();
$meta = p_get_metadata($ID, '', METADATA_DONT_RENDER);
foreach ($meta as $key => $value){
if ($this->_metatype[$key][0] == 'fieldset') {
foreach ($value as $bKey => $bValue){
$metadata[$key.':'.$bKey] = $bValue;
}
} else {
$metadata[$key] = $value;
}
}
return $metadata;
}
}
}
if (!class_exists('property')) {
/**
* Class property
*/
class property {
var $_key = '';
var $_metavalue = null;
var $_pattern = '';
var $_error = false; // only used by those classes which error check
var $_input = null; // only used by those classes which error check
var $_caution = null; // used by any property to provide an alert along with the property
// valid alerts, 'warning', 'danger', 'security'
// images matching the alerts are in the plugin's images directory
static protected $_validCautions = array('warning','danger','security');
/**
* @param string $key
* @param array|null $params array with metadata of property
*/
public function __construct($key, $params=null) {
$this->_key = $key;
if (is_array($params)) {
foreach($params as $property => $value) {
$this->$property = $value;
}
}
}
/**
* Receives current values for the property $key
*
* @param mixed $metavalue property value
*/
public function initialize($metavalue) {
if (isset($metavalue)) $this->_metavalue = $metavalue;
}
/**
* Build html for label and input of property
*
* @param DokuWiki_Plugin $plugin object of config plugin
* @param bool $echo true: show inputted value, when error occurred, otherwise the stored property
* @return string[] with content array(string $label_html, string $input_html)
*/
public function html(&$plugin, $echo=false) {
$disable = '';
if ($echo && $this->_error) {
$value = $this->_input;
} else {
$value = $this->_metavalue;
}
$key = htmlspecialchars($this->_key);
$value = formText($value);
$label = '<label for="config___'.$key.'">'.$this->prompt($plugin).'</label>';
$input = '<textarea rows="3" cols="40" id="config___'.$key.'" name="config['.$key.']" class="edit" '.$disable.'>'.$value.'</textarea>';
return array($label,$input);
}
/**
* Returns the localized prompt
*
* @param DokuWiki_Plugin $plugin object of config plugin
* @return string text
*/
public function prompt(&$plugin) {
$prompt = $plugin->getLang($this->_key);
if (!$prompt) $prompt = htmlspecialchars(str_replace(array('____','_'),' ',$this->_key));
return $prompt;
}
/**
* Has an error?
*
* @return bool
*/
public function error() { return $this->_error; }
/**
* Returns caution
*
* @return false|string caution string, otherwise false for invalid caution
*/
public function caution() {
if (!empty($this->_caution)) {
if (!in_array($this->_caution, property::$_validCautions)) {
trigger_error('Invalid caution string ('.$this->_caution.') in metadata for property "'.$this->_key.'"', E_USER_WARNING);
return false;
}
return $this->_caution;
}
// compatibility with previous cautionList
// TODO: check if any plugins use; remove
if (!empty($this->_cautionList[$this->_key])) {
$this->_caution = $this->_cautionList[$this->_key];
unset($this->_cautionList);
return $this->caution();
}
return false;
}
/**
* Returns setting key, eventually with referer to config: namespace at dokuwiki.org
*
* @param bool $pretty create nice key
* @param bool $url provide url to config: namespace
* @return string key
*/
public function _out_key() {
return '<a href="https://www.dokuwiki.org/devel:metadata">'.$this->_key.'</a>';
}
}
}
if (!class_exists('property_array')) {
/**
* Class property_array
*/
class property_array extends property {
/**
* Create a string from an array
*
* @param array $array
* @return string
*/
protected function _from_array($array){
//return join(', ', (array) $array);
return implode(', ', array_map(function ($v, $k) { return sprintf("%s='%s'", $k, $v); },$array,array_keys($array)));
}
/**
* Recursively implodes an array with optional key inclusion
*
* Example of $include_keys output: key, value, key, value, key, value
*
* @access public
* @param array $array multi-dimensional array to recursively implode
* @param string $glue value that glues elements together
* @param bool $include_keys include keys before their values
* @param bool $trim_all trim ALL whitespace from string
* @return string imploded array
*/
function recursive_implode(array $array, $glue = ',', $keyglue = '=', $include_keys = false, $trim_all = true)
{
$glued_string = '';
// Recursively iterates array and adds key/value to glued string
array_walk_recursive($array, function($value, $key) use ($glue, $keyglue, $include_keys, &$glued_string)
{
$include_keys and $glued_string .= $key.$keyglue;
$glued_string .= $value.$glue;
});
// Removes last $glue from string
strlen($glue) > 0 and $glued_string = substr($glued_string, 0, -strlen($glue));
// Trim ALL whitespace
$trim_all and $glued_string = preg_replace("/(\s)/ixsm", '', $glued_string);
return (string) $glued_string;
}
/**
* Build html for label and input of property
*
* @param DokuWiki_Plugin $plugin object of config plugin
* @param bool $echo true: show inputted value, when error occurred, otherwise the stored property
* @return string[] with content array(string $label_html, string $input_html)
*/
function html(&$plugin, $echo=false) {
if ($echo && $this->_error) {
$value = $this->_input;
} else {
$array = $this->_metavalue;
}
if (is_array($array) == true){
$value = $this->recursive_implode($array, '; ', '=',true, false);
} else {
$value = $array;
}
$key = htmlspecialchars($this->_key);
$value = htmlspecialchars($value);
$label = '<label for="config___'.$key.'">'.$this->prompt($plugin).'</label>';
$input = '<input id="config___'.$key.'" name="config['.$key.']" type="text" class="edit" value="'.$value.'" '.$disable.'/>';
return array($label,$input);
}
}
}
if (!class_exists('property_string')) {
/**
* Class property_string
*/
class property_string extends property {
/**
* Build html for label and input of property
*
* @param DokuWiki_Plugin $plugin object of config plugin
* @param bool $echo true: show inputted value, when error occurred, otherwise the stored property
* @return string[] with content array(string $label_html, string $input_html)
*/
function html(&$plugin, $echo=false) {
if ($echo && $this->_error) {
$value = $this->_input;
} else {
$value = $this->_metavalue;
}
$key = htmlspecialchars($this->_key);
$value = htmlspecialchars($value);
$label = '<label for="config___'.$key.'">'.$this->prompt($plugin).'</label>';
$input = '<input id="config___'.$key.'" name="config['.$key.']" type="text" class="edit" value="'.$value.'" '.$disable.'/>';
return array($label,$input);
}
}
}
if (!class_exists('property_timestamp')) {
/**
* Class property_string
*/
class property_timestamp extends property {
/**
* Build html for label and input of property
*
* @param DokuWiki_Plugin $plugin object of config plugin
* @param bool $echo true: show inputted value, when error occurred, otherwise the stored property
* @return string[] with content array(string $label_html, string $input_html)
*/
function html(&$plugin, $echo=false) {
if ($echo && $this->_error) {
$value = $this->_input;
} else {
if (is_numeric($this->_metavalue)){
$value = date('d.m.y', intval($this->_metavalue));
} else {
$value = $this->_metavalue;
}
}
$key = htmlspecialchars($this->_key);
$value = htmlspecialchars($value);
$label = '<label for="config___'.$key.'">'.$this->prompt($plugin).'</label>';
$input = '<input id="config___'.$key.'" name="config['.$key.']" type="text" class="edit" value="'.$value.'" '.$disable.'/>';
return array($label,$input);
}
}
}
if (!class_exists('property_email')) {
/**
* Class property_email
*/
class property_email extends property_string {
}
}
if (!class_exists('property_numeric')) {
/**
* Class property_numeric
*/
class property_numeric extends property_string {
}
}
if (!class_exists('property_numericopt')) {
/**
* Class property_numericopt
*/
class property_numericopt extends property_numeric {
}
}
if (!class_exists('property_onoff')) {
/**
* Class property_onoff
*/
class property_onoff extends property_numeric {
/**
* Build html for label and input of property
*
* @param DokuWiki_Plugin $plugin object of config plugin
* @param bool $echo true: show inputted value, when error occurred, otherwise the stored property
* @return string[] with content array(string $label_html, string $input_html)
*/
function html(&$plugin, $echo = false) {
$value = $this->_metavalue;
$key = htmlspecialchars($this->_key);
$checked = ($value) ? ' checked="checked"' : '';
$label = '<label for="config___'.$key.'">'.$this->prompt($plugin).'</label>';
$input = '<div class="input"><input id="config___'.$key.'" name="config['.$key.']" type="checkbox" class="checkbox" value="1"'.$checked.$disable.'/></div>';
return array($label,$input);
}
}
}
if (!class_exists('property_multichoice')) {
/**
* Class property_multichoice
*/
class property_multichoice extends property_string {
var $_choices = array();
var $lang; //some custom language strings are stored in property
/**
* Build html for label and input of property
*
* @param DokuWiki_Plugin $plugin object of config plugin
* @param bool $echo true: show inputted value, when error occurred, otherwise the stored property
* @return string[] with content array(string $label_html, string $input_html)
*/
function html(&$plugin, $echo = false) {
$value = $this->_metavalue;
// ensure current value is included
if (!in_array($value, $this->_choices)) {
$this->_choices[] = $value;
}
$key = htmlspecialchars($this->_key);
$label = '<label for="config___'.$key.'">'.$this->prompt($plugin).'</label>';
$input = "<div class=\"input\">\n";
$input .= '<select class="edit" id="config___'.$key.'" name="config['.$key.']"'.$disable.'>'."\n";
foreach ($this->_choices as $choice) {
$selected = ($value == $choice) ? ' selected="selected"' : '';
$option = $plugin->getLang($this->_key.'_o_'.$choice);
if (!$option && isset($this->lang[$this->_key.'_o_'.$choice])) $option = $this->lang[$this->_key.'_o_'.$choice];
if (!$option) $option = $choice;
$choice = htmlspecialchars($choice);
$option = htmlspecialchars($option);
$input .= ' <option value="'.$choice.'"'.$selected.' >'.$option.'</option>'."\n";
}
$input .= "</select> $nochoice \n";
$input .= "</div>\n";
return array($label,$input);
}
}
}
if (!class_exists('property_dirchoice')) {
/**
* Class property_dirchoice
*/
class property_dirchoice extends property_multichoice {
var $_dir = '';
/**
* Receives current values for the property $key
*
* @param mixed $metavalue default property value
* @param mixed $local local property value
* @param mixed $protected protected property value
*/
function initialize($default,$local,$protected) {
// populate $this->_choices with a list of directories
$list = array();
if ($dh = @opendir($this->_dir)) {
while (false !== ($entry = readdir($dh))) {
if ($entry == '.' || $entry == '..') continue;
if ($this->_pattern && !preg_match($this->_pattern,$entry)) continue;
$file = (is_link($this->_dir.$entry)) ? readlink($this->_dir.$entry) : $this->_dir.$entry;
if (is_dir($file)) $list[] = $entry;
}
closedir($dh);
}
sort($list);
$this->_choices = $list;
parent::initialize($default,$local,$protected);
}
}
}
if (!class_exists('property_hidden')) {
/**
* Class property_hidden
*/
class property_hidden extends property {
// Used to explicitly ignore a property in the configuration manager.
}
}
if (!class_exists('property_fieldset')) {
/**
* Class property_fieldset
*/
class property_fieldset extends property {
// A do-nothing class used to detect the 'fieldset' type.
// Used to start a new properties "display-group".
}
}
if (!class_exists('property_undefined')) {
/**
* Class property_undefined
*/
class property_undefined extends property_hidden {
// A do-nothing class used to detect properties with no metadata entry.
// Used internaly to hide undefined properties, and generate the undefined properties list.
}
}
if (!class_exists('property_no_class')) {
/**
* Class property_no_class
*/
class property_no_class extends property_undefined {
// A do-nothing class used to detect properties with a missing property class.
// Used internaly to hide undefined properties, and generate the undefined properties list.
}
}
if (!class_exists('property_no_default')) {
/**
* Class property_no_default
*/
class property_no_default extends property_undefined {
// A do-nothing class used to detect properties with no default value.
// Used internaly to hide undefined properties, and generate the undefined properties list.
}
}
if (!class_exists('property_multicheckbox')) {
/**
* Class property_multicheckbox
*/
class property_multicheckbox extends property_string {
var $_choices = array();
var $_combine = array();
/**
* Build html for label and input of property
*
* @param DokuWiki_Plugin $plugin object of config plugin
* @param bool $echo true: show inputted value, when error occurred, otherwise the stored property
* @return string[] with content array(string $label_html, string $input_html)
*/
function html(&$plugin, $echo=false) {
if ($echo && $this->_error) {
$value = $this->_input;
} else {
$value = $this->_metavalue;
}
$key = htmlspecialchars($this->_key);
// convert from comma separated list into array + combine complimentary actions
$value = $this->_str2array($value);
$metavalue = $this->_str2array($this->_metavalue);
$input = '';
foreach ($this->_choices as $choice) {
$idx = array_search($choice, $value);
$idx_default = array_search($choice,$default);
$checked = ($idx !== false) ? 'checked="checked"' : '';
// ideally this would be handled using a second class of "default", however IE6 does not
// correctly support CSS selectors referencing multiple class names on the same element
// (e.g. .default.selection).
$class = (($idx !== false) == (false !== $idx_default)) ? " selectiondefault" : "";
$prompt = ($plugin->getLang($this->_key.'_'.$choice) ?
$plugin->getLang($this->_key.'_'.$choice) : htmlspecialchars($choice));
$input .= '<div class="selection'.$class.'">'."\n";
$input .= '<label for="config___'.$key.'_'.$choice.'">'.$prompt."</label>\n";
$input .= '<input id="config___'.$key.'_'.$choice.'" name="config['.$key.'][]" type="checkbox" class="checkbox" value="'.$choice.'" '.$disable.' '.$checked."/>\n";
$input .= "</div>\n";
// remove this action from the disabledactions array
if ($idx !== false) unset($value[$idx]);
if ($idx_default !== false) unset($default[$idx_default]);
}
// handle any remaining values
$other = join(',',$value);
$class = ((count($default) == count($value)) && (count($value) == count(array_intersect($value,$default)))) ?
" selectiondefault" : "";
$input .= '<div class="other'.$class.'">'."\n";
$input .= '<label for="config___'.$key.'_other">'.$plugin->getLang($key.'_other')."</label>\n";
$input .= '<input id="config___'.$key.'_other" name="config['.$key.'][other]" type="text" class="edit" value="'.htmlspecialchars($other).'" '.$disable." />\n";
$input .= "</div>\n";
$label = '<label>'.$this->prompt($plugin).'</label>';
return array($label,$input);
}
/**
* convert comma separated list to an array and combine any complimentary values
*
* @param string $str
* @return array
*/
function _str2array($str) {
$array = explode(',',$str);
if (!empty($this->_combine)) {
foreach ($this->_combine as $key => $combinators) {
$idx = array();
foreach ($combinators as $val) {
if (($idx[] = array_search($val, $array)) === false) break;
}
if (count($idx) && $idx[count($idx)-1] !== false) {
foreach ($idx as $i) unset($array[$i]);
$array[] = $key;
}
}
}
return $array;
}
}
}
if (!class_exists('property_regex')){
/**
* Class property_regex
*/
class property_regex extends property_string {
}
}