MediaWiki code snippets
Contents
- 1 Returning content to the client
- 2 Articles
- 3 Security
- 4 Using the parser
- 4.1 Parse wikitext
- 4.2 Expand templates only
- 4.3 Replace triple-brace arguments in wikitext content
- 4.4 Using named parameters from a parser-function callback
- 4.5 Return codes for parser functions
- 4.6 Protecting raw HTML output from modification by MediaWiki
- 4.7 Post processing to remove unwanted breaks and entities from output
- 5 MediaWiki Environment
- 6 Article queries
- 7 Misc
- 8 Wikitext in Sidebar
- 9 See also
Returning content to the client
Raw wikitext content
This function returns the raw content specified in $text, it can be called any time from extension-setup or after. If the $save parameter is supplied it will bring up a download dialog with the default name set to $save, otherwise it will download and open unprompted. If $expand is set to true, then any templates, parser-functions or variables in the content will be expanded.
Return an HTTP error page
Domain-based default redirect
If no page title is specified, redirect to a default depending on the domain name in the requested URL. In this example requests to abc.org or any of it's subdomains with no title specified will redirect to the Welcome to ABC article, and any requests to the exact domain of www.xyz.org without a title end up at the XYZ Home article. Titleless requests to any other domain which resolves to this example wiki will be unaffected and left to the rest of the configuration to deal with. This code should be executed early in the LocalSettings before any extensions are included, but after $wgServer is defined.
Add a meta tags to the page
Articles
Get article content
Edit or create
Editing or creating an article are both achieved using the Article::doEdit method which takes three parameters (the third is optional). The first two are the text content and edit summary. The third optional parameter is for flags which adjust the operation. The available flags and their meaning are listed below followed by example usage.
- EDIT_NEW: Article is known or assumed to be non-existent, create a new one
- EDIT_UPDATE: Article is known or assumed to be pre-existing, update it
- EDIT_MINOR: Mark this edit minor, if the user is allowed to do so
- EDIT_SUPPRESS_RC: Do not log the change in recentchanges
- EDIT_FORCE_BOT: Mark the edit a "bot" edit regardless of user rights
- EDIT_DEFER_UPDATES: Defer some of the updates until the end of index.php
- EDIT_AUTOSUMMARY: Fill in blank summaries with generated text where possible
- Note: $wgUser must be set before calling this function.
Image URLs
Given the name of an image page, obtain the full URL of the image.
$title = Title::newFromText($v,NS_IMAGE); $image = Image::newFromTitle($title); $v = $image && $image->exists() ? $image->getURL()
Security
Here's an example which was created in response to a support-desk question. It prevents users from editing other users user-pages:
Here is a similar example which restricts all namespaces except MAIN from anonymous users:
Using the parser
Parse wikitext
Expand templates only
Replace triple-brace arguments in wikitext content
If $wikitext is set to "hello {{{you}}} this is {{{me}}}", then the "you" and "me" keys of the $args array will replace the corresponding triple-brace arguments in the wikitext content. This is a useful method to know because there are actually a number of difficulties involved in implementing it since it must account for both named and ordered parameters and default values (which can also contain brace-expressions).
Using named parameters from a parser-function callback
You can use named parameters in your parser functions, eg {{#drops:for=Rikkukin the Defender|zone=The Ascent|h=3}}, but you will need to manually split the args into key/value pairs in your callback function, such as in the following example code:
This snippet will create a hash from the arguments passed to your callback function (ignoring any which are objects such as the first one which is $parser). The resulting hash will contain numeric keys for all the normal non-named parameters in your parser-function, and non-numeric keys matching all the name=value parameters.
Return codes for parser functions
Typical example.
return array( $text, 'found' => true, 'nowiki' => false, 'noparse' => false, 'noargs' => false, 'isHTML' => false );
See also: http://www.organicdesign.co.nz/Extension:Example
Protecting raw HTML output from modification by MediaWiki
See: How can I avoid modification of my extension's HTML output on mediawiki.org. Thanks to Duesentrieb for digging out this info.
Post processing to remove unwanted breaks and entities from output
function efFixEmptyLineBug(&$parser,&$text) { $text = preg_replace('|<p>\s*<br\s*/>\s*</p>|s','',$text); return true; }
MediaWiki Environment
Article title
This should be called at an appropriate time such as from the OutputPageBeforeHTML hook.
Article queries
List article titles from a category
Adjust the $list addition to your own needs. This example creates a title object for each and then calls the getPrefixedText method which returns the title as a string including namespace.
List categories an article belongs to
Check if a title is in a category
Article queries using DPL
This DPL query example provides a standard list of page links in wikitext bullet list format.
Misc
examineBraces
This function returns an array of the brace structure found in the passed wikitext parameter.
') {
$brace =& $braces[$depths[$depth-1]]; $brace[LENGTH] = $match[0][1]-$brace[OFFSET]+2; $brace[DEPTH] = $depth--; } else { $depths[$depth++] = count($braces); $braces[] = array( NAME => $match[1][0], OFFSET => $match[0][1] ); } } return $braces; } </php>}}
The following input,
Gives the following array:
The array output is designed to integrate with the strpos_replace function arguments subject, replace, offset, length. The index 0, 1, 2 order referes to the order functions are found (from left to right).
Google Analytics
Force all headings to use outline numbering
There is a user-preference to make all headings use outline numbering, but no way to make that a default for all users. Here's a few lines of code which can be added to your LocalSettings.php file which do that.
Wikitext in Sidebar
Replace a section such as toolbox or navlinks in the skins/MonoBook.php file with the following: