Legoktm has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/166705

Change subject: [POC] Basic extension registration from an extension.json file
......................................................................

[POC] Basic extension registration from an extension.json file

Had to move up the loading of GlobalFunctions so it is usable
from LocalSettings.php

Change-Id: I7074b65d07c5c7d4e3f1fb0755d74a0b07ed4596
---
M includes/AutoLoader.php
A includes/ExtensionRegistrerer.php
M includes/GlobalFunctions.php
M includes/Setup.php
M includes/WebStart.php
M maintenance/doMaintenance.php
6 files changed, 142 insertions(+), 1 deletion(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/core 
refs/changes/05/166705/1

diff --git a/includes/AutoLoader.php b/includes/AutoLoader.php
index cd6a8ca..012c70a 100644
--- a/includes/AutoLoader.php
+++ b/includes/AutoLoader.php
@@ -69,6 +69,7 @@
        'DumpPipeOutput' => 'includes/Export.php',
        'EditPage' => 'includes/EditPage.php',
        'EmptyBloomCache' => 'includes/cache/bloom/BloomCache.php',
+       'ExtensionRegistrerer' => 'includes/ExtensionRegistrerer.php',
        'Fallback' => 'includes/Fallback.php',
        'FauxRequest' => 'includes/WebRequest.php',
        'FauxResponse' => 'includes/WebResponse.php',
diff --git a/includes/ExtensionRegistrerer.php 
b/includes/ExtensionRegistrerer.php
new file mode 100644
index 0000000..9b720ff
--- /dev/null
+++ b/includes/ExtensionRegistrerer.php
@@ -0,0 +1,128 @@
+<?php
+
+class ExtensionRegistrerer {
+
+       /**
+        * @var BagOStuff
+        */
+       protected $cache;
+       /**
+        * @var ConfigFactory
+        */
+       protected $configFactory;
+
+       /**
+        * @param ConfigFactory $configFactory
+        */
+       public function __construct( ConfigFactory $configFactory ) {
+               $this->configFactory = $configFactory;
+               // Setup APC caching if possible, otherwise just skip it
+               try {
+                       $this->cache = wfGetCache( CACHE_ACCEL );
+               } catch ( MWException $e ) {
+                       $this->cache = new EmptyBagOStuff();
+               }
+       }
+
+       /**
+        * @param string $filename
+        * @param int $mtime
+        * @return array
+        */
+       public function loadExtensionInfoFromFile( $filename, $mtime ) {
+               $key = wfMemcKey( 'extreg', md5( $filename ) );
+               $cached = $this->cache->get( $key );
+               if ( isset( $cached['mtime'] ) && $cached['mtime'] === $mtime ) 
{
+                       return $cached['info'];
+               }
+
+               $contents = file_get_contents( $filename );
+               $json = json_decode( $contents );
+               if ( is_object( $json ) ) {
+                       $json = wfObjectToArray( $json );
+                       $this->cache->set( $key, array( 'mtime' => $mtime, 
'info' => $json ) );
+               } else {
+                       // Don't throw an error here, but don't cache it either.
+                       $json = array();
+               }
+
+               return $json;
+       }
+
+       /**
+        * @param string $name
+        * @param string|null $path
+        * @return array
+        */
+       private function getPathAndMTime( $name, $path = null ) {
+               if ( !$path ) {
+                       global $IP;
+                       $path = "$IP/extensions";
+               }
+               $fullpath = $path . DIRECTORY_SEPARATOR . $name . 
DIRECTORY_SEPARATOR . "extension.json";
+               $mtime = filemtime( $fullpath );
+               return array( $fullpath, $mtime );
+       }
+
+       public function loadExtension( $name, $path = null ) {
+               list( $fullpath, $mtime ) = $this->getPathAndMTime( $name, 
$path );
+               $info = $this->loadExtensionInfoFromFile( $fullpath, $mtime );
+               //var_dump("Loading $name" );
+               $this->processInfo( $name, $info );
+
+       }
+
+       protected function processInfo( $name, $info ) {
+               $this->setCredits( $info );
+               foreach ( $info as $key => $val ) {
+                       if ( $key === 'config' ) {
+                               $this->registerConfig( $name, $val );
+                       } else {
+                               $this->setToGlobal( $key, $val );
+                       }
+               }
+       }
+
+       protected function setCredits( &$info ) {
+               $attrs = array(
+                       'name',
+                       'type',
+                       'authors',
+                       'path',
+                       'version',
+                       'url',
+                       'description',
+                       'descriptionmsg',
+                       'license-name',
+               );
+               $credits = array();
+               foreach ( $attrs as $attr ) {
+                       if ( isset( $info[$attr] ) ) {
+                               $credits[$attr] = $info[$attr];
+                               unset( $info[$attr] );
+                       }
+               }
+
+               $GLOBALS['wgExtensionCredits'][] = $credits;
+       }
+
+       protected function setToGlobal( $global, $value ) {
+               $GLOBALS["wg$global"] = array_merge_recursive( 
$GLOBALS["wg$global"], $value );
+       }
+
+       /**
+        * @param string $name
+        * @param array $config
+        */
+       protected function registerConfig( $name, $config ) {
+               $this->configFactory->register( $name . '-default', function() 
use ( $config ) {
+                       return new HashConfig( $config );
+               } );
+               $this->configFactory->register( $name, function( $name, 
ConfigFactory $factory ) {
+                       return new MultiConfig( array(
+                               GlobalVarConfig::newInstance(),
+                               $factory->makeConfig( $name . '-default' )
+                       ) );
+               } );
+       }
+}
diff --git a/includes/GlobalFunctions.php b/includes/GlobalFunctions.php
index 2c26fef..78ea796 100644
--- a/includes/GlobalFunctions.php
+++ b/includes/GlobalFunctions.php
@@ -160,6 +160,15 @@
 }
 /// @endcond
 
+function wfLoadExtension( $name, $path = null ) {
+       static $registerer = null;
+       if ( $registerer === null ) {
+               $registerer = new ExtensionRegistrerer( 
ConfigFactory::getDefaultInstance() );
+       }
+
+       $registerer->loadExtension( $name, $path );
+}
+
 /**
  * Like array_diff( $a, $b ) except that it works with two-dimensional arrays.
  * @param array $a
diff --git a/includes/Setup.php b/includes/Setup.php
index 743936e..08a1202 100644
--- a/includes/Setup.php
+++ b/includes/Setup.php
@@ -474,7 +474,6 @@
 
 wfProfileIn( $fname . '-includes' );
 require_once "$IP/includes/normal/UtfNormalUtil.php";
-require_once "$IP/includes/GlobalFunctions.php";
 require_once "$IP/includes/normal/UtfNormalDefines.php";
 wfProfileOut( $fname . '-includes' );
 
diff --git a/includes/WebStart.php b/includes/WebStart.php
index cb35ee5..4ba6eba 100644
--- a/includes/WebStart.php
+++ b/includes/WebStart.php
@@ -79,6 +79,9 @@
 # Load default settings
 require_once "$IP/includes/DefaultSettings.php";
 
+# Load global functions
+require_once "$IP/includes/GlobalFunctions.php";
+
 # Load composer's autoloader if present
 if ( is_readable( "$IP/vendor/autoload.php" ) ) {
        require_once "$IP/vendor/autoload.php";
diff --git a/maintenance/doMaintenance.php b/maintenance/doMaintenance.php
index 46844c9..c46e4ef 100644
--- a/maintenance/doMaintenance.php
+++ b/maintenance/doMaintenance.php
@@ -68,6 +68,7 @@
 // Some other requires
 require_once "$IP/includes/Defines.php";
 require_once "$IP/includes/DefaultSettings.php";
+require_once "$IP/includes/GlobalFunctions.php";
 
 # Load composer's autoloader if present
 if ( is_readable( "$IP/vendor/autoload.php" ) ) {

-- 
To view, visit https://gerrit.wikimedia.org/r/166705
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I7074b65d07c5c7d4e3f1fb0755d74a0b07ed4596
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/core
Gerrit-Branch: master
Gerrit-Owner: Legoktm <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to