Was this page helpful?

Creating a "Hello World" plugin

    Overview

    This plug-in will create a new special page within MindTouch which will simply return the phrase "Hello, World."

    Let's begin

    First, we create the actual file that will be executed: /deki/plugins/special_page/special_helloworld/special_helloworld.php

    <?php
    if (defined('MINDTOUCH_DEKI'))
    {
        // register this plug-in's location, and associate a PHP function to execute at this URL
        DekiPlugin::registerHook('Special:HelloWorld', 'wfSpecialHelloWorld');
    }
    
    // DekiPlugin passes in three parameters; the last two as references; ultimately $html is returned to the end user
    function wfSpecialHelloWorld($pageName, &$pageTitle, &$html)
    {
        $pageTitle = 'Hello, World!';
        $html = 'Hello, world.';
    }

    In order to allow this file to be executed, we must white-list its execution through LocalSettings.php: 

    $wgDekiSpecialPages[] = 'special_helloworld';

    If you now visit http://your-site/Special:HelloWorld, it will return the phrase "Hello, world." (The sample is attached)

    Example: "Hello World" special page plug-in (Expanded)

    MindTouch includes a SpecialPagePlugin class which simplifies your ability to push additional code down the pipe (primarily JS and CSS). This example will be far more useful if you're looking to create a robust special page. To create a "Hello World" which takes advantage of this, create /deki/plugins/special_page/special_helloworld_expanded/special_helloworld_expanded.php

    <?php
    
    if (defined('MINDTOUCH_DEKI'))
    {
    	// create a hook, but execute from class "SpecialHelloWorld" with method "create"
    	DekiPlugin::registerHook('Special:HelloWorldExpanded', array('SpecialHelloWorldExpanded', 'create'));
    }
    
    class SpecialHelloWorldExpanded extends SpecialPagePlugin
    {
    	protected $pageName = 'HelloWorld';
    
    	public static function create($pageName, &$pageTitle, &$html)
    	{
    		$Special = new self($pageName, basename(__FILE__, '.php'));
    		// this will automagically look in your resources file
    		// $pageTitle = $Special->getPageTitle();
    		$pageTitle = 'Hello, World (Expanded)';
    		$html = $Special->output();
    	}
    	
    	function &output()
    	{
    		$this->includeSpecialCss('special_helloworld_expanded.css');
    		$this->includeSpecialJavascript('special_helloworld_expanded.js');
    		return '<span id="helloworld">Hello, world.</span>'; 
    	}
    }

    For this example, a custom CSS file and custom JS file were also included. The CSS file made the text bolded and bigger, while the JS hooked a jQuery event which would hide the "Hello, World" text when clicked: 

    special_helloworld_expanded.css:

    #helloworld {
        font-weight: bold;
        font-size: 16px;
    }

    special_helloworld_expanded.js:

    Deki.$(document).ready(function(){
        Deki.$('#helloworld').click(function() {
            Deki.$(this).hide();
        });   
    });

    In order to allow this file to be executed, we must white-list its execution through LocalSettings.php: 

    $wgDekiSpecialPages[] = 'special_helloworld_expanded';

    If you now visit http://your-site/Special:HelloWorldExpanded, it will return the phrase "Hello, world.", which if you click, will hide the text (The sample is attached)

    Was this page helpful?
    Tag page

    Files 2

    FileVersionSizeModified 
    You must login to post a comment.

    Copyright © 2011 MindTouch, Inc. Powered by