0

I am trying to create a MediaWiki parser extension that will create a table and the parsed arguments will be used in the ajax URL for the dataTables script.

$wgHooks['ParserFirstCallInit'][] = 'AllQuesttableFunction';
$wgExtensionMessagesFiles['AllQuestTable'] = __DIR__ . '/_questtable.i18n.php';
function AllQuesttableFunction( &$parser ) {
   $parser->setFunctionHook( 'questtable', 'AllQuestTableParserFunction' );
   return true;
}


function AllQuestTableParserFunction( &$parser, $arg1='', $arg2='', $arg3='' ) {

$tableend = '<br><div class="hbody"><table class="lists list_basicitem width_100p" id="serverTable"><thead><tr><th>Area</th><th>Quest Name</th><th>Min<br>Level</th><th style="width:200px !important">Rewards</th></tr></thead></table><div class="cl"></div></div>';

return array( $tableend, 'noparse' => true, 'isHTML' => true );
}



$wgHooks['ParserBeforeTidy'][] = 'wgAddJquery';

function wgAddJquery(&$parser, &$text) {


  global $addJqueryScripts, $wgLang;
   $code = $wgLang->getCode();
  if ($addJqueryScripts === true) return true;

  $parser->mOutput->addHeadItem(
    '   <script type="text/javascript" language="javascript" src="http://mywiki.com/wiki/extensions/DataTables/DataTables-1.10.0/media/js/jquery.js"></script>
        <script type="text/javascript" language="javascript" src="http://mywiki.com/wiki/extensions/DataTables/DataTables-1.10.0/media/js/jquery.dataTables.js"></script>
        <script type="text/javascript" language="javascript" >
            $(document).ready(function() {
                var dataTable = $("#serverTable").DataTable( {
                    "columnDefs": [
                                {
                                targets: 0,
                                className: \'al\'
                                },
                                {
                                targets: 1,
                                className: \'al\',
                                type: "num-html"
                                },
                                {
                                targets: 2,
                                className: \'ar\'
                                },
                                {
                                targets: 3,
                                className: \'al\'
                                },
                                { targets: \'no-sorting\', orderable: false }
                                ],
                    "processing": true,
                    "language": {
                    "processing": "<img src=\'2.gif\'>"
                    },
                    "serverSide": true,
                    "order": [[ 2, "desc" ]],
                    "ajax":{
                        url :"http://mywiki.com/wiki/extensions/quest_tables/_questtables_ajax.php?lowerlevel=' . $arg1 . '&higherlevel=' . $arg2 . '&race=' . $arg3 . '&lang=en",
                        type: "post",
                    }
                } );
            } );
        </script>'
  );

  $addJqueryScripts = true;

  return true;

}

I used code provided here Mediawiki Extension add Javascript in Header

for adding scripts to the header but 1st of all it adds it to every single page, I want it only on pages with the magic word. 2nd of all it seems to be pushing the wgAddJquery way before AllQuestTableParserFunction which means I can't used the parsed arguments in the dataTables script.

tl;dr There is a page with {{#magicword:arg1|arg2|arg3}}. All 3 arguments get parsed to an extension in which one function creates a table and 2nd adds a script to the header with those 3 arguments as part of the ajax's url and only on that one page. I dont need those scripts anywhere else.

Any suggestions or is this not even possible?

Community
  • 1
  • 1

1 Answers1

0

You should probably use $parser->getOutput()->addModules(...) but that's beside the point?

Why do you need the ParserBeforeTidy hook? If you only want to add the script when encountering the parser function, why not do it from the parser function handler method?

Tgr
  • 27,442
  • 12
  • 81
  • 118