Made the profilers that output text not break js.
authorAaron Schulz <aschulz@wikimedia.org>
Tue, 11 Dec 2012 21:18:57 +0000 (13:18 -0800)
committerGerrit Code Review <gerrit@wikimedia.org>
Fri, 4 Jan 2013 21:29:24 +0000 (21:29 +0000)
Change-Id: I12377e7ac08203113e8d6650f57091a63ad1af27

includes/profiler/Profiler.php
includes/profiler/ProfilerSimpleText.php
includes/profiler/ProfilerSimpleTrace.php

index e3d22f8..baffcd8 100644 (file)
@@ -589,4 +589,18 @@ class Profiler {
                        wfDebug( $s );
                }
        }
+
+       /**
+        * Get the content type sent out to the client.
+        * Used for profilers that output instead of store data.
+        * @return string
+        */
+       protected function getContentType() {
+               foreach ( headers_list() as $header ) {
+                       if ( preg_match( '#^content-type: (\w+/\w+);?#i', $header, $m ) ) {
+                               return $m[1];
+                       }
+               }
+               return null;
+       }
 }
index 3e7d6fa..e62622f 100644 (file)
@@ -50,10 +50,18 @@ class ProfilerSimpleText extends ProfilerSimple {
                                : 0; // profiling mismatch error?
                        uasort( $this->mCollated, array('self','sort') );
                        array_walk( $this->mCollated, array('self','format'), $totalReal );
-                       if ( $this->visible ) {
-                               print '<pre>'.self::$out.'</pre>';
-                       } else {
+                       if ( php_sapi_name() === 'cli' ) {
                                print "<!--\n".self::$out."\n-->\n";
+                       } elseif ( $this->getContentType() === 'text/html' ) {
+                               if ( $this->visible ) {
+                                       print '<pre>'.self::$out.'</pre>';
+                               } else {
+                                       print "<!--\n".self::$out."\n-->\n";
+                               }
+                       } elseif ( $this->getContentType() === 'text/javascript' ) {
+                               print "\n/*\n".self::$out."*/\n";
+                       } elseif ( $this->getContentType() === 'text/css' ) {
+                               print "\n/*\n".self::$out."*/\n";
                        }
                }
        }
index 822e9fe..017e135 100644 (file)
@@ -69,6 +69,14 @@ class ProfilerSimpleTrace extends ProfilerSimple {
        }
 
        function logData() {
-               print "<!-- \n {$this->trace} \n -->";
+               if ( php_sapi_name() === 'cli' ) {
+                       print "<!-- \n {$this->trace} \n -->";
+               } elseif ( $this->getContentType() === 'text/html' ) {
+                       print "<!-- \n {$this->trace} \n -->";
+               } elseif ( $this->getContentType() === 'text/javascript' ) {
+                       print "\n/*\n {$this->trace}\n*/";
+               } elseif ( $this->getContentType() === 'text/css' ) {
+                       print "\n/*\n {$this->trace}\n*/";
+               }
        }
 }