]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/DebugBackendInfo.php
function run: @return mixed
[SourceForge/phpwiki.git] / lib / plugin / DebugBackendInfo.php
1 <?php
2
3 /**
4  * Copyright 1999,2000,2001,2002,2006,2007 $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 along
19  * with PhpWiki; if not, write to the Free Software Foundation, Inc.,
20  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21  */
22
23 require_once 'lib/Template.php';
24 /**
25  */
26 class WikiPlugin_DebugBackendInfo
27     extends WikiPlugin
28 {
29     function getDescription()
30     {
31         return sprintf(_("Get debugging information for %s."), '[pagename]');
32     }
33
34     function getDefaultArguments()
35     {
36         return array('page' => '[pagename]',
37             'notallversions' => 0);
38     }
39
40     /**
41      * @param WikiDB $dbi
42      * @param string $argstr
43      * @param WikiRequest $request
44      * @param string $basepage
45      * @return mixed
46      */
47     function run($dbi, $argstr, &$request, $basepage)
48     {
49         $args = $this->getArgs($argstr, $request);
50         extract($args);
51         if (empty($userid) or $userid == $request->_user->UserName()) {
52             $user = $request->_user;
53             $userid = $user->UserName();
54         } else {
55             $user = WikiUser($userid);
56         }
57         if (!$user->isAdmin() and !(DEBUG && _DEBUG_LOGIN)) {
58             $request->_notAuthorized(WIKIAUTH_ADMIN);
59             $this->disabled("! user->isAdmin");
60         }
61         if (empty($page))
62             return $this->error("page missing");
63
64         $backend = &$dbi->_backend;
65         $this->chunk_split = true;
66         $this->readonly_pagemeta = array();
67         $this->hidden_pagemeta = array('_cached_html');
68
69         $html = HTML(HTML::h2(fmt("Querying backend directly for ā€œ%sā€",
70             $page)));
71
72         $table = HTML::table(array('class' => 'bordered'));
73         $pagedata = $backend->get_pagedata($page);
74         if (!$pagedata) {
75             // FIXME: invalid HTML
76             $html->pushContent(HTML::p(fmt("No pagedata for %s", $page)));
77         } else {
78             $this->_fixupData($pagedata);
79             $table->pushContent($this->_showhash("get_pagedata('$page')", $pagedata));
80         }
81         if (!$notallversions) {
82             $version = $backend->get_latest_version($page);
83             $vdata = $backend->get_versiondata($page, $version, true);
84             $this->_fixupData($vdata);
85             $table->pushContent(HTML::tr(HTML::td(array('colspan' => 2))));
86             $table->pushContent($this->_showhash("get_versiondata('$page',$version)",
87                 $vdata));
88         } else {
89             for ($version = $backend->get_latest_version($page);
90                  $version;
91                  $version = $backend->get_previous_version($page, $version)) {
92                 $vdata = $backend->get_versiondata($page, $version, true);
93                 $this->_fixupData($vdata);
94                 $table->pushContent(HTML::tr(HTML::td(array('colspan' => 2))));
95                 $table->pushContent($this->_showhash("get_versiondata('$page',$version)",
96                     $vdata));
97             }
98         }
99
100         $linkdata = $backend->get_links($page, false);
101         if ($linkdata->count())
102             $table->pushContent($this->_showhash("get_links('$page')", $linkdata->asArray()));
103         $relations = $backend->get_links($page, false, false, false, false, false, true);
104         if ($relations->count()) {
105             $table->pushContent($this->_showhash("get_relations('$page')", array()));
106             while ($rel = $relations->next())
107                 $table->pushContent($this->_showhash(false, $rel));
108         }
109         $linkdata = $backend->get_links($page, true);
110         if ($linkdata->count())
111             $table->pushContent($this->_showhash("get_backlinks('$page')", $linkdata->asArray()));
112
113         $html->pushContent($table);
114         return $html;
115     }
116
117     /**
118      * Really should have a _fixupPagedata and _fixupVersiondata, but this works.
119      * also used in plugin/EditMetaData
120      */
121     protected function _fixupData(&$data, $prefix = '')
122     {
123         if (!is_array($data)) return;
124
125         global $request;
126         $user = $request->getUser();
127         foreach ($data as $key => $val) {
128             $fullkey = $prefix . '[' . $key . ']';
129             if (is_integer($key)) {
130                 ;
131             } elseif ($key == 'passwd' and !$user->isAdmin()) {
132                 $data[$key] = $val ? _("<not displayed>") : _("<empty>");
133             } elseif ($key and $key == '_cached_html') {
134                 $val = TransformedText::unpack($val);
135                 ob_start();
136                 print_r($val);
137                 $data[$key] = HTML::pre(ob_get_contents());
138                 ob_end_clean();
139             } elseif (is_bool($val)) {
140                 $data[$key] = $this->_showvalue($key, $val ? "true" : "false", $prefix);
141             } elseif (is_string($val) && ((substr($val, 0, 2) == 'a:'
142                 or (substr($val, 0, 2) == 'O:')))
143             ) {
144                 // how to indent this table?
145                 $val = unserialize($val);
146                 $this->_fixupData($val, $fullkey);
147                 $data[$key] = HTML::table(array('class' => 'bordered'),
148                     $this->_showhash(false, $val, $fullkey));
149             } elseif (is_array($val)) {
150                 // how to indent this table?
151                 $this->_fixupData($val, $fullkey);
152                 $data[$key] = HTML::table(array('class' => 'bordered'),
153                     $this->_showhash(false, $val, $fullkey));
154             } elseif (is_object($val)) {
155                 // how to indent this table?
156                 ob_start();
157                 print_r($val);
158                 $val = HTML::pre(ob_get_contents());
159                 ob_end_clean();
160                 $data[$key] = HTML::table(array('class' => 'bordered'),
161                     $this->_showhash(false, $val, $fullkey));
162             } elseif ($key and $key == '%content') {
163                 if ($val === true)
164                     $val = '<true>';
165                 elseif (strlen($val) > 40)
166                     $val = substr($val, 0, 40) . " ...";
167                 $data[$key] = $val;
168             }
169         }
170         unset($data['%pagedata']); // problem in backend
171     }
172
173     /* also used in plugin/EditMetaData */
174     protected function _showhash($heading, $hash, $prefix = '')
175     {
176         $rows = array();
177         if ($heading)
178             $rows[] = HTML::tr(array(
179                     'style' => 'color:black; background-color:#ffcccc'),
180                 HTML::td(array('colspan' => 2,
181                         'style' => 'color:black'),
182                     $heading));
183         if (!is_array($hash)) return array();
184         ksort($hash);
185         foreach ($hash as $key => $val) {
186             if ($this->chunk_split and is_string($val)) $val = chunk_split($val);
187             $rows[] = HTML::tr(HTML::td(array('class' => 'align-right',
188                         'style' => 'color:black; background-color:#ccc'),
189                     HTML(HTML::raw('&nbsp;'), $key,
190                         HTML::raw('&nbsp;'))),
191                 HTML::td(array(
192                         'style' => 'color:black; background-color:white'),
193                     $this->_showvalue($key, $val, $prefix))
194             );
195         }
196         return $rows;
197     }
198
199     /* also used in plugin/EditMetaData */
200     protected function _showvalue($key, $val, $prefix = '')
201     {
202         return $val ? $val : HTML::raw('&nbsp;');
203     }
204
205 }
206
207 // Local Variables:
208 // mode: php
209 // tab-width: 8
210 // c-basic-offset: 4
211 // c-hanging-comment-ender-p: nil
212 // indent-tabs-mode: nil
213 // End: