Difference between revisions of "MediaWiki code snippets"

From Organic Design wiki
(Misc: update)
m (Misc)
Line 69: Line 69:
 
}</php>
 
}</php>
 
The following input,
 
The following input,
foo{{#bar:baz|biz{{foo|shmoo}}}}{{moo}}baz
+
<pre>foo{{#bar:baz|biz{{foo|shmoo}}}}{{moo}}baz</pre>
 
Gives the following array:
 
Gives the following array:
 
<pre>
 
<pre>

Revision as of 09:32, 14 June 2007

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{{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
        )
    )