[MediaWiki-commits] [Gerrit] ZeroPortal setRawResult lua mode - change (mediawiki...ZeroPortal)

2014-10-28 Thread Yurik (Code Review)
Yurik has uploaded a new change for review.

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

Change subject: ZeroPortal setRawResult lua mode
..

ZeroPortal setRawResult lua mode

This replaces the very hacky (and partially escaping broken) mode
where lua module decides to output data in the raw format.
Now, lua can set raw output data directly with
  mw.zeroportal.setRawResult(text)

This function is only enabled when used from Special:ZeroPortal.

Eventually, we might want to port this to the scribunto ext.

A second patch will remove current raw=1 code once code is fixed.

Change-Id: Ibb97523f6b3164268eaa80d94ddb001e7f6a6088
---
M ZeroPortal.php
A includes/LuaLibrary.lua
A includes/LuaLibrary.php
M includes/PortalSpecialPage.php
4 files changed, 136 insertions(+), 2 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/ZeroPortal 
refs/changes/63/169563/1

diff --git a/ZeroPortal.php b/ZeroPortal.php
index 611d611..5f7056b 100644
--- a/ZeroPortal.php
+++ b/ZeroPortal.php
@@ -40,6 +40,7 @@
 foreach ( array(
  'ApiZeroPortal',
  'ConfigPageHooks',
+ 'LuaLibrary',
  'PortalSpecialPage',
  'ZeroConfigView',
   ) as $key = $class ) {
@@ -64,6 +65,7 @@
 
 $wgAPIModules['zeroportal'] = 'ZeroPortal\ApiZeroPortal';
 $wgHooks['BeforePageDisplay'][] = 
'ZeroPortal\ConfigPageHooks::onBeforePageDisplay';
+$wgHooks['ScribuntoExternalLibraries'][] = 
'ZeroPortal\LuaLibrary::onScribuntoExternalLibraries';
 
 // Set our own view class for the JsonZeroConfig model, and configure local 
storage
 $wgJsonConfigModels['JsonZeroConfig']['view'] = 'ZeroPortal\ZeroConfigView';
diff --git a/includes/LuaLibrary.lua b/includes/LuaLibrary.lua
new file mode 100644
index 000..2917e19
--- /dev/null
+++ b/includes/LuaLibrary.lua
@@ -0,0 +1,21 @@
+local p = {}
+local php
+
+function p.setupInterface( options )
+-- Boilerplate
+p.setupInterface = nil
+php = mw_interface
+mw_interface = nil
+
+-- Register this library in the mw global
+mw = mw or {}
+mw.zeroportal = p
+
+package.loaded['mw.zeroportal'] = p
+end
+
+function p.setRawResult( result )
+return php.setRawResult( result )
+end
+
+return p
diff --git a/includes/LuaLibrary.php b/includes/LuaLibrary.php
new file mode 100644
index 000..d4a8e87
--- /dev/null
+++ b/includes/LuaLibrary.php
@@ -0,0 +1,103 @@
+?php
+
+namespace ZeroPortal;
+
+use Scribunto_LuaError;
+use Scribunto_LuaLibraryBase;
+use IContextSource;
+
+class LuaLibrary extends Scribunto_LuaLibraryBase {
+
+   const luaNamespace = 'mw.zeroportal.';
+
+   /** @var IContextSource */
+   private static $context = null;
+
+   /** @var mixed */
+   private static $result = null;
+
+   /**
+* Add Lua library if the current title is our special module
+* @param string $engine
+* @param string[] $extraLibraries
+* @return bool
+*/
+   public static function onScribuntoExternalLibraries( $engine, array 
$extraLibraries ) {
+   if ( $engine == 'lua' ) {
+   $extraLibraries['mw.zeroportal'] = 
'ZeroPortal\LuaLibrary';
+   }
+   return true;
+   }
+
+   /**
+* @param IContextSource $context
+*/
+   public static function setContext( $context ) {
+   self::$context = $context;
+   }
+
+   /**
+* @return IContextSource
+* @throws Scribunto_LuaError
+*/
+   public static function getContext() {
+   if ( !self::$context ) {
+   // Something is seriously wrong - it shouldn't even be 
possible to call this function
+   throw new Scribunto_LuaError( 'This function is only 
available in Special:ZeroPortal' );
+   }
+   return self::$context;
+   }
+
+   /**
+* Finishes up the invocation, clean up context, and overrides output 
if needed
+*/
+   public static function endInvoke() {
+   $ctx = self::getContext();
+   $out = $ctx-getOutput();
+   $req = $ctx-getRequest();
+   $resp = $req-response();
+   self::setContext( null ); // Ensure that subsequent lua 
invocations don't permit
+
+   if ( !$req-wasPosted() ) {
+   $expiryUnixTime = time() + 300; // default 300 seconds 
caching
+   $resp-header( 'Expires: ' . wfTimestamp( TS_RFC2822, 
$expiryUnixTime ) );
+
+   $cacheMode = $ctx-getUser()-isAnon() ? 'public' : 
'private';
+   $resp-header( 'Cache-Control: ' . $cacheMode . ', 
must-revalidate, max-age=0' );
+   }
+
+   if ( self::$result ) {
+   $out-disable();
+   $resp-header( 'Content-Type: text/plain; 

[MediaWiki-commits] [Gerrit] ZeroPortal setRawResult lua mode - change (mediawiki...ZeroPortal)

2014-10-28 Thread jenkins-bot (Code Review)
jenkins-bot has submitted this change and it was merged.

Change subject: ZeroPortal setRawResult lua mode
..


ZeroPortal setRawResult lua mode

This replaces the very hacky (and partially escaping broken) mode
where lua module decides to output data in the raw format.
Now, lua can set raw output data directly with
  mw.zeroportal.setRawResult(text)

This function is only enabled when used from Special:ZeroPortal.

Eventually, we might want to port this to the scribunto ext.

A second patch will remove current raw=1 code once code is fixed.

Change-Id: Ibb97523f6b3164268eaa80d94ddb001e7f6a6088
---
M ZeroPortal.php
A includes/LuaLibrary.lua
A includes/LuaLibrary.php
M includes/PortalSpecialPage.php
4 files changed, 139 insertions(+), 2 deletions(-)

Approvals:
  Dr0ptp4kt: Looks good to me, approved
  Brion VIBBER: Looks good to me, but someone else must approve
  Jhobs: Looks good to me, but someone else must approve
  jenkins-bot: Verified



diff --git a/ZeroPortal.php b/ZeroPortal.php
index 611d611..5f7056b 100644
--- a/ZeroPortal.php
+++ b/ZeroPortal.php
@@ -40,6 +40,7 @@
 foreach ( array(
  'ApiZeroPortal',
  'ConfigPageHooks',
+ 'LuaLibrary',
  'PortalSpecialPage',
  'ZeroConfigView',
   ) as $key = $class ) {
@@ -64,6 +65,7 @@
 
 $wgAPIModules['zeroportal'] = 'ZeroPortal\ApiZeroPortal';
 $wgHooks['BeforePageDisplay'][] = 
'ZeroPortal\ConfigPageHooks::onBeforePageDisplay';
+$wgHooks['ScribuntoExternalLibraries'][] = 
'ZeroPortal\LuaLibrary::onScribuntoExternalLibraries';
 
 // Set our own view class for the JsonZeroConfig model, and configure local 
storage
 $wgJsonConfigModels['JsonZeroConfig']['view'] = 'ZeroPortal\ZeroConfigView';
diff --git a/includes/LuaLibrary.lua b/includes/LuaLibrary.lua
new file mode 100644
index 000..04be097
--- /dev/null
+++ b/includes/LuaLibrary.lua
@@ -0,0 +1,24 @@
+local p = {}
+local php
+
+function p.setupInterface( options )
+-- Copy the PHP callbacks to a local variable, and remove the global
+p.setupInterface = nil
+php = mw_interface
+mw_interface = nil
+
+-- Register this library in the mw global as mw.zeroportal.*
+mw = mw or {}
+mw.zeroportal = p
+
+-- Indicate that we're loaded
+package.loaded['mw.zeroportal'] = p
+end
+
+-- Each of the following functions should be documented in the corresponding 
LuaLibrary.php function
+
+function p.setRawResult( result )
+return php.setRawResult( result )
+end
+
+return p
diff --git a/includes/LuaLibrary.php b/includes/LuaLibrary.php
new file mode 100644
index 000..d4a8e87
--- /dev/null
+++ b/includes/LuaLibrary.php
@@ -0,0 +1,103 @@
+?php
+
+namespace ZeroPortal;
+
+use Scribunto_LuaError;
+use Scribunto_LuaLibraryBase;
+use IContextSource;
+
+class LuaLibrary extends Scribunto_LuaLibraryBase {
+
+   const luaNamespace = 'mw.zeroportal.';
+
+   /** @var IContextSource */
+   private static $context = null;
+
+   /** @var mixed */
+   private static $result = null;
+
+   /**
+* Add Lua library if the current title is our special module
+* @param string $engine
+* @param string[] $extraLibraries
+* @return bool
+*/
+   public static function onScribuntoExternalLibraries( $engine, array 
$extraLibraries ) {
+   if ( $engine == 'lua' ) {
+   $extraLibraries['mw.zeroportal'] = 
'ZeroPortal\LuaLibrary';
+   }
+   return true;
+   }
+
+   /**
+* @param IContextSource $context
+*/
+   public static function setContext( $context ) {
+   self::$context = $context;
+   }
+
+   /**
+* @return IContextSource
+* @throws Scribunto_LuaError
+*/
+   public static function getContext() {
+   if ( !self::$context ) {
+   // Something is seriously wrong - it shouldn't even be 
possible to call this function
+   throw new Scribunto_LuaError( 'This function is only 
available in Special:ZeroPortal' );
+   }
+   return self::$context;
+   }
+
+   /**
+* Finishes up the invocation, clean up context, and overrides output 
if needed
+*/
+   public static function endInvoke() {
+   $ctx = self::getContext();
+   $out = $ctx-getOutput();
+   $req = $ctx-getRequest();
+   $resp = $req-response();
+   self::setContext( null ); // Ensure that subsequent lua 
invocations don't permit
+
+   if ( !$req-wasPosted() ) {
+   $expiryUnixTime = time() + 300; // default 300 seconds 
caching
+   $resp-header( 'Expires: ' . wfTimestamp( TS_RFC2822, 
$expiryUnixTime ) );
+
+   $cacheMode = $ctx-getUser()-isAnon() ? 'public' :