Difference between revisions of "MediaWiki code snippets"
m (pre -> php tags) |
(→Raw wikitext content) |
||
Line 2: | Line 2: | ||
=== Raw wikitext content === | === 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> | <php> | ||
− | + | function raw($text,$expand = false,$save = false) { | |
− | $ | + | global $wgOut,$wgParser,$wgRequest; |
− | header(" | + | if ($expand) $text = $wgParser->preprocess($text,new Title(),new ParserOptions()); |
− | $text = $ | + | 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> | </php> |
Revision as of 04:42, 19 June 2007
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 $wgOut,$wgParser,$wgRequest; 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
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: <php> 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 ) )
</php>