MediaWiki code snippets

From Organic Design wiki
Revision as of 04:43, 14 June 2007 by Nad (talk | contribs) (just stick to small snippets)

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>