Merge "Hide 'redirectedfrom' notice when printing articles"
[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 /**
25 * Logs profiling data into the local DB
26 *
27 * @ingroup Profiler
28 * @since 1.25
29 */
30 class ProfilerOutputDb extends ProfilerOutput {
31 public function canUse() {
32 # Do not log anything if database is readonly (bug 5375)
33 return !wfReadOnly();
34 }
35
36 public function log( array $stats ) {
37 global $wgProfilePerHost;
38
39 if ( $wgProfilePerHost ) {
40 $pfhost = wfHostname();
41 } else {
42 $pfhost = '';
43 }
44
45 try {
46 $dbw = wfGetDB( DB_MASTER );
47 $useTrx = ( $dbw->getType() === 'sqlite' ); // much faster
48 if ( $useTrx ) {
49 $dbw->startAtomic( __METHOD__ );
50 }
51 foreach ( $stats as $data ) {
52 $name = $data['name'];
53 $eventCount = $data['calls'];
54 $timeSum = (float)$data['real'];
55 $memorySum = (float)$data['memory'];
56 $name = substr( $name, 0, 255 );
57
58 // Kludge
59 $timeSum = $timeSum >= 0 ? $timeSum : 0;
60 $memorySum = $memorySum >= 0 ? $memorySum : 0;
61
62 $dbw->upsert( 'profiling',
63 array(
64 'pf_name' => $name,
65 'pf_count' => $eventCount,
66 'pf_time' => $timeSum,
67 'pf_memory' => $memorySum,
68 'pf_server' => $pfhost
69 ),
70 array( array( 'pf_name', 'pf_server' ) ),
71 array(
72 "pf_count=pf_count+{$eventCount}",
73 "pf_time=pf_time+{$timeSum}",
74 "pf_memory=pf_memory+{$memorySum}",
75 ),
76 __METHOD__
77 );
78 }
79 if ( $useTrx ) {
80 $dbw->endAtomic( __METHOD__ );
81 }
82 } catch ( DBError $e ) {
83 }
84 }
85 }