-
Notifications
You must be signed in to change notification settings - Fork 7
/
helper.php
149 lines (133 loc) · 3.71 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
<?php
use dokuwiki\Extension\Event;
/**
* Class helper_plugin_confmanager
*/
class helper_plugin_confmanager extends DokuWiki_Plugin {
/**
* Get all registered config managers
*
* @return ConfigManagerConfigType[]
*/
public function getConfigFiles() {
static $configs = null;
if ($configs === null) {
$configs = [];
Event::createAndTrigger('CONFMANAGER_CONFIGFILES_REGISTER', $configs, null, false);
usort($configs, [$this, '_sortByConfigName']);
}
return $configs;
}
/**
* Get a specific config manager
*
* @param string $id Config ID
* @return ConfigManagerConfigType|false
*/
public function getConfigById($id) {
foreach ($this->getConfigFiles() as $config) {
if ($this->getConfigId($config) === $id) {
return $config;
}
}
return false;
}
/**
* Get the id of a config manager
*
* @param ConfigManagerConfigType $config
* @return string
*/
public function getConfigId(ConfigManagerConfigType $config) {
$hash = '';
$paths = $config->getPaths();
if (count($paths) == 0) return '';
$hash .= realpath($paths[0]);
return md5($hash);
}
/**
* @param string[] $files
* @return bool
*/
public function areWriteable($files) {
foreach ($files as $file) {
if (!is_writable($file)) {
return false;
}
}
return true;
}
/**
* @param string $file filename path to file
* @param string $content
*/
public function saveFile($file, $content) {
$success = io_saveFile($file, $content);
if ($success !== false ) {
msg($this->getLang('changes applied'), 1);
} elseif (!is_writable($file)) {
msg($this->getLang('error:saving failed not writable'), -1);
} else {
msg($this->getLang('error:saving failed'), -1);
}
}
/**
* Get header for config files created by confmanager
*
* @return string
*/
public function getCoreConfigHeader() {
return "# This config was generated by the confmanager plugin.\n"
. "# Changes made to this file may be overwritten by the plugin.\n"
. "# Please use the confmanager in the Dokuwiki admin interface.\n";
}
/**
* @param string $k1
* @param string $k2
* @return int
*/
public function _sortConf( $k1 , $k2 ) {
return strlen( $k2 ) - strlen( $k1 );
}
/**
* @param string $k1
* @param string $k2
* @return int
*/
public function _sortHuman( $k1 , $k2 ) {
$k1 = strtolower($k1);
$k2 = strtolower($k2);
return strnatcmp($k1,$k2);
}
/**
* Compare config managers by name
*
* @param ConfigManagerConfigType $left
* @param ConfigManagerConfigType $right
* @return int
*/
public function _sortByConfigName(ConfigManagerConfigType $left, ConfigManagerConfigType $right) {
return strnatcasecmp($left->getName(), $right->getName());
}
public function tplSaveButton() {
static $called = false;
if ($called) {
return;
}
include DOKU_PLUGIN . 'confmanager/tpl/formControls.php';
$called = true;
}
/**
* Prepare entity for saving
*
* @param string $str
* @return string
*/
public function prepareEntity($str) {
$str = trim($str);
$str = str_replace("\n", '', $str);
$str = str_replace("\r", '', $str);
$str = str_replace('#', '\\#', $str);
return $str;
}
}