2007-07-31 10:21:34 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Gadgets extension - lets users select custom javascript gadgets
|
|
|
|
*
|
2008-02-04 08:08:43 +00:00
|
|
|
*
|
2007-07-31 10:21:34 +00:00
|
|
|
* For more info see http://mediawiki.org/wiki/Extension:Gadgets
|
|
|
|
*
|
|
|
|
* @package MediaWiki
|
|
|
|
* @subpackage Extensions
|
|
|
|
* @author Daniel Kinzler, brightbyte.de
|
|
|
|
* @copyright © 2007 Daniel Kinzler
|
2008-07-09 14:57:19 +00:00
|
|
|
* @license GNU General Public Licence 2.0 or later
|
2007-07-31 10:21:34 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
if( !defined( 'MEDIAWIKI' ) ) {
|
|
|
|
echo( "This file is an extension to the MediaWiki software and cannot be used standalone.\n" );
|
|
|
|
die( 1 );
|
|
|
|
}
|
|
|
|
|
2008-02-04 08:08:43 +00:00
|
|
|
$wgExtensionCredits['other'][] = array(
|
2009-04-27 03:15:19 +00:00
|
|
|
'path' => __FILE__,
|
2008-02-04 08:08:43 +00:00
|
|
|
'name' => 'Gadgets',
|
|
|
|
'author' => 'Daniel Kinzler',
|
2007-07-31 10:21:34 +00:00
|
|
|
'url' => 'http://mediawiki.org/wiki/Extension:Gadgets',
|
2008-02-04 08:40:17 +00:00
|
|
|
'descriptionmsg' => 'gadgets-desc',
|
2007-07-31 10:21:34 +00:00
|
|
|
);
|
|
|
|
|
2009-04-24 01:31:17 +00:00
|
|
|
$wgHooks['GetPreferences'][] = 'wfGadgetsGetPreferences';
|
2007-07-31 10:21:34 +00:00
|
|
|
$wgHooks['BeforePageDisplay'][] = 'wfGadgetsBeforePageDisplay';
|
2007-11-27 16:51:48 +00:00
|
|
|
$wgHooks['ArticleSaveComplete'][] = 'wfGadgetsArticleSaveComplete';
|
2007-07-31 10:21:34 +00:00
|
|
|
|
2008-02-04 08:40:17 +00:00
|
|
|
$dir = dirname(__FILE__) . '/';
|
|
|
|
$wgExtensionMessagesFiles['Gadgets'] = $dir . 'Gadgets.i18n.php';
|
2009-05-24 22:12:11 +00:00
|
|
|
$wgExtensionAliasesFiles['Gadgets'] = $dir . 'Gadgets.alias.php';
|
2008-02-04 08:40:17 +00:00
|
|
|
$wgAutoloadClasses['SpecialGadgets'] = $dir . 'SpecialGadgets.php';
|
2007-07-31 10:21:34 +00:00
|
|
|
$wgSpecialPages['Gadgets'] = 'SpecialGadgets';
|
2008-07-09 14:57:19 +00:00
|
|
|
$wgSpecialPageGroups['Gadgets'] = 'wiki';
|
2007-07-31 10:21:34 +00:00
|
|
|
|
2009-05-07 20:18:15 +00:00
|
|
|
function wfGadgetsArticleSaveComplete( &$article, &$user, $text ) {
|
2007-11-27 16:51:48 +00:00
|
|
|
//update cache if MediaWiki:Gadgets-definition was edited
|
2007-11-16 20:13:25 +00:00
|
|
|
$title = $article->mTitle;
|
2007-08-09 22:01:56 +00:00
|
|
|
if( $title->getNamespace() == NS_MEDIAWIKI && $title->getText() == 'Gadgets-definition' ) {
|
2007-11-27 16:51:48 +00:00
|
|
|
wfLoadGadgetsStructured( $text );
|
2007-08-09 22:01:56 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2007-07-31 10:21:34 +00:00
|
|
|
function wfLoadGadgets() {
|
2009-05-07 19:23:10 +00:00
|
|
|
static $gadgets = null;
|
2007-07-31 10:21:34 +00:00
|
|
|
|
2009-05-07 19:23:10 +00:00
|
|
|
if ( $gadgets !== null ) return $gadgets;
|
2007-07-31 10:21:34 +00:00
|
|
|
|
|
|
|
$struct = wfLoadGadgetsStructured();
|
|
|
|
if ( !$struct ) {
|
|
|
|
$gadgets = $struct;
|
|
|
|
return $gadgets;
|
|
|
|
}
|
|
|
|
|
|
|
|
$gadgets = array();
|
|
|
|
foreach ( $struct as $section => $entries ) {
|
|
|
|
$gadgets = array_merge( $gadgets, $entries );
|
|
|
|
}
|
|
|
|
|
|
|
|
return $gadgets;
|
|
|
|
}
|
|
|
|
|
2009-05-07 19:23:10 +00:00
|
|
|
function wfLoadGadgetsStructured( $forceNewText = null ) {
|
2008-05-09 17:09:08 +00:00
|
|
|
global $wgMemc;
|
2007-08-01 21:25:27 +00:00
|
|
|
|
2009-05-07 19:23:10 +00:00
|
|
|
static $gadgets = null;
|
|
|
|
if ( $gadgets !== null && $forceNewText === null ) return $gadgets;
|
2007-08-09 22:01:56 +00:00
|
|
|
|
2008-05-09 17:09:08 +00:00
|
|
|
$key = wfMemcKey( 'gadgets-definition' );
|
2007-07-31 10:21:34 +00:00
|
|
|
|
2009-05-07 19:23:10 +00:00
|
|
|
if ( $forceNewText === null ) {
|
2007-11-27 16:51:48 +00:00
|
|
|
//cached?
|
|
|
|
$gadgets = $wgMemc->get( $key );
|
2008-07-07 18:50:47 +00:00
|
|
|
if ( is_array($gadgets) ) return $gadgets;
|
2008-02-04 08:08:43 +00:00
|
|
|
|
2007-11-27 16:51:48 +00:00
|
|
|
$g = wfMsgForContentNoTrans( "gadgets-definition" );
|
|
|
|
if ( wfEmptyMsg( "gadgets-definition", $g ) ) {
|
|
|
|
$gadgets = false;
|
|
|
|
return $gadgets;
|
|
|
|
}
|
2008-05-09 17:09:08 +00:00
|
|
|
} else {
|
2007-11-27 16:51:48 +00:00
|
|
|
$g = $forceNewText;
|
2007-07-31 10:21:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$g = preg_replace( '/<!--.*-->/s', '', $g );
|
|
|
|
$g = preg_split( '/(\r\n|\r|\n)+/', $g );
|
|
|
|
|
|
|
|
$gadgets = array();
|
|
|
|
$section = '';
|
|
|
|
|
|
|
|
foreach ( $g as $line ) {
|
|
|
|
if ( preg_match( '/^==+ *([^*:\s|]+?)\s*==+\s*$/', $line, $m ) ) {
|
|
|
|
$section = $m[1];
|
2008-07-07 18:50:47 +00:00
|
|
|
}
|
|
|
|
else if ( preg_match( '/^\*+ *([a-zA-Z](?:[-_:.\w\d ]*[a-zA-Z0-9])?)\s*((\|[^|]*)+)\s*$/', $line, $m ) ) {
|
2007-08-01 21:25:27 +00:00
|
|
|
//NOTE: the gadget name is used as part of the name of a form field,
|
|
|
|
// and must follow the rules defined in http://www.w3.org/TR/html4/types.html#type-cdata
|
|
|
|
// Also, title-normalization applies.
|
2008-07-07 18:50:47 +00:00
|
|
|
$name = str_replace(' ', '_', $m[1] );
|
2007-08-01 21:25:27 +00:00
|
|
|
|
2008-07-07 18:50:47 +00:00
|
|
|
$code = preg_split( '/\s*\|\s*/', $m[2], -1, PREG_SPLIT_NO_EMPTY );
|
2007-07-31 10:21:34 +00:00
|
|
|
|
2008-07-07 18:50:47 +00:00
|
|
|
if ( $code ) {
|
|
|
|
$gadgets[$section][$name] = $code;
|
2007-07-31 10:21:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-08-09 22:01:56 +00:00
|
|
|
//cache for a while. gets purged automatically when MediaWiki:Gadgets-definition is edited
|
2008-07-07 18:50:47 +00:00
|
|
|
$wgMemc->set( $key, $gadgets, 60*60*24 );
|
2009-05-07 19:23:10 +00:00
|
|
|
$source = $forceNewText !== null ? 'input text' : 'MediaWiki:Gadgets-definition';
|
2008-05-09 17:09:08 +00:00
|
|
|
wfDebug( __METHOD__ . ": $source parsed, cache entry $key updated\n");
|
2007-08-09 22:01:56 +00:00
|
|
|
|
2007-07-31 10:21:34 +00:00
|
|
|
return $gadgets;
|
|
|
|
}
|
|
|
|
|
2009-04-24 01:31:17 +00:00
|
|
|
function wfGadgetsGetPreferences( $user, &$preferences ) {
|
2007-07-31 10:21:34 +00:00
|
|
|
$gadgets = wfLoadGadgetsStructured();
|
2009-04-24 01:31:17 +00:00
|
|
|
if (!$gadgets) return true;
|
|
|
|
|
2008-02-04 08:40:17 +00:00
|
|
|
wfLoadExtensionMessages( 'Gadgets' );
|
2009-04-24 01:31:17 +00:00
|
|
|
|
2009-04-24 11:10:04 +00:00
|
|
|
$options = array();
|
2009-04-24 01:31:17 +00:00
|
|
|
foreach( $gadgets as $section => $thisSection ) {
|
2009-04-24 09:07:51 +00:00
|
|
|
$section = wfMsgExt( "gadget-section-$section", 'parseinline' );
|
2009-04-24 11:10:04 +00:00
|
|
|
$options[$section] = array();
|
2009-04-24 01:31:17 +00:00
|
|
|
foreach( $thisSection as $gname => $code ) {
|
|
|
|
$options[$section][wfMsgExt( "gadget-$gname", 'parseinline' )] = $gname;
|
2007-07-31 10:21:34 +00:00
|
|
|
}
|
|
|
|
}
|
2009-04-24 01:31:17 +00:00
|
|
|
|
|
|
|
$preferences['gadgets-intro'] =
|
|
|
|
array(
|
|
|
|
'type' => 'info',
|
|
|
|
'label' => ' ',
|
2009-07-19 16:54:29 +00:00
|
|
|
'default' => Xml::tags( 'tr', array(),
|
|
|
|
Xml::tags( 'td', array( 'colspan' => 2 ),
|
|
|
|
wfMsgExt( 'gadgets-prefstext', 'parse' ) ) ),
|
2009-04-24 01:31:17 +00:00
|
|
|
'section' => 'gadgets',
|
|
|
|
'raw' => 1,
|
|
|
|
'rawrow' => 1,
|
|
|
|
);
|
|
|
|
|
|
|
|
$preferences['gadgets'] =
|
|
|
|
array(
|
|
|
|
'type' => 'multiselect',
|
|
|
|
'options' => $options,
|
|
|
|
'section' => 'gadgets',
|
|
|
|
'label' => ' ',
|
|
|
|
'prefix' => 'gadget-',
|
|
|
|
);
|
|
|
|
|
2007-07-31 10:21:34 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
function wfGadgetsBeforePageDisplay( &$out ) {
|
2009-05-07 19:23:10 +00:00
|
|
|
global $wgUser;
|
2007-07-31 10:21:34 +00:00
|
|
|
if ( !$wgUser->isLoggedIn() ) return true;
|
|
|
|
|
2010-04-06 21:00:39 +00:00
|
|
|
//disable all gadgets on critical special pages
|
|
|
|
//NOTE: $out->isUserJsAllowed() is tempting, but always fals if $wgAllowUserJs is false.
|
|
|
|
// That would disable gadgets on wikis without user JS. Introducing $out->isJsAllowed()
|
|
|
|
// may work, but should that really apply also to MediaWiki:common.js? Even on the preference page?
|
|
|
|
// See bug 22929 for discussion.
|
|
|
|
$title = $out->getTitle();
|
|
|
|
if ( $title->isSpecial( 'Preferences' )
|
|
|
|
|| $title->isSpecial( 'Resetpass' )
|
|
|
|
|| $title->isSpecial( 'Userlogin' ) ) {
|
2010-04-06 20:05:16 +00:00
|
|
|
return true;
|
2007-07-31 10:21:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$gadgets = wfLoadGadgets();
|
|
|
|
if ( !$gadgets ) return true;
|
|
|
|
|
|
|
|
$done = array();
|
|
|
|
|
2008-07-07 18:50:47 +00:00
|
|
|
foreach ( $gadgets as $gname => $code ) {
|
|
|
|
$tname = "gadget-$gname";
|
|
|
|
if ( $wgUser->getOption( $tname ) ) {
|
|
|
|
wfApplyGadgetCode( $code, $out, $done );
|
2007-07-31 10:21:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
function wfApplyGadgetCode( $code, &$out, &$done ) {
|
2009-05-07 19:23:10 +00:00
|
|
|
global $wgJsMimeType;
|
2007-07-31 10:21:34 +00:00
|
|
|
|
|
|
|
//FIXME: stuff added via $out->addScript appears below usercss and userjs in the head tag.
|
2007-11-16 20:13:25 +00:00
|
|
|
// but we'd want it to appear above explicit user stuff, so it can be overwritten.
|
2007-07-31 10:21:34 +00:00
|
|
|
foreach ( $code as $codePage ) {
|
|
|
|
//include only once
|
2007-11-16 20:13:25 +00:00
|
|
|
if ( isset( $done[$codePage] ) ) continue;
|
|
|
|
$done[$codePage] = true;
|
2007-07-31 10:21:34 +00:00
|
|
|
|
2007-11-16 20:13:25 +00:00
|
|
|
$t = Title::makeTitleSafe( NS_MEDIAWIKI, "Gadget-$codePage" );
|
2007-07-31 10:21:34 +00:00
|
|
|
if ( !$t ) continue;
|
|
|
|
|
|
|
|
if ( preg_match( '/\.js/', $codePage ) ) {
|
2007-11-16 20:13:25 +00:00
|
|
|
$u = $t->getLocalURL( 'action=raw&ctype=' . $wgJsMimeType );
|
2009-04-16 22:28:23 +00:00
|
|
|
//switched to addScriptFile call to support scriptLoader
|
|
|
|
$out->addScriptFile( $u );
|
2007-07-31 10:21:34 +00:00
|
|
|
}
|
|
|
|
else if ( preg_match( '/\.css/', $codePage ) ) {
|
2007-11-16 20:13:25 +00:00
|
|
|
$u = $t->getLocalURL( 'action=raw&ctype=text/css' );
|
2010-04-06 20:05:16 +00:00
|
|
|
$out->addScript( Html::linkedStyle( $u ) );
|
2007-07-31 10:21:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-08-17 15:42:26 +00:00
|
|
|
|