Heya,
I am trying to write an extension to create magic words/parser functions via a SpecialPage, which then queries windows computers via WMI. There are too many class/column combinations in WMI to create a function for every single one of it, so I want to create a generic handler function which is called with class and column as function arguments. When trying to do that I run into 2 problems:
1) The code of the callback gets executed, but the return value is not displayed on the wiki page; the space where the result of the parser function should be just stays blank (the magic word is removed). I tried setting a non-anonymous callback function which worked fine.
2) Whenever I try to add a new magic word via my special page I get "internal error: invalid magic word", whenever I try to access any page of the wiki, until I run maintenance/rebuildLanguageCache.php --force. Can I in some way "enforce" just rebuild the language used for magic words? Or make it available in any other way?
======== Code Problem #1: <?php [... some more extension stuff...]
$wgHooks['ParserFirstCallInit'][] = 'setupWMIMagic'; $wgExtensionMessagesFiles['AddWMIProp'] = dirname( __FILE__ ) . '/newmagic.i18n.php'; $wgExtensionMessagesFiles['AddWMIPropAlias'] = dirname( __FILE__ ) . '/newmagic.alias.php'; require_once( dirname( __FILE__ ) . '/MagicWordHandler.inc.php' ); $hookFunctions = array();
function setupWMIMagic( &$parser ) {
// Load magic words from datafile // This will return an array of arrays of the form: // array( // 'mword' => 'someword', // 'class' => 'Class name', // 'column' => 'Column name' $mwh = new MagicWordHandler(); $mymagicwords = $mwh->loadMagicWords(); $magicWords = array();
// if there are no Magic Words in our datafile, we just return if( !$mymagicwords ) { return true; }
foreach( $mymagicwords as $word ) { // create an anonymous function for each word and set it as a hook $hookFunctions[ $word['mword'] ] = function() use ( $word, $parser ) { processWMIMagic( $parser, $word['class'], $word['column'] ); }; //setFunctionHook( WORDID, CALLBACK, FLAGS ) - SFH_NO_HASH creates {{MAGICWORD}} instead of {{#MAGICWORD}} $parser->setFunctionHook( $word['mword'], $hookFunctions[ $word['mword'] ], SFH_NO_HASH );
} return true; }
function processWMIMagic( $parser, $class, $column ) {
$title = $parser->getTitle(); // this appears in the apache's error log error_log( "GOT: $class, $column, $title" ); $output = "TITLE: $title CLASS: $class COLUMN: $column"; // this seems to be ignored. I also tried it with noparse => true and several other options return $output;
}
======== Code Problem #2 (newmagic.i18n.php):
[... some other lang stuff...]
require_once( dirname( __FILE__ ) . '/MagicWordHandler.inc.php' );
$mwh = new MagicWordHandler(); $mymagicwords = $mwh->loadMagicWords();
$magicWords = array(); if( $mymagicwords ) { foreach( $mymagicwords as $word ) { // Define the magic words $magicWords['en'][$word['mword']] = array( 0, $word['mword'] ); } }
So, are there any mistakes in the code? Is there anything I am missing? How can I debug this further? I am kind of lost here ;)
Thanks in advance.
Greetings Sven