Import: Handle uploads with sha1 starting with 0 properly
[mediawiki.git] / includes / profiler / output / ProfilerOutputDb.php
blob5efb3ab241f20006822b029285d4bd029d239ad6
1 <?php
2 /**
3 * Profiler storing information in the DB.
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.
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.
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
20 * @file
21 * @ingroup Profiler
24 /**
25 * Logs profiling data into the local DB
27 * @ingroup Profiler
28 * @since 1.25
30 class ProfilerOutputDb extends ProfilerOutput {
31 /** @var bool Whether to store host data with profiling calls */
32 private $perHost = false;
34 public function __construct( Profiler $collector, array $params ) {
35 parent::__construct( $collector, $params );
37 // Initialize per-host profiling from config, back-compat if available
38 if ( isset( $this->params['perHost'] ) ) {
39 $this->perHost = $this->params['perHost'];
43 public function canUse() {
44 # Do not log anything if database is readonly (bug 5375)
45 return !wfReadOnly();
48 public function log( array $stats ) {
49 $pfhost = $this->perHost ? wfHostname() : '';
51 try {
52 $dbw = wfGetDB( DB_MASTER );
53 $useTrx = ( $dbw->getType() === 'sqlite' ); // much faster
54 if ( $useTrx ) {
55 $dbw->startAtomic( __METHOD__ );
57 foreach ( $stats as $data ) {
58 $name = $data['name'];
59 $eventCount = $data['calls'];
60 $timeSum = (float)$data['real'];
61 $memorySum = (float)$data['memory'];
62 $name = substr( $name, 0, 255 );
64 // Kludge
65 $timeSum = $timeSum >= 0 ? $timeSum : 0;
66 $memorySum = $memorySum >= 0 ? $memorySum : 0;
68 $dbw->upsert( 'profiling',
69 array(
70 'pf_name' => $name,
71 'pf_count' => $eventCount,
72 'pf_time' => $timeSum,
73 'pf_memory' => $memorySum,
74 'pf_server' => $pfhost
76 array( array( 'pf_name', 'pf_server' ) ),
77 array(
78 "pf_count=pf_count+{$eventCount}",
79 "pf_time=pf_time+{$timeSum}",
80 "pf_memory=pf_memory+{$memorySum}",
82 __METHOD__
85 if ( $useTrx ) {
86 $dbw->endAtomic( __METHOD__ );
88 } catch ( DBError $e ) {