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