mediawiki-extensions-Gadgets/Gadgets.php
Aryeh Gregor d0d3d059b5 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/ /\ /g;s/—/―/g;s/•/•/g;s/á/á/g;s/´/´/g;s/à/à/g;s/α/α/g;s/ä/ä/g;s/ç/ç/g;s/©/©/g;s/↓/↓/g;s/°/°/g;s/é/é/g;s/ê/ê/g;s/ë/ë/g;s/è/è/g;s/€/€/g;s/↔//g;s/…/…/g;s/í/í/g;s/ì/ì/g;s/←/←/g;s/“/“/g;s/·/·/g;s/−/−/g;s/–/–/g;s/ó/ó/g;s/ô/ô/g;s/œ/œ/g;s/ò/ò/g;s/õ/õ/g;s/ö/ö/g;s/£/£/g;s/′/′/g;s/″/″/g;s/»/»/g;s/→/→/g;s/”/”/g;s/Σ/Σ/g;s/×/×/g;s/ú/ú/g;s/↑/↑/g;s/ü/ü/g;s/¥/¥/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

216 lines
6 KiB
PHP

<?php
/**
* Gadgets extension - lets users select custom javascript gadgets
*
*
* For more info see http://mediawiki.org/wiki/Extension:Gadgets
*
* @package MediaWiki
* @subpackage 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 );
}
$wgExtensionCredits['other'][] = array(
'path' => __FILE__,
'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;
if( $title->getNamespace() == NS_MEDIAWIKI && $title->getText() == 'Gadgets-definition' ) {
wfLoadGadgetsStructured( $text );
}
return true;
}
function wfLoadGadgets() {
static $gadgets = null;
if ( $gadgets !== null ) return $gadgets;
$struct = wfLoadGadgetsStructured();
if ( !$struct ) {
$gadgets = $struct;
return $gadgets;
}
$gadgets = array();
foreach ( $struct as $section => $entries ) {
$gadgets = array_merge( $gadgets, $entries );
}
return $gadgets;
}
function wfLoadGadgetsStructured( $forceNewText = null ) {
global $wgMemc;
static $gadgets = null;
if ( $gadgets !== null && $forceNewText === null ) return $gadgets;
$key = wfMemcKey( 'gadgets-definition' );
if ( $forceNewText === null ) {
//cached?
$gadgets = $wgMemc->get( $key );
if ( is_array($gadgets) ) return $gadgets;
$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 ) ) {
//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] );
$code = preg_split( '/\s*\|\s*/', $m[2], -1, PREG_SPLIT_NO_EMPTY );
if ( $code ) {
$gadgets[$section][$name] = $code;
}
}
}
//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");
return $gadgets;
}
function wfGadgetsGetPreferences( $user, &$preferences ) {
$gadgets = wfLoadGadgetsStructured();
if (!$gadgets) return true;
wfLoadExtensionMessages( 'Gadgets' );
$options = array();
foreach( $gadgets as $section => $thisSection ) {
$section = wfMsgExt( "gadget-section-$section", 'parseinline' );
$options[$section] = array();
foreach( $thisSection as $gname => $code ) {
$options[$section][wfMsgExt( "gadget-$gname", 'parseinline' )] = $gname;
}
}
$preferences['gadgets-intro'] =
array(
'type' => 'info',
'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',
'label' => '&#160;',
'prefix' => 'gadget-',
);
return true;
}
function wfGadgetsBeforePageDisplay( &$out ) {
global $wgUser;
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();
if ( !$gadgets ) return true;
$done = array();
foreach ( $gadgets as $gname => $code ) {
$tname = "gadget-$gname";
if ( $wgUser->getOption( $tname ) ) {
wfApplyGadgetCode( $code, $out, $done );
}
}
return true;
}
function wfApplyGadgetCode( $code, &$out, &$done ) {
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.
foreach ( $code as $codePage ) {
//include only once
if ( isset( $done[$codePage] ) ) continue;
$done[$codePage] = true;
$t = Title::makeTitleSafe( NS_MEDIAWIKI, "Gadget-$codePage" );
if ( !$t ) continue;
if ( preg_match( '/\.js/', $codePage ) ) {
$u = $t->getLocalURL( 'action=raw&ctype=' . $wgJsMimeType );
//switched to addScriptFile call to support scriptLoader
$out->addScriptFile( $u );
}
else if ( preg_match( '/\.css/', $codePage ) ) {
$u = $t->getLocalURL( 'action=raw&ctype=text/css' );
$out->addScript( Html::linkedStyle( $u ) );
}
}
}