Changed the way SQL queries are logged by profiling
authorMr. E23 <e23@users.mediawiki.org>
Tue, 18 Nov 2003 23:43:41 +0000 (23:43 +0000)
committerMr. E23 <e23@users.mediawiki.org>
Tue, 18 Nov 2003 23:43:41 +0000 (23:43 +0000)
includes/DatabaseFunctions.php

index d753a01..753d2c8 100644 (file)
@@ -95,7 +95,11 @@ function wfEmergencyAbort( $msg = "" ) {
 function wfQuery( $sql, $db, $fname = "" )
 {
        global $wgLastDatabaseQuery, $wgOut, $wgDebugDumpSql;
-       $profName = "wfQuery(\"" . strtr( substr($sql, 0, 80 ), "\t\n\r", "   " ) . "...\")";
+
+        # wfGeneralizeSQL will probably cut down the query to reasonable
+        # logging size most of the time. The substr is really just a sanity check.
+        $profName = "wfQuery: " . substr( wfGeneralizeSQL( $sql ), 0, 255 ); 
+       
        wfProfileIn( $profName );
 
        if ( !is_numeric( $db ) ) {
@@ -191,6 +195,18 @@ function wfInvertTimestamp( $ts ) {
        );
 }
 
+# Removes most variables from an SQL query and replaces them with X or N for numbers.
+# It's only slightly flawed. Don't use for anything important.
+function wfGeneralizeSQL( $sql )
+{
+        # This could be done faster with some arrays and a single preg_replace,
+        # but this show more clearly what's going on. 
+        $sql = preg_replace ( "/-?\d+/" , "N", $sql);
+        $sql = preg_replace ( "/([\'\"]).+?[^\\\\]\\1/", "\\1X\\1", $sql);
+        $sql = preg_replace ( "/\s+/", " ", $sql);
+        return $sql;
+}
+
 function wfFieldExists( $table, $field )
 {
        $fname = "wfFieldExists";