Difference between revisions of "MediaWiki code snippets"
(just stick to small snippets) |
(examineTemplates()) |
||
Line 42: | Line 42: | ||
while ($row = mysql_fetch_row($result)) $titleText = Title::newFromID($row[0])->getText(); | while ($row = mysql_fetch_row($result)) $titleText = Title::newFromID($row[0])->getText(); | ||
</php> | </php> | ||
+ | |||
+ | == Misc == | ||
+ | === examineTemplates === | ||
+ | <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]]; | ||
+ | $brace[SIMPLEFORMS_BRACES_LENGTH] = $match[0][1]-$brace[SIMPLEFORMS_BRACES_OFFSET]+2; | ||
+ | } | ||
+ | else { | ||
+ | $depths[$depth++] = count($braces); | ||
+ | $braces[] = array( | ||
+ | SIMPLEFORMS_BRACES_NAME => $match[1][0], | ||
+ | SIMPLEFORMS_BRACES_OFFSET => $match[0][1] | ||
+ | ); | ||
+ | } | ||
+ | } | ||
+ | return $braces; | ||
+ | } | ||
+ | </php> | ||
+ | The following input, | ||
+ | foo{{#bar:baz|biz{{foo|shmoo}}}}{{moo}}baz | ||
+ | Gives the following array: | ||
+ | <pre> | ||
+ | Array | ||
+ | ( | ||
+ | [0] => Array | ||
+ | ( | ||
+ | [0] => #bar | ||
+ | [1] => 3 | ||
+ | [2] => 29 | ||
+ | ) | ||
+ | |||
+ | [1] => Array | ||
+ | ( | ||
+ | [0] => foo | ||
+ | [1] => 17 | ||
+ | [2] => 13 | ||
+ | ) | ||
+ | |||
+ | [2] => Array | ||
+ | ( | ||
+ | [0] => moo | ||
+ | [1] => 32 | ||
+ | [2] => 7 | ||
+ | ) | ||
+ | |||
+ | ) | ||
+ | </pre> | ||
[[Category:MediaWiki]] | [[Category:MediaWiki]] |
Revision as of 09:10, 14 June 2007
Contents
Returning content to the client
Raw wikitext content
<php> if ($rev = Revision::newFromTitle($title)) { $lastmod = wfTimestamp(TS_RFC2822,$rev->getTimestamp()); header("Last-modified: $lastmod"); $text = $rev->getText(); return $text; } </php>
Return an HTTP error page
Creating and updating articles
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>
Misc
examineTemplates
<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]]; $brace[SIMPLEFORMS_BRACES_LENGTH] = $match[0][1]-$brace[SIMPLEFORMS_BRACES_OFFSET]+2; } else { $depths[$depth++] = count($braces); $braces[] = array( SIMPLEFORMS_BRACES_NAME => $match[1][0], SIMPLEFORMS_BRACES_OFFSET => $match[0][1] ); } } return $braces; } </php> The following input,
foo{{#bar:baz|biz{{#workflow:foo
|Stub=Template:Document/Stub |bar=baz }}}}Template:Moobaz Gives the following array:
Array ( [0] => Array ( [0] => #bar [1] => 3 [2] => 29 ) [1] => Array ( [0] => foo [1] => 17 [2] => 13 ) [2] => Array ( [0] => moo [1] => 32 [2] => 7 ) )