Difference between revisions of "MediaWiki code snippets"
(examineTemplates()) |
(→Misc: update) |
||
Line 54: | Line 54: | ||
$index = $match[0][1]+2; | $index = $match[0][1]+2; | ||
if ($match[0][0] == '}}') { | if ($match[0][0] == '}}') { | ||
− | $brace =& $braces[$depths[ | + | $brace =& $braces[$depths[$depth-1]]; |
− | $brace[ | + | $brace[LENGTH] = $match[0][1]-$brace[OFFSET]+2; |
+ | $brace[DEPTH] = $depth--; | ||
} | } | ||
else { | else { | ||
$depths[$depth++] = count($braces); | $depths[$depth++] = count($braces); | ||
$braces[] = array( | $braces[] = array( | ||
− | + | NAME => $match[1][0], | |
− | + | OFFSET => $match[0][1] | |
); | ); | ||
} | } | ||
} | } | ||
return $braces; | return $braces; | ||
− | } | + | }</php> |
− | </php> | ||
The following input, | The following input, | ||
foo{{#bar:baz|biz{{foo|shmoo}}}}{{moo}}baz | foo{{#bar:baz|biz{{foo|shmoo}}}}{{moo}}baz | ||
Gives the following array: | Gives the following array: | ||
<pre> | <pre> | ||
− | Array | + | Array( |
− | ( | + | [0] => Array( |
− | [0] => Array | + | [NAME] => #bar |
− | + | [OFFSET] => 3 | |
− | + | [LENGTH] => 29 | |
− | + | [DEPTH] => 1 | |
− | |||
) | ) | ||
− | [1] => Array | + | [1] => Array( |
− | + | [NAME] => foo | |
− | + | [OFFSET] => 17 | |
− | + | [LENGTH] => 13 | |
− | + | [DEPTH] => 2 | |
) | ) | ||
− | [2] => Array | + | [2] => Array( |
− | + | [NAME] => moo | |
− | + | [OFFSET] => 32 | |
− | + | [LENGTH] => 7 | |
− | + | [DEPTH] => 1 | |
) | ) | ||
− | + | ) | |
− | ) | ||
</pre> | </pre> | ||
[[Category:MediaWiki]] | [[Category:MediaWiki]] |
Revision as of 09:31, 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-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{{#workflow:foo
|Stub=Template:Document/Stub |bar=baz }}}}Template:Moobaz 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 ) )