/extensions/MwEmbedSupport/MwEmbedResourceManager.php

https://github.com/ChuguluGames/mediawiki-svn · PHP · 115 lines · 71 code · 12 blank · 32 comment · 12 complexity · 702187667efc395cae2f1f3881c5d3ce MD5 · raw file

  1. <?php
  2. /**
  3. * MwEmbedResourceManager adds some convenience functions for loading mwEmbed 'modules'.
  4. * Its shared between the mwEmbedStandAlone and the MwEmbed extension
  5. *
  6. * @file
  7. * @ingroup Extensions
  8. */
  9. class MwEmbedResourceManager {
  10. protected static $moduleSet = array();
  11. protected static $moduleConfig = array();
  12. /**
  13. * Register mwEmbeed resource set based on the
  14. *
  15. * Adds modules to ResourceLoader
  16. */
  17. public static function register( $mwEmbedResourcePath ) {
  18. global $IP, $wgExtensionMessagesFiles;
  19. $localResourcePath = $IP .'/' . $mwEmbedResourcePath;
  20. // Get the module name from the end of the path:
  21. $modulePathParts = explode( '/', $mwEmbedResourcePath );
  22. $moduleName = array_pop ( $modulePathParts );
  23. if( !is_dir( $localResourcePath ) ){
  24. throw new MWException( __METHOD__ . " not given readable path: " . htmlspecialchars( $localResourcePath ) );
  25. }
  26. if( substr( $mwEmbedResourcePath, -1 ) == '/' ){
  27. throw new MWException( __METHOD__ . " path has trailing slash: " . htmlspecialchars( $localResourcePath) );
  28. }
  29. // Add module messages if present:
  30. if( is_file( $localResourcePath . '/' . $moduleName . '.i18n.php' ) ){
  31. $wgExtensionMessagesFiles[ 'MwEmbed.' . $moduleName ] = $localResourcePath . '/' . $moduleName . '.i18n.php';
  32. }
  33. // Check that resource file is present:
  34. $resourceListFilePath = $localResourcePath . '/' . $moduleName . '.php';
  35. if( !is_file( $resourceListFilePath ) ){
  36. throw new MWException( __METHOD__ . " mwEmbed Module is missing resource list: " . htmlspecialchars( $resourceListFilePath ) );
  37. }
  38. // Get the mwEmbed module resource registration:
  39. $resourceList = include( $resourceListFilePath );
  40. // Look for special 'messages' => 'moduleFile' key and load all modules file messages:
  41. foreach( $resourceList as $name => $resources ){
  42. if( isset( $resources['messageFile'] ) && is_file( $localResourcePath . '/' .$resources['messageFile'] ) ){
  43. $resourceList[ $name ][ 'messages' ] = array();
  44. include( $localResourcePath . '/' .$resources['messageFile'] );
  45. foreach( $messages['en'] as $msgKey => $na ){
  46. $resourceList[ $name ][ 'messages' ][] = $msgKey;
  47. }
  48. }
  49. };
  50. // Check for module loader:
  51. if( is_file( $localResourcePath . '/' . $moduleName . '.loader.js' )){
  52. $resourceList[ $moduleName . '.loader' ] = array(
  53. 'loaderScripts' => $moduleName . '.loader.js'
  54. );
  55. }
  56. // Check for module config ( @@TODO support per-module config )
  57. $configPath = $localResourcePath . '/' . $moduleName . '.config.php';
  58. if( is_file( $configPath ) ){
  59. self::$moduleConfig = array_merge( self::$moduleConfig, include( $configPath ) );
  60. }
  61. // Add the resource list into the module set with its provided path
  62. self::$moduleSet[ $mwEmbedResourcePath ] = $resourceList;
  63. }
  64. public static function registerConfigVars( &$vars ){
  65. // Allow localSettings.php to override any module config by updating $wgMwEmbedModuleConfig var
  66. global $wgMwEmbedModuleConfig;
  67. foreach( self::$moduleConfig as $key => $value ){
  68. if( ! isset( $wgMwEmbedModuleConfig[ $key ] ) ){
  69. $wgMwEmbedModuleConfig[$key] = $value;
  70. }
  71. }
  72. $vars = array_merge( $vars, $wgMwEmbedModuleConfig );
  73. return $vars;
  74. }
  75. /**
  76. * ResourceLoaderRegisterModules hook
  77. *
  78. * Adds any mwEmbedResources to the ResourceLoader
  79. */
  80. public static function registerModules( &$resourceLoader ) {
  81. global $IP, $wgEnableMwEmbedStandAlone, $wgScriptPath;
  82. // Register all the resources with the resource loader
  83. foreach( self::$moduleSet as $path => $modules ) {
  84. foreach ( $modules as $name => $resources ) {
  85. // Register the resource with MwEmbed extended class if in standAlone resource loader mode:
  86. if( $wgEnableMwEmbedStandAlone === true ){
  87. $resourceLoader->register(
  88. // Resource loader expects trailing slash:
  89. $name, new MwEmbedResourceLoaderFileModule( $resources, "$IP/$path", $path)
  90. );
  91. } else {
  92. // Register with normal resource loader:
  93. $resourceLoader->register(
  94. // Resource loader expects trailing slash:
  95. $name, new ResourceLoaderFileModule( $resources, "$IP/$path", $wgScriptPath .'/' . $path)
  96. );
  97. }
  98. }
  99. }
  100. // Continue module processing
  101. return true;
  102. }
  103. }