Topic on Project:Support desk

I am trying to make a new Extension, but getting an error at line '$parser->getOutput()->addModules( 'ext.E.scripts' )';

11
Summary by 103.118.50.4

The code is edited,and it is working now.

103.118.50.4 (talkcontribs)

[f49fe744377fe4fd5f717ceb] /wiki/index.php/Main_Page Error from line 7 of C:\wamp64\www\wiki\extensions\E\E_body.php: Call to a member function addModules() on null

Backtrace:

#0 C:\wamp64\www\wiki\includes\Hooks.php(174): EC::onParserInit(Parser)

#1 C:\wamp64\www\wiki\includes\Hooks.php(202): Hooks::callHook(string, array, array, NULL)

#2 C:\wamp64\www\wiki\includes\parser\Parser.php(369): Hooks::run(string, array)

#3 C:\wamp64\www\wiki\includes\cache\MessageCache.php(1190): Parser->firstCallInit()

#4 C:\wamp64\www\wiki\includes\cache\MessageCache.php(1166): MessageCache->getParser()

#5 C:\wamp64\www\wiki\includes\Message.php(1282): MessageCache->transform(string, boolean, LanguageEn, Title)

#6 C:\wamp64\www\wiki\includes\Message.php(883): Message->transformText(string)

#7 C:\wamp64\www\wiki\includes\Message.php(943): Message->toString(string)

#8 C:\wamp64\www\wiki\includes\OutputPage.php(924): Message->text()

#9 C:\wamp64\www\wiki\includes\OutputPage.php(971): OutputPage->setHTMLTitle(Message)

#10 C:\wamp64\www\wiki\includes\page\Article.php(622): OutputPage->setPageTitle(string)

#11 C:\wamp64\www\wiki\includes\actions\ViewAction.php(68): Article->view()

#12 C:\wamp64\www\wiki\includes\MediaWiki.php(501): ViewAction->show()

#13 C:\wamp64\www\wiki\includes\MediaWiki.php(294): MediaWiki->performAction(Article, Title)

#14 C:\wamp64\www\wiki\includes\MediaWiki.php(860): MediaWiki->performRequest()

#15 C:\wamp64\www\wiki\includes\MediaWiki.php(517): MediaWiki->main()

#16 C:\wamp64\www\wiki\index.php(42): MediaWiki->run()

#17 {main}

MarkAHershberger (talkcontribs)

Well, at this point you do not have anything in $parser: "Call to a member function addModules() on null". If you want us to help, you'll need to share your source code.

103.118.50.4 (talkcontribs)

OK, right now it has only 4 files(I am just learning how to create extensions).The Javascript module is not working.

1.Extension.json--

{
    "name": "E",
	"version": "2.9.4",
	"author": [
		"PS",
		"xyz"
	],
	"license-name": "GPL-2.0-or-later",
	"type": "parserhook",
	"requires": {
		"MediaWiki": ">= 1.30.0"
	},
	"AutoloadClasses": {
		"EC": "E_body.php"
	},
	"Hooks": {
		"ParserFirstCallInit": "EC::onParserInit"
		},
	"ResourceFileModulePaths": {
		"localBasePath": "",
		"remoteExtPath": "E"
	},
	"ResourceModules": {
	    "ext.E.scripts": {
			"scripts": "resources/E.js",
			"dependencies": [ "mediawiki.api" ]
		},
		"ext.E.styles": {
			"styles": "resources/E.css"
		}
	},
    "manifest_version": 1	
}


2.E_body.php-->

<?php
class EC {
	static function onParserInit( Parser $parser ) {
		$parser->setHook( 'E', array( __CLASS__, 'ERender' ) ); 
		$parser->getOutput()->addModules( 'ext.E.scripts' );
		return true;
	}
	static function ERender( $input, array $args, Parser $parser, PPFrame $frame ) {
		$ret='<style>table.wtable { border-style:solid; border-width:1px; border-collapse:collapse;} </style>';
	    $ret .= '<table class="wtable">';
		$ret .= '<tr>';
		$ret .= '<td>Feedback</td>';
		$ret .= '<td><input id="inp001" type="text" /></td>';
		$ret .= '</tr>';
		$ret .= '<tr>';
		$ret .= '<td>upvote</td>';
		$ret .= '<td><input id="chk001" type="radio" name="r1"/></td>';
		$ret .= '</tr>';
		$ret .= '<tr>';
		$ret .= '<td><p font-colour=red>downvote</p></td>';
		$ret .= '<td><input id="chk001" type="radio" name="r1" /></td>';
		$ret .= '</tr>';
		$ret .= '<tr><p>here</p>';
        $ret .= '<td align="center" colspan=2><input id="btn001" type="button" value="Submit"></td>';
        $ret .= '</tr>';
		$ret .= '</table>';
		$ret .= '<input type="submit" value="Vote" onclick="myFunction()">/>';
		return $ret;
	}
}


3.E.js-->

$("#btn001").click(function() {
	$("#inp001").html("Hello <b>world!</b>");
	//inp001.innerHTML("Clicked");
	$('p').html("Hello <b>world!</b>");
	alert("Button clicked.");
});


4.E.css-->

table.wtable {
	border-style:solid; border-width:1px; border-collapse:collapse;
}

@MarkAHershberger

103.118.50.4 (talkcontribs)

Will someone look at this please ?

I am trying to learn extension creation but right now I am stuck due to this.

Please see this...

Falcopragati (talkcontribs)

having similar problem

MarkAHershberger (talkcontribs)

The problem is your call

$parser->getOutput()->addModules( 'ext.E.scripts' );

in the ParserFirstCallInit hook. That is called too early to have getOutput() be populated.

Move the call so that it is just inside EC:ERender and you'll get what you want.

103.118.50.4 (talkcontribs)

Thanks Mark ! It worked.

Actually I took this code from [Manual:Tag extensions/Example], in which a tutorial is given about creating an interacting tag.

But due to this ' $parser->getOutput()->addModules' in the wrong place, it does'nt works, and this is not the only line of code on that page which should be corrected..

Maybe you should take a look, and edit the codes on that page to make them right...

I can edit, but I don't think I am qualified to edit that page.

Bawolff (talkcontribs)

The code you linked to is wrong, but for a different reasons. Its calling $wgOut->addModules(). $wgOut is available at the parserFirstCallInit, but it doesn't tie the module to your tag. Instead it would cause the module to be added on every page view where the parser is invoked at least once, which would be incorrect for other reasons.

Anyways, i edited the page

103.118.50.4 (talkcontribs)

I just saw the page. I saw a warning-->

Warning:The MediaWiki ajax action which the code below uses, is deprecated. New code should register an API module instead
can you/@MarkAHershberger or anyone please elaborate more on this? Or can you please add the required modification to the code so that it can work as intended?? It would be very helpful for me and all the people who are exploring MediWiki and trying to learn Extension development. Thanks
MarkAHershberger (talkcontribs)

Have a look at the API documentation. You can create an API module to handle the JS calls.

103.118.50.4 (talkcontribs)

Thanks Brian

Reply to "I am trying to make a new Extension, but getting an error at line '$parser->getOutput()->addModules( 'ext.E.scripts' )';"