FANDOM


Overview

How to create new Wikia extensions. This manual shows the steps to create the fictitious new module FooAndBar

Steps

Extension directory

Wikia extensions should be placed in the extensions/wikia directory using the name of the extension as the directory name:

 mkdir extensions/wikia/FooAndBar

Layout

Extensions will typically have 4 files and 4 directories:

 cd extensions/wikia/FooAndBar
 touch FooAndBar.php FooAndBar.class.php FooAndBar.setup.php FooAndBar.i18n.php
 mkdir templates js css images

The purpose of files and directories are as follows:

  • FooAndBar.setup.php - This file contains require_once calls to include other files needed by the extension and set any variables needed.
  • FooAndBar.class.php - If the extension defines a new class, it should be defined here.
  • FooAndBarController.class.php - This is where the interface code lives. Handlers for web requests are defined here.
  • FooAndBar.i18n.php - This is where the translations for the strings used within extension are defined.
  • templates/ - Any HTML templates needed by the extension should be put here.
  • js/ - Javascript files.
  • css/ - CSS files.
  • images/ - Images needed by the extension.

Depending on the extension some files or directories will not be needed or some additional files will be required.

Setup file

The setup file includes all other necessary PHP and language files. The following is skeleton of a typical setup file:

<?php
 
/**
 * FooAndBar
 *
 * A short description of the FooAndBar extention
 *
 * @author Author Name <author-email@wikia-inc.com>
 * @date 2010-01-09
 * @copyright Copyright (C) 2010 Wikia Inc.
 * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License 2.0 or later
 *
 */
 
// Extension credits
$wgExtensionCredits['other'][] = array(
	'name' => 'FooAndBar',
	'version' => '1.0',
	'url' => 'http://www.wikia.com/wiki/c:help:Help:FooAndBar',
	'author' => array( '[http://www.wikia.com/wiki/User:AuthorName Author Name]', 'Author Name' ),
	'descriptionmsg' => 'fooandbar-desc',
);
 
$dir = dirname(__FILE__);
 
// Interface code
include("$dir/FooAndBarController.class.php");
 
// autoloaded classes
$wgAutoloadClasses['FooAndBar'] = "$dir/FooAndBar.class.php";
 
// i18n
$wgExtensionMessagesFiles['FooAndBar'] = $dir.'/FooAndBar.i18n.php';
 
// hooks
$wgHooks['hookIntoMWCode'][] = 'FooAndBar::handleAction';

Class file

The following is a skeleton for the class file. There is nothing very specific to wiki here, PHP OO guidelines should be followed:

<?php
 
class FooAndBar {
 
    // Members
 
    /** @var int foo */
    public foo;
 
    // Methods
 
    /**
     * Method description
     *
     * @param string $some_arg A description of the argument
     * @return int A description about what is being returned
     */
     public some_method ( $some_arg = '' ) {
        return 1;
    }
};

4. Edit the interface file

A short skeleton for the interface file. The main thing to include here is the code to load the i18n messages and any JS or CSS:

<?php
 
function urlHandler () {
    $this->response->addAsset( 'fooandbar_js' );
    $this->response->addAsset( 'fooandbar_scss' );
    $this->response->addAsset( 'fooandbar_css' );
 
    // Interface code
    // ...
 
    // Data to be used in FooAndBarController_urlhandler.php template
    $this->templateValue = 'foo';
}

Internationalization file

Messages for i18n are defined as follows:

<?php
/** Internationalisation file for the FooAndBar extension. */
 
$messages = [];
 
$messages['en'] = array(
	'fooandbar-desc'     => 'This is the Foo and Bar extension',
	'fooandbar-some-msg' => 'Currently there are $1 foo(s) and $2 bar(s)',
);

See also

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.