Extension:PropertyTable/PropertyTable.php

From MediaWiki.org
Jump to navigation Jump to search
<?php
// PropertyTable MediaWiki Extension.
// Outputs a table with keys and values.

// Copyright (C) 2007, Benner Sistemas.
//
// 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

$PropertyTableVersion = '1.2.0';

#----------------------------------------------------------------------------
#    Internationalized messages
#----------------------------------------------------------------------------

$wgPropertyTablePrefix = "propertytable_";
$wgPropertyTableMessages = array();

// English
$wgPropertyTableMessages['en'] = array(
    $wgPropertyTablePrefix . 'tag' => 'properties',    
);

// Polish (PL)
$wgPropertyTableMessages['pl'] = array(
    $wgPropertyTablePrefix . 'tag' => 'właściwości',
);

// Portuguese (Brazilian)
$wgPropertyTableMessages['pt-br'] = array(
    $wgPropertyTablePrefix . 'tag' => 'propriedades',
);

#----------------------------------------------------------------------------
#    Extension initialization
#----------------------------------------------------------------------------

// HTML attributes of the table tag
$wgPropertyTableAttributes = 'class="property_table" border="0" cellpadding="2" cellspacing="4"';
// HTML attributes of key cells
$wgPropertyTableKeyAttributes = 'class="property_key_cell" valign="top" bgcolor="#E8F3FF" style="font-weight: bold"';
// HTML attributes of value cells
$wgPropertyTableValueAttributes = 'class="property_value_cell" valign="top" bgcolor="#F3F3F3"';

// credits
$wgExtensionCredits['parserhook'][] = array(
    'name'=>'PropertyTable',
    'version'=>$PropertyTableVersion,
    'author'=>'Fernando Correia',
    'url'=>'http://www.mediawiki.org/wiki/Extension:PropertyTable',
    'description' => 'Outputs a table with keys and values'
    );

// extension initialization
$wgExtensionFunctions[] = "fnPropertyTableExtension";
$wgExtensionFunctions[] = 'wfPropertyTableParserFunctionSetup';
$wgHooks['LanguageGetMagic'][] = 'wfPropertyTableParserFunctionMagic';

// Registers the extension with the WikiText parser.
function fnPropertyTableExtension() {
    // register messages
    global $wgMessageCache, $wgPropertyTableMessages;
    foreach( $wgPropertyTableMessages as $sLang => $aMsgs ) {
        $wgMessageCache->addMessages( $aMsgs, $sLang );
    }       
    // register the extension with the WikiText parser
    global $wgParser;
    $wgParser->setHook( wfMsg('propertytable_tag'), "fnPropertyTableTag" );
}

// Initializes the parser function.
function wfPropertyTableParserFunctionSetup() {
	global $wgParser;
	# Set a function hook associating the magic word with our function
	$wgParser->setFunctionHook( "properties", 'wfPropertyTableParserFunctionRender' );
}

// Configures the parser function magic word.
function wfPropertyTableParserFunctionMagic( &$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 $wgPropertyTableMessages
	# 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 wfPropertyTableParserFunctionRender( &$parser, $lines ) {
    // return HTML using extension tag
    $tag = wfMsg('propertytable_tag');
    $result = "<$tag>\n";
    $result .= $lines;
    $result .= "</$tag>\n";
    return $result;
}

// Processes the tag.
function fnPropertyTableTag($input, $argv, &$parser) {
	// initialize
	$sequence = 0;
    // parse input
    $properties = array();
    $tok = strtok($input, "\n");
    while ($tok !== false) {
        $line = explode("=", $tok);
        if (count($line) == 2) {
            $properties[] = $line;
        }
        $tok = strtok("\n");
    }
    // produce output
    if (count($properties) == 0) return '';
    global $wgPropertyTableAttributes, $wgPropertyTableKeyAttributes, $wgPropertyTableValueAttributes;
    $result = "<table $wgPropertyTableAttributes>\n";
    foreach ($properties as $line) {
        $key = trim($line[0]);
        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();
        if (!empty($key) && !empty($value)) {
            $result .= "<tr><td $wgPropertyTableKeyAttributes>$html_key</td>" . 
                "<td $wgPropertyTableValueAttributes>$html_value</td></tr>\n";
        }
    }
    $result .= "</table>";
    return $result;
}

?>