All configuration variables in Moodle code – Part two

Save on DeliciousDigg This
Share on Facebook+1Share on LinkedInPin it on PinterestSubmit to redditSubmit to StumbleUponShare on TumblrShare on Twitter

Earlier we looked at how you can extract a list of all the $CFG variables in your Moodle code. Now that’s not of much use! We need to know where in the code all these variables hide. I feel some spooky awk a coming on.

Please note this doesn’t show all the variables available in Moodle, it shows all the Moodle variables in a particular codebase. Below output is shown from one of my own codebases which is a little different to what you might expect to see in MOODLE_19_STABLEIn All configuration variables in Moodle code we came up with:

grep -r –no-filename -o ‘\$CFG->[a-z][a-z]*’ <path_to_moodle> | sort -u

This gives us some useful information but hides a lot of the detail. Let’s try and include which files the variable appears in even the line numbers. For this we’ll go back to:

grep -r -no ‘\$CFG->[a-z][a-z]*’

This will print line numbers for matches. Hmmm, what happens if two or more matches are found on one line? Well the second var will be printed without the file and line number — silly badness, see below!

./admin/auth.php:11:$CFG->libdir
./admin/auth.php:12:$CFG->libdir
./admin/auth.php:17:$CFG->wwwroot
$CFG->admin
./admin/auth.php:26:$CFG->auth
./admin/auth.php:29:$CFG->auth
./admin/auth.php:52:$CFG->registerauth
./admin/auth_config.php:7:$CFG->libdir
./admin/auth_config.php:11:$CFG->pagepath

To add some context to $CFG->admin (which appears on line 17 of admin/auth.php) we introduce awk to the fray.

grep -r -no ‘\$CFG->[a-z][a-z]*’ . | awk -F: ‘BEGIN{ s=“” }{ if($3) { print $0; s=$1“:”$2“:”}else  print s$0}

Here, awk saves the previous line’s first and second fields (separated by ‘:’) if the third field is not empty. If the third field is empty it prepends the first and second fields of the previous line to whatever is on the current line. Nifty, eh? So when we run our command line we now get:

./admin/auth.php:11:$CFG->libdir
./admin/auth.php:12:$CFG->libdir
./admin/auth.php:17:$CFG->wwwroot
./admin/auth.php:17:$CFG->admin
./admin/auth.php:26:$CFG->auth
./admin/auth.php:29:$CFG->auth
./admin/auth.php:52:$CFG->registerauth
./admin/auth_config.php:7:$CFG->libdir
./admin/auth_config.php:11:$CFG->pagepath

Much better! Now that we are getting all the data we can do something useful. Let’s sort everything for convenience.

grep -r -no ‘\$CFG->[a-z][a-z]*’ . | awk -F: ‘BEGIN{ s=“” }{ if($3) { print $0; s=$1“:”$2“:”}else  print s$0}‘ | sort -t: -k3

We are sorting our data by the third field in each row. So we tell sort to use ‘:’ as our field delimiter with the -t and we tell sort to use the third field with -k3.

Now we’ll make everything prettier by using awk again (our data should be small enough). We use awk to keep track of the last file and variable awk sees and then make formatting improvements as these values change; e.g. if the Variable changes from $CFG->admin to $CFG->wwwroot then prepend variable name with \n\n which inserts two line returns. This all looks pretty scary…

grep -r -no ‘\$CFG->[a-z][a-z]*’ . | awk -F: ‘BEGIN{ s=“” }{ if($3) { print $0; s=$1“:”$2“:”}else  print s$0}‘ | sort -t: -k3 | awk -F: ‘BEGIN{ v=“”; f=“”}{ if(v!=$3) { v = $3; f=“”printf “\n\n”$3} if(f!=$1) { f = $1printf “\n\t”$1} printf “:”$2 }

This gives us something nice and pretty for our terminal window.

...
$CFG->apachemaxmem
        ./lib/moodlelib.php:7544:7545:7547:7548
$CFG->aspellextradicts
        ./lib/speller/server-scripts/spellchecker.php:27:28
$CFG->aspellpath
        ./admin/settings/appearance.php:57
        ./lib/adminlib.php:2870:2881
        ./lib/editor/htmlarea/htmlarea.php:174:174:279:279
        ./lib/javascript.php:8
        ./lib/speller/changes.txt:19:1:29
        ./lib/speller/server-scripts/spellchecker.php:14:5
        ./lib/weblib.php:4988
...

We can, however, spruce things up a bit for the web:

grep -r -no ‘\$CFG->[a-z][a-z]*’ . | awk -F: ‘BEGIN{ s=“” }{ if($3) { print $0; s=$1“:”$2“:”}else  print s$0}‘ | sort -t: -k3 | awk -F: ‘BEGIN{ v=“”; f=“”}{ if(v!=$3) { v = $3; f=“”printf “<br /><br /><h1>”$3“</h1>”}  if(f!=$1) { f = $1printf “<br><span style=\”font-weight: bold\“>”$1“</span>”} printf “:”$2 }

This gives us something like the following output:

$CFG->ADMIN

./admin/auth.php:17
./admin/auth_config.php:18
./admin/block.php:44
./admin/blocks.php:157:172:237
./admin/collect/cron.php:11
./admin/collect/index.php:108:114:120:126:14:22:22:43:80:91
./admin/collect/lib.php:31:709:75:76
./admin/collect/queue.php:105:106:123:13
./admin/cron.php:168:170
./admin/environment.php:124
./admin/filter.php:16
./admin/filters.php:11
./admin/index.php:422:427:432:437:441:445:448:451:456:459:460:463:464:465:468:471:471:523:602
./admin/lang.php:102:122:127:130:131:132:142:493:754
./admin/langdoc.php:101
./admin/mnet/access_control.php:48:63:76:87
./admin/mnet/adminlib.php:75
./admin/mnet/delete.html:3:4
./admin/mnet/enr_courses.php:40
./admin/mnet/enr_hosts.php:40:52
./admin/module.php:41
./admin/modules.php:201
./admin/pagelib.php:72
./admin/register.php:37
./admin/report/assess/index.php:12:13:193:50
./admin/report/certify/index.php:11:159:270
./admin/report/courseoverview/index.php:18:88
./admin/report/courseoverview/reportsgraph.php:32
./admin/report/lastaccess/index.php:12
./admin/report/lateforleveltwo/index.php:14:86:90
./admin/report/numberofcourses/index.php:12
./admin/report/stats/index.php:31:47
./admin/roles/assign.php:381
./admin/roles/managetabs.php:10:12:14
./admin/roles/override.php:160:187
./admin/roles/tabs.php:182:190
./admin/settings.php:19:42:6
./admin/settings/collect.php:12:17:18:19
./admin/settings/courses.php:12
./admin/settings/frontpage.php:56
./admin/settings/language.php:24:25
./admin/settings/location.php:23
./admin/settings/misc.php:19:23:24:25:26
./admin/settings/mnet.php:13:16:19:22:9
./admin/settings/plugins.php:141:33:41:64
./admin/settings/server.php:161:218:219:247:248
./admin/settings/top.php:16:19:36:46:55
./admin/settings/unsupported.php:10:7:8:9
./admin/settings/users.php:58:71:72:74:75:81:82
./admin/stickyblocks.php:47:73
./admin/upgradesettings.php:31
./admin/uploaduser.php:65:66
./admin/user/user_bulk.php:22:23:24:25:26:5:6
./admin/user/user_bulk_confirm.php:14
./admin/user/user_bulk_delete.php:14
./admin/user/user_bulk_display.php:11
./admin/user/user_bulk_download.php:14
./admin/user/user_bulk_message.php:13
./admin/xmldb/actions/XMLDBAction.class.php:161
./admin/xmldb/index.php:106:78:88
./admin/xmldb/javascript.php:42:43:49:50
./backup/backup.php:104:76:91
./backup/backup_scheduled.php:180
./backup/log.php:11
./backup/restore.php:103:96
./backup/restore_check.html:268
./blocks/admin/block_admin.php:67
./blocks/admin_bookmarks/block_admin_bookmarks.php:55
./blocks/admin_bookmarks/create.php:36
./blocks/admin_bookmarks/delete.php:32
./blocks/admin_tree/block_admin_tree.php:212:67
./blocks/certify/certify.php:11
./blocks/certify/close.php:86
./blocks/certify/config_instance.html:13
./blocks/course_summary/block_course_summary.php:33
./blocks/moodleblock.class.php:475
./blocks/reports/block_reports.php:41:50:85
./blocks/trouble_ticket/thanks.php:25
./calendar/event.php:64
./calendar/export.php:19
./calendar/view.php:54
./config-dist.php:125
./config.php:23
./course/category.php:140:384
./course/delete.php:34:65
./course/edit.php:118:150
./course/editcategory.php:179:323
./course/importstudents.php:19
./course/mod.php:706
./course/modedit.php:446
./course/report/stats/index.php:28
./course/search.php:275
./enrol/authorize/enrol.php:414:746
./enrol/imsenterprise/importnow.php:13
./filter/algebra/algebradebug.php:261
./filter/tex/texdebug.php:263
./grade/report/grader/preferences.php:87
./index.php:38:56:62:74:83
./install.php:139:547
./lib/adminlib.php:3458:3498:3822:3972:3977:3985:4172:4175:4176:4335:776
./lib/db/mysql.php:1473
./lib/db/postgres7.php:1216
./lib/ddllib.php:818:820
./lib/dmllib.php:510
./lib/environmentlib.php:320:323
./lib/moodlelib.php:5099
./lib/pagelib.php:471
./lib/setup.php:184:185
./login/index.php:9
./mod/assignment/lib.php:2837
./mod/glossary/formats.php:27:39
./mod/hotpot/db/update_to_v2.php:519
./my/pagelib.php:59
./question/upgrade.php:90
./user/editadvanced.php:153:158
./user/index.php:100:542:548
./user/tabs.php:225:233:235

$CFG->ADMINBLOCKS

./admin/index.php:522

$CFG->ADMINEDITALWAYS

./config-dist.php:147
./search/documents/forum_document.php:169

$CFG->ADMINSEESALL

./calendar/lib.php:1426:1427

$CFG->ADMINUSEHTMLEDITOR

./admin/settings.php:166
./admin/upgradesettings.php:58
./lib/adminlib.php:2607:2608

$CFG->AJAXCAPABLE

./course/format/topics/ajax.php:7
./course/format/weeks/ajax.php:7
./course/view.php:153:158

$CFG->AJAXTESTEDBROWSERS

./course/format/topics/ajax.php:8
./course/format/weeks/ajax.php:8
./course/view.php:154:161

$CFG->ALLOWCATEGORYTHEMES

./course/category.php:97
./course/editcategory.php:64
./course/editcategory_form.php:20
./lib/weblib.php:3003

$CFG->ALLOWCOURSETHEMES

./course/edit_form.php:143
./lib/weblib.php:2998
./theme/chameleon/ui/chameleon.php:14
./theme/chameleon/ui/css.php:18

$CFG->ALLOWEMAILADDRESSES

./lib/moodlelib.php:4440:4441:4457

$CFG->ALLOWOBJECTEMBED

./lib/weblib.php:6584:6590

$CFG->ALLOWTHEMECHANGEONURL

./lib/setup.php:599

$CFG->ALLOWUNENROL

./lib/db/access.php:383

$CFG->ALLOWUSERBLOCKHIDING

./blocks/moodleblock.class.php:376
./lib/weblib.php:6479

Save on DeliciousDigg This
Share on Facebook+1Share on LinkedInPin it on PinterestSubmit to redditSubmit to StumbleUponShare on TumblrShare on Twitter
Tagged with: , , , , , ,
Posted in Computing, Stuff

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>