Scripting
- Scripting in MediaWiki
Introduction
Scripting in MediaWiki refers to extending the functionality of a wiki beyond its core capabilities through the use of programming languages. While MediaWiki itself is built on PHP, the primary scripting languages used by wiki *users* for customization and automation are JavaScript, Lua, and, to a lesser extent, CSS. This article will serve as a beginner's guide to understanding and utilizing these scripting languages to enhance your MediaWiki experience. We will cover the basics of each language, how they are implemented within MediaWiki, and provide examples to get you started. Understanding scripting is crucial for anyone wanting to move beyond basic editing and truly tailor a wiki to their specific needs. It allows for dynamic content, interactive tools, and sophisticated customizations.
Why Script in MediaWiki?
Before diving into the specifics, let's outline *why* you might want to learn scripting for your MediaWiki. The benefits are numerous:
- **Customization:** Modify the look and feel of the wiki beyond what templates and CSS alone can achieve.
- **Automation:** Automate repetitive tasks, such as page creation, data updates, or user management.
- **Interactive Features:** Add dynamic elements like calculators, forms, or interactive maps.
- **Integration with External Services:** Connect the wiki to external APIs to pull in data or provide additional functionality. Think integrating with a stock market API for real-time data, or a weather API for localized forecasts.
- **Enhanced User Experience:** Create a more engaging and user-friendly experience for wiki visitors.
- **Advanced Templates:** Create templates with complex logic and dynamic behavior.
- **Improved Extensions:** Contribute to or create custom extensions to add entirely new features.
JavaScript in MediaWiki
JavaScript is the most commonly used scripting language for front-end web development, and MediaWiki is no exception. It allows you to manipulate the Document Object Model (DOM) – the structure of a web page – to dynamically change content, respond to user interactions, and perform other client-side operations.
How JavaScript Works in MediaWiki
MediaWiki provides several ways to include JavaScript code:
- **`MediaWiki:Common.js`:** This page contains JavaScript code that is loaded for *all* users of the wiki. It’s ideal for global customizations and functionalities. Requires administrator privileges.
- **`MediaWiki:Skin.js`:** This page contains JavaScript code that is loaded for users of a specific skin (e.g., Vector, Monobook). Allows for skin-specific customizations. Requires administrator privileges.
- **`User:YourUsername/common.js`:** This page contains JavaScript code that is loaded only for *you*. It’s perfect for personal customizations.
- **`User:YourUsername/skin.js`:** This page contains JavaScript code loaded only for you and the selected skin.
- **Inline Scripting (Discouraged):** While possible, embedding JavaScript code directly within wiki pages using `<script>` tags is generally discouraged due to maintainability and security concerns.
Basic JavaScript Example
Let's create a simple script that displays an alert message when the page loads:
```javascript $(document).ready(function() {
alert('Welcome to the wiki!');
}); ```
Place this code in `User:YourUsername/common.js` (replace "YourUsername" with your actual username). After saving the page, clear your browser cache and reload the wiki. You should see the alert message.
jQuery
MediaWiki comes with jQuery pre-loaded, which simplifies JavaScript development by providing a concise and powerful API for manipulating the DOM. The `$` symbol in the example above refers to the jQuery object. Learning jQuery is *highly* recommended for working with JavaScript in MediaWiki.
Common JavaScript Tasks
- **Modifying Page Content:** Change the text, attributes, or styles of HTML elements.
- **Handling User Events:** Respond to clicks, key presses, mouse movements, and other user interactions.
- **Making AJAX Requests:** Fetch data from external sources without reloading the page. This is useful for integrating with APIs.
- **Creating Custom UI Elements:** Build custom buttons, forms, and other interactive components. Consider building a custom candlestick chart viewer.
- **Implementing Dynamic Filters:** Allow users to filter data on a page based on certain criteria.
- **Adding Tooltips:** Provide helpful information when users hover over specific elements.
Resources for Learning JavaScript
- MDN Web Docs JavaScript Guide: [1](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide)
- jQuery Documentation: [2](https://api.jquery.com/)
- W3Schools JavaScript Tutorial: [3](https://www.w3schools.com/js/default.asp)
Lua in MediaWiki
Lua is a lightweight, embeddable scripting language that is gaining popularity in MediaWiki, primarily through the Scribunto extension. Scribunto allows you to write Lua code directly within wiki pages, which is then executed by the server.
Scribunto and Module Pages
Lua scripts in MediaWiki are typically written on Module pages. These pages have a special namespace dedicated to containing reusable code modules. This promotes code organization and reuse.
How Lua Works in MediaWiki
1. **Install Scribunto:** The Scribunto extension must be installed and configured on the MediaWiki server. This is typically done by a system administrator. 2. **Create a Module Page:** Create a page in the `Module:` namespace (e.g., `Module:MyModule`). 3. **Write Lua Code:** Write your Lua code within the module page, enclosed within `