Merge "language: Use item 'fallbackSequence' instead of duplicating logic"
[lhc/web/wiklou.git] / includes / resourceloader / ResourceLoaderWikiModule.php
index 1a1a6d0..74ad774 100644 (file)
  * because of its dependence on the functionality of
  * Title::isCssJsSubpage.
  */
-abstract class ResourceLoaderWikiModule extends ResourceLoaderModule {
+class ResourceLoaderWikiModule extends ResourceLoaderModule {
+       /** @var string Position on the page to load this module at */
+       protected $position = 'bottom';
 
-       /* Protected Members */
-
-       # Origin is user-supplied code
+       // Origin defaults to users with sitewide authority
        protected $origin = self::ORIGIN_USER_SITEWIDE;
 
        // In-object cache for title info
        protected $titleInfo = array();
 
-       /* Abstract Protected Methods */
+       // List of page names that contain CSS
+       protected $styles = array();
+
+       // List of page names that contain JavaScript
+       protected $scripts = array();
+
+       // Group of module
+       protected $group;
+
+       /**
+        * @param array $options For back-compat, this can be omitted in favour of overwriting getPages.
+        */
+       public function __construct( array $options = null ) {
+               if ( is_null( $options ) ) {
+                       return;
+               }
+
+               foreach ( $options as $member => $option ) {
+                       switch ( $member ) {
+                               case 'position':
+                                       $this->isPositionDefined = true;
+                                       // Don't break since we need the member set as well
+                               case 'styles':
+                               case 'scripts':
+                               case 'group':
+                                       $this->{$member} = $option;
+                                       break;
+                       }
+               }
+       }
 
        /**
         * Subclasses should return an associative array of resources in the module.
@@ -57,9 +86,34 @@ abstract class ResourceLoaderWikiModule extends ResourceLoaderModule {
         * @param ResourceLoaderContext $context
         * @return array
         */
-       abstract protected function getPages( ResourceLoaderContext $context );
+       protected function getPages( ResourceLoaderContext $context ) {
+               $config = $this->getConfig();
+               $pages = array();
+
+               // Filter out pages from origins not allowed by the current wiki configuration.
+               if ( $config->get( 'UseSiteJs' ) ) {
+                       foreach ( $this->scripts as $script ) {
+                               $pages[$script] = array( 'type' => 'script' );
+                       }
+               }
 
-       /* Protected Methods */
+               if ( $config->get( 'UseSiteCss' ) ) {
+                       foreach ( $this->styles as $style ) {
+                               $pages[$style] = array( 'type' => 'style' );
+                       }
+               }
+
+               return $pages;
+       }
+
+       /**
+        * Get group name
+        *
+        * @return string
+        */
+       public function getGroup() {
+               return $this->group;
+       }
 
        /**
         * Get the Database object used in getTitleMTimes(). Defaults to the local slave DB
@@ -105,8 +159,6 @@ abstract class ResourceLoaderWikiModule extends ResourceLoaderModule {
                return $content->serialize( $format );
        }
 
-       /* Methods */
-
        /**
         * @param ResourceLoaderContext $context
         * @return string
@@ -184,16 +236,15 @@ abstract class ResourceLoaderWikiModule extends ResourceLoaderModule {
        }
 
        /**
-        * Get the definition summary for this module.
-        *
         * @param ResourceLoaderContext $context
         * @return array
         */
        public function getDefinitionSummary( ResourceLoaderContext $context ) {
-               return array(
-                       'class' => get_class( $this ),
+               $summary = parent::getDefinitionSummary( $context );
+               $summary[] = array(
                        'pages' => $this->getPages( $context ),
                );
+               return $summary;
        }
 
        /**
@@ -263,4 +314,8 @@ abstract class ResourceLoaderWikiModule extends ResourceLoaderModule {
                }
                return $this->titleInfo[$hash];
        }
+
+       public function getPosition() {
+               return $this->position;
+       }
 }