Optionally require both username and email for password resets
[lhc/web/wiklou.git] / profileinfo.php
1 <?php
2 /**
3 * Simple interface for displaying request profile data stored in
4 * the wikis' primary database.
5 *
6 * See also https://www.mediawiki.org/wiki/Manual:Profiling.
7 *
8 * To add profiling information to the database:
9 *
10 * - set $wgProfiler['class'] in LocalSetings.php to a Profiler class other than ProfilerStub.
11 * - set $wgProfiler['output'] to 'db' to force the profiler to save its the
12 * information in the database.
13 * - apply the maintenance/archives/patch-profiling.sql patch to the database.
14 * - set $wgEnableProfileInfo to true.
15 *
16 * Copyright 2005 Kate Turner.
17 *
18 * Permission is hereby granted, free of charge, to any person obtaining a copy
19 * of this software and associated documentation files (the "Software"), to deal
20 * in the Software without restriction, including without limitation the rights
21 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
22 * copies of the Software, and to permit persons to whom the Software is
23 * furnished to do so, subject to the following conditions:
24 *
25 * The above copyright notice and this permission notice shall be included in
26 * all copies or substantial portions of the Software.
27 *
28 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
29 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
30 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
31 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
32 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
33 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
34 * SOFTWARE.
35 *
36 * @file
37 */
38
39 // This endpoint is supposed to be independent of request cookies and other
40 // details of the session. Enforce this constraint with respect to session use.
41 define( 'MW_NO_SESSION', 1 );
42
43 define( 'MW_ENTRY_POINT', 'profileinfo' );
44
45 ini_set( 'zlib.output_compression', 'off' );
46
47 require __DIR__ . '/includes/WebStart.php';
48
49 header( 'Content-Type: text/html; charset=utf-8' );
50
51 ?>
52 <!DOCTYPE html>
53 <html>
54 <head>
55 <meta charset="UTF-8" />
56 <title>Profiling data</title>
57 <style>
58 /* noc.wikimedia.org/base.css */
59
60 * {
61 margin: 0;
62 padding: 0;
63 }
64
65 body {
66 padding: 0.5em 1em;
67 background: #fff;
68 font: 14px/1.6 sans-serif;
69 color: #333;
70 }
71
72 p, ul, ol, table {
73 margin: 0.5em 0;
74 }
75
76 a {
77 color: #0645AD;
78 text-decoration: none;
79 }
80
81 a:hover {
82 text-decoration: underline;
83 }
84
85 /*!
86 * Bootstrap v2.1.1
87 *
88 * Copyright 2012 Twitter, Inc
89 * Licensed under the Apache License v2.0
90 * http://www.apache.org/licenses/LICENSE-2.0
91 *
92 * Designed and built with all the love in the world @twitter by @mdo and @fat.
93 */
94
95 table {
96 max-width: 100%;
97 background-color: transparent;
98 border-collapse: collapse;
99 border-spacing: 0;
100 }
101
102 .table {
103 width: 100%;
104 margin-bottom: 20px;
105 }
106
107 .table th,
108 .table td {
109 padding: 0.1em;
110 text-align: left;
111 vertical-align: top;
112 border-top: 1px solid #ddd;
113 }
114
115 .table th {
116 font-weight: bold;
117 }
118
119 .table thead th {
120 vertical-align: bottom;
121 }
122
123 .table thead:first-child tr:first-child th,
124 .table thead:first-child tr:first-child td {
125 border-top: 0;
126 }
127
128 .table tbody + tbody {
129 border-top: 2px solid #ddd;
130 }
131
132 .table-condensed th,
133 .table-condensed td {
134 padding: 4px 5px;
135 }
136
137 .table-striped tbody tr:nth-child(odd) td,
138 .table-striped tbody tr:nth-child(odd) th {
139 background-color: #f9f9f9;
140 }
141
142 .table-hover tbody tr:hover td,
143 .table-hover tbody tr:hover th {
144 background-color: #f5f5f5;
145 }
146
147 hr {
148 margin: 20px 0;
149 border: 0;
150 border-top: 1px solid #eee;
151 border-bottom: 1px solid #fff;
152 }
153 </style>
154 </head>
155 <body>
156 <?php
157
158 if ( !$wgEnableProfileInfo ) {
159 echo '<p>Disabled</p>'
160 . '</body></html>';
161 exit( 1 );
162 }
163
164 $dbr = wfGetDB( DB_REPLICA );
165
166 if ( !$dbr->tableExists( 'profiling' ) ) {
167 echo '<p>No <code>profiling</code> table exists, so we can\'t show you anything.</p>'
168 . '<p>If you want to log profiling data, enable <code>$wgProfiler[\'output\'] = \'db\'</code>'
169 . ' in LocalSettings.php and run <code>maintenance/update.php</code> to'
170 . ' create the profiling table.'
171 . '</body></html>';
172 exit( 1 );
173 }
174
175 $expand = [];
176 if ( isset( $_REQUEST['expand'] ) ) {
177 foreach ( explode( ',', $_REQUEST['expand'] ) as $f ) {
178 $expand[$f] = true;
179 }
180 }
181
182 // phpcs:ignore Squiz.Classes.ValidClassName.NotCamelCaps
183 class profile_point {
184
185 public $name;
186 public $count;
187 public $time;
188 public $children;
189
190 public static $totaltime, $totalmemory, $totalcount;
191
192 public function __construct( $name, $count, $time, $memory ) {
193 $this->name = $name;
194 $this->count = $count;
195 $this->time = $time;
196 $this->memory = $memory;
197 $this->children = [];
198 }
199
200 public function add_child( $child ) {
201 $this->children[] = $child;
202 }
203
204 public function display( $expand, $indent = 0.0 ) {
205 usort( $this->children, 'compare_point' );
206
207 $ex = isset( $expand[$this->name()] );
208
209 $anchor = str_replace( '"', '', $this->name() );
210
211 if ( !$ex ) {
212 if ( count( $this->children ) ) {
213 $url = getEscapedProfileUrl( false, false, $expand + [ $this->name() => true ] );
214 $extet = " <a id=\"{$anchor}\" href=\"{$url}#{$anchor}\">[+]</a>";
215 } else {
216 $extet = '';
217 }
218 } else {
219 $e = [];
220 foreach ( $expand as $name => $ep ) {
221 if ( $name != $this->name() ) {
222 $e += [ $name => $ep ];
223 }
224 }
225 $url = getEscapedProfileUrl( false, false, $e );
226 $extet = " <a id=\"{$anchor}\" href=\"{$url}#{$anchor}\">[–]</a>";
227 }
228 ?>
229 <tr>
230 <th>
231 <div style="margin-left: <?php echo (int)$indent; ?>em;">
232 <?php echo htmlspecialchars( str_replace( ',', ', ', $this->name() ) ) . $extet ?>
233 </div>
234 </th>
235 <?php // phpcs:disable Generic.Files.LineLength,Generic.PHP.NoSilencedErrors ?>
236 <td class="mw-profileinfo-timep"><?php echo @wfPercent( $this->time() / self::$totaltime * 100 ); ?></td>
237 <td class="mw-profileinfo-memoryp"><?php echo @wfPercent( $this->memory() / self::$totalmemory * 100 ); ?></td>
238 <td class="mw-profileinfo-count"><?php echo $this->count(); ?></td>
239 <td class="mw-profileinfo-cpr"><?php echo round( sprintf( '%.2f', $this->callsPerRequest() ), 2 ); ?></td>
240 <td class="mw-profileinfo-tpc"><?php echo round( sprintf( '%.2f', $this->timePerCall() ), 2 ); ?></td>
241 <td class="mw-profileinfo-mpc"><?php echo round( sprintf( '%.2f', $this->memoryPerCall() / 1024 ), 2 ); ?></td>
242 <td class="mw-profileinfo-tpr"><?php echo @round( sprintf( '%.2f', $this->time() / self::$totalcount ), 2 ); ?></td>
243 <td class="mw-profileinfo-mpr"><?php echo @round( sprintf( '%.2f', $this->memory() / self::$totalcount / 1024 ), 2 ); ?></td>
244 <?php // phpcs:enable ?>
245 </tr>
246 <?php
247 if ( $ex ) {
248 foreach ( $this->children as $child ) {
249 $child->display( $expand, $indent + 2 );
250 }
251 }
252 }
253
254 public function name() {
255 return $this->name;
256 }
257
258 public function count() {
259 return $this->count;
260 }
261
262 public function time() {
263 return $this->time;
264 }
265
266 public function memory() {
267 return $this->memory;
268 }
269
270 public function timePerCall() {
271 // phpcs:ignore Generic.PHP.NoSilencedErrors.Discouraged
272 return @( $this->time / $this->count );
273 }
274
275 public function memoryPerCall() {
276 // phpcs:ignore Generic.PHP.NoSilencedErrors.Discouraged
277 return @( $this->memory / $this->count );
278 }
279
280 public function callsPerRequest() {
281 // phpcs:ignore Generic.PHP.NoSilencedErrors.Discouraged
282 return @( $this->count / self::$totalcount );
283 }
284
285 public function timePerRequest() {
286 // phpcs:ignore Generic.PHP.NoSilencedErrors.Discouraged
287 return @( $this->time / self::$totalcount );
288 }
289
290 public function memoryPerRequest() {
291 // phpcs:ignore Generic.PHP.NoSilencedErrors.Discouraged
292 return @( $this->memory / self::$totalcount );
293 }
294
295 public function fmttime() {
296 return sprintf( '%5.02f', $this->time );
297 }
298 }
299
300 function compare_point( profile_point $a, profile_point $b ) {
301 // phpcs:ignore MediaWiki.NamingConventions.ValidGlobalName.wgPrefix
302 global $sort;
303
304 switch ( $sort ) {
305 // Sorted ascending:
306 case 'name':
307 return strcmp( $a->name(), $b->name() );
308 // Sorted descending:
309 case 'time':
310 return $b->time() <=> $a->time();
311 case 'memory':
312 return $b->memory() <=> $a->memory();
313 case 'count':
314 return $b->count() <=> $a->count();
315 case 'time_per_call':
316 return $b->timePerCall() <=> $a->timePerCall();
317 case 'memory_per_call':
318 return $b->memoryPerCall() <=> $a->memoryPerCall();
319 case 'calls_per_req':
320 return $b->callsPerRequest() <=> $a->callsPerRequest();
321 case 'time_per_req':
322 return $b->timePerRequest() <=> $a->timePerRequest();
323 case 'memory_per_req':
324 return $b->memoryPerRequest() <=> $a->memoryPerRequest();
325 }
326 }
327
328 $sorts = [ 'time', 'memory', 'count', 'calls_per_req', 'name',
329 'time_per_call', 'memory_per_call', 'time_per_req', 'memory_per_req' ];
330 $sort = 'time';
331 if ( isset( $_REQUEST['sort'] ) && in_array( $_REQUEST['sort'], $sorts ) ) {
332 $sort = $_REQUEST['sort'];
333 }
334
335 $res = $dbr->select(
336 'profiling',
337 '*',
338 [],
339 'profileinfo.php',
340 [ 'ORDER BY' => 'pf_name ASC' ]
341 );
342
343 $filter = $_REQUEST['filter'] ?? '';
344
345 ?>
346 <form method="get" action="profileinfo.php">
347 <p>
348 <input type="text" name="filter" value="<?php echo htmlspecialchars( $filter ); ?>">
349 <input type="hidden" name="sort" value="<?php echo htmlspecialchars( $sort ); ?>">
350 <input type="hidden" name="expand" value="<?php
351 echo htmlspecialchars( implode( ",", array_keys( $expand ) ) );
352 ?>">
353 <input type="submit" value="Filter">
354 </p>
355 </form>
356
357 <table class="mw-profileinfo-table table table-striped table-hover">
358 <thead>
359 <tr>
360 <th><a href="<?php
361 echo getEscapedProfileUrl( false, 'name' );
362 ?>">Name</a></th>
363 <th><a href="<?php
364 echo getEscapedProfileUrl( false, 'time' );
365 ?>">Time (%)</a></th>
366 <th><a href="<?php
367 echo getEscapedProfileUrl( false, 'memory' );
368 ?>">Memory (%)</a></th>
369 <th><a href="<?php
370 echo getEscapedProfileUrl( false, 'count' );
371 ?>">Count</a></th>
372 <th><a href="<?php
373 echo getEscapedProfileUrl( false, 'calls_per_req' );
374 ?>">Calls/req</a></th>
375 <th><a href="<?php
376 echo getEscapedProfileUrl( false, 'time_per_call' );
377 ?>">ms/call</a></th>
378 <th><a href="<?php
379 echo getEscapedProfileUrl( false, 'memory_per_call' );
380 ?>">kb/call</a></th>
381 <th><a href="<?php
382 echo getEscapedProfileUrl( false, 'time_per_req' );
383 ?>">ms/req</a></th>
384 <th><a href="<?php
385 echo getEscapedProfileUrl( false, 'memory_per_req' );
386 ?>">kb/req</a></th>
387 </tr>
388 </thead>
389 <tbody>
390 <?php
391 profile_point::$totaltime = 0.0;
392 profile_point::$totalcount = 0;
393 profile_point::$totalmemory = 0.0;
394
395 function getEscapedProfileUrl( $_filter = false, $_sort = false, $_expand = false ) {
396 // phpcs:ignore MediaWiki.NamingConventions.ValidGlobalName.wgPrefix
397 global $filter, $sort, $expand;
398
399 if ( $_expand === false ) {
400 $_expand = $expand;
401 }
402
403 return htmlspecialchars(
404 '?' .
405 wfArrayToCgi( [
406 'filter' => $_filter ?: $filter,
407 'sort' => $_sort ?: $sort,
408 'expand' => implode( ',', array_keys( $_expand ) )
409 ] )
410 );
411 }
412
413 $points = [];
414 $queries = [];
415 $sqltotal = 0.0;
416
417 /** @var profile_point|false $last */
418 $last = false;
419 foreach ( $res as $o ) {
420 $next = new profile_point( $o->pf_name, $o->pf_count, $o->pf_time, $o->pf_memory );
421 if ( $next->name() == '-total' || $next->name() == 'main()' ) {
422 profile_point::$totaltime = $next->time();
423 profile_point::$totalcount = $next->count();
424 profile_point::$totalmemory = $next->memory();
425 }
426 if ( $last !== false ) {
427 if ( preg_match( '/^' . preg_quote( $last->name(), '/' ) . '/', $next->name() ) ) {
428 $last->add_child( $next );
429 continue;
430 }
431 }
432 $last = $next;
433 if ( preg_match( '/^query: /', $next->name() ) || preg_match( '/^query-m: /', $next->name() ) ) {
434 $sqltotal += $next->time();
435 $queries[] = $next;
436 } else {
437 $points[] = $next;
438 }
439 }
440
441 $s = new profile_point( 'SQL Queries', 0, $sqltotal, 0 );
442 foreach ( $queries as $q ) {
443 $s->add_child( $q );
444 }
445 $points[] = $s;
446
447 // phpcs:ignore Generic.PHP.NoSilencedErrors.Discouraged
448 @usort( $points, 'compare_point' );
449
450 foreach ( $points as $point ) {
451 if ( strlen( $filter ) && !strstr( $point->name(), $filter ) ) {
452 continue;
453 }
454
455 $point->display( $expand );
456 }
457 ?>
458 </tbody>
459 </table>
460 <hr />
461 <p>Total time: <code><?php printf( '%5.02f', profile_point::$totaltime ); ?></code></p>
462
463 <p>Total memory: <code><?php printf( '%5.02f', profile_point::$totalmemory / 1024 ); ?></code></p>
464 <hr />
465 </body>
466 </html>