2010-11-13 19:13:43 +00:00
|
|
|
<?php
|
2010-11-11 18:19:57 +00:00
|
|
|
/**
|
|
|
|
* Gadgets extension - lets users select custom javascript gadgets
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Wrapper for one gadget.
|
|
|
|
*/
|
|
|
|
class Gadget {
|
|
|
|
/**
|
|
|
|
* Increment this when changing class structure
|
|
|
|
*/
|
2015-04-30 05:33:04 +00:00
|
|
|
const GADGET_CLASS_VERSION = 8;
|
|
|
|
|
|
|
|
const CACHE_TTL = 86400;
|
2010-11-11 18:19:57 +00:00
|
|
|
|
2014-09-05 21:22:42 +00:00
|
|
|
private $scripts = array(),
|
2011-09-23 06:48:37 +00:00
|
|
|
$styles = array(),
|
2010-11-13 18:45:21 +00:00
|
|
|
$dependencies = array(),
|
2011-09-23 06:48:37 +00:00
|
|
|
$name,
|
2010-11-11 18:19:57 +00:00
|
|
|
$definition,
|
2011-04-03 19:01:52 +00:00
|
|
|
$resourceLoaded = false,
|
2011-04-12 18:09:50 +00:00
|
|
|
$requiredRights = array(),
|
2011-10-22 19:09:25 +00:00
|
|
|
$requiredSkins = array(),
|
2013-04-26 00:32:59 +00:00
|
|
|
$targets = array( 'desktop' ),
|
2011-04-15 19:41:47 +00:00
|
|
|
$onByDefault = false,
|
2013-07-23 21:11:14 +00:00
|
|
|
$position = 'bottom',
|
2011-04-15 19:41:47 +00:00
|
|
|
$category;
|
2010-11-11 18:19:57 +00:00
|
|
|
|
2015-04-30 05:33:04 +00:00
|
|
|
/** @var array|bool Result of loadStructuredList() */
|
|
|
|
private static $definitionCache;
|
|
|
|
|
2015-07-18 07:26:25 +00:00
|
|
|
public function __construct( array $options ) {
|
|
|
|
foreach ( $options as $member => $option ) {
|
|
|
|
switch ( $member ) {
|
|
|
|
case 'scripts':
|
|
|
|
case 'styles':
|
|
|
|
case 'dependencies':
|
|
|
|
case 'name':
|
|
|
|
case 'definition':
|
|
|
|
case 'resourceLoaded':
|
|
|
|
case 'requiredRights':
|
|
|
|
case 'requiredSkins':
|
|
|
|
case 'targets':
|
|
|
|
case 'onByDefault':
|
|
|
|
case 'position':
|
|
|
|
case 'category':
|
|
|
|
$this->{$member} = $option;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw new InvalidArgumentException( "Unrecognized '$member' parameter" );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Whether the provided gadget id is valid
|
|
|
|
*
|
|
|
|
* @param string $id
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public static function isValidGadgetID( $id ) {
|
|
|
|
return strlen( $id ) > 0 && ResourceLoader::isValidModuleName( "ext.gadget.$id" );
|
|
|
|
}
|
|
|
|
|
2010-11-11 18:19:57 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return String: Gadget name
|
|
|
|
*/
|
|
|
|
public function getName() {
|
|
|
|
return $this->name;
|
|
|
|
}
|
|
|
|
|
2011-04-15 19:41:47 +00:00
|
|
|
/**
|
|
|
|
* @return String: Gadget description parsed into HTML
|
|
|
|
*/
|
|
|
|
public function getDescription() {
|
|
|
|
return wfMessage( "gadget-{$this->getName()}" )->parse();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return String: Wikitext of gadget description
|
|
|
|
*/
|
|
|
|
public function getRawDescription() {
|
|
|
|
return wfMessage( "gadget-{$this->getName()}" )->plain();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return String: Name of category (aka section) our gadget belongs to. Empty string if none.
|
|
|
|
*/
|
|
|
|
public function getCategory() {
|
|
|
|
return $this->category;
|
|
|
|
}
|
|
|
|
|
2010-11-11 18:19:57 +00:00
|
|
|
/**
|
2012-02-02 19:04:52 +00:00
|
|
|
* @return String: Name of ResourceLoader module for this gadget
|
2010-11-11 18:19:57 +00:00
|
|
|
*/
|
|
|
|
public function getModuleName() {
|
|
|
|
return "ext.gadget.{$this->name}";
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-04-03 19:01:52 +00:00
|
|
|
* Checks whether this gadget is enabled for given user
|
|
|
|
*
|
|
|
|
* @param $user User: user to check against
|
|
|
|
* @return Boolean
|
2010-11-11 18:19:57 +00:00
|
|
|
*/
|
2011-04-03 19:01:52 +00:00
|
|
|
public function isEnabled( $user ) {
|
2011-04-12 18:09:50 +00:00
|
|
|
return (bool)$user->getOption( "gadget-{$this->name}", $this->onByDefault );
|
2011-04-03 19:01:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks whether given user has permissions to use this gadget
|
|
|
|
*
|
|
|
|
* @param $user User: user to check against
|
|
|
|
* @return Boolean
|
|
|
|
*/
|
|
|
|
public function isAllowed( $user ) {
|
2011-10-22 19:09:25 +00:00
|
|
|
return count( array_intersect( $this->requiredRights, $user->getRights() ) ) == count( $this->requiredRights )
|
|
|
|
&& ( !count( $this->requiredSkins ) || in_array( $user->getOption( 'skin' ), $this->requiredSkins ) );
|
2010-11-11 18:19:57 +00:00
|
|
|
}
|
|
|
|
|
2011-04-12 18:09:50 +00:00
|
|
|
/**
|
|
|
|
* @return Boolean: Whether this gadget is on by default for everyone (but can be disabled in preferences)
|
|
|
|
*/
|
|
|
|
public function isOnByDefault() {
|
|
|
|
return $this->onByDefault;
|
|
|
|
}
|
|
|
|
|
2010-11-11 18:19:57 +00:00
|
|
|
/**
|
|
|
|
* @return Boolean: Whether all of this gadget's JS components support ResourceLoader
|
|
|
|
*/
|
|
|
|
public function supportsResourceLoader() {
|
|
|
|
return $this->resourceLoaded;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return Boolean: Whether this gadget has resources that can be loaded via ResourceLoader
|
|
|
|
*/
|
|
|
|
public function hasModule() {
|
2012-02-02 19:04:52 +00:00
|
|
|
return count( $this->styles )
|
|
|
|
+ ( $this->supportsResourceLoader() ? count( $this->scripts ) : 0 )
|
|
|
|
> 0;
|
2010-11-11 18:19:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return String: Definition for this gadget from MediaWiki:gadgets-definition
|
|
|
|
*/
|
|
|
|
public function getDefinition() {
|
|
|
|
return $this->definition;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return Array: Array of pages with JS not prefixed with namespace
|
|
|
|
*/
|
|
|
|
public function getScripts() {
|
|
|
|
return $this->scripts;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return Array: Array of pages with CSS not prefixed with namespace
|
|
|
|
*/
|
|
|
|
public function getStyles() {
|
|
|
|
return $this->styles;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return Array: Array of all of this gadget's resources
|
|
|
|
*/
|
|
|
|
public function getScriptsAndStyles() {
|
|
|
|
return array_merge( $this->scripts, $this->styles );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-02-02 19:04:52 +00:00
|
|
|
* Returns module for ResourceLoader, see getModuleName() for its name.
|
|
|
|
* If our gadget has no scripts or styles suitable for RL, false will be returned.
|
2010-11-11 18:19:57 +00:00
|
|
|
* @return Mixed: GadgetResourceLoaderModule or false
|
|
|
|
*/
|
|
|
|
public function getModule() {
|
2012-02-02 19:04:52 +00:00
|
|
|
$pages = array();
|
2011-12-13 10:41:51 +00:00
|
|
|
|
2012-02-02 19:04:52 +00:00
|
|
|
foreach ( $this->styles as $style ) {
|
|
|
|
$pages['MediaWiki:' . $style] = array( 'type' => 'style' );
|
2011-12-13 10:41:51 +00:00
|
|
|
}
|
|
|
|
|
2010-11-11 18:19:57 +00:00
|
|
|
if ( $this->supportsResourceLoader() ) {
|
|
|
|
foreach ( $this->scripts as $script ) {
|
2011-02-20 18:14:38 +00:00
|
|
|
$pages['MediaWiki:' . $script] = array( 'type' => 'script' );
|
2010-11-11 18:19:57 +00:00
|
|
|
}
|
|
|
|
}
|
2011-09-23 06:48:37 +00:00
|
|
|
|
2010-11-11 18:19:57 +00:00
|
|
|
if ( !count( $pages ) ) {
|
2012-02-02 19:04:52 +00:00
|
|
|
return null;
|
2010-11-11 18:19:57 +00:00
|
|
|
}
|
2011-09-23 06:48:37 +00:00
|
|
|
|
2013-07-23 21:11:14 +00:00
|
|
|
return new GadgetResourceLoaderModule( $pages, $this->dependencies, $this->targets, $this->position );
|
2010-11-11 18:19:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns list of scripts that don't support ResourceLoader
|
|
|
|
* @return Array
|
|
|
|
*/
|
|
|
|
public function getLegacyScripts() {
|
|
|
|
if ( $this->supportsResourceLoader() ) {
|
|
|
|
return array();
|
|
|
|
}
|
|
|
|
return $this->scripts;
|
|
|
|
}
|
|
|
|
|
2010-11-13 18:45:21 +00:00
|
|
|
/**
|
|
|
|
* Returns names of resources this gadget depends on
|
|
|
|
* @return Array
|
|
|
|
*/
|
|
|
|
public function getDependencies() {
|
|
|
|
return $this->dependencies;
|
|
|
|
}
|
|
|
|
|
2011-04-03 19:01:52 +00:00
|
|
|
/**
|
|
|
|
* Returns array of permissions required by this gadget
|
|
|
|
* @return Array
|
|
|
|
*/
|
|
|
|
public function getRequiredRights() {
|
|
|
|
return $this->requiredRights;
|
|
|
|
}
|
|
|
|
|
2011-10-22 19:09:25 +00:00
|
|
|
/**
|
|
|
|
* Returns array of skins where this gadget works
|
|
|
|
* @return Array
|
|
|
|
*/
|
|
|
|
public function getRequiredSkins() {
|
|
|
|
return $this->requiredSkins;
|
|
|
|
}
|
|
|
|
|
2013-07-23 21:11:14 +00:00
|
|
|
/**
|
|
|
|
* Returns the position of this Gadget's ResourceLoader module
|
|
|
|
* @return String: 'bottom' or 'top'
|
|
|
|
*/
|
|
|
|
public function getPosition() {
|
|
|
|
return $this->position;
|
|
|
|
}
|
2010-11-11 18:19:57 +00:00
|
|
|
}
|
|
|
|
|