Difference between revisions of "Sven/PHP"
m (uncat) |
(Change source-code blocks to standard format) |
||
Line 1: | Line 1: | ||
==PHP errors== | ==PHP errors== | ||
To see PHP errors, add this to the very top of <tt>[[MW:Manual:LocalSettings.php|LocalSettings.php]]</tt>: | To see PHP errors, add this to the very top of <tt>[[MW:Manual:LocalSettings.php|LocalSettings.php]]</tt>: | ||
− | + | <source lang="php"> | |
− | <php> | ||
error_reporting(E_ALL); | error_reporting(E_ALL); | ||
ini_set("display_errors", 1); | ini_set("display_errors", 1); | ||
− | </ | + | </source> |
− | |||
Line 16: | Line 14: | ||
Debuggers generally use a [[W:Stack trace]]. This can be approximated by using [http://www.php.net/print print] and [http://www.php.net/die die] statements in sections of code. The combination of [http://www.php.net/print_r print_r] and [http://www.php.net/func_get_args func_get_args] allows you to return an array of args that a function was called with; | Debuggers generally use a [[W:Stack trace]]. This can be approximated by using [http://www.php.net/print print] and [http://www.php.net/die die] statements in sections of code. The combination of [http://www.php.net/print_r print_r] and [http://www.php.net/func_get_args func_get_args] allows you to return an array of args that a function was called with; | ||
− | + | <source lang="php"> | |
− | <php> | ||
print_r(func_get_args()); | print_r(func_get_args()); | ||
− | </ | + | </source> |
− | |||
Casting an object of an array builds an array of the properties, mapping property names to values. e.g. | Casting an object of an array builds an array of the properties, mapping property names to values. e.g. | ||
− | + | <source lang="php"> | |
− | <php> | + | class Person { |
− | |||
var $name = "Fred"; | var $name = "Fred"; | ||
var $age = 35; | var $age = 35; | ||
Line 32: | Line 27: | ||
$a = (array) $o | $a = (array) $o | ||
print_r($a); | print_r($a); | ||
− | </ | + | </source> |
− | |||
outputs; | outputs; | ||
− | + | <source lang="php"> | |
− | <php> | ||
Array | Array | ||
( | ( | ||
Line 44: | Line 37: | ||
[age] => 35 | [age] => 35 | ||
) | ) | ||
− | </ | + | </source> |
− | |||
=== Logging information to file === | === Logging information to file === | ||
From [[Sven/Notes]], [[Extension:Fasta.php]]; | From [[Sven/Notes]], [[Extension:Fasta.php]]; | ||
− | + | <source lang="php"> | |
− | <php> | ||
# Logging to an article from within the MW environment | # Logging to an article from within the MW environment | ||
function logAdd($article,$msg) { | function logAdd($article,$msg) { | ||
Line 67: | Line 58: | ||
# @file_put_contents($wgPayPalLog,"$ts $text\n",FILE_APPEND); | # @file_put_contents($wgPayPalLog,"$ts $text\n",FILE_APPEND); | ||
} | } | ||
− | </ | + | </source> |
− | |||
==Introspection== | ==Introspection== | ||
Line 77: | Line 67: | ||
*[http://www.php.net/class_exists class_exists] | *[http://www.php.net/class_exists class_exists] | ||
− | + | <source lang="php"> | |
− | <php> | ||
# Example: | # Example: | ||
$classes = get_declared_classes(); | $classes = get_declared_classes(); | ||
Line 103: | Line 92: | ||
return(); | return(); | ||
} | } | ||
− | </ | + | </source> |
− | |||
==MediaWiki profiler== | ==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; | 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; | ||
− | + | <source lang="php"> | |
− | <php> | ||
/** | /** | ||
* The debug log file should be not be publicly accessible if it is used, as it | * The debug log file should be not be publicly accessible if it is used, as it | ||
Line 146: | Line 133: | ||
/** Lots of debugging output from SquidUpdate.php */ | /** Lots of debugging output from SquidUpdate.php */ | ||
$wgDebugSquid = false; | $wgDebugSquid = false; | ||
− | </ | + | </source> |
− | |||
However if you include in an extension; | However if you include in an extension; | ||
− | + | <source lang="php"> | |
− | <php> | ||
include("$IP/includes/GlobalFunctions.php"); | include("$IP/includes/GlobalFunctions.php"); | ||
wfDebug("This is just testing the debug tracing stuff\n"); | wfDebug("This is just testing the debug tracing stuff\n"); | ||
− | </ | + | </source> |
− | |||
there seems to be no output in the generated log file. | there seems to be no output in the generated log file. | ||
==Globals== | ==Globals== | ||
− | + | <source lang="php"> | |
− | <php> | ||
$wgOut | $wgOut | ||
OutputPage object for HTTP response. | OutputPage object for HTTP response. | ||
Line 186: | Line 169: | ||
$wgLoadBalancer | $wgLoadBalancer | ||
A LoadBalancer object, manages database connections. | A LoadBalancer object, manages database connections. | ||
− | </ | + | </source> |
− | |||
==See also== | ==See also== |
Latest revision as of 18:10, 22 May 2015
Contents
PHP errors
To see PHP errors, add this to the very top of LocalSettings.php:
error_reporting(E_ALL);
ini_set("display_errors", 1);
Debugging PHP
The idea is to debug sections of computer code which are under executional focus, this is considerably easier in an OOP paradigm than a procedural one which uses encapsulation. The class provies certain methods and properties to the code that uses the objects, so outside code does not directly access the data structures of those objects, they do so through the interface.
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
)
Logging information to file
From Sven/Notes, Extension:Fasta.php;
# 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");
}
# 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");
# $ts = date("Y-m-d H:i:s");
# @file_put_contents($wgPayPalLog,"$ts $text\n",FILE_APPEND);
}
Introspection
# Example:
$classes = get_declared_classes();
foreach($classes as $list) {
print ("$list <br />");
}
</php>
;PHP Help doumentation
*[http://www.php.net/get_declared_classes get_declared_classes]
*[http://www.php.net/get_class_methods get_class_methods]
*[http://www.php.net/get_class_vars get_class_vars]
*[http://www.php.net/get_parent_class get_parent_class]
<php>
function printArray(&$array) {
if (isset($wgCommandLineMode)) {
$rc = "\n";
} else {
$rc ="<br />";
}
foreach($array as $key => $value) {
print "array: key => $value$rc";
}
return();
}
MediaWiki profiler
The article how to debug MediaWiki provides information on setting up the profiler by adding the globals to your LocalSettings.php file;
/**
* 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;
However if you include in an extension;
include("$IP/includes/GlobalFunctions.php");
wfDebug("This is just testing the debug tracing stuff\n");
there seems to be no output in the generated log file.
Globals
$wgOut
OutputPage object for HTTP response.
$wgUser
User object for the user associated with the current
request.
$wgTitle
Title object created from the request URL.
$wgLang
Language object selected by user preferences
$wgContLang
Language object associated with the wiki being
viewed.
$wgArticle
Article object corresponding to $wgTitle.
$wgParser
Parser object. Parser extensions register their
hooks here.
$wgLoadBalancer
A LoadBalancer object, manages database connections.
See also
- Debugging
- PHP Documentation
- Classes and Objects (PHP 5)
- OOP documentation
- Introspection:Class/Object Functions
- Scope Resolution Operator (::)
- Advanced php debugger
- MediaWiki Documentation
- Deoxygen MediaWiki Documentation
- MW:Manual:Coding_conventions
- MW:Manual:Extensions
- MW:Manual:$wgExtensionFunctions
- Extensions
- Extension:PayPal.php - currently contains debugging
- Online Resources