Most of index.php now in Wiki.php
[lhc/web/wiklou.git] / profileinfo.php
1 <!--
2 Show profiling data.
3
4 Copyright 2005 Kate Turner.
5
6 Permission is hereby granted, free of charge, to any person obtaining a copy
7 of this software and associated documentation files (the "Software"), to deal
8 in the Software without restriction, including without limitation the rights
9 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 copies of the Software, and to permit persons to whom the Software is
11 furnished to do so, subject to the following conditions:
12
13 The above copyright notice and this permission notice shall be included in
14 all copies or substantial portions of the Software.
15
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 SOFTWARE.
23
24 -->
25 <html>
26 <head>
27 <title>Profiling data</title>
28 <style type="text/css">
29 th {
30 text-align: left;
31 border-bottom: solid 1px black;
32 }
33
34 th, td {
35 padding-left: 0.5em;
36 padding-right: 0.5em;
37 }
38
39 td.time, td.count {
40 text-align: right;
41 }
42 </style>
43 </head>
44 <body>
45 <?php
46
47 $wgDBadminuser = $wgDBadminpassword = $wgDBserver = $wgDBname = $wgEnableProfileInfo = false;
48
49 define("MEDIAWIKI", 1);
50 if ( isset( $_REQUEST['GLOBALS'] ) ) {
51 print $GLOBALS;
52 die( '<a href="http://www.hardened-php.net/index.76.html">$GLOBALS overwrite vulnerability</a>');
53 }
54
55 require_once("./includes/Defines.php");
56 require_once("./LocalSettings.php");
57 require_once("./AdminSettings.php");
58
59 if (!$wgEnableProfileInfo)
60 die("disabled");
61
62 foreach (array("wgDBadminuser", "wgDBadminpassword", "wgDBserver", "wgDBname") as $var)
63 if ($$var === false)
64 die("AdminSettings.php not correct");
65
66 $expand = array();
67 if (isset($_REQUEST['expand']))
68 foreach(explode(",", $_REQUEST['expand']) as $f)
69 $expand[$f] = true;
70
71 class profile_point {
72 var $name;
73 var $count;
74 var $time;
75 var $children;
76
77 function profile_point($name, $count, $time) {
78 $this->name = $name;
79 $this->count = $count;
80 $this->time = $time;
81 $this->children = array();
82 }
83
84 function add_child($child) {
85 $this->children[] = $child;
86 }
87
88 function display($indent = 0.0) {
89 global $expand;
90 usort($this->children, "compare_point");
91
92 $extet = '';
93 if (isset($expand[$this->name()]))
94 $ex = true;
95 else $ex = false;
96 if (!$ex) {
97 if (count($this->children)) {
98 $url = makeurl(false, false, $expand + array($this->name() => true));
99 $extet = " <a href=\"$url\">[+]</a>";
100 } else $extet = '';
101 } else {
102 $e = array();
103 foreach ($expand as $name => $ep)
104 if ($name != $this->name())
105 $e += array($name => $ep);
106
107 $extet = " <a href=\"" . makeurl(false, false, $e) . "\">[&ndash;]</a>";
108 }
109 ?>
110 <tr>
111 <td class="time"><tt><?php echo $this->fmttime() ?></tt></td>
112 <td class="count"><?php echo $this->count() ?></td>
113 <td class="name" style="padding-left: <?php echo $indent ?>em">
114 <?php echo htmlspecialchars($this->name()) . $extet ?>
115 </td>
116 </tr>
117 <?php
118 if ($ex)
119 foreach ($this->children as $child)
120 $child->display($indent + 2);
121 }
122
123 function name() {
124 return $this->name;
125 }
126
127 function count() {
128 return $this->count;
129 }
130
131 function time() {
132 return $this->time;
133 }
134
135 function fmttime() {
136 return sprintf("%5.02f", $this->time);
137 }
138 };
139
140 function compare_point($a, $b) {
141 global $sort;
142 switch ($sort) {
143 case "name":
144 return strcmp($a->name(), $b->name());
145 case "time":
146 return $a->time() > $b->time() ? -1 : 1;
147 case "count":
148 return $a->count() > $b->count() ? -1 : 1;
149 }
150 }
151
152 $sorts = array("time", "count", "name");
153 $sort = 'time';
154 if (isset($_REQUEST['sort']) && in_array($_REQUEST['sort'], $sorts))
155 $sort = $_REQUEST['sort'];
156
157 $dbh = mysql_connect($wgDBserver, $wgDBadminuser, $wgDBadminpassword)
158 or die("mysql server failed: " . mysql_error());
159 mysql_select_db($wgDBname, $dbh) or die(mysql_error($dbh));
160 $res = mysql_query("
161 SELECT pf_count, pf_time, pf_name
162 FROM profiling
163 ORDER BY pf_name ASC
164 ", $dbh) or die("query failed: " . mysql_error());
165
166 if (isset($_REQUEST['filter']))
167 $filter = $_REQUEST['filter'];
168 else $filter = '';
169
170 ?>
171 <form method="profiling.php">
172 <p>
173 <input type="text" name="filter" value="<?php echo htmlspecialchars($filter)?>"/>
174 <input type="hidden" name="sort" value="<?php echo htmlspecialchars($sort)?>"/>
175 <input type="hidden" name="expand" value="<?php echo htmlspecialchars(implode(",", array_keys($expand)))?>"/>
176 <input type="submit" value="Filter" />
177 </p>
178 </form>
179
180 <table cellspacing="0">
181 <tr id="top">
182 <th><a href="<?php echo makeurl(false, "time") ?>">Time</a></th>
183 <th><a href="<?php echo makeurl(false, "count") ?>">Count</a></th>
184 <th><a href="<?php echo makeurl(false, "name") ?>">Name</a></th>
185 </tr>
186 <?php
187 $totaltime = 0.0;
188
189 function makeurl($_filter = false, $_sort = false, $_expand = false) {
190 global $filter, $sort, $expand;
191
192 if ($_expand === false)
193 $_expand = $expand;
194
195 $nfilter = $_filter ? $_filter : $filter;
196 $nsort = $_sort ? $_sort : $sort;
197 $exp = urlencode(implode(',', array_keys($_expand)));
198 return "?filter=$nfilter&amp;sort=$nsort&amp;expand=$exp";
199 }
200
201 $points = array();
202 $queries = array();
203 $sqltotal = 0.0;
204
205 $last = false;
206 while (($o = mysql_fetch_object($res)) !== false) {
207 $next = new profile_point($o->pf_name, $o->pf_count, $o->pf_time);
208 $totaltime += $next->time();
209 if ($last !== false) {
210 if (preg_match("/^".preg_quote($last->name(), "/")."/", $next->name())) {
211 $last->add_child($next);
212 continue;
213 }
214 }
215 $last = $next;
216 if (preg_match("/^query: /", $next->name())) {
217 $sqltotal += $next->time();
218 $queries[] = $next;
219 } else {
220 $points[] = $next;
221 }
222 }
223
224 $s = new profile_point("SQL Queries", 0, $sqltotal);
225 foreach ($queries as $q)
226 $s->add_child($q);
227 $points[] = $s;
228
229 usort($points, "compare_point");
230
231 foreach ($points as $point) {
232 if (strlen($filter) && !strstr($point->name(), $filter))
233 continue;
234
235 $point->display();
236 }
237 ?>
238 </table>
239
240 <p>Total time: <tt><?php printf("%5.02f", $totaltime) ?></p>
241 <?php
242
243 mysql_free_result($res);
244 mysql_close($dbh);
245
246 ?>
247 </body>
248 </html>