1
|
|
|
<?php // -*-php-*- |
2
|
|
|
rcs_id('$Id: SqlResult.php,v 1.7 2005/02/27 12:37:14 rurban Exp $'); |
3
|
|
|
/* |
4
|
|
|
Copyright 2004 $ThePhpWikiProgrammingTeam |
5
|
|
|
|
6
|
|
|
This file is part of PhpWiki. |
7
|
|
|
|
8
|
|
|
PhpWiki is free software; you can redistribute it and/or modify |
9
|
|
|
it under the terms of the GNU General Public License as published by |
10
|
|
|
the Free Software Foundation; either version 2 of the License, or |
11
|
|
|
(at your option) any later version. |
12
|
|
|
|
13
|
|
|
PhpWiki is distributed in the hope that it will be useful, |
14
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of |
15
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16
|
|
|
GNU General Public License for more details. |
17
|
|
|
|
18
|
|
|
You should have received a copy of the GNU General Public License |
19
|
|
|
along with PhpWiki; if not, write to the Free Software |
20
|
|
|
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
21
|
|
|
*/ |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* This plugin displays results of arbitrary SQL select statements |
25
|
|
|
* in table form. |
26
|
|
|
* The database definition, the DSN, must be defined in the local file |
27
|
|
|
* config/SqlResult.ini |
28
|
|
|
* A simple textfile with alias = dsn lines. |
29
|
|
|
* |
30
|
|
|
* Optional template file to format the result and handle some logic. |
31
|
|
|
* Template vars: %%where%%, %%sortby%%, %%limit%% |
32
|
|
|
* TODO: paging |
33
|
|
|
* |
34
|
|
|
* Usage: |
35
|
|
|
* <?plugin SqlResult alias=mysql |
36
|
|
|
* SELECT 'mysql password for string "xx":', |
37
|
|
|
* PASSWORD('xx') |
38
|
|
|
* ?> |
39
|
|
|
* <?plugin SqlResult alias=videos template=videos |
40
|
|
|
* SELECT rating,title,date |
41
|
|
|
* FROM video |
42
|
|
|
* ORDER BY rating DESC |
43
|
|
|
* LIMIT 5 |
44
|
|
|
* ?> |
45
|
|
|
* <?plugin SqlResult alias=imdb template=imdbmovies where||="Davies, Jeremy%" |
46
|
|
|
* SELECT m.title, m.date, n.name, c.role |
47
|
|
|
* FROM movies as m, names as n, jobs as j, characters as c |
48
|
|
|
* WHERE n.name LIKE "%%where%%" |
49
|
|
|
* AND m.title_id = c.title_id |
50
|
|
|
* AND n.name_id = c.name_id |
51
|
|
|
* AND c.job_id = j.job_id |
52
|
|
|
* AND j.description = 'Actor' |
53
|
|
|
* ORDER BY m.date DESC |
54
|
|
|
?> |
55
|
|
|
* |
56
|
|
|
* @author: ReiniUrban |
57
|
|
|
*/ |
58
|
|
|
|
59
|
|
|
require_once("lib/PageList.php"); |
60
|
|
|
|
61
|
|
|
class WikiPlugin_SqlResult |
62
|
|
|
extends WikiPlugin |
63
|
|
|
{ |
64
|
|
|
var $_args; |
65
|
|
|
|
66
|
|
|
function getName () { |
67
|
|
|
return _("SqlResult"); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
function getDescription () { |
71
|
|
|
return _("Display arbitrary SQL result tables"); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
function getVersion() { |
75
|
|
|
return preg_replace("/[Revision: $]/", '', |
76
|
|
|
"\$Revision: 1.7 $"); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
function getDefaultArguments() { |
80
|
|
|
return array( |
|
|
|
|
81
|
|
|
'alias' => false, // DSN database specification |
82
|
|
|
'ordered' => false, // if to display as <ol> list: single col only without template |
83
|
|
|
'template' => false, // use a custom <theme>/template.tmpl |
84
|
|
|
'where' => false, // custom filter for the query |
85
|
|
|
'sortby' => false, // for paging, default none |
86
|
|
|
'limit' => "0,50", // for paging, default: only the first 50 |
87
|
|
|
); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
function getDsn($alias) { |
91
|
|
|
$ini = parse_ini_file(FindFile("config/SqlResult.ini")); |
92
|
|
|
return $ini[$alias]; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** Get the SQL statement from the rest of the lines |
96
|
|
|
*/ |
97
|
|
|
function handle_plugin_args_cruft($argstr, $args) { |
98
|
|
|
$this->_sql = str_replace("\n"," ",$argstr); |
99
|
|
|
return; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
function run($dbi, $argstr, &$request, $basepage) { |
103
|
|
|
global $DBParams; |
104
|
|
|
//$request->setArg('nocache','1'); |
105
|
|
|
extract($this->getArgs($argstr, $request)); |
|
|
|
|
106
|
|
|
if (!$alias) |
107
|
|
|
return $this->error(_("No DSN alias for SqlResult.ini specified")); |
108
|
|
|
$sql = $this->_sql; |
109
|
|
|
|
110
|
|
|
// apply custom filters |
111
|
|
|
if ($where and strstr($sql, "%%where%%")) |
112
|
|
|
$sql = str_replace("%%where%%", $where, $sql); |
113
|
|
|
// TODO: use a SQL construction library? |
114
|
|
|
if ($limit) { |
115
|
|
|
$pagelist = new PageList(); |
116
|
|
|
$limit = $pagelist->limit($limit); |
117
|
|
|
if (strstr($sql, "%%limit%%")) |
118
|
|
|
$sql = str_replace("%%limit%%", $limit, $sql); |
119
|
|
|
else { |
120
|
|
|
if (strstr($sql, "LIMIT")) |
121
|
|
|
$sql = preg_replace("/LIMIT\s+[\d,]+\s+/m", "LIMIT ".$limit." ", $sql); |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
if (strstr($sql, "%%sortby%%")) { |
125
|
|
|
if (!$sortby) |
126
|
|
|
$sql = preg_replace("/ORDER BY .*%%sortby%%\s/m", "", $sql); |
127
|
|
|
else |
128
|
|
|
$sql = str_replace("%%sortby%%", $sortby, $sql); |
129
|
|
|
} elseif (PageList::sortby($sortby,'db')) { // add sorting: support paging sortby links |
130
|
|
|
if (preg_match("/\sORDER\s/",$sql)) |
131
|
|
|
$sql = preg_replace("/ORDER BY\s\S+\s/m", "ORDER BY " . PageList::sortby($sortby,'db'), $sql); |
132
|
|
|
else |
133
|
|
|
$sql .= " ORDER BY " . PageList::sortby($sortby,'db'); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
$inidsn = $this->getDsn($alias); |
137
|
|
|
if (!$inidsn) |
138
|
|
|
return $this->error(sprintf(_("No DSN for alias %s in SqlResult.ini found"), |
139
|
|
|
$alias)); |
140
|
|
|
// adodb or pear? adodb as default, since we distribute per default it. |
141
|
|
|
// for pear there may be overrides. |
142
|
|
|
// TODO: native PDO support (for now we use ADODB) |
143
|
|
|
if ($DBParams['dbtype'] == 'SQL') { |
144
|
|
|
$dbh = DB::connect($inidsn); |
145
|
|
|
$all = $dbh->getAll($sql); |
146
|
|
|
if (DB::isError($all)) { |
147
|
|
|
return $this->error($all->getMessage(). ' ' . $all->userinfo); |
148
|
|
|
} |
149
|
|
|
} else { // unless PearDB use the included ADODB, regardless if dba, file or PDO, ... |
150
|
|
|
if ($DBParams['dbtype'] != 'ADODB') { |
151
|
|
|
// require_once('lib/WikiDB/adodb/adodb-errorhandler.inc.php'); |
152
|
|
|
require_once('lib/WikiDB/adodb/adodb.inc.php'); |
153
|
|
|
} |
154
|
|
|
$parsed = parseDSN($inidsn); |
155
|
|
|
$dbh = &ADONewConnection($parsed['phptype']); |
156
|
|
|
$conn = $dbh->Connect($parsed['hostspec'],$parsed['username'], |
157
|
|
|
$parsed['password'], $parsed['database']); |
158
|
|
|
if (!$conn) |
159
|
|
|
return $this->error($dbh->errorMsg()); |
160
|
|
|
$GLOBALS['ADODB_FETCH_MODE'] = ADODB_FETCH_ASSOC; |
161
|
|
|
$dbh->SetFetchMode(ADODB_FETCH_ASSOC); |
162
|
|
|
|
163
|
|
|
$all = $dbh->getAll($sql); |
164
|
|
|
|
165
|
|
|
$GLOBALS['ADODB_FETCH_MODE'] = ADODB_FETCH_NUM; |
166
|
|
|
$dbh->SetFetchMode(ADODB_FETCH_NUM); |
167
|
|
|
if (!$all) |
168
|
|
|
return $this->error($dbh->errorMsg()); |
169
|
|
|
} |
170
|
|
|
$args = array(); |
171
|
|
|
if ($limit) { // fill paging vars (see PageList) |
|
|
|
|
172
|
|
|
$args = $pagelist->pagingTokens(count($all), count($all[0]), $limit); |
173
|
|
|
if (!$args) $args = array(); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
if ($template) { |
177
|
|
|
$args = array_merge( |
178
|
|
|
array('SqlResult' => $all, // the resulting array of rows |
179
|
|
|
'ordered' => $ordered, // whether to display as <ul>/<dt> or <ol> |
180
|
|
|
'where' => $where, |
181
|
|
|
'sortby' => $sortby, |
182
|
|
|
'limit' => $limit), |
183
|
|
|
$args); // paging params override given params |
184
|
|
|
return Template($template, $args); |
185
|
|
|
} else { |
186
|
|
|
if ($ordered) { |
187
|
|
|
$html = HTML::ol(array('class'=>'sqlresult')); |
188
|
|
|
if ($all) |
189
|
|
|
foreach ($all as $row) { |
190
|
|
|
$html->pushContent(HTML::li(array('class'=> $i++ % 2 ? 'evenrow' : 'oddrow'), $row[0])); |
191
|
|
|
} |
192
|
|
|
} else { |
193
|
|
|
$html = HTML::table(array('class'=>'sqlresult')); |
194
|
|
|
$i = 0; |
195
|
|
|
if ($all) |
196
|
|
|
foreach ($all as $row) { |
197
|
|
|
$tr = HTML::tr(array('class'=> $i++ % 2 ? 'evenrow' : 'oddrow')); |
198
|
|
|
if ($row) |
199
|
|
|
foreach ($row as $col) { |
200
|
|
|
$tr->pushContent(HTML::td($col)); |
201
|
|
|
} |
202
|
|
|
$html->pushContent($tr); |
203
|
|
|
} |
204
|
|
|
} |
205
|
|
|
} |
206
|
|
|
// do paging via pagelink template |
207
|
|
|
if (!empty($args['NUMPAGES'])) { |
208
|
|
|
$paging = Template("pagelink", $args); |
209
|
|
|
$html = $table->pushContent(HTML::thead($paging), |
210
|
|
|
HTML::tbody($html), |
211
|
|
|
HTML::tfoot($paging)); |
212
|
|
|
} |
213
|
|
|
if (0 and DEBUG) { // test deferred error/warning/notice collapsing |
214
|
|
|
trigger_error("test notice", E_USER_NOTICE); |
215
|
|
|
trigger_error("test warning", E_USER_WARNING); |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
return $html; |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
}; |
222
|
|
|
|
223
|
|
|
// $Log: SqlResult.php,v $ |
224
|
|
|
// Revision 1.7 2005/02/27 12:37:14 rurban |
225
|
|
|
// update comments |
226
|
|
|
// |
227
|
|
|
// Revision 1.6 2005/02/27 12:24:25 rurban |
228
|
|
|
// prevent SqlResult.ini from being imported |
229
|
|
|
// |
230
|
|
|
// Revision 1.5 2004/09/24 18:50:46 rurban |
231
|
|
|
// fix paging of SqlResult |
232
|
|
|
// |
233
|
|
|
// Revision 1.4 2004/09/17 14:23:21 rurban |
234
|
|
|
// support paging, force limit 50 |
235
|
|
|
// |
236
|
|
|
// Revision 1.3 2004/09/06 08:36:28 rurban |
237
|
|
|
// support templates, with some vars |
238
|
|
|
// |
239
|
|
|
// Revision 1.2 2004/05/03 21:57:47 rurban |
240
|
|
|
// locale updates: we previously lost some words because of wrong strings in |
241
|
|
|
// PhotoAlbum, german rewording. |
242
|
|
|
// fixed $_SESSION registering (lost session vars, esp. prefs) |
243
|
|
|
// fixed ending slash in listAvailableLanguages/Themes |
244
|
|
|
// |
245
|
|
|
// Revision 1.1 2004/05/03 20:44:58 rurban |
246
|
|
|
// fixed gettext strings |
247
|
|
|
// new SqlResult plugin |
248
|
|
|
// _WikiTranslation: fixed init_locale |
249
|
|
|
// |
250
|
|
|
|
251
|
|
|
// For emacs users |
252
|
|
|
// Local Variables: |
253
|
|
|
// mode: php |
254
|
|
|
// tab-width: 8 |
255
|
|
|
// c-basic-offset: 4 |
256
|
|
|
// c-hanging-comment-ender-p: nil |
257
|
|
|
// indent-tabs-mode: nil |
258
|
|
|
// End: |
259
|
|
|
?> |
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.