Merge "Perform a permission check on the title when changing the page language"
[lhc/web/wiklou.git] / includes / profiler / output / ProfilerOutputDb.php
1 <?php
2 /**
3 * Profiler storing information in the DB.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @ingroup Profiler
22 */
23
24 use Wikimedia\Rdbms\DBError;
25
26 /**
27 * Logs profiling data into the local DB
28 *
29 * @ingroup Profiler
30 * @since 1.25
31 */
32 class ProfilerOutputDb extends ProfilerOutput {
33 /** @var bool Whether to store host data with profiling calls */
34 private $perHost = false;
35
36 public function __construct( Profiler $collector, array $params ) {
37 parent::__construct( $collector, $params );
38
39 // Initialize per-host profiling from config, back-compat if available
40 if ( isset( $this->params['perHost'] ) ) {
41 $this->perHost = $this->params['perHost'];
42 }
43 }
44
45 public function canUse() {
46 # Do not log anything if database is readonly (T7375)
47 return !wfReadOnly();
48 }
49
50 public function log( array $stats ) {
51 $pfhost = $this->perHost ? wfHostname() : '';
52
53 try {
54 $dbw = wfGetDB( DB_MASTER );
55 $useTrx = ( $dbw->getType() === 'sqlite' ); // much faster
56 if ( $useTrx ) {
57 $dbw->startAtomic( __METHOD__ );
58 }
59 foreach ( $stats as $data ) {
60 $name = $data['name'];
61 $eventCount = $data['calls'];
62 $timeSum = (float)$data['real'];
63 $memorySum = (float)$data['memory'];
64 $name = substr( $name, 0, 255 );
65
66 // Kludge
67 $timeSum = $timeSum >= 0 ? $timeSum : 0;
68 $memorySum = $memorySum >= 0 ? $memorySum : 0;
69
70 $dbw->upsert( 'profiling',
71 [
72 'pf_name' => $name,
73 'pf_count' => $eventCount,
74 'pf_time' => $timeSum,
75 'pf_memory' => $memorySum,
76 'pf_server' => $pfhost
77 ],
78 [ [ 'pf_name', 'pf_server' ] ],
79 [
80 "pf_count=pf_count+{$eventCount}",
81 "pf_time=pf_time+{$timeSum}",
82 "pf_memory=pf_memory+{$memorySum}",
83 ],
84 __METHOD__
85 );
86 }
87 if ( $useTrx ) {
88 $dbw->endAtomic( __METHOD__ );
89 }
90 } catch ( DBError $e ) {
91 }
92 }
93 }