MediaWiki code snippets
Contents
Returning content to the client
Raw wikitext content
This function returns the raw content specified in $text, it can be callled any time from extension-setup or after. If the $save parameter is supplied it will bring up a download dialog with teh 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. <php> function raw($text,$expand = false,$save = false) { global $wgParser; if ($expand) $text = $wgParser->preprocess($text,new Title(),new ParserOptions()); while(@ob_end_clean()); header('Content-Type: application/octet-stream'); if ($save) header("Content-Disposition: attachment; filename=\"$save\""); if (in_array('Content-Encoding: gzip',headers_list())) $text = gzencode($text); echo $text; die; } </php>
Return an HTTP error page
<php> while(@ob_end_clean()); header('HTTP/1.0 404 Not Found'); $err = '<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
Not Found
The requested URL was not found on this server.
'; if (in_array('Content-Encoding: gzip',headers_list())) $err = gzencode($err); echo $err; die; </php>
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. <php> $d = $_SERVER['SERVER_NAME']; $t = $_REQUEST['title']; if (empty($t)) $t = ereg_replace('^/',,$_SERVER['PATH_INFO']); if (empty($t) || $t == 'Main_Page') { if (ereg('abc.org$',$d)) header("Location: $wgServer/Welcome_to_ABC") && die; if ($d == 'www.xyz.com') header("Location: $wgServer/XYZ_Home") && die; } </php>
Articles
Get article content
<php> $title = Title::newFromText($titleText); $article = new Article($title); $wikitext = $article->getContent(); </php>
Edit or create
Using the parser
Parse wikitext
<php> $html = $wgParser->parse($wikitext,$title,new ParserOptions(),true,true)->getText(); </php>
Expand templates only
<php> $wikitext = $wgParser->preprocess($wikitext,$title,new ParserOptions()); </php>
MediaWiki Environment
Article title
This should be called at an appropriate time such as from the OutputPageBeforeHTML hook. <php> $wgOut->setPageTitle('foo'); </php>
Article queries
List article titles from a category
<php> $db = &wfGetDB(DB_SLAVE); $cl = $db->tableName('categorylinks'); $result = $db->query("SELECT cl_from FROM $cl WHERE cl_to = '$category' ORDER BY cl_sortkey"); while ($row = mysql_fetch_row($result)) $titleText = Title::newFromID($row[0])->getText(); </php>
Check if a title is in a category
<php> function inCat($title,$cat) { if (!is_object($title)) $title = Title::newFromText($title)); $id = $title->getArticleID() $db = &wfGetDB(DB_SLAVE); $cl = $db->tableName('categorylinks'); $result = $db->query("SELECT 0 FROM $cl WHERE cl_from = $id AND cl_to = '$cat'"); return is_array(mysql_fetch_row($result)); } </php>
Misc
examineBraces
This function returns an array of the brace structure found in the passed wikitext parameter. <php> function examineBraces(&$content) { $braces = array(); $depths = array(); $depth = 1; $index = 0; while (preg_match('/\\{\\{\\s*([#a-z0-9_]*)|\\}\\}/is',$content,$match,PREG_OFFSET_CAPTURE,$index)) { $index = $match[0][1]+2; if ($match[0][0] == '}}') { $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,
foo{{#bar:baz|biz{{foo|shmoo}}}}{{moo}}baz
Gives the following array:
Array( [0] => Array( [NAME] => #bar [OFFSET] => 3 [LENGTH] => 29 [DEPTH] => 1 ) [1] => Array( [NAME] => foo [OFFSET] => 17 [LENGTH] => 13 [DEPTH] => 2 ) [2] => Array( [NAME] => moo [OFFSET] => 32 [LENGTH] => 7 [DEPTH] => 1 ) )