Difference between revisions of "MediaWiki code snippets"

From Organic Design wiki
m (Update templates)
(just stick to small snippets)
Line 2: Line 2:
  
 
=== Raw wikitext content ===
 
=== 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 ===
 
=== Return an HTTP error page ===
  
== Creating an updating articles ==
+
== Creating and updating articles ==
  
 
=== Edit or create ===
 
=== Edit or create ===
  
=== Expand templates ===
+
== 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>
  
=== Update templates ===
+
== MediaWiki Environment ==
This function takes two strings of wikitext. The first is some content containing transcluded templates (or vriables/parser-functions). The parameter contains new content for one or more of the templates. If no templates match, the content will act
+
=== Article title ===
 +
This should be called at an appropriate time such as from the ''OutputPageBeforeHTML'' hook.
 
<php>
 
<php>
function updateTemplates($content,$updates) {
+
$wgOut->setPageTitle('foo');
 +
</php>
  
# Get the offsets and lengths of template definitions in $content
+
== Article queries ==
$cbraces = array();
+
=== List article titles from a category ===
$cdepths = array();
+
<php>
$this->examineBraces($content,$cbraces,$cdepths);
+
$db = &wfGetDB(DB_SLAVE);
 
+
$cl = $db->tableName('categorylinks');
# Get the offsets and lengths of template definitions in $updates
+
$result = $db->query("SELECT cl_from FROM $cl WHERE cl_to = '$category' ORDER BY cl_sortkey");
$ubraces = array();
+
while ($row = mysql_fetch_row($result)) $titleText = Title::newFromID($row[0])->getText();
$udepths = array();
 
$this->examineBraces($updates,$ubraces,$udepths);
 
 
 
# Loop through the top-level braces in $updates
 
foreach ($udepths[1] as $ui) {
 
 
 
# Get the update text
 
$uoffset = $ubraces[$ui][SIMPLEFORMS_BRACES_OFFSET];
 
$ulength = $ubraces[$ui][SIMPLEFORMS_BRACES_LENGTH];
 
$update  = substr($updates,$uoffset,$ulength);
 
 
 
# Get braces in content with the same name as this update
 
$matches = array();
 
$uname  = $ubraces[$ui][SIMPLEFORMS_BRACES_NAME];
 
foreach ($cbraces as $ci => $cb) if ($cb[SIMPLEFORMS_BRACES_NAME] == $uname) $matches[] = $ci;
 
 
 
# todo: while (count($matches) > 1), remove if par($par++) not equal (break if can't reduce to less than 1)
 
 
 
# If matches has been reduced to a single item, update the template in the content
 
if (count($matches) == 1) {
 
$coffset = $cbraces[$matches[0]][SIMPLEFORMS_BRACES_OFFSET];
 
$clength = $cbraces[$matches[0]][SIMPLEFORMS_BRACES_LENGTH];
 
$content = substr_replace($content,$update,$coffset,$clength);
 
}
 
}
 
return $content;
 
}
 
 
</php>
 
</php>
 
[[Category:MediaWiki]]
 
[[Category:MediaWiki]]

Revision as of 04:43, 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>