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
|
<?php
/**
* Plugin Lib
*
* A port of PhpWiki WikiPlugin class
* Principal use is port PhpWiki plugins, but can be used to make new ones.
* Use:
* - Extends PluginsLib with your class
* - add the lines
* <code>
* include "pluginslib.php";
*
* function wikiplugin_backlinks($data, $params) {
* $plugin = new BackLinks();
* return $plugin->run($data, $params);
* }
* function wikiplugin_backlinks_help() {
* $plugin = new BackLinks();
* return $plugin->getDescription();
* } * </code>
* @package wiki
*/
/**
* @package wiki
* @subpackage PluginsLib
* @author Claudio Bustos
* @version $Revision$
*/
namespace Bitweaver\Wiki;
use Bitweaver\KernelTools;
class PluginsLib extends \Bitweaver\BitBase {
public $_errors;
public $_data;
public $_params;
/**
* Array of params to be expanded as arrays. Explode the string with {@link $separator}
* @var array
*/
public $expanded_params = [];
/**
* Separator used to explote params listed on {@link $expanded_params}
* @var string
*/
public $separator = "|";
/**
* List of fields retrieved from {@link BitBase::list_pages()}
* Keys are the name of the fields and values the names for tra();
* @var array
*/
public $aInfoPresetNames = [
"hits" => "Hits", "last_modified" => "Last mod", "user" => "Last author", "len" => "Size", "comment" => "Com", "creator" => "Creator", "version" => "Last ver", "flag" => "Status", "versions" => "Vers", "links" => "Links", "backlinks" => "Backlinks", ];
/**
* Process the params, in this order:
* - default values, asigned on {@link PluginsLib::getDefaultArguments()}
* - request values, sended by GET or POST method, if $request is put to true
* - explicit values, asigned on the Wiki
* @param array sended to wikiplugin_name($data, $params)
* @param bool if set to true, accept values from $_REQUEST
* @param bool if set to true, assign default values from {@link PluginsLib::getDefaultArguments()}
* @return array list of params
*/
function getParams($params, $request = false, $defaults = false) {
if ($defaults === false) {
$defaults = $this->getDefaultArguments();
}
$args = [];
foreach ($defaults as $arg => $default_val) {
if (isset($params[$arg])) {
$args[$arg] = $params[$arg];
} elseif(isset($_REQUEST[$arg])) {
$args[$arg] = $_REQUEST[$arg];
} else {
// maybe this kind of transformation can be grouped on a external function
if ($default_val==="[pagename]") {
$default_val=$_REQUEST["page"];
}
$args[$arg] = $default_val;
}
if (in_array($arg, $this->expanded_params)) {
if ($args[$arg]) {
$args[$arg] = explode($this->separator, $args[$arg]);
foreach($args[$arg] as $id=>$value) {
$args[$arg][$id]=trim($value);
}
} else {
$args[$arg]=[];
}
}
}
return $args;
}
/**
* Returns the name of the Plugin
* By default, erase the first 'WikiPlugin'
* Made for overload it.
* @return string
*/
function getName() {
return preg_replace('/^WikiPlugin/', '', get_class($this));
}
/**
* Returns a description of the Plugin
* Made for overload it.
* @return string
*/
function getDescription() {
return $this->getName();
}
/**
* Returns the version of the version
* Made for overload it.
* @return string
*/
function getVersion() {
return KernelTools::tra("No version indicated");
//return preg_replace("/[Revision: $]/", '',
// "\$Revision$");
}
/**
* Returns the default arguments for the plugin
* Use keys as the arguments and values as ... the default values
* @return array
*/
function getDefaultArguments() {
return ['description' => $this->getDescription()];
}
/**
* Run the plugin
* For sake of God, overload it!
* @param string
* @param array
*/
function run ($data, $params) {
/**
* UGLY ERROR!.
*/
return $this->error("PluginsLib::run: pure virtual function. Don't be so lazy!");
}
function error ($message) {
return "~np~<span class='warn'>Plugin ".$this->getName()." ".KernelTools::tra("failed")." : ".KernelTools::tra($message)."</span>~/np~";
}
function getErrorDetail() {
return $this->_errors;
}
function _error($message) {
$this->_errors = $message;
return false;
}
}
/**
* Class with utilities for Plugins
*
* @package wiki
* @subpackage PluginsLib
* @author Claudio Bustos
* @version $Revision$
*/
class PluginsLibUtil {
/**
* Create a table with information from pages
* @param array key ["data"] from one of the functions that retrieve informaci�n about pages
* @param array list of keys to show.
* @param array definition of the principal field. By default:
* array("field"=>"title","name"=>"Page")
* @return string
*/
public function createTable($aData,$aInfo=false,$aPrincipalField=false) {
// contract
if (!$aPrincipalField or !is_array($aPrincipalField)) {
$aPrincipalField=["field"=>"title","name"=>"Page"];
}
if (!is_array($aInfo)) {
$aInfo=false;
}
// ~contract
$sOutput="";
if ($aInfo) {
$iNumCol=count($aInfo)+1;
$sStyle=" style='width:".(floor(100/$iNumCol))."%' ";
// Header for info
$sOutput .= "<table class='normal'><tr><td class='heading' $sStyle>".KernelTools::tra($aPrincipalField["name"])."</td>";
foreach($aInfo as $iInfo => $sHeader) {
$sOutput .= "<td class='heading' $sStyle >".KernelTools::tra($sHeader)."</td>";
}
$sOutput .= "</tr>";
}
$iCounter=1;
foreach($aData as $aPage) {
$sClass=($iCounter%2)?"odd":"even";
if (!$aInfo) {
$sOutput .= "*((".$aPage[$aPrincipalField["field"]]."))\n";
} else {
$sOutput .= "<tr><td class='$sClass'>((".$aPage[$aPrincipalField["field"]]."))</td>";
foreach($aInfo as $sInfo) {
if (isset($aPage[$sInfo])) {
$sOutput .= "<td class='$sClass'>".$aPage[$sInfo]."</td>";
}
}
}
$iCounter++;
}
if ($aInfo) {
$sOutput .= "</table>";
}
return $sOutput;
}
}
|