Here's a useful checklist of things to have set up before starting with MediaWiki development; see also MW:How to debug and MW:Developer hub.
Full browser and shell access to a running instance of every major version
Local access to the source code for all those versions
Use wfDebugDieBacktrace() to stop code and report the call stack
A good text editor with regular expression support file search capability (we use Geany which runs on most platforms)
A wikia setup so that extension code changes can be quickly tested in all versions and different extension versions can be quickly swapped
Tools or code for being able to stop the php code and output the necessary items in the scope
Tools for debugging the JS and CSS environment (eg. firebug)
Bookmarks for all the documentation you've found useful
Bookmarks to code snippets and examples (within your own wiki, http://mediawiki.org, other sites and in the mediawiki code)
Your own repository of snippets for achieving common objectives in the mediawiki runtime environment
Articles
Get article content
$title = Title::newFromText($titleText);
$article = new Article($title);
$wikitext = $article->getContent();
Edit or create an article
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
$title = Title::newFromText($titleText);
$article = new Article($title);
$article->doEdit($text, $summary, EDIT_UPDATE|EDIT_MINOR);
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);
if($image && $image->exists()){$url = $image->getURL(); # Gets the URL for the image at its normal size$url_50px = $image->getThumbnail(50,50)->getUrl(); # Gets the URL for the image at a specified size}
Image links
Sometimes you might want to prevent images from linking to the image page. This extension function removes the link.
Probably the most common security-related LocalSettings hack is to allow pages in locked down wikis to be publicly viewable if they're in a particular category. This following example adds articles in Category:Public to the $wgWhitelistRead array.
From MediaWiki 1.17 onwards, the OutputPageBodyAttributes hook can be used to modify the classes and other attributed of the body tag. In the following example the hook is being used to add classes to the body tag depending on whether the user is anonymous, logged-in or a sysop.
The following snippet causes all requests to be redirected to HTTPS if the user is a sysop. It also bounces users to non-HTTPS if not a sysop which can be useful if you don't want HTTPS links showing up in search engines for example if you have a self-signed certificate (it doesn't do this if the user is on the login page though since that confuses our bots that use HTTPS connections):
# Force HTTPS for sysops (and non-HTTPS for non-sysops)$wgExtensionFunctions[] = 'wfSecureSysops';
function wfSecureSysops(){global$wgUser;
if(in_array('sysop', $wgUser->getEffectiveGroups())){if( !isset($_SERVER['HTTPS'])){header("Location: https://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']);
exit;
}}else{if(isset($_SERVER['HTTPS']) && ( !array_key_exists('title', $_REQUEST) || $_REQUEST['title'] != 'Special:UserLogin')){header("Location: http://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']);
exit;
}}}
The following code checks if a request is local, and works regardless of the IP address or whether it's IPv4 or IPv6.
function isLocal(){returnpreg_match_all("|inet6? addr:\s*([0-9a-f.:]+)|", `/sbin/ifconfig`, $matches) && in_array($_SERVER['REMOTE_ADDR'], $matches[1]);
}
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.
function raw($text, $expand = false, $save = false){global$wgOut, $wgParser;
if($expand)$text = $wgParser->preprocess($text, new Title(), new ParserOptions());
$wgOut->disable();
wfResetOutputBuffers();
header('Content-Type: application/octet-stream');
if($save)header("Content-Disposition: attachment; filename=\"$save\"");
echo$text;
}
Return an HTTP error page
global$wgOut, $wgParser;
$wgOut->disable();
wfResetOutputBuffers();
header('HTTP/1.0 404 Not Found');
$err = '<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN"><html><head><title>404 Not Found</title></head>
<body><h1>Not Found</h1><p>The requested URL was not found on this server.</p></body></html>';
echo$err;
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.
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.
Make sortable table states persistent with cookies
Note that this function requires getCookie and setCookie which are currently in our navbar.js. They're just the standard cookie getting and setting functions recommended by W3C.
addOnloadHook(function(){
$('.sortable').each(function(){var id = $(this).attr('id');
document.shCookie = getCookie('sortheader-'+id);
document.sortheaderId = 0;
$('#'+id+' a.sortheader').each(function(){var id = $(this).parent().parent().parent().parent().attr('id');
var sh = document.sortheaderId++;
if( sh+100 == document.shCookie){ ts_resortTable(this); ts_resortTable(this); }if( sh == document.shCookie){ ts_resortTable(this); sh += 100; }
$(this).bind('click', {id: id, sh: sh}, function(e){
setCookie('sortheader-'+e.data.id, e.data.sh, 1);
});
});
});
});
MediaWiki Environment
Article title
This should be called at an appropriate time such as from the OutputPageBeforeHTML hook.
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.
function removeEmptyCategories(){// Get all category pages$dbr = &wfGetDB(DB_SLAVE);
$cats = array();
$table = $dbr->tableName('page');
$res = $dbr->select($table, 'page_title', "page_namespace = " . NS_CATEGORY );
while($row = $dbr->fetchRow($res))$cats[] = $row[0];
$dbr->freeResult($res);
// Delete those which are for empty catsforeach($catsas$cat){$dbr = &wfGetDB( DB_SLAVE );
$cl = $dbr->tableName('categorylinks');
$qcat = $dbr->addQuotes( Title::newFromText($cat)->getDBkey());
if( !$dbr->selectRow($cl, 'cl_from', "cl_to = $qcat")){$title = Title::newFromText($cat, NS_CATEGORY );
$article = new Article($title);
$article->doDelete("Obsolete category page - this category contains no items");
}}}
Article queries using DPL
This DPL query example provides a standard list of page links in wikitext bullet list format.
{{#dpl:category=Foo|format=,*[[%PAGE%]],\n,}}
Misc
examineBraces
This function returns an array of the brace structure found in the passed wikitext parameter. This has also been implemented in Perl, see the wikiExamineBraces function in wiki.pl.
The array output is designed to integrate with the substr_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
$wgExtensionFunctions[] = 'wfGoogleAnalytics';
function wfGoogleAnalytics(){global$wgOut;
$wgOut->addScript('<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src=\'" + gaJsHost + "google-analytics.com/ga.js\' type=\'text/javascript\'%3E%3C/script%3E"));
</script><script type="text/javascript">
var pageTracker = _gat._getTracker("INSERT YOUR TRACKING CODE HERE");
pageTracker._trackPageview();</script>');
}
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.
$wgExtensionFunctions[] = 'wfNumberHeadings';
function wfNumberHeadings(){global$wgUser;
$wgUser->setOption('numberheadings', true);
}
Integrating with the Skin
Adding a new action tab
The following code adds a new action tab with a corresponding link. To process the new action, add a processing function to the UnknownAction hook.
The Vector skin (optional on 1.16, default on 1.17 onwards) uses a new hook, SkinTemplateNavigation. You can cover both Vector and earlier skins like Monobook or Modern by including code for both hooks. The example below uses the MediaWiki namespace to name the relevant elements of the array. You can put this code in LocalSettings after the inclusion of extensions.
By default the MediaWiki:Sidebar article content is not normal wikitext. This snippet fixes that and also ensures that the content can fall back to an i18n message if the article is not present - the normal behaviour for articles in the MediaWiki namespace. Replace a section such as toolbox or navlinks in the skins/MonoBook.php file with the following:
global$wgUser, $wgTitle, $wgParser;
$title = 'sidebar'; # note the lcfirst is important here since it's also a msg key$article = new Article( Title::newFromText($title, NS_MEDIAWIKI ));
$text = $article->fetchContent();
if(empty($text))$text = wfMsg($title);
if(is_object($wgParser)){$psr = $wgParser; $opt = $wgParser->mOptions; }else{$psr = new Parser; $opt = NULL; }if( !is_object($opt))$opt = ParserOptions::newFromUser($wgUser);
echo$psr->parse($text, $wgTitle, $opt, true, true)->getText();
The procedure is the same with Vector-based skins, the change is you now place the code inside divs of id portal and then id body, as per the other examples in skins/Vector.php. It should replace the following code and/or the toolbox:
The idea is to have a picture or text that allows a developer to quickly see that they are on the development instance and not the production instance of MediaWiki.
By adding to the content of $wgSiteNotice in a conditional every article will have the change for that domain map.
if($wgServer == "http://localhost")# or any other domain$wgSiteNotice = '<span style="position:absolute;top:0px;left:-140px;z-index:10">[[Image:Gnome-devel.png|100px]]</span>';
The following snippet imports articles from the XML export file named in $file into the main namespace. The $resultCount variable holds the number of articles imported on success.
Here's a useful checklist of things to have set up before starting with MediaWiki development; see also MW:How to debug and MW:Developer hub.
Full browser and shell access to a running instance of every major version
Local access to the source code for all those versions
Use wfDebugDieBacktrace() to stop code and report the call stack
A good text editor with regular expression support file search capability (we use Geany which runs on most platforms)
A wikia setup so that extension code changes can be quickly tested in all versions and different extension versions can be quickly swapped
Tools or code for being able to stop the php code and output the necessary items in the scope
Tools for debugging the JS and CSS environment (eg. firebug)
Bookmarks for all the documentation you've found useful
Bookmarks to code snippets and examples (within your own wiki, http://mediawiki.org, other sites and in the mediawiki code)
Your own repository of snippets for achieving common objectives in the mediawiki runtime environment
Articles
Get article content
$title = Title::newFromText($titleText);
$article = new Article($title);
$wikitext = $article->getContent();
Edit or create an article
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
$title = Title::newFromText($titleText);
$article = new Article($title);
$article->doEdit($text, $summary, EDIT_UPDATE|EDIT_MINOR);
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);
if($image && $image->exists()){$url = $image->getURL(); # Gets the URL for the image at its normal size$url_50px = $image->getThumbnail(50,50)->getUrl(); # Gets the URL for the image at a specified size}
Image links
Sometimes you might want to prevent images from linking to the image page. This extension function removes the link.
Probably the most common security-related LocalSettings hack is to allow pages in locked down wikis to be publicly viewable if they're in a particular category. This following example adds articles in Category:Public to the $wgWhitelistRead array.
From MediaWiki 1.17 onwards, the OutputPageBodyAttributes hook can be used to modify the classes and other attributed of the body tag. In the following example the hook is being used to add classes to the body tag depending on whether the user is anonymous, logged-in or a sysop.
The following snippet causes all requests to be redirected to HTTPS if the user is a sysop. It also bounces users to non-HTTPS if not a sysop which can be useful if you don't want HTTPS links showing up in search engines for example if you have a self-signed certificate (it doesn't do this if the user is on the login page though since that confuses our bots that use HTTPS connections):
# Force HTTPS for sysops (and non-HTTPS for non-sysops)$wgExtensionFunctions[] = 'wfSecureSysops';
function wfSecureSysops(){global$wgUser;
if(in_array('sysop', $wgUser->getEffectiveGroups())){if( !isset($_SERVER['HTTPS'])){header("Location: https://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']);
exit;
}}else{if(isset($_SERVER['HTTPS']) && ( !array_key_exists('title', $_REQUEST) || $_REQUEST['title'] != 'Special:UserLogin')){header("Location: http://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']);
exit;
}}}
The following code checks if a request is local, and works regardless of the IP address or whether it's IPv4 or IPv6.
function isLocal(){returnpreg_match_all("|inet6? addr:\s*([0-9a-f.:]+)|", `/sbin/ifconfig`, $matches) && in_array($_SERVER['REMOTE_ADDR'], $matches[1]);
}
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.
function raw($text, $expand = false, $save = false){global$wgOut, $wgParser;
if($expand)$text = $wgParser->preprocess($text, new Title(), new ParserOptions());
$wgOut->disable();
wfResetOutputBuffers();
header('Content-Type: application/octet-stream');
if($save)header("Content-Disposition: attachment; filename=\"$save\"");
echo$text;
}
Return an HTTP error page
global$wgOut, $wgParser;
$wgOut->disable();
wfResetOutputBuffers();
header('HTTP/1.0 404 Not Found');
$err = '<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN"><html><head><title>404 Not Found</title></head>
<body><h1>Not Found</h1><p>The requested URL was not found on this server.</p></body></html>';
echo$err;
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.
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.
Make sortable table states persistent with cookies
Note that this function requires getCookie and setCookie which are currently in our navbar.js. They're just the standard cookie getting and setting functions recommended by W3C.
addOnloadHook(function(){
$('.sortable').each(function(){var id = $(this).attr('id');
document.shCookie = getCookie('sortheader-'+id);
document.sortheaderId = 0;
$('#'+id+' a.sortheader').each(function(){var id = $(this).parent().parent().parent().parent().attr('id');
var sh = document.sortheaderId++;
if( sh+100 == document.shCookie){ ts_resortTable(this); ts_resortTable(this); }if( sh == document.shCookie){ ts_resortTable(this); sh += 100; }
$(this).bind('click', {id: id, sh: sh}, function(e){
setCookie('sortheader-'+e.data.id, e.data.sh, 1);
});
});
});
});
MediaWiki Environment
Article title
This should be called at an appropriate time such as from the OutputPageBeforeHTML hook.
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.
function removeEmptyCategories(){// Get all category pages$dbr = &wfGetDB(DB_SLAVE);
$cats = array();
$table = $dbr->tableName('page');
$res = $dbr->select($table, 'page_title', "page_namespace = " . NS_CATEGORY );
while($row = $dbr->fetchRow($res))$cats[] = $row[0];
$dbr->freeResult($res);
// Delete those which are for empty catsforeach($catsas$cat){$dbr = &wfGetDB( DB_SLAVE );
$cl = $dbr->tableName('categorylinks');
$qcat = $dbr->addQuotes( Title::newFromText($cat)->getDBkey());
if( !$dbr->selectRow($cl, 'cl_from', "cl_to = $qcat")){$title = Title::newFromText($cat, NS_CATEGORY );
$article = new Article($title);
$article->doDelete("Obsolete category page - this category contains no items");
}}}
Article queries using DPL
This DPL query example provides a standard list of page links in wikitext bullet list format.
{{#dpl:category=Foo|format=,*[[%PAGE%]],\n,}}
Misc
examineBraces
This function returns an array of the brace structure found in the passed wikitext parameter. This has also been implemented in Perl, see the wikiExamineBraces function in wiki.pl.
The array output is designed to integrate with the substr_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
$wgExtensionFunctions[] = 'wfGoogleAnalytics';
function wfGoogleAnalytics(){global$wgOut;
$wgOut->addScript('<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src=\'" + gaJsHost + "google-analytics.com/ga.js\' type=\'text/javascript\'%3E%3C/script%3E"));
</script><script type="text/javascript">
var pageTracker = _gat._getTracker("INSERT YOUR TRACKING CODE HERE");
pageTracker._trackPageview();</script>');
}
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.
$wgExtensionFunctions[] = 'wfNumberHeadings';
function wfNumberHeadings(){global$wgUser;
$wgUser->setOption('numberheadings', true);
}
Integrating with the Skin
Adding a new action tab
The following code adds a new action tab with a corresponding link. To process the new action, add a processing function to the UnknownAction hook.
The Vector skin (optional on 1.16, default on 1.17 onwards) uses a new hook, SkinTemplateNavigation. You can cover both Vector and earlier skins like Monobook or Modern by including code for both hooks. The example below uses the MediaWiki namespace to name the relevant elements of the array. You can put this code in LocalSettings after the inclusion of extensions.
By default the MediaWiki:Sidebar article content is not normal wikitext. This snippet fixes that and also ensures that the content can fall back to an i18n message if the article is not present - the normal behaviour for articles in the MediaWiki namespace. Replace a section such as toolbox or navlinks in the skins/MonoBook.php file with the following:
global$wgUser, $wgTitle, $wgParser;
$title = 'sidebar'; # note the lcfirst is important here since it's also a msg key$article = new Article( Title::newFromText($title, NS_MEDIAWIKI ));
$text = $article->fetchContent();
if(empty($text))$text = wfMsg($title);
if(is_object($wgParser)){$psr = $wgParser; $opt = $wgParser->mOptions; }else{$psr = new Parser; $opt = NULL; }if( !is_object($opt))$opt = ParserOptions::newFromUser($wgUser);
echo$psr->parse($text, $wgTitle, $opt, true, true)->getText();
The procedure is the same with Vector-based skins, the change is you now place the code inside divs of id portal and then id body, as per the other examples in skins/Vector.php. It should replace the following code and/or the toolbox:
The idea is to have a picture or text that allows a developer to quickly see that they are on the development instance and not the production instance of MediaWiki.
By adding to the content of $wgSiteNotice in a conditional every article will have the change for that domain map.
if($wgServer == "http://localhost")# or any other domain$wgSiteNotice = '<span style="position:absolute;top:0px;left:-140px;z-index:10">[[Image:Gnome-devel.png|100px]]</span>';
The following snippet imports articles from the XML export file named in $file into the main namespace. The $resultCount variable holds the number of articles imported on success.