Merge "Use the MWDebug class to display debug log back in the request."
authorNikerabbit <niklas.laxstrom@gmail.com>
Tue, 28 Aug 2012 05:49:41 +0000 (05:49 +0000)
committerGerrit Code Review <gerrit@wikimedia.org>
Tue, 28 Aug 2012 05:49:41 +0000 (05:49 +0000)
1  2 
includes/GlobalFunctions.php
includes/Skin.php
tests/phpunit/includes/GlobalFunctions/GlobalTest.php

@@@ -391,7 -391,7 +391,7 @@@ function wfArrayToCgi( $array1, $array
  
        $cgi = '';
        foreach ( $array1 as $key => $value ) {
 -              if ( $value !== false ) {
 +              if ( !is_null($value) && $value !== false ) {
                        if ( $cgi != '' ) {
                                $cgi .= '&';
                        }
                        } else {
                                if ( is_object( $value ) ) {
                                        $value = $value->__toString();
 -                              } elseif( !is_null( $value ) ) {
 -                                      $cgi .= urlencode( $key ) . '=' . urlencode( $value );
 -                              } else {
 -                                      $cgi .= urlencode( $key );
                                }
 +                              $cgi .= urlencode( $key ) . '=' . urlencode( $value );
                        }
                }
        }
@@@ -440,14 -443,15 +440,14 @@@ function wfCgiToArray( $query ) 
                        continue;
                }
                if ( strpos( $bit, '=' ) === false ) {
 -                      // Pieces like &qwerty become 'qwerty' => null
 -                      $key = urldecode( $bit );
 -                      $value = null;
 +                      // Pieces like &qwerty become 'qwerty' => '' (at least this is what php does)
 +                      $key = $bit;
 +                      $value = '';
                } else {
                        list( $key, $value ) = explode( '=', $bit );
 -                      $key = urldecode( $key );
 -                      $value = urldecode( $value );
                }
 -
 +              $key = urldecode( $key );
 +              $value = urldecode( $value );
                if ( strpos( $key, '[' ) !== false ) {
                        $keys = array_reverse( explode( '[', $key ) );
                        $key = array_pop( $keys );
   * Append a query string to an existing URL, which may or may not already
   * have query string parameters already. If so, they will be combined.
   *
 - * @deprecated in 1.20. Use Uri class.
   * @param $url String
   * @param $query Mixed: string or associative array
   * @return string
   */
  function wfAppendQuery( $url, $query ) {
 -      $obj = new Uri( $url );
 -      $obj->extendQuery( $query );
 -      return $obj->toString();
 +      if ( is_array( $query ) ) {
 +              $query = wfArrayToCgi( $query );
 +      }
 +      if( $query != '' ) {
 +              if( false === strpos( $url, '?' ) ) {
 +                      $url .= '?';
 +              } else {
 +                      $url .= '&';
 +              }
 +              $url .= $query;
 +      }
 +      return $url;
  }
  
  /**
@@@ -576,49 -572,13 +576,49 @@@ function wfExpandUrl( $url, $defaultPro
   * @todo Need to integrate this into wfExpandUrl (bug 32168)
   *
   * @since 1.19
 - * @deprecated
   * @param $urlParts Array URL parts, as output from wfParseUrl
   * @return string URL assembled from its component parts
   */
  function wfAssembleUrl( $urlParts ) {
 -      $obj = new Uri( $urlParts );
 -      return $obj->toString();
 +      $result = '';
 +
 +      if ( isset( $urlParts['delimiter'] ) ) {
 +              if ( isset( $urlParts['scheme'] ) ) {
 +                      $result .= $urlParts['scheme'];
 +              }
 +
 +              $result .= $urlParts['delimiter'];
 +      }
 +
 +      if ( isset( $urlParts['host'] ) ) {
 +              if ( isset( $urlParts['user'] ) ) {
 +                      $result .= $urlParts['user'];
 +                      if ( isset( $urlParts['pass'] ) ) {
 +                              $result .= ':' . $urlParts['pass'];
 +                      }
 +                      $result .= '@';
 +              }
 +
 +              $result .= $urlParts['host'];
 +
 +              if ( isset( $urlParts['port'] ) ) {
 +                      $result .= ':' . $urlParts['port'];
 +              }
 +      }
 +
 +      if ( isset( $urlParts['path'] ) ) {
 +              $result .= $urlParts['path'];
 +      }
 +
 +      if ( isset( $urlParts['query'] ) ) {
 +              $result .= '?' . $urlParts['query'];
 +      }
 +
 +      if ( isset( $urlParts['fragment'] ) ) {
 +              $result .= '#' . $urlParts['fragment'];
 +      }
 +
 +      return $result;
  }
  
  /**
@@@ -765,58 -725,13 +765,58 @@@ function wfUrlProtocolsWithoutProtRel(
   * 2) Handles protocols that don't use :// (e.g., mailto: and news: , as well as protocol-relative URLs) correctly
   * 3) Adds a "delimiter" element to the array, either '://', ':' or '//' (see (2))
   *
 - * @deprecated
   * @param $url String: a URL to parse
   * @return Array: bits of the URL in an associative array, per PHP docs
   */
  function wfParseUrl( $url ) {
 -      $obj = new Uri( $url );
 -      return $obj->getComponents();
 +      global $wgUrlProtocols; // Allow all protocols defined in DefaultSettings/LocalSettings.php
 +
 +      // Protocol-relative URLs are handled really badly by parse_url(). It's so bad that the easiest
 +      // way to handle them is to just prepend 'http:' and strip the protocol out later
 +      $wasRelative = substr( $url, 0, 2 ) == '//';
 +      if ( $wasRelative ) {
 +              $url = "http:$url";
 +      }
 +      wfSuppressWarnings();
 +      $bits = parse_url( $url );
 +      wfRestoreWarnings();
 +      // parse_url() returns an array without scheme for some invalid URLs, e.g.
 +      // parse_url("%0Ahttp://example.com") == array( 'host' => '%0Ahttp', 'path' => 'example.com' )
 +      if ( !$bits || !isset( $bits['scheme'] ) ) {
 +              return false;
 +      }
 +
 +      // most of the protocols are followed by ://, but mailto: and sometimes news: not, check for it
 +      if ( in_array( $bits['scheme'] . '://', $wgUrlProtocols ) ) {
 +              $bits['delimiter'] = '://';
 +      } elseif ( in_array( $bits['scheme'] . ':', $wgUrlProtocols ) ) {
 +              $bits['delimiter'] = ':';
 +              // parse_url detects for news: and mailto: the host part of an url as path
 +              // We have to correct this wrong detection
 +              if ( isset( $bits['path'] ) ) {
 +                      $bits['host'] = $bits['path'];
 +                      $bits['path'] = '';
 +              }
 +      } else {
 +              return false;
 +      }
 +
 +      /* Provide an empty host for eg. file:/// urls (see bug 28627) */
 +      if ( !isset( $bits['host'] ) ) {
 +              $bits['host'] = '';
 +
 +              /* parse_url loses the third / for file:///c:/ urls (but not on variants) */
 +              if ( substr( $bits['path'], 0, 1 ) !== '/' ) {
 +                      $bits['path'] = '/' . $bits['path'];
 +              }
 +      }
 +
 +      // If the URL was protocol-relative, fix scheme and delimiter
 +      if ( $wasRelative ) {
 +              $bits['scheme'] = '';
 +              $bits['delimiter'] = '//';
 +      }
 +      return $bits;
  }
  
  /**
@@@ -932,10 -847,7 +932,7 @@@ function wfMatchesDomainList( $url, $do
   * @param $logonly Bool: set true to avoid appearing in HTML when $wgDebugComments is set
   */
  function wfDebug( $text, $logonly = false ) {
-       global $wgOut, $wgDebugLogFile, $wgDebugComments, $wgProfileOnly, $wgDebugRawPage;
-       global $wgDebugLogPrefix, $wgShowDebug;
-       static $cache = array(); // Cache of unoutputted messages
+       global $wgDebugLogFile, $wgProfileOnly, $wgDebugRawPage, $wgDebugLogPrefix;
  
        if ( !$wgDebugRawPage && wfIsDebugRawPage() ) {
                return;
                $text = preg_replace( '/[^\n]/', $timer . '\0', $text, 1 );
        }
  
-       if ( ( $wgDebugComments || $wgShowDebug ) && !$logonly ) {
-               $cache[] = $text;
-               if ( isset( $wgOut ) && is_object( $wgOut ) ) {
-                       // add the message and any cached messages to the output
-                       array_map( array( $wgOut, 'debug' ), $cache );
-                       $cache = array();
-               }
+       if ( !$logonly ) {
+               MWDebug::debugMsg( $text );
        }
        if ( wfRunHooks( 'Debug', array( $text, null /* no log group */ ) ) ) {
                if ( $wgDebugLogFile != '' && !$wgProfileOnly ) {
                        # Strip unprintables; they can switch terminal modes when binary data
                        wfErrorLog( $text, $wgDebugLogFile );
                }
        }
-       MWDebug::debugMsg( $text );
  }
  
  /**
diff --combined includes/Skin.php
@@@ -437,7 -437,7 +437,7 @@@ abstract class Skin extends ContextSour
                if ( !empty( $allCats['normal'] ) ) {
                        $t = $embed . implode( "{$pop}{$embed}" , $allCats['normal'] ) . $pop;
  
 -                      $msg = $this->msg( 'pagecategories', count( $allCats['normal'] ) )->escaped();
 +                      $msg = $this->msg( 'pagecategories' )->numParams( count( $allCats['normal'] ) )->escaped();
                        $linkPage = wfMessage( 'pagecategorieslink' )->inContentLanguage()->text();
                        $s .= '<div id="mw-normal-catlinks" class="mw-normal-catlinks">' .
                                Linker::link( Title::newFromText( $linkPage ), $msg )
                        }
  
                        $s .= "<div id=\"mw-hidden-catlinks\" class=\"mw-hidden-catlinks$class\">" .
 -                              $this->msg( 'hidden-categories', count( $allCats['hidden'] ) )->escaped() .
 +                              $this->msg( 'hidden-categories' )->numParams( count( $allCats['hidden'] ) )->escaped() .
                                $colon . '<ul>' . $embed . implode( "{$pop}{$embed}" , $allCats['hidden'] ) . $pop . '</ul>' .
                                '</div>';
                }
         * @return String HTML containing debug data, if enabled (otherwise empty).
         */
        protected function generateDebugHTML() {
-               global $wgShowDebug;
-               $html = MWDebug::getDebugHTML( $this->getContext() );
-               if ( $wgShowDebug ) {
-                       $listInternals = $this->formatDebugHTML( $this->getOutput()->mDebugtext );
-                       $html .= "\n<hr />\n<strong>Debug data:</strong><ul id=\"mw-debug-html\">" .
-                               $listInternals . "</ul>\n";
-               }
-               return $html;
-       }
-       /**
-        * @param $debugText string
-        * @return string
-        */
-       private function formatDebugHTML( $debugText ) {
-               global $wgDebugTimestamps;
-               $lines = explode( "\n", $debugText );
-               $curIdent = 0;
-               $ret = '<li>';
-               foreach ( $lines as $line ) {
-                       $pre = '';
-                       if ( $wgDebugTimestamps ) {
-                               $matches = array();
-                               if ( preg_match( '/^(\d+\.\d+ {1,3}\d+.\dM\s{2})/', $line, $matches ) ) {
-                                       $pre = $matches[1];
-                                       $line = substr( $line, strlen( $pre ) );
-                               }
-                       }
-                       $display = ltrim( $line );
-                       $ident = strlen( $line ) - strlen( $display );
-                       $diff = $ident - $curIdent;
-                       $display = $pre . $display;
-                       if ( $display == '' ) {
-                               $display = "\xc2\xa0";
-                       }
-                       if ( !$ident && $diff < 0 && substr( $display, 0, 9 ) != 'Entering ' && substr( $display, 0, 8 ) != 'Exiting ' ) {
-                               $ident = $curIdent;
-                               $diff = 0;
-                               $display = '<span style="background:yellow;">' . htmlspecialchars( $display ) . '</span>';
-                       } else {
-                               $display = htmlspecialchars( $display );
-                       }
-                       if ( $diff < 0 ) {
-                               $ret .= str_repeat( "</li></ul>\n", -$diff ) . "</li><li>\n";
-                       } elseif ( $diff == 0 ) {
-                               $ret .= "</li><li>\n";
-                       } else {
-                               $ret .= str_repeat( "<ul><li>\n", $diff );
-                       }
-                       $ret .= "<tt>$display</tt>\n";
-                       $curIdent = $ident;
-               }
-               $ret .= str_repeat( '</li></ul>', $curIdent ) . '</li>';
-               return $ret;
+               return MWDebug::getHTMLDebugLog();
        }
  
        /**
@@@ -108,7 -108,7 +108,7 @@@ class GlobalTest extends MediaWikiTestC
                        array( array( 'foo' => 1 ), 'foo=1' ), // number test
                        array( array( 'foo' => true ), 'foo=1' ), // true test
                        array( array( 'foo' => false ), '' ), // false test
 -                      array( array( 'foo' => null ), 'foo' ), // null test
 +                      array( array( 'foo' => null ), '' ), // null test
                        array( array( 'foo' => 'A&B=5+6@!"\'' ), 'foo=A%26B%3D5%2B6%40%21%22%27' ), // urlencoding test
                        array( array( 'foo' => 'bar', 'baz' => 'is', 'asdf' => 'qwerty' ), 'foo=bar&baz=is&asdf=qwerty' ), // multi-item test
                        array( array( 'foo' => array( 'bar' => 'baz' ) ), 'foo%5Bbar%5D=baz' ),
        
        function testDebugFunctionTest() {
        
-               global $wgDebugLogFile, $wgOut, $wgShowDebug, $wgDebugTimestamps;
+               global $wgDebugLogFile, $wgDebugTimestamps;
                
                $old_log_file = $wgDebugLogFile;
                $wgDebugLogFile = tempnam( wfTempDir(), 'mw-' );
                wfDebug( "\00305This has böth UTF and control chars\003" );
                $this->assertEquals( " 05This has böth UTF and control chars ", file_get_contents( $wgDebugLogFile ) );
                unlink( $wgDebugLogFile );
-               
-               
-               
-               $old_wgOut = $wgOut;
-               $old_wgShowDebug = $wgShowDebug;
-               
-               $wgOut = new MockOutputPage;
-               
-               $wgShowDebug = true;
-               
-               $message = "\00305This has böth UTF and control chars\003";
-               
-               wfDebug( $message );
-               
-               if( $wgOut->message == "JAJA is a stupid error message. Anyway, here's your message: $message" ) {
-                       $this->assertTrue( true, 'MockOutputPage called, set the proper message.' );
-               }
-               else {
-                       $this->assertTrue( false, 'MockOutputPage was not called.' );
-               }
-               
-               $wgOut = $old_wgOut;
-               $wgShowDebug = $old_wgShowDebug;                
-               unlink( $wgDebugLogFile );
-               
-               
-               
                wfDebugMem();
                $this->assertGreaterThan( 5000, preg_replace( '/\D/', '', file_get_contents( $wgDebugLogFile ) ) );
                unlink( $wgDebugLogFile );