Extension architecture
- Extension Architecture
- Introduction
This article provides a comprehensive overview of the architecture of extensions in MediaWiki. It is aimed at beginners who wish to understand how extensions work, how they are structured, and how they interact with the core MediaWiki software. Understanding extension architecture is crucial for both installing and configuring existing extensions, and for developing new ones. We will cover the core components, the extension lifecycle, and best practices for development.
- Core Components of a MediaWiki Extension
A MediaWiki extension isn't a monolithic piece of code; it's typically a collection of files organized in a specific manner. These files fall into several key categories:
- **Extension.json:** This is the manifest file, introduced in MediaWiki 1.19, and is now *required*. It contains metadata about the extension, such as its name, version, description, author, dependencies, and the paths to the files that comprise the extension. It's the primary way MediaWiki identifies and manages extensions. The structure is JSON formatted and requires strict adherence to the schema. See Manual:Extension.json for detailed information.
- **PHP Files:** These are the heart of most extensions. They contain the logic that modifies MediaWiki's behavior. PHP files can define:
* **Special Pages:** Pages accessible through the `Special:` namespace (e.g., `Special:Search`). These pages provide administrative or utility functions. * **Hooks:** Points in MediaWiki’s code where extensions can inject their own functionality. This is the most common way extensions modify core behavior without directly altering MediaWiki's source code. * **API Endpoints:** Allow external applications to interact with MediaWiki via its API. * **Classes and Functions:** Reusable code blocks that provide specific functionality.
- **JavaScript Files:** Used to add dynamic behavior to the frontend, such as interactive elements, AJAX requests, and enhanced user interface features. They often work in conjunction with PHP to provide a complete solution. Modern extensions frequently utilize JavaScript frameworks like jQuery (still prevalent in older extensions) or newer frameworks.
- **CSS Files:** Used to style the frontend, changing the appearance of pages and elements modified by the extension. CSS can customize the look and feel of Special Pages, user interfaces, and other aspects of the wiki.
- **SQL Schema Files:** Used to define database tables required by the extension. These files are used during extension installation or updates to create, modify, or delete database schema elements. They are crucial for extensions that store persistent data.
- **Images, Icons, and Other Assets:** These files provide visual elements for the extension, such as icons for Special Pages or images for the user interface.
- **Language Files:** Used to provide translations for the extension’s user interface elements. These files allow the extension to be used in multiple languages. They follow a specific format and are managed by MediaWiki’s localization system.
- The Extension Lifecycle
Understanding the lifecycle of an extension helps to grasp how it's integrated into MediaWiki.
1. **Installation:** The extension files are typically uploaded to the `extensions/` directory of your MediaWiki installation. This can be done manually via FTP or using tools like Git. 2. **Configuration:** After uploading the files, you need to enable the extension in your `LocalSettings.php` file. This is done by adding a line like `wfLoadExtension( 'MyExtension' );`. The extension name corresponds to the directory name of the extension within the `extensions/` directory. This triggers MediaWiki to load and initialize the extension. 3. **Initialization:** When MediaWiki loads the extension, it parses the `Extension.json` file to determine the extension's dependencies and the files to be included. It then loads the PHP files, registers hooks, and sets up any necessary database tables. 4. **Hook Registration:** The extension registers its hooks with MediaWiki. Hooks are functions that are called at specific points during MediaWiki's execution. This allows the extension to modify the behavior of core functions without directly altering the core code. See Development:Hooks for a complete list of available hooks. 5. **Runtime Execution:** As users interact with the wiki, MediaWiki calls the extension's hooks when the corresponding events occur. The extension's code then executes, modifying the behavior of the wiki as defined by the extension. 6. **Uninstallation:** To uninstall an extension, you remove the corresponding line from `LocalSettings.php` and delete the extension directory from `extensions/`. A well-written extension should also provide hooks for uninstallation to clean up any database tables or configuration settings it created.
- Hooks: The Core of Extension Interaction
Hooks are the primary mechanism by which extensions modify MediaWiki’s behavior. They allow extensions to inject code into various points in MediaWiki's execution flow without directly changing the core code.
- **Types of Hooks:** Hooks can be categorized based on when they are called:
* **Before Hooks:** Called *before* a core function executes. They can modify the input parameters of the function. * **After Hooks:** Called *after* a core function executes. They can modify the output of the function. * **Replace Hooks:** Completely replace the original core function. Use these with caution, as they can have significant consequences.
- **Hook Parameters:** Each hook passes parameters to the extension's function. These parameters provide information about the event that triggered the hook and allow the extension to modify the behavior of the wiki. Understanding the parameters of each hook is crucial for writing effective extensions.
- **Hook Priorities:** When multiple extensions register for the same hook, MediaWiki executes them in order of priority. Extensions with higher priorities are executed first. This allows extensions to override or modify the behavior of other extensions.
- **Common Hook Uses:**
* **Modifying Output:** Altering the HTML generated by MediaWiki. * **Adding User Interface Elements:** Adding new buttons or links to pages. * **Intercepting User Actions:** Responding to user events like saving a page or submitting a form. * **Extending the API:** Adding new API endpoints.
- File Structure Best Practices
A well-organized file structure is crucial for maintainability and collaboration. Here's a recommended structure for a typical extension:
``` MyExtension/ ├── Extension.json # Extension manifest ├── README.md # Extension documentation ├── LICENSE # License information ├── autoloader.php # Autoloading definitions ├── specialpages/ # PHP files for Special Pages │ └── SpecialMyPage.php ├── hooks/ # PHP files containing hook functions │ └── MyExtensionHooks.php ├── includes/ # General PHP includes │ └── MyClass.php ├── resources/ # Frontend resources (JavaScript, CSS, images) │ ├── js/ │ │ └── MyExtension.js │ ├── css/ │ │ └── MyExtension.css │ └── images/ │ └── MyIcon.png ├── languages/ # Language files │ └── en.json └── schema/ # SQL schema files
└── MyExtensionTables.sql
```
- Autoloading
MediaWiki uses autoloading to automatically load PHP classes when they are needed. This avoids the need to manually include files. Extensions should define an `autoloader.php` file that registers the extension's classes with MediaWiki's autoloader. This is typically done using the `wfLoader->addExtPath` function.
- Database Schema Management
If your extension requires database tables, you need to define them in SQL schema files. MediaWiki provides tools for managing database schema changes during extension installation and updates. See Manual:Database for more details. It's critical to design your schema carefully to avoid conflicts with other extensions or MediaWiki's core schema. Use prefixes for your table names to minimize the risk of collisions.
- Security Considerations
Security is paramount when developing extensions. Here are some key considerations:
- **Input Validation:** Always validate user input to prevent cross-site scripting (XSS) and SQL injection attacks.
- **Output Escaping:** Escape all output to prevent XSS attacks. Use MediaWiki's built-in escaping functions.
- **Permissions and Access Control:** Enforce appropriate permissions and access control to prevent unauthorized access to sensitive data.
- **Code Review:** Have your code reviewed by other developers to identify potential security vulnerabilities.
- **Dependency Management:** Keep your extension's dependencies up to date to address known security vulnerabilities.
- Debugging Extensions
Debugging extensions can be challenging. Here are some tips:
- **Logging:** Use MediaWiki's logging system to record debugging information.
- **Error Reporting:** Enable error reporting in your `LocalSettings.php` file.
- **Debugging Tools:** Use a debugger to step through your code and inspect variables.
- **Browser Developer Tools:** Use your browser's developer tools to inspect the frontend code and network requests.
- **MediaWiki's Developer Documentation:** Refer to MediaWiki's developer documentation for detailed information about debugging.
- Advanced Topics
- **API Integration:** Creating custom API endpoints for external applications.
- **Event Dispatchers:** Using MediaWiki's event dispatcher to decouple extension components.
- **Configuration Options:** Providing configuration options to allow users to customize the extension's behavior.
- **Dependency Injection:** Using dependency injection to improve code testability and maintainability.
- Resources
- Manual:How to develop extensions
- Development:Hooks
- Manual:Extension.json
- Manual:Database
- MediaWiki Developer Documentation
- Technical Analysis & Trading Strategies (Related Links)
While not directly part of extension architecture, many wikis host content about trading. Here are some related links (for informational context only):
- **Technical Indicators:** [Moving Averages](https://www.investopedia.com/terms/m/movingaverage.asp), [MACD](https://www.investopedia.com/terms/m/macd.asp), [RSI](https://www.investopedia.com/terms/r/rsi.asp), [Bollinger Bands](https://www.investopedia.com/terms/b/bollingerbands.asp), [Fibonacci Retracements](https://www.investopedia.com/terms/f/fibonacciretracement.asp)
- **Trading Strategies:** [Day Trading](https://www.investopedia.com/terms/d/daytrading.asp), [Swing Trading](https://www.investopedia.com/terms/s/swingtrading.asp), [Scalping](https://www.investopedia.com/terms/s/scalping.asp), [Trend Following](https://www.investopedia.com/terms/t/trendfollowing.asp), [Breakout Trading](https://www.investopedia.com/terms/b/breakout.asp)
- **Market Analysis:** [Fundamental Analysis](https://www.investopedia.com/terms/f/fundamentalanalysis.asp), [Technical Analysis](https://www.investopedia.com/terms/t/technicalanalysis.asp), [Sentiment Analysis](https://www.investopedia.com/terms/s/sentimentanalysis.asp)
- **Market Trends:** [Uptrend](https://www.investopedia.com/terms/u/uptrend.asp), [Downtrend](https://www.investopedia.com/terms/d/downtrend.asp), [Sideways Trend](https://www.investopedia.com/terms/s/sidewaysmarket.asp), [Bull Market](https://www.investopedia.com/terms/b/bullmarket.asp), [Bear Market](https://www.investopedia.com/terms/b/bearmarket.asp)
- **Risk Management:** [Stop-Loss Orders](https://www.investopedia.com/terms/s/stop-lossorder.asp), [Take-Profit Orders](https://www.investopedia.com/terms/t/takeprofitorder.asp), [Position Sizing](https://www.investopedia.com/terms/p/position-sizing.asp), [Diversification](https://www.investopedia.com/terms/d/diversification.asp)
- **Trading Psychology:** [Fear and Greed](https://www.investopedia.com/articles/trading/09/fear-greed.asp), [Discipline](https://www.investopedia.com/articles/trading/09/trading-discipline.asp)
Main Page Help:Contents Manual:Configuration Development:Coding conventions API:Main page Extension:Semantic MediaWiki Extension:VisualEditor Extension:Cargo Extension:AbuseFilter Extension:OATHAuth
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