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-08-03 06:34:16 +00:00
|
|
|
const GADGET_CLASS_VERSION = 9;
|
2015-04-30 05:33:04 +00:00
|
|
|
|
|
|
|
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(),
|
2015-08-03 06:34:16 +00:00
|
|
|
$messages = 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,
|
2015-08-03 06:34:16 +00:00
|
|
|
$hidden = 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-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':
|
2015-08-03 06:34:16 +00:00
|
|
|
case 'messages':
|
2015-07-18 07:26:25 +00:00
|
|
|
case 'name':
|
|
|
|
case 'definition':
|
|
|
|
case 'resourceLoaded':
|
|
|
|
case 'requiredRights':
|
|
|
|
case 'requiredSkins':
|
|
|
|
case 'targets':
|
|
|
|
case 'onByDefault':
|
|
|
|
case 'position':
|
2015-08-03 06:34:16 +00:00
|
|
|
case 'hidden':
|
2015-07-18 07:26:25 +00:00
|
|
|
case 'category':
|
|
|
|
$this->{$member} = $option;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw new InvalidArgumentException( "Unrecognized '$member' parameter" );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-03 06:37:32 +00:00
|
|
|
/**
|
|
|
|
* Create a object based on the metadata in a GadgetDefinitionContent object
|
|
|
|
*
|
|
|
|
* @param string $id
|
|
|
|
* @param GadgetDefinitionContent $content
|
|
|
|
* @return Gadget
|
|
|
|
*/
|
|
|
|
public static function newFromDefinitionContent( $id, GadgetDefinitionContent $content ) {
|
|
|
|
$data = $content->getAssocArray();
|
|
|
|
$prefixGadgetNs = function ( $page ) {
|
|
|
|
return 'Gadget:' . $page;
|
|
|
|
};
|
|
|
|
$info = array(
|
|
|
|
'name' => $id,
|
|
|
|
'resourceLoaded' => true,
|
|
|
|
'requiredRights' => $data['settings']['rights'],
|
|
|
|
'onByDefault' => $data['settings']['default'],
|
|
|
|
'hidden' => $data['settings']['hidden'],
|
|
|
|
'requiredSkins' => $data['settings']['skins'],
|
|
|
|
'category' => $data['settings']['category'],
|
|
|
|
'scripts' => array_map( $prefixGadgetNs, $data['module']['scripts'] ),
|
|
|
|
'styles' => array_map( $prefixGadgetNs, $data['module']['styles'] ),
|
|
|
|
'dependencies' => $data['module']['dependencies'],
|
|
|
|
'messages' => $data['module']['messages'],
|
|
|
|
'position' => $data['module']['position'],
|
|
|
|
);
|
|
|
|
|
|
|
|
return new self( $info );
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a placeholder object to use if a gadget doesn't exist
|
|
|
|
*
|
|
|
|
* @param string $id name
|
|
|
|
* @return Gadget
|
|
|
|
*/
|
|
|
|
public static function newEmptyGadget( $id ) {
|
|
|
|
return new self( array( 'name' => $id ) );
|
|
|
|
}
|
|
|
|
|
2015-07-18 07:26:25 +00:00
|
|
|
/**
|
|
|
|
* Whether the provided gadget id is valid
|
|
|
|
*
|
|
|
|
* @param string $id
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public static function isValidGadgetID( $id ) {
|
2015-08-03 06:37:32 +00:00
|
|
|
return strlen( $id ) > 0 && ResourceLoader::isValidModuleName( Gadget::getModuleName( $id ) );
|
2015-07-18 07:26:25 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
/**
|
2015-08-03 06:37:32 +00:00
|
|
|
* @param string $id Name of gadget
|
|
|
|
* @return string Name of ResourceLoader module for the gadget
|
2010-11-11 18:19:57 +00:00
|
|
|
*/
|
2015-08-03 06:37:32 +00:00
|
|
|
public static function getModuleName( $id ) {
|
|
|
|
return "ext.gadget.{$id}";
|
2010-11-11 18:19:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
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 )
|
2015-08-03 06:37:32 +00:00
|
|
|
&& ( $this->requiredSkins === true || !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;
|
|
|
|
}
|
|
|
|
|
2015-08-03 06:34:16 +00:00
|
|
|
/**
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function isHidden() {
|
|
|
|
return $this->hidden;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-08-03 06:37:32 +00:00
|
|
|
* @return Array: Array of pages with JS (including namespace)
|
2010-11-11 18:19:57 +00:00
|
|
|
*/
|
|
|
|
public function getScripts() {
|
|
|
|
return $this->scripts;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-08-03 06:37:32 +00:00
|
|
|
* @return Array: Array of pages with CSS (including namespace)
|
2010-11-11 18:19:57 +00:00
|
|
|
*/
|
|
|
|
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 );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-08-03 06:37:32 +00:00
|
|
|
* @return array
|
2010-11-11 18:19:57 +00:00
|
|
|
*/
|
2015-08-03 06:37:32 +00:00
|
|
|
public function getTargets() {
|
|
|
|
return $this->targets;
|
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;
|
|
|
|
}
|
|
|
|
|
2015-08-03 06:34:16 +00:00
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getMessages() {
|
|
|
|
return $this->messages;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|