Hi,
I am trying to write the simplest extension using a parser function (to get the feel of it). My extension is supposed to greet anyone who uses it. I am using this wikipage ( http://www.mediawiki.org/wiki/Manual:Parser_functions) as the reference However, I am unable to get even this simplest extension to work. Can anyone help me fix this. I am quite lost.
Any help will be greatly appreciated.
Thanks, Alok
This is what I have in my LocalSettings.php
#################### Say Hello Extensions ############################ require_once( "$IP/extensions/sayHello.php"); #################### End of exension ################################
And here is the file $IP/extensions/sayHello.php
<?php // Is this the right entry point ? if ( !defined( 'MEDIAWIKI' ) ) { die( 'Not an entry point.' ); }
global $wgExtensionFunctions; // I dont know if this is needed. I could not find a clear description about it. global $wgExtensionCredits; // Same as above.... I am doing this out of desperation to make this work $wgExtensionFunctions[] = 'wfSayHello' $wgExtensionCredits['SayHello'][] = array( 'name' => 'SayHello', 'author' => 'Alok', 'url' => '', 'description' => 'Extension that greets user', 'descriptionmsg' => 'This is a test extension', 'version' => '1.0.0', );
global $wgHooks $wgHooks['SayHello'][] = "wfSayHello";
global $wgParser; $wgParser->setFunctionHook('SayHello', 'wfSayHello');
function wfSayHello($parser, $userNameInput) { return "Hello " . $userNameInput; } return true; ?>
Alok Watve wrote:
I am trying to write the simplest extension using a parser function [...]
Remove the last two lines:
return true; ?>
You "return" when passing a value back out of a function; you are not "in" a function at this point.
The trailing ?> is optional, and generally not recommended in a PHP script included by other scripts, so might as well leave it out :)
Thanks for the reply. I did correct that but that does not solve the problem. I created a small page which has only one line
{{#SayHello:John}}
I was expecting the page to display "Hello John" But instead it displayed the same line. I am not sure why is this happening.
Alok
On Fri, Jul 30, 2010 at 10:05 PM, Ross McKay rosko@zeta.org.au wrote:
Alok Watve wrote:
I am trying to write the simplest extension using a parser function [...]
Remove the last two lines:
return true; ?>
You "return" when passing a value back out of a function; you are not "in" a function at this point.
The trailing ?> is optional, and generally not recommended in a PHP script included by other scripts, so might as well leave it out :) -- Ross McKay, Toronto, NSW Australia "Let the laddie play wi the knife - he'll learn"
- The Wee Book of Calvin
G'day Alok,
Thanks for the reply. I did correct that but that does not solve the problem. I created a small page which has only one line
{{#SayHello:John}}
I was expecting the page to display "Hello John" But instead it displayed the same line. I am not sure why is this happening.
Firstly, when testing extensions don't forget to append ?action=purge to URLs to make sure that MediaWiki executes your code again. If you've already worked out the way to fix your code and it's not having any effect, maybe this is your problem!
Now, I actually looked above the last two lines of code this time and saw that you're trying to directly access the parser global object. I don't know if that will even work, but the correct way to approach this is to use event hooks to install your parser function, as per the example you linked to.
Try this code, which is a slight reworking of your code to use event hooks to install the parser function:
<?php
if ( !defined( 'MEDIAWIKI' ) ) die( 'Not an entry point.' );
$wgExtensionCredits['SayHello'][] = array( 'name' => 'SayHello', 'author' => 'Alok', 'url' => '', 'description' => 'Extension that greets user', 'descriptionmsg' => 'This is a test extension', 'version' => '1.0.0', );
// create hooks for initialising your parser function $wgHooks['ParserFirstCallInit'][] = "SayHello_init"; $wgHooks['LanguageGetMagic'][] = "SayHello_magic";
// hook your parser function on the parser init event function SayHello_init( &$parser ) { $parser->setFunctionHook('SayHello', 'SayHello'); return TRUE; }
// add your magic word(s) function SayHello_magic( &$magicWords, $langCode ) { $magicWords['SayHello'] = array(0, 'SayHello'); return TRUE; }
// your parser function function SayHello($parser, $userNameInput = '') { return "Hello " . $userNameInput; }
Hi,
I don't create a parser extension, but on special pages you output a text with $wgOut->addWiki( '<wikitext>' ); or $wgOut->addHtml( '<html text>' );
Viele Grüße Jan
-----Ursprüngliche Nachricht----- Von: mediawiki-l-bounces@lists.wikimedia.org [mailto:mediawiki-l-bounces@lists.wikimedia.org] Im Auftrag von Ross McKay Gesendet: Samstag, 31. Juli 2010 09:20 An: alok.watve@gmail.com Cc: MediaWiki announcements and site admin list Betreff: Re: [Mediawiki-l] Help with "Hello World" extension
G'day Alok,
Thanks for the reply. I did correct that but that does not solve the problem. I created a small page which has only one line
{{#SayHello:John}}
I was expecting the page to display "Hello John" But instead it displayed the same line. I am not sure why is this happening.
Firstly, when testing extensions don't forget to append ?action=purge to URLs to make sure that MediaWiki executes your code again. If you've already worked out the way to fix your code and it's not having any effect, maybe this is your problem!
Now, I actually looked above the last two lines of code this time and saw that you're trying to directly access the parser global object. I don't know if that will even work, but the correct way to approach this is to use event hooks to install your parser function, as per the example you linked to.
Try this code, which is a slight reworking of your code to use event hooks to install the parser function:
<?php
if ( !defined( 'MEDIAWIKI' ) ) die( 'Not an entry point.' );
$wgExtensionCredits['SayHello'][] = array( 'name' => 'SayHello', 'author' => 'Alok', 'url' => '', 'description' => 'Extension that greets user', 'descriptionmsg' => 'This is a test extension', 'version' => '1.0.0', );
// create hooks for initialising your parser function $wgHooks['ParserFirstCallInit'][] = "SayHello_init"; $wgHooks['LanguageGetMagic'][] = "SayHello_magic";
// hook your parser function on the parser init event function SayHello_init( &$parser ) { $parser->setFunctionHook('SayHello', 'SayHello'); return TRUE; }
// add your magic word(s) function SayHello_magic( &$magicWords, $langCode ) { $magicWords['SayHello'] = array(0, 'SayHello'); return TRUE; }
// your parser function function SayHello($parser, $userNameInput = '') { return "Hello " . $userNameInput; }
Jan Luca wrote:
I don't create a parser extension, but on special pages you output a text with $wgOut->addWiki( '<wikitext>' ); or $wgOut->addHtml( '<html text>' );
With special pages, the extension is creating the whole page and must feed all of the output. With tag and parser extensions, the extension is only handling a magic word and must return either wikitext to be further parsed or HTML to be directly embedded in the output.
Thanks a lot for the help (And patience). The page cache is now cleared but even after the refresh, I still see the original wiki command as the final result. I don't know what to do. I will try to get permission on the log files (its not my system)
Alok
On Sat, Jul 31, 2010 at 6:43 PM, Ross McKay rosko@zeta.org.au wrote:
Jan Luca wrote:
I don't create a parser extension, but on special pages you output a text with $wgOut->addWiki( '<wikitext>' ); or $wgOut->addHtml( '<html text>' );
With special pages, the extension is creating the whole page and must feed all of the output. With tag and parser extensions, the extension is only handling a magic word and must return either wikitext to be further parsed or HTML to be directly embedded in the output. -- Ross McKay, Toronto, NSW Australia Man's Prayer: "I'm a man, but I can change. If I have to. I guess." - Red Green
MediaWiki-l mailing list MediaWiki-l@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-l
Hello Ross,
I think I found the problem. It was related to the file encoding on Windows/Unix system.I was editing the file in windows and running it on Unix. I was using FTP to transfer the files and Filezilla was set to use "Automatic"transfer mode. I forced it to "binary" and that solved the problem.
Thanks a ton for your help and patience. I really appreciate it, Alok
On Sun, Aug 1, 2010 at 12:10 AM, Alok Watve alok.watve@gmail.com wrote:
Thanks a lot for the help (And patience). The page cache is now cleared but even after the refresh, I still see the original wiki command as the final result. I don't know what to do. I will try to get permission on the log files (its not my system)
Alok
On Sat, Jul 31, 2010 at 6:43 PM, Ross McKay rosko@zeta.org.au wrote:
Jan Luca wrote:
I don't create a parser extension, but on special pages you output a text with $wgOut->addWiki( '<wikitext>' ); or $wgOut->addHtml( '<html text>' );
With special pages, the extension is creating the whole page and must feed all of the output. With tag and parser extensions, the extension is only handling a magic word and must return either wikitext to be further parsed or HTML to be directly embedded in the output. -- Ross McKay, Toronto, NSW Australia Man's Prayer: "I'm a man, but I can change. If I have to. I guess." - Red Green
MediaWiki-l mailing list MediaWiki-l@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-l
Alok Watve wrote:
I think I found the problem. It was related to the file encoding on Windows/Unix system.I was editing the file in windows and running it on Unix. I was using FTP to transfer the files and Filezilla was set to use "Automatic"transfer mode. I forced it to "binary" and that solved the problem.
Yay! Bugs you when it's something like that, eh? Best of luck building the real extension now :)
mediawiki-l@lists.wikimedia.org