Open main menu

Extension:PropertyTable2/PropertyTable2 1.php

This snip of code works on MW 1.17 and should work on 1.16. Copy and save as PropertyTable.php and replace the older one.

The locale tag name feature of 1.2.0 is not present, but it is more simple to add a class for each table: just add the class attribute to the properties tag.

<?php
// PropertyTable2 MediaWiki Extension.
// Outputs a table with keys and values.
 
// Copyright (C) 2007, Benner Sistemas.
// Copyright (C) 2007-2011, 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

if( !defined( 'MEDIAWIKI' ) ) {
	echo( "This is an extension to the MediaWiki package and cannot be run standalone.\n" );
	die( -1 );
}

// Extension credits that will show up on Special:Version    
$wgExtensionCredits['parserhook'][] = array(
        'path'           => __FILE__,
        'name'           => 'PropertyTable2',
        'version'        => '1.3.0',
        'author'         => 'Giuseppe Briotti', 
        'url'            => 'http://www.mediawiki.org/wiki/Extension:PropertyTable2',
        'descriptionmsg' => 'Outputs a table with keys and values',
        'description'    => 'Create a table starting to a key-value pairs'
);

$wgHooks['ParserFirstCallInit'][] = 'efPropertyTable2Init';

#----------------------------------------------------------------------------
#    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"';
 
// Hook our callback function into the parser
function efPropertyTable2Init( Parser &$parser ) {
    $parser->setHook( 'properties', 'efPropertyTable2' );
    return true;
}
 
// Execute 
function efPropertyTable2( $input, array $args, Parser $parser, PPFrame $frame ) {
    // check if a class attribute is present in property tag and pass to the function
    $classe = $args['class'] ;
    $output = fnPropertyTable2Tag($input, $parser, $classe);      
    return $output ;
}

// Processes the tag.
function fnPropertyTable2Tag($input, $parser, $classe) {
    // initialize
    $sequence = 0;
    $rowcounter = 0;
    $maxrowcounter = 0;
    $blocks=0;
        
    // parse input
    $properties = array(); 
    
    $tok = strtok($input, "\n");
    while ($tok !== false) {
        $line = explode("=", $tok);
        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;
    if($classe==""){
        $result = "<table $wgPropertyTable2Attributes>\n";
    } else {
        $result = "<table class='$classe'>\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);
            //hack to strip off NewPP limit report text that may be appended to string as html comment
            $html_title = preg_replace('/<!--[^>]*>\s*/','',$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);
            //hack to strip off NewPP limit report text that may be appended to string as html comment
            $html_key  = preg_replace('/<!--[^>]*>\s*/','',$pout->getText());

            $pout = $parser->parse($value, $parser->mTitle, $parser->mOptions, false, false);
            //hack to strip off NewPP limit report text that may be appended to string as html comment
            $html_value = preg_replace('/<!--[^>]*>\s*/','',$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;
}