mediawiki-extensions-Gadgets/Gadgets.php

241 lines
6.3 KiB
PHP
Raw Normal View History

<?php
/**
* Gadgets extension - lets users select custom javascript gadgets
*
2008-02-04 08:08:43 +00:00
*
* For more info see http://mediawiki.org/wiki/Extension:Gadgets
*
* @file
* @ingroup Extensions
* @author Daniel Kinzler, brightbyte.de
* @copyright © 2007 Daniel Kinzler
* @license GNU General Public Licence 2.0 or later
*/
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(
'path' => __FILE__,
2008-02-04 08:08:43 +00:00
'name' => 'Gadgets',
'author' => 'Daniel Kinzler',
'url' => 'http://mediawiki.org/wiki/Extension:Gadgets',
'descriptionmsg' => 'gadgets-desc',
);
$wgHooks['GetPreferences'][] = 'wfGadgetsGetPreferences';
$wgHooks['BeforePageDisplay'][] = 'wfGadgetsBeforePageDisplay';
$wgHooks['ArticleSaveComplete'][] = 'wfGadgetsArticleSaveComplete';
$dir = dirname(__FILE__) . '/';
$wgExtensionMessagesFiles['Gadgets'] = $dir . 'Gadgets.i18n.php';
$wgExtensionAliasesFiles['Gadgets'] = $dir . 'Gadgets.alias.php';
$wgAutoloadClasses['SpecialGadgets'] = $dir . 'SpecialGadgets.php';
$wgSpecialPages['Gadgets'] = 'SpecialGadgets';
$wgSpecialPageGroups['Gadgets'] = 'wiki';
function wfGadgetsArticleSaveComplete( $article, $user, $text ) {
//update cache if MediaWiki:Gadgets-definition was edited
$title = $article->mTitle;
2007-08-09 22:01:56 +00:00
if( $title->getNamespace() == NS_MEDIAWIKI && $title->getText() == 'Gadgets-definition' ) {
wfLoadGadgetsStructured( $text );
2007-08-09 22:01:56 +00:00
}
return true;
}
function wfLoadGadgets() {
static $gadgets = null;
2010-10-29 21:36:49 +00:00
if ( $gadgets !== null ) {
return $gadgets;
}
$struct = wfLoadGadgetsStructured();
if ( !$struct ) {
$gadgets = $struct;
return $gadgets;
}
$gadgets = array();
2010-10-29 21:36:49 +00:00
foreach ( $struct as $entries ) {
$gadgets = array_merge( $gadgets, $entries );
}
return $gadgets;
}
function wfLoadGadgetsStructured( $forceNewText = null ) {
global $wgMemc;
2007-08-01 21:25:27 +00:00
static $gadgets = null;
2010-10-29 21:36:49 +00:00
if ( $gadgets !== null && $forceNewText === null ) {
return $gadgets;
}
2007-08-09 22:01:56 +00:00
$key = wfMemcKey( 'gadgets-definition' );
if ( $forceNewText === null ) {
//cached?
$gadgets = $wgMemc->get( $key );
if ( is_array($gadgets) ) return $gadgets;
2008-02-04 08:08:43 +00:00
$g = wfMsgForContentNoTrans( "gadgets-definition" );
if ( wfEmptyMsg( "gadgets-definition", $g ) ) {
$gadgets = false;
return $gadgets;
}
} else {
$g = $forceNewText;
}
$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];
}
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.
$name = str_replace(' ', '_', $m[1] );
2007-08-01 21:25:27 +00:00
$code = preg_split( '/\s*\|\s*/', $m[2], -1, PREG_SPLIT_NO_EMPTY );
if ( $code ) {
$gadgets[$section][$name] = $code;
}
}
}
2007-08-09 22:01:56 +00:00
//cache for a while. gets purged automatically when MediaWiki:Gadgets-definition is edited
$wgMemc->set( $key, $gadgets, 60*60*24 );
$source = $forceNewText !== null ? 'input text' : 'MediaWiki:Gadgets-definition';
wfDebug( __METHOD__ . ": $source parsed, cache entry $key updated\n");
2007-08-09 22:01:56 +00:00
return $gadgets;
}
function wfGadgetsGetPreferences( $user, &$preferences ) {
$gadgets = wfLoadGadgetsStructured();
2010-10-29 21:36:49 +00:00
if (!$gadgets) {
return true;
}
2009-04-24 11:10:04 +00:00
$options = array();
foreach( $gadgets as $section => $thisSection ) {
if ( $section !== '' ) {
$section = wfMsgExt( "gadget-section-$section", 'parseinline' );
$options[$section] = array();
$destination = &$options[$section];
} else {
$destination = &$options;
}
foreach( $thisSection as $gname => $code ) {
$destination[wfMsgExt( "gadget-$gname", 'parseinline' )] = $gname;
}
}
$preferences['gadgets-intro'] =
array(
'type' => 'info',
Remove most named character references from output Recommit of r66254 to trunk. This was just find extensions phase3 -iname '*.php' \! -iname '*.i18n.php' \! -iname 'Messages*.php' \! -iname '*_Messages.php' -exec sed -i 's/&nbsp;/\&#160;/g;s/&mdash;/―/g;s/&bull;/•/g;s/&aacute;/á/g;s/&acute;/´/g;s/&agrave;/à/g;s/&alpha;/α/g;s/&auml;/ä/g;s/&ccedil;/ç/g;s/&copy;/©/g;s/&darr;/↓/g;s/&deg;/°/g;s/&eacute;/é/g;s/&ecirc;/ê/g;s/&euml;/ë/g;s/&egrave;/è/g;s/&euro;/€/g;s/&harr;//g;s/&hellip;/…/g;s/&iacute;/í/g;s/&igrave;/ì/g;s/&larr;/←/g;s/&ldquo;/“/g;s/&middot;/·/g;s/&minus;/−/g;s/&ndash;/–/g;s/&oacute;/ó/g;s/&ocirc;/ô/g;s/&oelig;/œ/g;s/&ograve;/ò/g;s/&otilde;/õ/g;s/&ouml;/ö/g;s/&pound;/£/g;s/&prime;/′/g;s/&Prime;/″/g;s/&raquo;/»/g;s/&rarr;/→/g;s/&rdquo;/”/g;s/&Sigma;/Σ/g;s/&times;/×/g;s/&uacute;/ú/g;s/&uarr;/↑/g;s/&uuml;/ü/g;s/&yen;/¥/g' {} + followed by reading over every single line of the resulting diff and fixing a whole bunch of false positives. The reason for this change is given in <http://lists.wikimedia.org/pipermail/wikitech-l/2010-April/047617.html>. I cleared it with Tim and Brion on IRC before committing. It might cause a few problems, but I tried to be careful; please report any issues. I skipped all messages files. I plan to make a follow-up commit that alters wfMsgExt() with 'escapenoentities' to sanitize all the entities. That way, the only messages that will be problems will be ones that output raw HTML, and we want to get rid of those anyway. This should get rid of all named entities everywhere except messages. I skipped a few things like &nbsp that I noticed in manual inspection, because they weren't well-formed XML anyway. Also, to everyone who uses non-breaking spaces when they could use a normal space, or nothing at all, or CSS padding: I still hate you. Die.
2010-05-30 17:33:59 +00:00
'label' => '&#160;',
'default' => Xml::tags( 'tr', array(),
Xml::tags( 'td', array( 'colspan' => 2 ),
wfMsgExt( 'gadgets-prefstext', 'parse' ) ) ),
'section' => 'gadgets',
'raw' => 1,
'rawrow' => 1,
);
$preferences['gadgets'] =
array(
'type' => 'multiselect',
'options' => $options,
'section' => 'gadgets',
Remove most named character references from output Recommit of r66254 to trunk. This was just find extensions phase3 -iname '*.php' \! -iname '*.i18n.php' \! -iname 'Messages*.php' \! -iname '*_Messages.php' -exec sed -i 's/&nbsp;/\&#160;/g;s/&mdash;/―/g;s/&bull;/•/g;s/&aacute;/á/g;s/&acute;/´/g;s/&agrave;/à/g;s/&alpha;/α/g;s/&auml;/ä/g;s/&ccedil;/ç/g;s/&copy;/©/g;s/&darr;/↓/g;s/&deg;/°/g;s/&eacute;/é/g;s/&ecirc;/ê/g;s/&euml;/ë/g;s/&egrave;/è/g;s/&euro;/€/g;s/&harr;//g;s/&hellip;/…/g;s/&iacute;/í/g;s/&igrave;/ì/g;s/&larr;/←/g;s/&ldquo;/“/g;s/&middot;/·/g;s/&minus;/−/g;s/&ndash;/–/g;s/&oacute;/ó/g;s/&ocirc;/ô/g;s/&oelig;/œ/g;s/&ograve;/ò/g;s/&otilde;/õ/g;s/&ouml;/ö/g;s/&pound;/£/g;s/&prime;/′/g;s/&Prime;/″/g;s/&raquo;/»/g;s/&rarr;/→/g;s/&rdquo;/”/g;s/&Sigma;/Σ/g;s/&times;/×/g;s/&uacute;/ú/g;s/&uarr;/↑/g;s/&uuml;/ü/g;s/&yen;/¥/g' {} + followed by reading over every single line of the resulting diff and fixing a whole bunch of false positives. The reason for this change is given in <http://lists.wikimedia.org/pipermail/wikitech-l/2010-April/047617.html>. I cleared it with Tim and Brion on IRC before committing. It might cause a few problems, but I tried to be careful; please report any issues. I skipped all messages files. I plan to make a follow-up commit that alters wfMsgExt() with 'escapenoentities' to sanitize all the entities. That way, the only messages that will be problems will be ones that output raw HTML, and we want to get rid of those anyway. This should get rid of all named entities everywhere except messages. I skipped a few things like &nbsp that I noticed in manual inspection, because they weren't well-formed XML anyway. Also, to everyone who uses non-breaking spaces when they could use a normal space, or nothing at all, or CSS padding: I still hate you. Die.
2010-05-30 17:33:59 +00:00
'label' => '&#160;',
'prefix' => 'gadget-',
);
return true;
}
function wfGadgetsBeforePageDisplay( $out ) {
global $wgUser;
2010-10-29 21:36:49 +00:00
if ( !$wgUser->isLoggedIn() ) {
return true;
}
//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' ) ) {
return true;
}
$gadgets = wfLoadGadgets();
2010-10-29 21:36:49 +00:00
if ( !$gadgets ) {
return true;
}
$lb = new LinkBatch();
$lb->setCaller( __METHOD__ );
$pages = array();
foreach ( $gadgets as $gname => $id ) {
$tname = "gadget-$gname";
if ( $wgUser->getOption( $tname ) ) {
foreach ( $id as $page ) {
$lb->add( NS_MEDIAWIKI, "Gadget-$page" );
$pages[] = $page;
}
}
}
$lb->execute( __METHOD__ );
$done = array();
foreach ( $pages as $page ) {
2010-10-29 21:36:49 +00:00
if ( isset( $done[$page] ) ) {
continue;
}
$done[$page] = true;
wfApplyGadgetCode( $page, $out );
}
return true;
}
function wfApplyGadgetCode( $page, $out ) {
global $wgJsMimeType;
//FIXME: stuff added via $out->addScript appears below usercss and userjs in the head tag.
// but we'd want it to appear above explicit user stuff, so it can be overwritten.
$t = Title::makeTitleSafe( NS_MEDIAWIKI, "Gadget-$page" );
2010-10-29 21:36:49 +00:00
if ( !$t ) {
return;
}
if ( preg_match( '/\.js/', $page ) ) {
$u = $t->getLocalURL( 'action=raw&ctype=' . $wgJsMimeType );
//switched to addScriptFile call to support scriptLoader
$out->addScriptFile( $u, $t->getLatestRevID() );
} elseif ( preg_match( '/\.css/', $page ) ) {
$u = $t->getLocalURL( 'action=raw&ctype=text/css&' . $t->getLatestRevID() );
$out->addScript( Html::linkedStyle( $u ) );
}
}
2008-08-17 15:42:26 +00:00