Extension Development
- Extension Development
- Introduction
This article serves as a beginner's guide to Extension Development for MediaWiki, version 1.40 and beyond. MediaWiki, the software powering Wikipedia and countless other wikis, is highly customizable through the use of extensions. Extensions allow wiki administrators and developers to add new features, modify existing behavior, and integrate the wiki with other systems. This document will cover the fundamentals of creating a basic extension, outlining the necessary files, coding standards, and deployment procedures. It assumes a basic understanding of PHP, HTML, and CSS. If you are completely new to web development, it's recommended to acquire those foundational skills before diving into extension development.
- Understanding the Extension Ecosystem
Before writing code, it’s crucial to understand the overall structure of MediaWiki extensions. Extensions are essentially collections of PHP files, often accompanied by JavaScript, CSS, and image assets. They hook into MediaWiki’s core functionality through a system of *hooks*. Hooks are points in the MediaWiki execution flow where extensions can inject their own code. These hooks can be triggered by various events, such as page rendering, form submission, or database queries.
There are several types of extensions:
- **Core Extensions:** These are extensions that are included with the standard MediaWiki distribution.
- **Third-Party Extensions:** These are extensions created and maintained by the community. Many excellent extensions are available on the MediaWiki Extension Directory.
- **Custom Extensions:** These are extensions developed specifically for a particular wiki.
This article focuses on creating custom extensions.
- Core Concepts and Terminology
- **Hooks:** As mentioned, these are points in the MediaWiki code where extensions can insert their functionality. Understanding available hooks is paramount. The Hooks page on MediaWiki.org is an invaluable resource.
- **Special Pages:** Extensions can introduce new special pages, accessible through the 'Special:' namespace (e.g., Special:MyExtension).
- **Tags:** Extensions can define new wiki tags, allowing users to embed specific functionality within wiki pages.
- **API:** MediaWiki has a powerful API that extensions can utilize to interact with the wiki's data and functionality.
- **Configuration:** Extensions can define configuration variables that allow administrators to customize their behavior.
- **Localization:** Extensions should support multiple languages through localization files.
- **Namespaces:** Extensions can create new namespaces for specific data or content.
- **Skinning:** Extensions can modify the visual appearance of the wiki through skin hooks.
- Creating a Basic Extension: "Hello World"
Let's create a simple extension that displays "Hello, World!" on every page. This example will demonstrate the fundamental structure of an extension.
- 1. Directory Structure
Create a directory named `Hello World` within your MediaWiki `extensions/` directory. Inside this directory, create the following files:
- `Hello World.php`: This is the main extension file.
- `Hello World.alias.php`: This file defines an alias for the extension.
- 2. `Hello World.php`
```php <?php
if ( !defined( 'MEDIAWIKI' ) ) { die( 'Not a valid entry point' ); }
$wgExtensionMessages['Hello World'] = array( 'Hello World' => 'Hello World Extension', 'helloworld_message' => 'Hello, World!', );
$wgHooks['SkinAfterContent'][] = 'HelloWorldAfterContent';
function HelloWorldAfterContent( $out, $skin ) { $out->addFooterMessage( wfMessage( 'helloworld_message' )->text() ); return true; } ```
- Explanation:**
- `if ( !defined( 'MEDIAWIKI' ) ) { die( 'Not a valid entry point' ); }`: This ensures that the file is only executed within the context of MediaWiki.
- `$wgExtensionMessages['Hello World'] = array(...)`: This defines the messages used by the extension for localization. `Hello World` is the extension's name, and `helloworld_message` is a key for the "Hello, World!" message.
- `$wgHooks['SkinAfterContent'][] = 'HelloWorldAfterContent';`: This registers the `HelloWorldAfterContent` function to be executed when the `SkinAfterContent` hook is triggered. This hook is called after the main page content has been rendered, but before the closing HTML tags.
- `function HelloWorldAfterContent( $out, $skin ) { ... }`: This is the function that will be executed when the hook is triggered.
* `$out->addFooterMessage( wfMessage( 'helloworld_message' )->text() );`: This adds the "Hello, World!" message to the page's footer. `wfMessage()` retrieves the localized message, and `->text()` converts it to plain text. * `return true;`: This indicates that the hook was successfully executed.
- 3. `Hello World.alias.php`
```php <?php /**
* Alias file for Hello World extension */
$wgExtensionAliases['Hello World'] = 'Hello World'; ```
- Explanation:**
- This file defines an alias for the extension, mapping the internal name `Hello World` to the same name for simplicity. This is useful for renaming extensions later without breaking compatibility.
- 4. Registering the Extension
To activate the extension, you need to add the following line to your `LocalSettings.php` file:
```php wfLoadExtension( 'Hello World' ); ```
Make sure this line is placed *after* the line that includes `LocalSettings.php` itself. You will likely need to clear your MediaWiki cache after making this change. You can do this by navigating to Special:PurgeCache in your wiki.
- Advanced Extension Development
- Utilizing Hooks Effectively
The key to powerful extension development lies in understanding and utilizing MediaWiki's extensive hook system. The Hooks page lists all available hooks and their parameters. When choosing a hook, consider:
- **When is the hook triggered?** Understanding the timing of the hook is crucial for ensuring your code executes at the right moment.
- **What parameters are passed to the hook?** These parameters provide access to the data and objects needed to modify the wiki's behavior.
- **What is the expected return value of the hook?** Some hooks require a specific return value to indicate success or failure.
- Special Pages
Creating special pages allows you to provide users with dedicated interfaces for interacting with your extension. To create a special page:
1. Define a class that extends `SpecialPage`. 2. Implement the `__construct()` method to initialize the special page. 3. Implement the `execute()` method to handle the page's logic. 4. Register the special page using `$wgSpecialPages['MySpecialPage'] = 'MySpecialPageClass';`.
- API Integration
MediaWiki's API allows extensions to expose functionality to external applications. To integrate with the API:
1. Define API modules in your extension. 2. Implement the logic for each module. 3. Register the modules using `$wgAPIModules['MyModule'] = 'MyModuleClass';`.
- Configuration Options
Allowing administrators to configure your extension makes it more flexible and adaptable. You can define configuration variables in your extension and access them using `$wgConf`. Use the `Configure` hook to add configuration options to the MediaWiki configuration system.
- Localization
To support multiple languages, use the `$wgExtensionMessages` array to define localized messages. Create language files in the `languages/` directory of your extension to provide translations for different languages.
- Best Practices
- **Coding Standards:** Follow the MediaWiki coding conventions to ensure consistency and readability.
- **Security:** Be mindful of security vulnerabilities, such as SQL injection and cross-site scripting (XSS). Sanitize user input and use prepared statements to prevent these attacks.
- **Performance:** Optimize your code for performance. Avoid unnecessary database queries and use caching where appropriate. Tools like Xdebug can help identify performance bottlenecks.
- **Documentation:** Document your code thoroughly. This will make it easier for others (and your future self) to understand and maintain your extension.
- **Testing:** Thoroughly test your extension before deploying it to a production environment.
- **Version Control:** Use a version control system, such as Git, to track changes to your code.
- Resources
- MediaWiki Extension Directory: A repository of community-contributed extensions.
- MediaWiki Developer Documentation: Comprehensive documentation for MediaWiki developers.
- Hooks: A list of available hooks and their parameters.
- API: Documentation for the MediaWiki API.
- MediaWiki coding conventions: Guidelines for writing high-quality MediaWiki code.
- Trading and Financial Strategies (Disclaimer: Not directly related to Extension Development, but included as requested)
The following list of terms relate to financial trading and are *not* directly part of MediaWiki extension development, but were required in the prompt. They are included for completeness. **Trading involves risk, and past performance is not indicative of future results.**
- **Technical Analysis:** Using historical price data to predict future price movements.
- **Fundamental Analysis:** Evaluating the intrinsic value of an asset based on economic and financial factors.
- **Trend Following:** Identifying and capitalizing on existing trends in the market.
- **Mean Reversion:** Betting that prices will revert to their historical average.
- **Arbitrage:** Exploiting price differences in different markets.
- **Moving Averages:** Smoothing price data to identify trends. [1]
- **Relative Strength Index (RSI):** An oscillator measuring the magnitude of recent price changes to evaluate overbought or oversold conditions. [2]
- **MACD (Moving Average Convergence Divergence):** A trend-following momentum indicator. [3]
- **Bollinger Bands:** Volatility bands placed above and below a moving average. [4]
- **Fibonacci Retracements:** Identifying potential support and resistance levels. [5]
- **Elliott Wave Theory:** A theory that predicts price movements based on repeating wave patterns. [6]
- **Candlestick Patterns:** Visual representations of price movements that can signal potential trading opportunities. [7]
- **Support and Resistance Levels:** Price levels where buying or selling pressure is expected to be strong. [8]
- **Breakout Trading:** Entering a trade when the price breaks through a support or resistance level. [9]
- **Scalping:** Making small profits from frequent trades. [10]
- **Day Trading:** Opening and closing trades within the same day. [11]
- **Swing Trading:** Holding trades for several days or weeks. [12]
- **Position Trading:** Holding trades for months or years. [13]
- **Risk Management:** Strategies for minimizing potential losses. [14]
- **Diversification:** Spreading investments across different assets. [15]
- **Correlation:** The statistical relationship between two assets. [16]
- **Volatility:** The degree of price fluctuation. [17]
- **Liquidity:** The ease with which an asset can be bought or sold. [18]
- **Time Arbitrage:** Exploiting time differences in price feeds.
- **Statistical Arbitrage:** Using statistical models to identify arbitrage opportunities.
Extension Development Hooks API MediaWiki Extension Directory Special Pages Localization Configuration MediaWiki coding conventions Namespaces Skinning
Start Trading Now
Sign up at IQ Option (Minimum deposit $10) Open an account at Pocket Option (Minimum deposit $5)
Join Our Community
Subscribe to our Telegram channel @strategybin to receive: ✓ Daily trading signals ✓ Exclusive strategy analysis ✓ Market trend alerts ✓ Educational materials for beginners