MediaWiki 1.40.4
GeneralizedSql.php
Go to the documentation of this file.
1<?php
20namespace Wikimedia\Rdbms;
21
31 private $rawSql;
33 private $prefix;
34
36 private $genericSql;
37
42 public function __construct( $rawSql, $prefix ) {
43 $this->rawSql = $rawSql;
44 $this->prefix = $prefix;
45 }
46
55 private static function generalizeSQL( $sql ) {
56 # This does the same as the regexp below would do, but in such a way
57 # as to avoid crashing php on some large strings.
58 # $sql = preg_replace( "/'([^\\\\']|\\\\.)*'|\"([^\\\\\"]|\\\\.)*\"/", "'X'", $sql );
59
60 $sql = str_replace( "\\\\", '', $sql );
61 $sql = str_replace( "\\'", '', $sql );
62 $sql = str_replace( "\\\"", '', $sql );
63 $sql = preg_replace( "/'.*'/s", "'X'", $sql );
64 $sql = preg_replace( '/".*"/s', "'X'", $sql );
65
66 # All newlines, tabs, etc replaced by single space
67 $sql = preg_replace( '/\s+/', ' ', $sql );
68
69 # All numbers => N,
70 # except the ones surrounded by characters, e.g. l10n
71 $sql = preg_replace( '/-?\d+(,-?\d+)+/s', 'N,...,N', $sql );
72 $sql = preg_replace( '/(?<![a-zA-Z])-?\d+(?![a-zA-Z])/s', 'N', $sql );
73
74 return $sql;
75 }
76
80 public function stringify() {
81 if ( $this->genericSql !== null ) {
82 return $this->genericSql;
83 }
84
85 $this->genericSql = $this->prefix .
86 substr( self::generalizeSQL( $this->rawSql ), 0, 255 );
87
88 return $this->genericSql;
89 }
90
91 public function getRawSql() {
92 return $this->rawSql;
93 }
94}
Lazy-loaded wrapper for simplification and scrubbing of SQL queries for profiling.