Difference between revisions of "Sven/PHP"

From Organic Design wiki
(See also)
(wfDebug(); for MediaWiki profiler doesn't seem to work)
Line 58: Line 58:
 
ini_set("display_errors", 1);
 
ini_set("display_errors", 1);
 
</php>
 
</php>
 +
 +
==MediaWiki profiler==
 +
The article [[MW:How to debug|how to debug] MediaWiki provides information on setting up the profiler by adding the globals to your LocalSettings.php file;
 +
 +
<php>
 +
/**
 +
* The debug log file should be not be publicly accessible if it is used, as it
 +
* may contain private data.  But it must be in a directory to which PHP run
 +
* within your Web server can write. */
 +
$wgDebugLogFile  = '/tmp/MW_log.txt';
 +
 +
#
 +
# Profiling / debugging
 +
#
 +
 +
/** Enable for more detailed by-function times in debug log */
 +
$wgProfiling    = true;
 +
/** Only record profiling info for pages that took longer than this */
 +
$wgProfileLimit = 0.0;
 +
/** Don't put non-profiling info into log file */
 +
$wgProfileOnly  = false;
 +
/** Log sums from profiling into "profiling" table in db. */
 +
$wgProfileToDatabase = false;
 +
/** Only profile every n requests when profiling is turned on */
 +
$wgProfileSampleRate = 1;
 +
/** If true, print a raw call tree instead of per-function report */
 +
$wgProfileCallTree = false;
 +
/** If not empty, specifies profiler type to load */
 +
$wgProfilerType = '';
 +
 +
/** Settings for UDP profiler */
 +
$wgUDPProfilerHost = '127.0.0.1';
 +
$wgUDPProfilerPort = '3811';
 +
 +
/** Detects non-matching wfProfileIn/wfProfileOut calls */
 +
$wgDebugProfiling = true;
 +
/** Output debug message on every wfProfileIn/wfProfileOut */
 +
$wgDebugFunctionEntry = 1;
 +
/** Lots of debugging output from SquidUpdate.php */
 +
$wgDebugSquid = false;
 +
</php>
 +
However if you include in an extension;
 +
<php
 +
include("$IP/includes/GlobalFunctions.php");
 +
wfDebug("This is just testing the debug tracing stuff\n");
 +
</php>
 +
there seems to be no output in the generated log file.
  
 
==See also==
 
==See also==

Revision as of 03:37, 10 December 2007

Debugging PHP

Info.svg In MediaWiki there is a profiler that can be set, see MW:How to debug.


Debuggers generally use a W:Stack trace. This can be approximated by using print and die statements in sections of code. The combination of print_r and func_get_args allows you to return an array of args that a function was called with;

print_r(func_get_args());

Casting an object of an array builds an array of the properties, mapping property names to values. e.g.

class Person {
 var $name = "Fred";
 var $age  = 35;
}
$o = new Person;
$a = (array) $o
print_r($a);

outputs;

Array
(
[name] => Fred
[age] => 35
)


Info.svg See pages 147-153 (Programming php) for introspection functions for Objects in php.


From Sven/Notes, Extension:Fasta.php; <php>

  1. Logging to an article from within the MW environment

function logAdd($article,$msg) { $ts = $GLOBALS['wgLang']->timeanddate(wfTimestampNow(),true); $la = new Article(Title::newFromText($article)); $la->quickEdit($la->getContent()."\n*$ts: $msg"); }

  1. Logging to a file

function logFile($file, $msg) { $fh = fopen($file,'a'); $ts = $GLOBALS['wgLang']->timeanddate(wfTimestampNow(),true); if(is_array($msg)) $msg = print_r($msg, true); fwrite($fh, "\n$ts: $msg"); } </php>


PHP errors

To see PHP errors, add this to the very top of LocalSettings.php: <php> error_reporting(E_ALL); ini_set("display_errors", 1); </php>

MediaWiki profiler

The article [[MW:How to debug|how to debug] MediaWiki provides information on setting up the profiler by adding the globals to your LocalSettings.php file;

<php> /**

* The debug log file should be not be publicly accessible if it is used, as it
* may contain private data.  But it must be in a directory to which PHP run
* within your Web server can write. */

$wgDebugLogFile = '/tmp/MW_log.txt';

  1. Profiling / debugging

/** Enable for more detailed by-function times in debug log */ $wgProfiling = true; /** Only record profiling info for pages that took longer than this */ $wgProfileLimit = 0.0; /** Don't put non-profiling info into log file */ $wgProfileOnly = false; /** Log sums from profiling into "profiling" table in db. */ $wgProfileToDatabase = false; /** Only profile every n requests when profiling is turned on */ $wgProfileSampleRate = 1; /** If true, print a raw call tree instead of per-function report */ $wgProfileCallTree = false; /** If not empty, specifies profiler type to load */ $wgProfilerType = ;

/** Settings for UDP profiler */ $wgUDPProfilerHost = '127.0.0.1'; $wgUDPProfilerPort = '3811';

/** Detects non-matching wfProfileIn/wfProfileOut calls */ $wgDebugProfiling = true; /** Output debug message on every wfProfileIn/wfProfileOut */ $wgDebugFunctionEntry = 1; /** Lots of debugging output from SquidUpdate.php */ $wgDebugSquid = false; </php> However if you include in an extension; <php include("$IP/includes/GlobalFunctions.php"); wfDebug("This is just testing the debug tracing stuff\n"); </php> there seems to be no output in the generated log file.

See also