Skip to content

Commit

Permalink
MDL-39474 introduce $CFG->debugdeveloper and cleanup $CFG init
Browse files Browse the repository at this point in the history
  • Loading branch information
skodak committed Aug 12, 2013
1 parent 50ff861 commit 96f81ea
Show file tree
Hide file tree
Showing 43 changed files with 191 additions and 184 deletions.
3 changes: 1 addition & 2 deletions admin/cli/automated_backups.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,7 @@
$DB->set_debug(true);
}
if (!empty($CFG->showcrondebugging)) {
$CFG->debug = DEBUG_DEVELOPER;
$CFG->debugdisplay = true;
set_debugging(DEBUG_DEVELOPER, true);
}

$starttime = microtime();
Expand Down
3 changes: 3 additions & 0 deletions admin/cli/install.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,9 @@
$CFG->early_install_lang = true;
$CFG->ostype = (stristr(PHP_OS, 'win') && !stristr(PHP_OS, 'darwin')) ? 'WINDOWS' : 'UNIX';
$CFG->dboptions = array();
$CFG->debug = (E_ALL | E_STRICT);
$CFG->debugdisplay = true;
$CFG->debugdeveloper = true;

$parts = explode('/', str_replace('\\', '/', dirname(dirname(__FILE__))));
$CFG->admin = array_pop($parts);
Expand Down
2 changes: 1 addition & 1 deletion admin/tool/generator/cli/generate.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
require(dirname(__FILE__) . '/../../../../config.php');
require_once(dirname(__FILE__) . '/../locallib.php');

if (!debugging('', DEBUG_DEVELOPER)) {
if (!$CFG->debugdeveloper) {
echo("This script is for developers only!!!\n");
exit(1);
}
Expand Down
2 changes: 1 addition & 1 deletion admin/tool/generator/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
error('Only for admins');
}

if (!debugging('', DEBUG_DEVELOPER)) {
if (!$CFG->debugdeveloper) {
error('This script is for developers only!!!');
}

Expand Down
6 changes: 3 additions & 3 deletions admin/tool/phpunit/webrunner.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
navigation_node::override_active_url(new moodle_url('/admin/tool/phpunit/index.php'));
admin_externalpage_setup('toolphpunitwebrunner');

if (!debugging('', DEBUG_DEVELOPER)) {
if (!$CFG->debugdeveloper) {
error('Not available on production sites, sorry.');
}

Expand Down Expand Up @@ -82,7 +82,7 @@
if ($code != 0) {
tool_phpunit_problem('Can not initialize database');
}
$CFG->debug = 0; // no pesky redirect warning, we really want to redirect
set_debugging(DEBUG_NONE, false); // Hack: no redirect warning, we really want to redirect.
redirect(new moodle_url($PAGE->url, array('execute'=>1, 'tespath'=>$testpath, 'testclass'=>$testclass, 'sesskey'=>sesskey())), 'Reloading page');
echo $OUTPUT->footer();
die();
Expand All @@ -103,7 +103,7 @@
if ($code != 0) {
tool_phpunit_problem('Can not initialize database');
}
$CFG->debug = 0; // no pesky redirect warning, we really want to redirect
set_debugging(DEBUG_NONE, false); // Hack: no redirect warning, we really want to redirect.
redirect(new moodle_url($PAGE->url, array('execute'=>1, 'tespath'=>$testpath, 'testclass'=>$testclass, 'sesskey'=>sesskey())), 'Reloading page');
die();

Expand Down
2 changes: 1 addition & 1 deletion auth/cas/cli/sync_users.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
require_once($CFG->dirroot.'/course/lib.php');

// Ensure errors are well explained
$CFG->debug = DEBUG_NORMAL;
set_debugging(DEBUG_DEVELOPER, true);

if (!is_enabled_auth('cas')) {
error_log('[AUTH CAS] '.get_string('pluginnotenabled', 'auth_ldap'));
Expand Down
2 changes: 1 addition & 1 deletion auth/ldap/cli/sync_users.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
require_once($CFG->dirroot.'/course/lib.php');

// Ensure errors are well explained
$CFG->debug = DEBUG_NORMAL;
set_debugging(DEBUG_DEVELOPER, true);

if (!is_enabled_auth('ldap')) {
error_log('[AUTH LDAP] '.get_string('pluginnotenabled', 'auth_ldap'));
Expand Down
2 changes: 1 addition & 1 deletion backup/util/factories/backup_factory.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public static function get_logger_chain($interactive, $execution, $backupid) {
global $CFG;

$dfltloglevel = backup::LOG_WARNING; // Default logging level
if (debugging('', DEBUG_DEVELOPER)) { // Debug developer raises default logging level
if ($CFG->debugdeveloper) { // Debug developer raises default logging level
$dfltloglevel = backup::LOG_DEBUG;
}

Expand Down
1 change: 0 additions & 1 deletion backup/util/factories/tests/factories_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,6 @@ function test_backup_factory() {

// Instantiate with debugging enabled and $CFG->backup_error_log_logger_level not set
$CFG->debugdisplay = true;
$CFG->debug = DEBUG_DEVELOPER;
unset($CFG->backup_error_log_logger_level);
$logger1 = backup_factory::get_logger_chain(backup::INTERACTIVE_YES, backup::EXECUTION_INMEDIATE, 'test');
$this->assertTrue($logger1 instanceof error_log_logger); // 1st logger is error_log_logger
Expand Down
9 changes: 2 additions & 7 deletions cache/tests/administration_helper_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -189,11 +189,8 @@ public function test_get_edit_store_form() {
* Test the hash_key functionality.
*/
public function test_hash_key() {
global $CFG;

$currentdebugging = $CFG->debug;

$CFG->debug = E_ALL;
$this->resetAfterTest();
set_debugging(DEBUG_ALL);

// First with simplekeys
$instance = cache_config_phpunittest::instance(true);
Expand Down Expand Up @@ -230,7 +227,5 @@ public function test_hash_key() {

$result = cache_helper::hash_key('test/test', $definition);
$this->assertEquals(sha1($definition->generate_single_key_prefix().'-test/test'), $result);

$CFG->debug = $currentdebugging;
}
}
2 changes: 1 addition & 1 deletion enrol/ldap/cli/sync.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
require_once("$CFG->libdir/clilib.php");

// Ensure errors are well explained.
$CFG->debug = DEBUG_DEVELOPER;
set_debugging(DEBUG_DEVELOPER, true);

if (!enrol_is_enabled('ldap')) {
cli_error(get_string('pluginnotenabled', 'enrol_ldap'), 2);
Expand Down
3 changes: 3 additions & 0 deletions install.php
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,9 @@
$CFG->running_installer = true;
$CFG->early_install_lang = true;
$CFG->ostype = (stristr(PHP_OS, 'win') && !stristr(PHP_OS, 'darwin')) ? 'WINDOWS' : 'UNIX';
$CFG->debug = (E_ALL | E_STRICT);
$CFG->debugdisplay = true;
$CFG->debugdeveloper = true;

// Require all needed libs
require_once($CFG->libdir.'/setuplib.php');
Expand Down
2 changes: 1 addition & 1 deletion lib/accesslib.php
Original file line number Diff line number Diff line change
Expand Up @@ -3792,7 +3792,7 @@ function get_users_by_capability(context $context, $capability, $fields = '', $s
$fields = 'u.*';
}
} else {
if (debugging('', DEBUG_DEVELOPER) && strpos($fields, 'u.*') === false && strpos($fields, 'u.id') === false) {
if ($CFG->debugdeveloper && strpos($fields, 'u.*') === false && strpos($fields, 'u.id') === false) {
debugging('u.id must be included in the list of fields passed to get_users_by_capability().', DEBUG_DEVELOPER);
}
}
Expand Down
4 changes: 3 additions & 1 deletion lib/adminlib.php
Original file line number Diff line number Diff line change
Expand Up @@ -968,6 +968,8 @@ public function prune($name) {
* @return bool True if successfully added, false if $something can not be added.
*/
public function add($parentname, $something, $beforesibling = null) {
global $CFG;

$parent = $this->locate($parentname);
if (is_null($parent)) {
debugging('parent does not exist!');
Expand All @@ -979,7 +981,7 @@ public function add($parentname, $something, $beforesibling = null) {
debugging('error - parts of tree can be inserted only into parentable parts');
return false;
}
if (debugging('', DEBUG_DEVELOPER) && !is_null($this->locate($something->name))) {
if ($CFG->debugdeveloper && !is_null($this->locate($something->name))) {
// The name of the node is already used, simply warn the developer that this should not happen.
// It is intentional to check for the debug level before performing the check.
debugging('Duplicate admin page name: ' . $something->name, DEBUG_DEVELOPER);
Expand Down
5 changes: 1 addition & 4 deletions lib/classes/component.php
Original file line number Diff line number Diff line change
Expand Up @@ -189,12 +189,9 @@ protected static function init() {
protected static function is_developer() {
global $CFG;

// Note we can not rely on $CFG->debug here because DB is not initialised yet.
if (isset($CFG->config_php_settings['debug'])) {
// Standard moodle script.
$debug = (int)$CFG->config_php_settings['debug'];
} else if (isset($CFG->debug)) {
// Usually script with ABORT_AFTER_CONFIG.
$debug = (int)$CFG->debug;
} else {
return false;
}
Expand Down
36 changes: 18 additions & 18 deletions lib/classes/event/base.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ private final function __construct() {
* @throws \coding_exception
*/
public static final function create(array $data = null) {
global $PAGE, $USER;
global $PAGE, $USER, $CFG;

$data = (array)$data;

Expand Down Expand Up @@ -178,7 +178,7 @@ public static final function create(array $data = null) {
}

// Warn developers if they do something wrong.
if (debugging('', DEBUG_DEVELOPER)) { // This should be replaced by new $CFG->slowdebug flag if introduced.
if ($CFG->debugdeveloper) {
static $automatickeys = array('eventname', 'component', 'action', 'target', 'contextlevel', 'contextinstanceid', 'timecreated');
static $initkeys = array('crud', 'level', 'objecttable');

Expand All @@ -187,10 +187,10 @@ public static final function create(array $data = null) {
continue;

} else if (in_array($key, $automatickeys)) {
debugging("Data key '$key' is not allowed in \\core\\event\\base::create() method, it is set automatically");
debugging("Data key '$key' is not allowed in \\core\\event\\base::create() method, it is set automatically", DEBUG_DEVELOPER);

} else if (in_array($key, $initkeys)) {
debugging("Data key '$key' is not allowed in \\core\\event\\base::create() method, you need to set it in init() method");
debugging("Data key '$key' is not allowed in \\core\\event\\base::create() method, you need to set it in init() method", DEBUG_DEVELOPER);

} else if (!in_array($key, self::$fields)) {
debugging("Data key '$key' does not exist in \\core\\event\\base");
Expand Down Expand Up @@ -395,7 +395,7 @@ protected function get_legacy_logdata() {
* @throws \coding_exception
*/
protected final function validate_before_trigger() {
global $DB;
global $DB, $CFG;

if (empty($this->data['crud'])) {
throw new \coding_exception('crud must be specified in init() method of each method');
Expand All @@ -407,38 +407,38 @@ protected final function validate_before_trigger() {
throw new \coding_exception('objecttable must be specified in init() method if objectid present');
}

if (debugging('', DEBUG_DEVELOPER)) { // This should be replaced by new $CFG->slowdebug flag if introduced.
if ($CFG->debugdeveloper) {
// Ideally these should be coding exceptions, but we need to skip these for performance reasons
// on production servers.

if (!in_array($this->data['crud'], array('c', 'r', 'u', 'd'), true)) {
debugging("Invalid event crud value specified.");
debugging("Invalid event crud value specified.", DEBUG_DEVELOPER);
}
if (!is_number($this->data['level'])) {
debugging('Event property level must be a number');
debugging('Event property level must be a number', DEBUG_DEVELOPER);
}
if (self::$fields !== array_keys($this->data)) {
debugging('Number of event data fields must not be changed in event classes');
debugging('Number of event data fields must not be changed in event classes', DEBUG_DEVELOPER);
}
$encoded = json_encode($this->data['other']);
if ($encoded === false or $this->data['other'] !== json_decode($encoded, true)) {
debugging('other event data must be compatible with json encoding');
debugging('other event data must be compatible with json encoding', DEBUG_DEVELOPER);
}
if ($this->data['userid'] and !is_number($this->data['userid'])) {
debugging('Event property userid must be a number');
debugging('Event property userid must be a number', DEBUG_DEVELOPER);
}
if ($this->data['courseid'] and !is_number($this->data['courseid'])) {
debugging('Event property courseid must be a number');
debugging('Event property courseid must be a number', DEBUG_DEVELOPER);
}
if ($this->data['objectid'] and !is_number($this->data['objectid'])) {
debugging('Event property objectid must be a number');
debugging('Event property objectid must be a number', DEBUG_DEVELOPER);
}
if ($this->data['relateduserid'] and !is_number($this->data['relateduserid'])) {
debugging('Event property relateduserid must be a number');
debugging('Event property relateduserid must be a number', DEBUG_DEVELOPER);
}
if ($this->data['objecttable']) {
if (!$DB->get_manager()->table_exists($this->data['objecttable'])) {
debugging('Unknown table specified in objecttable field');
debugging('Unknown table specified in objecttable field', DEBUG_DEVELOPER);
}
}
}
Expand Down Expand Up @@ -521,17 +521,17 @@ public final function is_restored() {
* @throws \coding_exception if used after ::trigger()
*/
public final function add_record_snapshot($tablename, $record) {
global $DB;
global $DB, $CFG;

if ($this->triggered) {
throw new \coding_exception('It is not possible to add snapshots after triggering of events');
}

// NOTE: this might use some kind of MUC cache,
// hopefully we will not run out of memory here...
if (debugging('', DEBUG_DEVELOPER)) { // This should be replaced by new $CFG->slowdebug flag if introduced.
if ($CFG->debugdeveloper) {
if (!$DB->get_manager()->table_exists($tablename)) {
debugging("Invalid table name '$tablename' specified, database table does not exist.");
debugging("Invalid table name '$tablename' specified, database table does not exist.", DEBUG_DEVELOPER);
}
}
$this->recordsnapshots[$tablename][$record->id] = $record;
Expand Down
3 changes: 1 addition & 2 deletions lib/cronlib.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,7 @@ function cron_run() {
$DB->set_debug(true);
}
if (!empty($CFG->showcrondebugging)) {
$CFG->debug = DEBUG_DEVELOPER;
$CFG->debugdisplay = true;
set_debugging(DEBUG_DEVELOPER, true);
}

set_time_limit(0);
Expand Down
5 changes: 3 additions & 2 deletions lib/dml/pdo_moodle_database.php
Original file line number Diff line number Diff line change
Expand Up @@ -308,16 +308,17 @@ public function get_fieldset_sql($sql, array $params=null) {
* @return array of objects, or empty array if no records were found, or false if an error occurred.
*/
public function get_records_sql($sql, array $params=null, $limitfrom=0, $limitnum=0) {
global $CFG;

$rs = $this->get_recordset_sql($sql, $params, $limitfrom, $limitnum);
if (!$rs->valid()) {
$rs->close(); // Not going to iterate (but exit), close rs
return false;
}
$objects = array();
$debugging = debugging('', DEBUG_DEVELOPER);
foreach($rs as $value) {
$key = reset($value);
if ($debugging && array_key_exists($key, $objects)) {
if ($CFG->debugdeveloper && array_key_exists($key, $objects)) {
debugging("Did you remember to make the first column something unique in your call to get_records? Duplicate value '$key' found in column first column of '$sql'.", DEBUG_DEVELOPER);
}
$objects[$key] = (object)$value;
Expand Down
12 changes: 4 additions & 8 deletions lib/dml/tests/dml_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -1475,8 +1475,6 @@ public function test_get_records_list() {
}

public function test_get_records_sql() {
global $CFG;

$DB = $this->tdb;
$dbman = $DB->get_manager();

Expand Down Expand Up @@ -1518,11 +1516,11 @@ public function test_get_records_sql() {
$records = $DB->get_records_sql("SELECT course AS id, course AS course FROM {{$tablename}}", null);
$this->assertDebuggingCalled();
$this->assertEquals(6, count($records));
$CFG->debug = DEBUG_MINIMAL;
set_debugging(DEBUG_MINIMAL);
$records = $DB->get_records_sql("SELECT course AS id, course AS course FROM {{$tablename}}", null);
$this->assertDebuggingNotCalled();
$this->assertEquals(6, count($records));
$CFG->debug = DEBUG_DEVELOPER;
set_debugging(DEBUG_DEVELOPER);

// negative limits = no limits
$records = $DB->get_records_sql("SELECT * FROM {{$tablename}} ORDER BY id", null, -1, -1);
Expand Down Expand Up @@ -1734,8 +1732,6 @@ public function test_get_record_select() {
}

public function test_get_record_sql() {
global $CFG;

$DB = $this->tdb;
$dbman = $DB->get_manager();

Expand Down Expand Up @@ -1774,10 +1770,10 @@ public function test_get_record_sql() {

$this->assertNotEmpty($DB->get_record_sql("SELECT * FROM {{$tablename}}", array(), IGNORE_MISSING));
$this->assertDebuggingCalled();
$CFG->debug = DEBUG_MINIMAL;
set_debugging(DEBUG_MINIMAL);
$this->assertNotEmpty($DB->get_record_sql("SELECT * FROM {{$tablename}}", array(), IGNORE_MISSING));
$this->assertDebuggingNotCalled();
$CFG->debug = DEBUG_DEVELOPER;
set_debugging(DEBUG_DEVELOPER);

// multiple matches ignored
$this->assertNotEmpty($DB->get_record_sql("SELECT * FROM {{$tablename}}", array(), IGNORE_MULTIPLE));
Expand Down
2 changes: 1 addition & 1 deletion lib/editor/tinymce/classes/plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ public function get_tinymce_file_url($file='', $absolute=true) {
// Version number comes from plugin version.php, except in developer
// mode where the special string 'dev' is used (prevents cacheing and
// serves unminified JS).
if (debugging('', DEBUG_DEVELOPER)) {
if ($CFG->debugdeveloper) {
$version = '-1';
} else {
$version = $this->get_version();
Expand Down
2 changes: 1 addition & 1 deletion lib/editor/tinymce/cli/update_lang_files.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

require __DIR__ . '/../../../../config.php';

if (!debugging('', DEBUG_DEVELOPER)) {
if (!$CFG->debugdeveloper) {
die('Only for developers!!!!!');
}

Expand Down
2 changes: 1 addition & 1 deletion lib/editor/tinymce/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public function head_setup() {
public function use_editor($elementid, array $options=null, $fpoptions=null) {
global $PAGE, $CFG;
// Note: use full moodle_url instance to prevent standard JS loader, make sure we are using https on profile page if required.
if (debugging('', DEBUG_DEVELOPER)) {
if ($CFG->debugdeveloper) {
$PAGE->requires->js(new moodle_url($CFG->httpswwwroot.'/lib/editor/tinymce/tiny_mce/'.$this->version.'/tiny_mce_src.js'));
} else {
$PAGE->requires->js(new moodle_url($CFG->httpswwwroot.'/lib/editor/tinymce/tiny_mce/'.$this->version.'/tiny_mce.js'));
Expand Down
Loading

0 comments on commit 96f81ea

Please sign in to comment.