Difference between revisions of "MediaWiki code snippets"

From Organic Design wiki
(Starting an article for code snippets, functions and where they're used)
 
m (Update templates)
Line 13: Line 13:
 
=== Update templates ===
 
=== Update templates ===
 
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
 
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
 +
<php>
 +
function updateTemplates($content,$updates) {
  
 +
# Get the offsets and lengths of template definitions in $content
 +
$cbraces = array();
 +
$cdepths = array();
 +
$this->examineBraces($content,$cbraces,$cdepths);
 +
 +
# Get the offsets and lengths of template definitions in $updates
 +
$ubraces = array();
 +
$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>
 
[[Category:MediaWiki]]
 
[[Category:MediaWiki]]

Revision as of 04:26, 14 June 2007

Returning content to the client

Raw wikitext content

Return an HTTP error page

Creating an updating articles

Edit or create

Expand templates

Update templates

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 <php> function updateTemplates($content,$updates) {

# Get the offsets and lengths of template definitions in $content $cbraces = array(); $cdepths = array(); $this->examineBraces($content,$cbraces,$cdepths);

# Get the offsets and lengths of template definitions in $updates $ubraces = array(); $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>