* Use double quoted consistently on attributes in linker output; preparing for new...
[mediawiki.git] / profileinfo.php
blobc7305b3515e7e5665ebce6c80cae813e9b873b14
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("MEDIAWIKI", 1);
50 if ( isset( $_REQUEST['GLOBALS'] ) ) {
51 print $GLOBALS;
52 echo '<a href="http://www.hardened-php.net/index.76.html">$GLOBALS overwrite vulnerability</a>';
53 die( -1 );
56 require_once("./includes/Defines.php");
57 require_once("./LocalSettings.php");
58 require_once("./AdminSettings.php");
60 if (!$wgEnableProfileInfo) {
61 wfDie("disabled");
64 foreach (array("wgDBadminuser", "wgDBadminpassword", "wgDBserver", "wgDBname") as $var)
65 if ($$var === false)
66 wfDie("AdminSettings.php not correct");
68 $expand = array();
69 if (isset($_REQUEST['expand']))
70 foreach(explode(",", $_REQUEST['expand']) as $f)
71 $expand[$f] = true;
73 class profile_point {
74 var $name;
75 var $count;
76 var $time;
77 var $children;
79 function profile_point($name, $count, $time) {
80 $this->name = $name;
81 $this->count = $count;
82 $this->time = $time;
83 $this->children = array();
86 function add_child($child) {
87 $this->children[] = $child;
90 function display($indent = 0.0) {
91 global $expand;
92 usort($this->children, "compare_point");
94 $extet = '';
95 if (isset($expand[$this->name()]))
96 $ex = true;
97 else $ex = false;
98 if (!$ex) {
99 if (count($this->children)) {
100 $url = makeurl(false, false, $expand + array($this->name() => true));
101 $extet = " <a href=\"$url\">[+]</a>";
102 } else $extet = '';
103 } else {
104 $e = array();
105 foreach ($expand as $name => $ep)
106 if ($name != $this->name())
107 $e += array($name => $ep);
109 $extet = " <a href=\"" . makeurl(false, false, $e) . "\">[&ndash;]</a>";
112 <tr>
113 <td class="time"><tt><?php echo $this->fmttime() ?></tt></td>
114 <td class="count"><?php echo $this->count() ?></td>
115 <td class="name" style="padding-left: <?php echo $indent ?>em">
116 <?php echo htmlspecialchars($this->name()) . $extet ?>
117 </td>
118 </tr>
119 <?php
120 if ($ex)
121 foreach ($this->children as $child)
122 $child->display($indent + 2);
125 function name() {
126 return $this->name;
129 function count() {
130 return $this->count;
133 function time() {
134 return $this->time;
137 function fmttime() {
138 return sprintf("%5.02f", $this->time);
142 function compare_point($a, $b) {
143 global $sort;
144 switch ($sort) {
145 case "name":
146 return strcmp($a->name(), $b->name());
147 case "time":
148 return $a->time() > $b->time() ? -1 : 1;
149 case "count":
150 return $a->count() > $b->count() ? -1 : 1;
154 $sorts = array("time", "count", "name");
155 $sort = 'time';
156 if (isset($_REQUEST['sort']) && in_array($_REQUEST['sort'], $sorts))
157 $sort = $_REQUEST['sort'];
159 $dbh = mysql_connect($wgDBserver, $wgDBadminuser, $wgDBadminpassword)
160 or wfDie("mysql server failed: " . mysql_error());
161 mysql_select_db($wgDBname, $dbh) or wfDie(mysql_error($dbh));
162 $res = mysql_query("
163 SELECT pf_count, pf_time, pf_name
164 FROM profiling
165 ORDER BY pf_name ASC
166 ", $dbh) or wfDie("query failed: " . mysql_error());
168 if (isset($_REQUEST['filter']))
169 $filter = $_REQUEST['filter'];
170 else $filter = '';
173 <form method="profiling.php">
175 <input type="text" name="filter" value="<?php echo htmlspecialchars($filter)?>"/>
176 <input type="hidden" name="sort" value="<?php echo htmlspecialchars($sort)?>"/>
177 <input type="hidden" name="expand" value="<?php echo htmlspecialchars(implode(",", array_keys($expand)))?>"/>
178 <input type="submit" value="Filter" />
179 </p>
180 </form>
182 <table cellspacing="0">
183 <tr id="top">
184 <th><a href="<?php echo makeurl(false, "time") ?>">Time</a></th>
185 <th><a href="<?php echo makeurl(false, "count") ?>">Count</a></th>
186 <th><a href="<?php echo makeurl(false, "name") ?>">Name</a></th>
187 </tr>
188 <?php
189 $totaltime = 0.0;
191 function makeurl($_filter = false, $_sort = false, $_expand = false) {
192 global $filter, $sort, $expand;
194 if ($_expand === false)
195 $_expand = $expand;
197 $nfilter = $_filter ? $_filter : $filter;
198 $nsort = $_sort ? $_sort : $sort;
199 $exp = urlencode(implode(',', array_keys($_expand)));
200 return "?filter=$nfilter&amp;sort=$nsort&amp;expand=$exp";
203 $points = array();
204 $queries = array();
205 $sqltotal = 0.0;
207 $last = false;
208 while (($o = mysql_fetch_object($res)) !== false) {
209 $next = new profile_point($o->pf_name, $o->pf_count, $o->pf_time);
210 $totaltime += $next->time();
211 if ($last !== false) {
212 if (preg_match("/^".preg_quote($last->name(), "/")."/", $next->name())) {
213 $last->add_child($next);
214 continue;
217 $last = $next;
218 if (preg_match("/^query: /", $next->name())) {
219 $sqltotal += $next->time();
220 $queries[] = $next;
221 } else {
222 $points[] = $next;
226 $s = new profile_point("SQL Queries", 0, $sqltotal);
227 foreach ($queries as $q)
228 $s->add_child($q);
229 $points[] = $s;
231 usort($points, "compare_point");
233 foreach ($points as $point) {
234 if (strlen($filter) && !strstr($point->name(), $filter))
235 continue;
237 $point->display();
240 </table>
242 <p>Total time: <tt><?php printf("%5.02f", $totaltime) ?></p>
243 <?php
245 mysql_free_result($res);
246 mysql_close($dbh);
249 </body>
250 </html>