Extension:PropertyTable2/PropertyTable2.php
From MediaWiki.org
<?php // PropertyTable2 MediaWiki Extension. // Outputs a table with keys and values. // Copyright (C) 2007, Benner Sistemas. // Copyright (C) 2007, Giuseppe Briotti. // // This program is free software; you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation; either version 2 of the License, or // (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this program; if not, write to the Free Software // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA $PropertyTable2Version = '1.2.0-GB'; #---------------------------------------------------------------------------- # Internationalized messages #---------------------------------------------------------------------------- $wgPropertyTable2Prefix = "propertytable2_"; $wgPropertyTable2Messages = array(); // English $wgPropertyTable2Messages['en'] = array( $wgPropertyTable2Prefix . 'tag' => 'properties', ); // Polish (PL) $wgPropertyTable2Messages['pl'] = array( $wgPropertyTable2Prefix . 'tag' => 'wlasciwosci', ); // Portuguese (Brazilian) $wgPropertyTable2Messages['pt-br'] = array( $wgPropertyTable2Prefix . 'tag' => 'propriedades', ); #---------------------------------------------------------------------------- # Extension initialization #---------------------------------------------------------------------------- // HTML attributes of the table tag //$wgPropertyTable2Attributes = 'class="property_table" border="0" cellpadding="2" cellspacing="4"'; $wgPropertyTable2Attributes = 'class="tabella-normale"'; // HTML attributes of key cells //$wgPropertyTable2KeyAttributes = 'class="property_key_cell" valign="top" bgcolor="#E8F3FF" style="font-weight: bold"'; $wgPropertyTable2KeyAttributes = ''; // HTML attributes of value cells //$wgPropertyTable2ValueAttributes = 'class="property_value_cell" valign="top" bgcolor="#F3F3F3"'; $wgPropertyTable2ValueAttributes = 'align="center"'; // HTML attributes of header cells //$wgPropertyTable2HeaderAttributes = 'class="property_value_cell" valign="top" bgcolor="#F3F3F3" align="center" colspan="2"'; $wgPropertyTable2HeaderAttributes = 'colspan="2"'; // credits $wgExtensionCredits['parserhook'][] = array( 'name'=>'PropertyTable2', 'version'=>$PropertyTable2Version, 'author'=>'Fernando Correia, Giuseppe Briotti', 'url'=>'http://www.mediawiki.org/wiki/Extension:PropertyTable2', 'description' => 'Outputs a table with keys and values' ); // extension initialization $wgExtensionFunctions[] = "fnPropertyTable2Extension"; $wgExtensionFunctions[] = 'wfPropertyTable2ParserFunctionSetup'; $wgHooks['LanguageGetMagic'][] = 'wfPropertyTable2ParserFunctionMagic'; // Registers the extension with the WikiText parser. function fnPropertyTable2Extension() { // register messages global $wgMessageCache, $wgPropertyTable2Messages; foreach( $wgPropertyTable2Messages as $sLang => $aMsgs ) { $wgMessageCache->addMessages( $aMsgs, $sLang ); } // register the extension with the WikiText parser global $wgParser; $wgParser->setHook( wfMsg('propertytable2_tag'), "fnPropertyTable2Tag" ); } // Initializes the parser function. function wfPropertyTable2ParserFunctionSetup() { global $wgParser; # Set a function hook associating the magic word with our function $wgParser->setFunctionHook( "properties", 'wfPropertyTable2ParserFunctionRender' ); } // Configures the parser function magic word. function wfPropertyTable2ParserFunctionMagic( &$magicWords, $langCode ) { # Add the magic word # The first array element is case sensitivity, in this case it is not case sensitive # All remaining elements are synonyms for our parser function $magicWords["properties"] = array( 0, "properties", "propriedades", "wlasciwosci" ); // TODO: build from $wgPropertyTable2Messages # unless we return true, other parser functions extensions won't get loaded. return true; } #---------------------------------------------------------------------------- # Event handlers #---------------------------------------------------------------------------- // Processes the parser function. // Generates HTML using extension tag. // The parser function can be used within templates. function wfPropertyTable2ParserFunctionRender( &$parser, $lines ) { // return HTML using extension tag $tag = wfMsg('propertytable2_tag'); $result = "<$tag>\n"; $result .= $lines; $result .= "</$tag>\n"; return $result; } // Processes the tag. function fnPropertyTable2Tag($input, $argv, &$parser) { // initialize $sequence = 0; $rowcounter = 0; $maxrowcounter = 0; $blocks=0; // parse input $properties = array(); $tok = strtok($input, "\n"); while ($tok !== false) { //$line = explode("=", $tok); //Don't use explode, better is this: $eqpos=strpos($tok,"="); $line = array(substr($tok,0,$eqpos),substr($tok,$eqpos+1)); if (count($line) == 2) { $properties[] = $line; // search for the max number of rows in all blocks if (trim($line[0]) == "@title") { $blocks++; $maxrowcounter = max($maxrowcounter, $rowcounter); $rowcounter=0; } else { $rowcounter++; } } $tok = strtok("\n"); } $maxrowcounter = max($maxrowcounter, $rowcounter); // produce output if (count($properties) == 0) return ''; global $wgPropertyTable2Attributes, $wgPropertyTable2KeyAttributes, $wgPropertyTable2ValueAttributes, $wgPropertyTable2HeaderAttributes; $result = "<table $wgPropertyTable2Attributes>\n"; //block titles and data rows must be assembled in $result at the end of process $title = ""; //pre size the array of rows $rows = array_fill(1,$maxrowcounter,"<tr>"); $rowcounter=0; $blockcounter=0; foreach ($properties as $line) { $key = trim($line[0]); //the key only introduce a title, fill the variable for titles row... if ($key == "@title") { $blockcounter++; $pout = $parser->parse(trim($line[1]), $parser->mTitle, $parser->mOptions, false, false); $html_title = $pout->getText(); if ($title == "") { $title .= "<tr>"; } $title .= "<th $wgPropertyTable2HeaderAttributes>$html_title</th>"; //insert the empty cells (if any) in previous block if ($blockcounter > 1 && $rowcounter < $maxrowcounter) { for ($i = $rowcounter + 1; $i <= $maxrowcounter; $i++) { $rows[$i] .= "<td></td><td></td>"; } } $rowcounter=0; } else { $rowcounter++; //...or the key is a property or a counter (as usual) if ($key == "#") { $sequence++; $key = strval($sequence); } else if ($key == "@") { $sequence++; $key = chr(64 + $sequence); } $value = trim($line[1]); $pout = $parser->parse($key, $parser->mTitle, $parser->mOptions, false, false); $html_key = $pout->getText(); $pout = $parser->parse($value, $parser->mTitle, $parser->mOptions, false, false); $html_value = $pout->getText(); $rows[$rowcounter] .= "<td $wgPropertyTable2KeyAttributes>$html_key</td>" . "<td $wgPropertyTable2ValueAttributes>$html_value</td>"; } } //insert the empty cells (if any) in previous block if ($blockcounter > 1 && $rowcounter < $maxrowcounter) { for ($i = $rowcounter + 1; $i <= $maxrowcounter; $i++) { $rows[$i] .= "<td></td><td></td>"; } } // finally, the assembly $result .= $title . "</tr>\n"; for ($i = 1; $i<=$maxrowcounter; $i++) { $result .= $rows[$i] . "</tr>\n"; } $result .= "</table>\n"; return $result; } ?>
