fixing html escape of page title in <h1> tag (bug 6986)
[mediawiki.git] / profileinfo.php
blob388e0cdc096089b28b404161d355008808b7a8ce
1 <!--
2 Show profiling data.
4 Copyright 2005 Kate Turner.
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:
13 The above copyright notice and this permission notice shall be included in
14 all copies or substantial portions of the Software.
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.
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;
34 th, td {
35 padding-left: 0.5em;
36 padding-right: 0.5em;
39 td.time, td.count {
40 text-align: right;
42 </style>
43 </head>
44 <body>
45 <?php
47 $wgDBadminuser = $wgDBadminpassword = $wgDBserver = $wgDBname = $wgEnableProfileInfo = false;
49 define( 'MW_NO_SETUP', 1 );
50 require_once( './includes/WebStart.php' );
51 require_once("./AdminSettings.php");
53 if (!$wgEnableProfileInfo) {
54 wfDie("disabled");
57 foreach (array("wgDBadminuser", "wgDBadminpassword", "wgDBserver", "wgDBname") as $var)
58 if ($$var === false)
59 wfDie("AdminSettings.php not correct");
61 $expand = array();
62 if (isset($_REQUEST['expand']))
63 foreach(explode(",", $_REQUEST['expand']) as $f)
64 $expand[$f] = true;
66 class profile_point {
67 var $name;
68 var $count;
69 var $time;
70 var $children;
72 function profile_point($name, $count, $time) {
73 $this->name = $name;
74 $this->count = $count;
75 $this->time = $time;
76 $this->children = array();
79 function add_child($child) {
80 $this->children[] = $child;
83 function display($indent = 0.0) {
84 global $expand;
85 usort($this->children, "compare_point");
87 $extet = '';
88 if (isset($expand[$this->name()]))
89 $ex = true;
90 else $ex = false;
91 if (!$ex) {
92 if (count($this->children)) {
93 $url = makeurl(false, false, $expand + array($this->name() => true));
94 $extet = " <a href=\"$url\">[+]</a>";
95 } else $extet = '';
96 } else {
97 $e = array();
98 foreach ($expand as $name => $ep)
99 if ($name != $this->name())
100 $e += array($name => $ep);
102 $extet = " <a href=\"" . makeurl(false, false, $e) . "\">[&ndash;]</a>";
105 <tr>
106 <td class="time"><tt><?php echo $this->fmttime() ?></tt></td>
107 <td class="count"><?php echo $this->count() ?></td>
108 <td class="name" style="padding-left: <?php echo $indent ?>em">
109 <?php echo htmlspecialchars($this->name()) . $extet ?>
110 </td>
111 </tr>
112 <?php
113 if ($ex)
114 foreach ($this->children as $child)
115 $child->display($indent + 2);
118 function name() {
119 return $this->name;
122 function count() {
123 return $this->count;
126 function time() {
127 return $this->time;
130 function fmttime() {
131 return sprintf("%5.02f", $this->time);
135 function compare_point($a, $b) {
136 global $sort;
137 switch ($sort) {
138 case "name":
139 return strcmp($a->name(), $b->name());
140 case "time":
141 return $a->time() > $b->time() ? -1 : 1;
142 case "count":
143 return $a->count() > $b->count() ? -1 : 1;
147 $sorts = array("time", "count", "name");
148 $sort = 'time';
149 if (isset($_REQUEST['sort']) && in_array($_REQUEST['sort'], $sorts))
150 $sort = $_REQUEST['sort'];
152 $dbh = mysql_connect($wgDBserver, $wgDBadminuser, $wgDBadminpassword)
153 or wfDie("mysql server failed: " . mysql_error());
154 mysql_select_db($wgDBname, $dbh) or wfDie(mysql_error($dbh));
155 $res = mysql_query("
156 SELECT pf_count, pf_time, pf_name
157 FROM profiling
158 ORDER BY pf_name ASC
159 ", $dbh) or wfDie("query failed: " . mysql_error());
161 if (isset($_REQUEST['filter']))
162 $filter = $_REQUEST['filter'];
163 else $filter = '';
166 <form method="profiling.php">
168 <input type="text" name="filter" value="<?php echo htmlspecialchars($filter)?>"/>
169 <input type="hidden" name="sort" value="<?php echo htmlspecialchars($sort)?>"/>
170 <input type="hidden" name="expand" value="<?php echo htmlspecialchars(implode(",", array_keys($expand)))?>"/>
171 <input type="submit" value="Filter" />
172 </p>
173 </form>
175 <table cellspacing="0">
176 <tr id="top">
177 <th><a href="<?php echo makeurl(false, "time") ?>">Time</a></th>
178 <th><a href="<?php echo makeurl(false, "count") ?>">Count</a></th>
179 <th><a href="<?php echo makeurl(false, "name") ?>">Name</a></th>
180 </tr>
181 <?php
182 $totaltime = 0.0;
184 function makeurl($_filter = false, $_sort = false, $_expand = false) {
185 global $filter, $sort, $expand;
187 if ($_expand === false)
188 $_expand = $expand;
190 $nfilter = $_filter ? $_filter : $filter;
191 $nsort = $_sort ? $_sort : $sort;
192 $exp = urlencode(implode(',', array_keys($_expand)));
193 return "?filter=$nfilter&amp;sort=$nsort&amp;expand=$exp";
196 $points = array();
197 $queries = array();
198 $sqltotal = 0.0;
200 $last = false;
201 while (($o = mysql_fetch_object($res)) !== false) {
202 $next = new profile_point($o->pf_name, $o->pf_count, $o->pf_time);
203 $totaltime += $next->time();
204 if ($last !== false) {
205 if (preg_match("/^".preg_quote($last->name(), "/")."/", $next->name())) {
206 $last->add_child($next);
207 continue;
210 $last = $next;
211 if (preg_match("/^query: /", $next->name())) {
212 $sqltotal += $next->time();
213 $queries[] = $next;
214 } else {
215 $points[] = $next;
219 $s = new profile_point("SQL Queries", 0, $sqltotal);
220 foreach ($queries as $q)
221 $s->add_child($q);
222 $points[] = $s;
224 usort($points, "compare_point");
226 foreach ($points as $point) {
227 if (strlen($filter) && !strstr($point->name(), $filter))
228 continue;
230 $point->display();
233 </table>
235 <p>Total time: <tt><?php printf("%5.02f", $totaltime) ?></p>
236 <?php
238 mysql_free_result($res);
239 mysql_close($dbh);
242 </body>
243 </html>