Extension:HalloWorld NewbieLessons1

From MediaWiki.org
Jump to navigation Jump to search
MediaWiki extensions manual
Crystal Clear action run.svg
HalloWorld NewbieLessons1
Release status: stable
Implementation Parser extension, Tag
Description Beginners Introduction to an TAG and Parcer Hook
Author(s) Jan Boer (jackqboxtalk)
Latest version 1.0 (2016-10-05)
MediaWiki 1.25.5
PHP 5.5.36
Database changes No
License GNU General Public License 2.0 or later
Download No link
Translate the HalloWorld NewbieLessons1 extension if it is available at translatewiki.net
Check usage and version matrix.
MediaWiki-extensions-icon.svg
QBox ABC Information template:

This page contains Jack QBox php Extension (lessons)

intro[edit]

Creating an QBox4u Mediawiki extension is really not as hard as it looks

If you follow the basic rules, it is as they say PIECE OF CAKE

To provide you as an real Newbie an helping hand, we will use an existing basic Mediawiki BoilerPlate Extension, and modify the content step by step in several examples

Actions[edit]

Prepare[edit]

  • Create in your server an new page called Create new extension
  • Copy the Extension:HalloWorld_NewbieLessons1 page text content into this new page
  • Download the wikimedia MediaWiki extensions icon
  • Upload this icon into your QBox server with the name MediaWiki-extensions-icon_02.png
  • Download the wikimedia Download extension 01 icon
  • Upload this icon into your QBox server with the name Download_extension_01.png
  • Download the wikimedia Special Pag 01 icon
  • Upload this icon into your QBox server with the name Special_Page_01.png
  • Download the wikimedia Special Page 02 icon
  • Upload this icon into your QBox server with the name Special_Page_02.png
  • Download the wikimedia Special Page 03 icon
  • Upload this icon into your QBox server with the name Special_Page_03.png
  • Download the wikimedia Template overview 03 icon
  • Upload this icon into your QBox server with the name Template_overview_03.PNG


  • Create an new Blank Template inside your server with the name Template:QPE
  • Copy the below content into this new Template page
TAG
Click on expand to vieuw the content of the new Template:QPE
{|style="font-size:100%"
| [[Image:MediaWiki-extensions-icon_02.png|90px|left]]
|QBox ABC Information template: 
'''This page contains QBox php Extension lessons'''<br>
<categorytree mode=pages depth=0>Category:QBox_Extension_lessons</categorytree>
|}<includeonly>
{{#ifeq:{{FULLPAGENAME}}|Template:QPE||{{#ifeq:{{FULLPAGENAME}}|Category:QBox_template||[[Category:QBox_Extension_lessons]]}}}}</includeonly><noinclude>
== Use ==
*Add '''{{QPE}} ''' 
To any page that contains QBox php Extension lessons related data
 
[[Category:QBox_template]]

</noinclude>
HANDY: Any Template located inside the page Category:QBox_template will not be added to the Category list, 
       so you can make an Template overview page in your server by eg
TAG
Click on expand to view an example.. THIS IS NOT THE TEMPLATE !!!!!
{| class="wikitable sortable" style="font-size: 80%;" 
|+ Template overview
! Template
! Template
|-
|[[Template:QPE |{{QPE}}]] ||{{QPE}}
|}
Template overview

Install[edit]

Download extension 01.png
  • Unzip and copy the extension into the main /extensions folder
  • Add at the bottom in the base file LocalSettings.php the text: wfLoadExtension( 'BoilerPlate' );
  • Type in search the text: Special:HelloWorld

The following special page should appear

Special Page 01.png

Modify en.json[edit]

Add as below an new line ABOVE "helloworld": "Hello world" an new line in the file: i18n/en.json

"boilerplate-welcome": "Welcome to the \"Hello world\" special page of the BoilerPlate extension.",

Modify SpecialHelloWorld.php[edit]

Modify as below the file: specials/SpecialHelloWorld.php

CLICK ON EXPAND TO VIEW THE SOURCE LISTINGS, OR ON COLLAPSE TO HIDE THE SOURCE LISTINGS
Original Modified
/**
* Show the page to the user
*
* @param string $sub The subpage string argument (if any).
*  [[Special:HelloWorld/subpage]].
*/
public function execute( $sub ) {
	$out = $this->getOutput();

	$out->setPageTitle( $this->msg( 'boilerplate-helloworld' ) );

	$out->addHelpLink( 'How to become a MediaWiki hacker' );

	$out->addWikiMsg( 'boilerplate-helloworld-intro' );
}
	
/**
* Show the page to the user
*
* @param string $sub The subpage string argument (if any).
*  [[Special:HelloWorld/subpage]].
*/
public function execute( $par ) {
	global $wgUser;

	$output = $this->getOutput();	
	$output->setPageTitle( $this->msg( 'boilerplate-helloworld' ) );	
	$request = $this->getRequest();
	$this->setHeaders();

	# ...Add the base instructions from mediawiki
	$output->addHelpLink( 'Extension:BoilerPlate' );		
	# ...User name
	$a = $wgUser->getName();
	# ...Messages		
	$SpecialPageinfo = "Hello user:$a !<br>";
	$SpecialPageinfo .= $this->msg( 'boilerplate-welcome' )->parse().'<br>';		
	$output->addWikiText( $SpecialPageinfo );
		
	$output->addWikiText( " Follow the Training page: [[Create_new_extension]]" );
}

The following special page should appear

Special Page 02.png

Modify en.json[edit]

Add as below an new line ABOVE "helloworld": "Hello world" in the file: i18n/en.json

"boilerplate-testme": "my first test with the BoilerPlate extension.",
"boilerplate-lesson1":"QBox4u first \"practical\" extension starters lessons, based on the existing boilerplate extension https://www.mediawiki.org/wiki/Extension:BoilerPlate.",

Create an BoilerPlateParser.body.php[edit]

CLICK ON EXPAND TO VIEW THE SOURCE LISTINGS, OR ON COLLAPSE TO HIDE THE SOURCE LISTINGS
Copy the source
<?php
/******************
 *  variable Extension - this class is an QBox Training extention
 *  Copyright (C) 2010 qbox4u.com <qbox4u@gmail.com> 
 *
 *  This program is not free software therefore you can-not redistribute 
 *  it and/or modify it under the terms of the GNU General Public License 
 *  as published by the Free Software Foundation; either version 2 of the 
 *  License, or (at your option) any later version.
 *
 *  Please consult and/or request the administrator of qbox4u@gmail.com
 *  to use the information and samples
 *
 *  To copy the data an written autorisation of the developer as stated in 
 *  the $wgExtensionCredits is required 
 * 
 *  You should have received a copy of the GNU General Public License along
 *  with this program; if not, write to the Free Software Foundation, Inc.,
 *  59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *  http://www.gnu.org/copyleft/gpl.html
 *
 *
 *  @ingroup Extensions
 *  @author Jan boer <qbox4u@gmail.com>
 *  @LinkedIn https://www.linkedin.com/in/jan-boer-a24640113
 *  @version 1.0
 *  @link http://QBox4u.com
 *
 */ 

class BoilerPlateParserBody {	

/**********************************
 * ID		: wf_BoilerPlate_ParserFunction_Render( &$parser )
 * Release	: NA 
 * Date		: Created 26-10-2016 by JBoe
 * Notes	: Impementation of Boilerplate
 *
 * Purpose	: To retrieve the parameters used inside the TAG
 * Info		: http://php.net/manual/en/function.require-once.php	
 * Function	: Incude parameters 
 * Input	: {{#FUNCTION11:Hallo|World}} 	
 *			  
 * Output	: Success ==>   
 *	     	  Failure ==>  
 * Error	:
 * Example	:      
 * Implementation :   
 *  
 */
	
	public static function wf_BoilerPlate_ParserFunction_Render( &$parser ) { 
		global $wgUser;

		$access = $wgUser->isAllowed( 'read' );
		# first of all, retrieve the parameters that we have in the hook
		$arg = func_get_args();
		array_shift($arg); // 0th parameter is the $parser
		$html_body  = '<p class="error">'.wfMessage( 'boilerplate-testme' )->parse().'</p>';
		$html_body .= "<strong>My first Parser function</strong><br>\n";
		$html_body .= "---- \n";
		$html_body .= "This is my first typical syntax for a Parser function <strong>{{#FUNCTION1: arg1 | arg2 }}</strong><br>";
		$html_body .= 'my Parser hook contains '.count($arg)." elements<br />\n";
		$html_body .= '$attributes[0] <strong>arg1='.($arg[0])."</strong><br>\n";
		$html_body .= '$attributes[1] <strong>arg2='.($arg[1])."</strong><br>\n";

		/********
		* Space for your personal application
		* 	
		********/
				
		$output = $html_body;
		
		return array( $output, 'noparse' => false, 'isHTML' => false);
		
	}
	
}

Create an BoilerPlateTag.body.php[edit]

CLICK ON EXPAND TO VIEW THE SOURCE LISTINGS, OR ON COLLAPSE TO HIDE THE SOURCE LISTINGS
Copy the source
<?php
/******************
 *  variable Extension - this class is an QBox Training extention
 *  Copyright (C) 2010 qbox4u.com <qbox4u@gmail.com> 
 *
 *  This program is not free software therefore you can-not redistribute 
 *  it and/or modify it under the terms of the GNU General Public License 
 *  as published by the Free Software Foundation; either version 2 of the 
 *  License, or (at your option) any later version.
 *
 *  Please consult and/or request the administrator of qbox4u@gmail.com
 *  to use the information and samples
 *
 *  To copy the data an written autorisation of the developer as stated in 
 *  the $wgExtensionCredits is required 
 * 
 *  You should have received a copy of the GNU General Public License along
 *  with this program; if not, write to the Free Software Foundation, Inc.,
 *  59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *  http://www.gnu.org/copyleft/gpl.html
 *
 *
 *  @ingroup Extensions
 *  @author Jan boer <qbox4u@gmail.com>
 *  @LinkedIn https://www.linkedin.com/in/jan-boer-a24640113
 *  @version 1.0
 *  @link http://QBox4u.com
 *
 */

class BoilerPlateTagBody {
/**********************************
 * ID		: wf_BoilerPlate_TagFunctionRender( $input, array $args, Parser $parser, PPFrame $frame )
 * Release	: NA 
 * Date		: Created 26-10-2016 by JBoe
 * Notes	: Impementation of Boilerplate
 *
 * Purpose	: To retrieve the parameters used inside the TAG
 * Info		: http://php.net/manual/en/function.require-once.php	
 * Function	: Incude parameters 
 * Input	: <TAG11 const1=aaa const2=bbbb> Hallo World </TAG11> 	
 *			  
 * Output	: Success ==>   
 *	     	  Failure ==>  
 * Error	:
 * Example	:      
 * Implementation :   
 *  
 */
	public static function wf_BoilerPlate_TagFunctionRender( $input, array $args, Parser $parser, PPFrame $frame ) { 
		global $wgUser;

		$access = $wgUser->isAllowed( 'read' );
		$attr = array();
    
		# first of all, retrieve the parameters that we have in the hook
		foreach( $args as $name => $value )
			$attr[] = '<strong>' . htmlspecialchars( $name ) . '</strong> = ' . htmlspecialchars( $value );

		$html_body  = '<p class="error">'.wfMessage( 'boilerplate-testme' )->parse().'</p>';		
		$html_body .= "<strong>My first TAG function</strong><br>\n";
		$html_body .= "---- \n";
		$html_body .= "This is my  first typical syntax for a TAG function <strong><Tag1 arg1='xxx' arg2='xxx'> my data </Tag1></strong><br />\n";	
		$html_body .= 'my Tag hook contains '.count($args)." elements<br>\n";
		$html_body .= '$attributes[0] '.$attr[0]."<br>\n";
		$html_body .= '$attributes[1] '.$attr[1]."<br>\n";
		$html_body .= ' The data between 2 TAGS is:'.htmlspecialchars( $input )."\n";

		/********
		* Space for your personal application
		* 	
		********/
		
		$output = $parser->recursiveTagParse( $html_body, $frame );
		
		return array( $output, 'noparse' => false, 'isHTML' => false);
		
	}	
	
}

Modify BoilerPlate.hooks.php[edit]

CLICK ON EXPAND TO VIEW THE SOURCE LISTINGS, OR ON COLLAPSE TO HIDE THE SOURCE LISTINGS
Copy the source
<?php
/******************
 *  variable Extension - This BoilerPlateHooks class is an QBox Training extention
 *  Copyright (C) 2010 qbox4u.com <qbox4u@gmail.com> 
 *
 *  This program is not free software therefore you can-not redistribute 
 *  it and/or modify it under the terms of the GNU General Public License 
 *  as published by the Free Software Foundation; either version 2 of the 
 *  License, or (at your option) any later version.
 *
 *  Please consult and/or request the administrator of qbox4u@gmail.com
 *  to use the information and samples
 *
 *  To copy the data an written autorisation of the developer as stated in 
 *  the $wgExtensionCredits is required 
 * 
 *  You should have received a copy of the GNU General Public License along
 *  with this program; if not, write to the Free Software Foundation, Inc.,
 *  59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *  http://www.gnu.org/copyleft/gpl.html
 *
 *
 *  @ingroup Extensions
 *  @author Jan boer <qbox4u@gmail.com>
 *  @LinkedIn https://www.linkedin.com/in/jan-boer-a24640113
 *  @version 1.0
 *  @link http://QBox4u.com
 *
 */ 

class BoilerPlateHooks {
	
	/**
	 * Description
	 *
	 * @param $userId Integer
	 * @param $userPageTitle Title
	 * @param $toolLinks Array
	 *
	 * @return true
	 */
	public static function onParserFirstCallInit( Parser $parser ) {
		
		// set the hook <TAG1> Hallo World </TAG1>
		$parser->setHook( 'TAG11', 'BoilerPlateTagBody::wf_BoilerPlate_TagFunctionRender' );
		
		// set the hook {{#MyFirsthook:Hallo|World}}
		$parser->setFunctionHook( 'FUNCTION11', 'BoilerPlateParserBody::wf_BoilerPlate_ParserFunction_Render' );
		
    return true;
	
	}
	/**
	 * Description
	 *
	 * @param $userId Integer
	 * @param $userPageTitle Title
	 * @param $toolLinks Array
	 *
	 * @return true
	 */
	public static function onLanguageGetMagic( array &$magicWords, $langCode) {
		
		// Identify the hook {{#MyFirsthook:xxxxxxxxx}}
		// The first array element is whether to be case sensitive, in this case (0) it is not case sensitive, 1 would be sensitive
        $magicWords['FUNCTION11'] = array( 0, 'FUNCTION11' );
		
    # unless we return true, other parser functions extensions won't get loaded.
    return true;
	
	}

}


Modify SpecialHelloWorld.php[edit]

CLICK ON EXPAND TO VIEW THE SOURCE LISTINGS, OR ON COLLAPSE TO HIDE THE SOURCE LISTINGS
Copy the source
<?php
/**
 * HelloWorld SpecialPage for BoilerPlate extension
 *
 * @file
 * @ingroup Extensions
 */
 /******************
 *  variable Extension - this class is an QBox Training extention
 *  Copyright (C) 2010 qbox4u.com <qbox4u@gmail.com> 
 *
 *  This program is not free software therefore you can-not redistribute 
 *  it and/or modify it under the terms of the GNU General Public License 
 *  as published by the Free Software Foundation; either version 2 of the 
 *  License, or (at your option) any later version.
 *
 *  Please consult and/or request the administrator of qbox4u@gmail.com
 *  to use the information and samples
 *
 *  To copy the data an written autorisation of the developer as stated in 
 *  the $wgExtensionCredits is required 
 * 
 *  You should have received a copy of the GNU General Public License along
 *  with this program; if not, write to the Free Software Foundation, Inc.,
 *  59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *  http://www.gnu.org/copyleft/gpl.html
 *
 *
 *  @ingroup Extensions
 *  @author Jan boer <qbox4u@gmail.com>
 *  @LinkedIn https://www.linkedin.com/in/jan-boer-a24640113
 *  @version 1.0
 *  @link http://QBox4u.com
 *
 */ 


class SpecialHelloWorld extends SpecialPage {
	
	
	public function __construct() {
		parent::__construct( 'HelloWorld' );
	}

const B_TagTest = <<<QBoxFSP

=Testing the functionality=
{|class="wikitable sortable mw-collapsible mw-collapsed" style="white-space: nowrap;font-size:12px;" width=80%
! TAG !! TAG
|+ ''' CLICK ON EXPAND TO VIEW THE TEST, OR ON COLLAPSE TO HIDE THE TEST'''
|- style="vertical-align: top;"
|
 <nowiki><TAG11 const1=aaa const2=bbbb> Hallo World </TAG11></nowiki>
<TAG11 const1=aaa const2=bbbb> Hallo World </TAG11>
|
 <nowiki>{{#FUNCTION11:Hallo|World}}</nowiki>
{{#FUNCTION11:Hallo|World}}
|}

QBoxFSP;

	/**
	 * Show the page to the user
	 *
	 * @param string $sub The subpage string argument (if any).
	 *  [[Special:HelloWorld/subpage]].
	 */

	function execute( $par ) {
		global $wgUser,$B_TestPage;
		
		$output = $this->getOutput();
		$output->setPageTitle( $this->msg( 'boilerplate-helloworld' ) );
		$request = $this->getRequest();
		$this->setHeaders();
		
		# ...Add the base instructions from mediawiki
		$output->addHelpLink( 'Extension:BoilerPlate' );
		# ...User name
		$a = $wgUser->getName();
		# ...Messages		
		$SpecialPageinfo = "Hello user:$a !<br>";
		$SpecialPageinfo .= $this->msg( 'boilerplate-welcome' )->parse().'<br>';		
		$output->addWikiText( $SpecialPageinfo );
		
		$output->addWikiText( " Follow the Training page: [[Create_new_extension]]" ); 
		

		$output->addWikiText( self::B_TagTest );
		
	}

	protected function getGroupName() {
		return 'other';
	}
}

Modify extension.json[edit]

CLICK ON EXPAND TO VIEW THE SOURCE LISTINGS, OR ON COLLAPSE TO HIDE THE SOURCE LISTINGS
ORIGINAL NEW
{
	"name": "BoilerPlate",
	"version": "0.0.0",
	"author": [
		"Your Name"
	],
	"url": "https://www.mediawiki.org/wiki/Extension:BoilerPlate",
	"descriptionmsg": "boilerplate-desc",
	"license-name": "MIT",
	"type": "other",
	"AutoloadClasses": {
		"BoilerPlateHooks": "BoilerPlate.hooks.php",
		"SpecialHelloWorld": "specials/SpecialHelloWorld.php"
	},
	"config": {
		"BoilerPlateEnableFoo": true
	},
	"ExtensionMessagesFiles": {
		"BoilerPlateAlias": "BoilerPlate.i18n.alias.php"
	},
	"Hooks": {
		"NameOfHook": [
			"BoilerPlateHooks::onNameOfHook"
		]
	},
	"MessagesDirs": {
		"BoilerPlate": [
			"i18n"
		]
	},
	"ResourceModules": {
		"ext.boilerPlate.foo": {
			"scripts": [
				"modules/ext.boilerPlate.js",
				"modules/ext.boilerPlate.foo.js"
			],
			"styles": [
				"modules/ext.boilerPlate.foo.css"
			],
			"messages": [],
			"dependencies": []
		}
	},
	"ResourceFileModulePaths": {
		"localBasePath": "",
		"remoteExtPath": "BoilerPlate"
	},
	"SpecialPages": {
		"HelloWorld": "SpecialHelloWorld"
	},
	"manifest_version": 1
}

{
	"name": "BoilerPlate",
	"version": "0.0.1",
	"author": [
		"Jan Boer",
		"LinkedIn https://www.linkedin.com/in/jan-boer-a24640113",
		"QBox4u.com"
	],
	"url": "https://www.mediawiki.org/wiki/Extension:BoilerPlate",
	"descriptionmsg": "boilerplate-lesson1",
	"license-name": "MIT",
	"type": "other",
	"AutoloadClasses": {
		"BoilerPlateHooks": "BoilerPlate.hooks.php",
		"SpecialHelloWorld": "specials/SpecialHelloWorld.php",
		"BoilerPlateTagBody": "BoilerPlateTag.body.php",
		"BoilerPlateParserBody": "BoilerPlateParser.body.php"
	},
	"config": {
		"BoilerPlateEnableFoo": true
	},
	"ExtensionMessagesFiles": {
		"BoilerPlateAlias": "BoilerPlate.i18n.alias.php"
	},
	"Hooks": {
		"ParserFirstCallInit": [
			"BoilerPlateHooks::onParserFirstCallInit"
		],
		"LanguageGetMagic": [
			"BoilerPlateHooks::onLanguageGetMagic"
		]
	},
	"MessagesDirs": {
		"BoilerPlate": [
			"i18n"
		]
	},
	"ResourceModules": {
		"ext.boilerPlate.foo": {
			"scripts": [
				"modules/ext.boilerPlate.js",
				"modules/ext.boilerPlate.foo.js"
			],
			"styles": [
				"modules/ext.boilerPlate.foo.css"
			],
			"messages": [],
			"dependencies": []
		}
	},
	"ResourceFileModulePaths": {
		"localBasePath": "",
		"remoteExtPath": "BoilerPlate"
	},
	"SpecialPages": {
		"HelloWorld": "SpecialHelloWorld"
	},
	"manifest_version": 1
}

Verification[edit]

  • Type in your personal QBox wiki search the text: Special:HelloWorld
  • Click in the new Special page at the chapter Testing the functionality on EXPAND

The following special page should appear

Special Page 03.png

Your application[edit]

Congratulations, You have made an working personal interface to the QBox WIKI inside your server

You have available:

  • Tag commands
  • Text input and Text output
  • WIKI and PHP Functions
  • etc etc etc
You can add from now on an unique PHP application inside BoilerPlateParser.body.php or BoilerPlateTag.body.php