2009-01-27 02:18:51 +03:00
< ? php
/**
*
* {{ #widget:<WidgetName>|<name1>=<value1>|<name2>=<value2>}}
*
* @ author Sergey Chernyshev
* @ version $Id : Widgets . php 15 2008 - 06 - 25 21 : 22 : 40 Z sergey . chernyshev $
*/
2009-05-11 16:31:02 +04:00
if ( ! defined ( 'MEDIAWIKI' ) ) {
echo " This file is not a valid entry point. " ;
exit ( 1 );
}
2009-01-27 02:18:51 +03:00
$wgExtensionCredits [ 'parserhook' ][] = array (
2009-04-27 08:45:10 +04:00
'path' => __FILE__ ,
2009-01-27 02:18:51 +03:00
'name' => 'Widgets' ,
'description' => 'Allows wiki administrators to add free-form widgets to wiki by just editing pages within Widget namespace. Originally developed for [http://www.ardorado.com Ardorado.com]' ,
2009-03-12 20:55:13 +03:00
'version' => '0.8.5' ,
2009-01-27 02:18:51 +03:00
'author' => '[http://www.sergeychernyshev.com Sergey Chernyshev] (for [http://www.semanticcommunities.com Semantic Communities LLC.])' ,
'url' => 'http://www.mediawiki.org/wiki/Extension:Widgets'
);
2009-05-11 16:31:02 +04:00
/**
* Set this to the index of the Widget namespace
*/
$widgetNamespaceIndex = 274 ;
2009-01-27 02:18:51 +03:00
// Initialize Smarty
2009-05-11 16:31:02 +04:00
require dirname ( __FILE__ ) . " /smarty/Smarty.class.php " ;
2009-01-27 02:18:51 +03:00
// Parser function registration
2009-05-11 16:31:02 +04:00
$wgExtensionFunctions [] = 'widgetNamespacesInit' ;
2009-01-27 02:18:51 +03:00
$wgHooks [ 'LanguageGetMagic' ][] = 'widgetLanguageGetMagic' ;
2009-05-11 16:31:02 +04:00
$wgHooks [ 'ParserFirstCallInit' ][] = 'widgetParserFunctions' ;
2009-01-27 02:18:51 +03:00
2009-05-11 16:31:02 +04:00
function widgetParserFunctions ( & $parser )
2009-01-27 02:18:51 +03:00
{
2009-05-11 16:31:02 +04:00
$parser -> setFunctionHook ( 'widget' , 'renderWidget' );
2009-01-27 02:18:51 +03:00
}
function widgetLanguageGetMagic ( & $magicWords , $langCode = " en " )
{
switch ( $langCode ) {
default :
$magicWords [ 'widget' ] = array ( 0 , 'widget' );
}
return true ;
}
function renderWidget ( & $parser , $widgetName )
{
global $IP ;
$smarty = new Smarty ;
$smarty -> left_delimiter = '<!--{' ;
$smarty -> right_delimiter = '}-->' ;
$smarty -> compile_dir = " $IP /extensions/Widgets/compiled_templates/ " ;
2009-02-10 23:47:38 +03:00
// registering custom Smarty plugins
$smarty -> plugins_dir [] = " $IP /extensions/Widgets/smarty_plugins/ " ;
2009-01-27 02:18:51 +03:00
$smarty -> security = true ;
$smarty -> security_settings = array (
2009-02-10 23:47:38 +03:00
'IF_FUNCS' => array (
'is_array' ,
'isset' ,
'array' ,
'list' ,
'count' ,
'sizeof' ,
'in_array' ,
'true' ,
'false' ,
'null'
),
'MODIFIER_FUNCS' => array ( 'validate' )
2009-01-27 02:18:51 +03:00
);
// register the resource name "db"
$smarty -> register_resource ( " wiki " , array ( " wiki_get_template " ,
" wiki_get_timestamp " ,
" wiki_get_secure " ,
" wiki_get_trusted " ));
$params = func_get_args ();
array_shift ( $params ); # first one is parser - we don't need it
array_shift ( $params ); # second one is widget name
$params_tree = array ();
foreach ( $params as $param )
{
$pair = explode ( '=' , $param , 2 );
if ( count ( $pair ) == 2 )
{
$key = trim ( $pair [ 0 ]);
$val = trim ( $pair [ 1 ]);
2009-03-12 20:55:13 +03:00
}
else
{
$key = $param ;
$val = true ;
}
2009-01-27 02:18:51 +03:00
2009-03-12 20:55:13 +03:00
if ( $val == 'false' )
{
$val = false ;
}
2009-01-27 02:18:51 +03:00
2009-03-12 20:55:13 +03:00
/* If the name of the parameter has object notation
2009-01-27 02:18:51 +03:00
2009-03-12 20:55:13 +03:00
a . b . c . d
2009-01-27 02:18:51 +03:00
2009-03-12 20:55:13 +03:00
then we assign stuff to hash of hashes , not scalar
2009-01-27 02:18:51 +03:00
2009-03-12 20:55:13 +03:00
*/
$keys = explode ( '.' , $key );
2009-01-27 02:18:51 +03:00
2009-03-12 20:55:13 +03:00
// $subtree will be moved from top to the bottom and at the end will point to the last level
$subtree =& $params_tree ;
2009-01-27 02:18:51 +03:00
2009-03-12 20:55:13 +03:00
// go throught all the keys but last one
$last_key = array_pop ( $keys );
foreach ( $keys as $subkey )
{
// if next level of subtree doesn't exist yet, create an empty one
if ( ! array_key_exists ( $subkey , $subtree ))
2009-01-27 02:18:51 +03:00
{
2009-03-12 20:55:13 +03:00
$subtree [ $subkey ] = array ();
2009-01-27 02:18:51 +03:00
}
2009-03-12 20:55:13 +03:00
// move to the lower level
$subtree =& $subtree [ $subkey ];
}
2009-01-27 02:18:51 +03:00
2009-03-12 20:55:13 +03:00
// last portion of the key points to itself
if ( isset ( $subtree [ $last_key ]))
{
// if already an array, push into it, otherwise, convert into array first
if ( ! is_array ( $subtree [ $last_key ]))
2009-01-27 02:18:51 +03:00
{
2009-03-12 20:55:13 +03:00
$subtree [ $last_key ] = array ( $subtree [ $last_key ]);
2009-01-27 02:18:51 +03:00
}
2009-03-12 20:55:13 +03:00
$subtree [ $last_key ][] = $val ;
}
else
{
// doesn't exist yet, just setting a value
$subtree [ $last_key ] = $val ;
}
2009-01-27 02:18:51 +03:00
}
$smarty -> assign ( $params_tree );
try
{
$output = $smarty -> fetch ( " wiki: $widgetName " );
}
catch ( Exception $e )
{
return " <div class= \" error \" >Error in [[Widget: $widgetName ]]</div> " ;
}
return $parser -> insertStripItem ( $output , $parser -> mStripState );
}
function widgetNamespacesInit () {
global $widgetNamespaceIndex , $wgExtraNamespaces , $wgNamespacesWithSubpages ,
$wgGroupPermissions , $wgNamespaceProtection ;
define ( 'NS_WIDGET' , $widgetNamespaceIndex );
define ( 'NS_WIDGET_TALK' , $widgetNamespaceIndex + 1 );
// Register namespace identifiers
if ( ! is_array ( $wgExtraNamespaces )) { $wgExtraNamespaces = array (); }
$wgExtraNamespaces = $wgExtraNamespaces + array ( NS_WIDGET => 'Widget' , NS_WIDGET_TALK => 'Widget_talk' );
// Support subpages only for talk pages by default
$wgNamespacesWithSubpages = $wgNamespacesWithSubpages + array (
NS_WIDGET_TALK => true
);
// Assign editing to 3idgeteditor group only (widgets can be dangerous so we do it here, not in LocalSettings)
$wgGroupPermissions [ '*' ][ 'editwidgets' ] = false ;
$wgGroupPermissions [ 'widgeteditor' ][ 'editwidgets' ] = true ;
// Setting required namespace permission rights
$wgNamespaceProtection [ NS_WIDGET ] = array ( 'editwidgets' );
}
// put these function somewhere in your application
function wiki_get_template ( $widgetName , & $widgetCode , & $smarty_obj )
{
$widgetTitle = Title :: newFromText ( $widgetName , NS_WIDGET );
if ( $widgetTitle && $widgetTitle -> exists ())
{
2009-01-27 22:06:20 +03:00
$widgetArticle = new Article ( $widgetTitle , 0 );
2009-01-27 02:18:51 +03:00
$widgetCode = $widgetArticle -> getContent ();
// Remove <noinclude> sections and <includeonly> tags from form definition
$widgetCode = StringUtils :: delimiterReplace ( '<noinclude>' , '</noinclude>' , '' , $widgetCode );
$widgetCode = strtr ( $widgetCode , array ( '<includeonly>' => '' , '</includeonly>' => '' ));
return true ;
}
else
{
return false ;
}
}
function wiki_get_timestamp ( $widgetName , & $widgetTimestamp , & $smarty_obj )
{
$widgetTitle = Title :: newFromText ( $widgetName , NS_WIDGET );
if ( $widgetTitle && $widgetTitle -> exists ())
{
2009-01-27 22:06:20 +03:00
$widgetArticle = new Article ( $widgetTitle , 0 );
2009-01-27 02:18:51 +03:00
$widgetTimestamp = $widgetArticle -> getTouched ();
return true ;
}
else
{
return false ;
}
}
function wiki_get_secure ( $tpl_name , & $smarty_obj )
{
// assume all templates are secure
return true ;
}
function wiki_get_trusted ( $tpl_name , & $smarty_obj )
{
// not used for templates
}