Pastakhov has uploaded a new change for review.

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

Change subject: use hook ParserClearState (v 3.1.0)
......................................................................

use hook ParserClearState (v 3.1.0)

* modify $wgPhpTagsNamespaces
* add exception FATAL_DENIED_FOR_NAMESPACE
* add function Runtime::reset()
* add function \PhpTags::initialize()
* remove runction \PhpTags::isBanned()

Change-Id: I879023677e3da72d14c80282ed34914264ee1d37
---
M PhpTags.body.php
M PhpTags.php
M includes/PhpTagsException.php
M includes/Runtime.php
4 files changed, 79 insertions(+), 98 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/PhpTags 
refs/changes/42/152842/1

diff --git a/PhpTags.body.php b/PhpTags.body.php
index dfa457c..3fc5e32 100644
--- a/PhpTags.body.php
+++ b/PhpTags.body.php
@@ -16,6 +16,8 @@
        private static $bytecodeNeedsUpdate = array();
        private static $frames=array();
        private static $needInitRuntime = true;
+       private static $bytecodeCache = array();
+       private static $bytecodeLoaded = array();
 
        /**
         *
@@ -24,24 +26,10 @@
         * @param array $args
         */
        public static function renderFunction( $parser, $frame, $args ) {
-               global $wgPhpTagsTime, $wgPhpTagsMaxLoops;
+               global $wgPhpTagsTime;
 
                //$time = microtime(true);
                $time = $parser->mOutput->getTimeSinceStart('cpu');
-
-               $is_banned = self::isBanned($frame);
-               if ( $is_banned ) {
-                       return $is_banned;
-               }
-
-               if ( self::$needInitRuntime ) {
-                       \wfRunHooks( 'PhpTagsRuntimeFirstInit' );
-                       \PhpTags\Runtime::$loopsLimit = $wgPhpTagsMaxLoops;
-                       self::$needInitRuntime = false;
-               }
-
-               \PhpTags\Runtime::$transit[PHPTAGS_TRANSIT_PARSER] = $parser;
-               \PhpTags\Runtime::$transit[PHPTAGS_TRANSIT_PPFRAME] = $frame;
 
                $command = array_shift($args);
                if ( count( $args ) > 0 ) {
@@ -54,12 +42,12 @@
                }
 
                $frameTitle = $frame->getTitle();
-               $titleText = $frameTitle->getPrefixedText();
-               $arguments = array( $titleText ) + $frame->getArguments();
+               $frameTitleText = $frameTitle->getPrefixedText();
+               $arguments = array( $frameTitleText ) + $frame->getArguments();
                $scope = self::getScope( $frame );
 
                try {
-                       $bytecode = self::getBytecode( $frameTitle, $command, 
$time, $parser );
+                       $bytecode = self::getBytecode( $command, $parser, 
$frame, $frameTitle, $frameTitleText, $time );
                        $result = \PhpTags\Runtime::run( $bytecode, $arguments, 
$scope );
                        $return = implode( $result );
                } catch ( \PhpTags\PhpTagsException $exc ) {
@@ -74,34 +62,21 @@
                return \UtfNormal::cleanUp($return);
        }
 
-       public static function render($input, array $args, Parser $parser, 
PPFrame $frame) {
-               global $wgPhpTagsTime, $wgPhpTagsMaxLoops;
+       public static function render( $input, array $args, Parser $parser, 
PPFrame $frame ) {
+               global $wgPhpTagsTime;
 
                //$time = microtime(true);
                $time = $parser->mOutput->getTimeSinceStart( 'cpu' );
 
-               $is_banned = self::isBanned( $frame );
-               if ( false !== $is_banned ) {
-                       return $is_banned;
-               }
-
-               if ( self::$needInitRuntime ) {
-                       \wfRunHooks( 'PhpTagsRuntimeFirstInit' );
-                       \PhpTags\Runtime::$loopsLimit = $wgPhpTagsMaxLoops;
-                       self::$needInitRuntime = false;
-               }
-
-               \PhpTags\Runtime::$transit[PHPTAGS_TRANSIT_PARSER] = $parser;
-               \PhpTags\Runtime::$transit[PHPTAGS_TRANSIT_PPFRAME] = $frame;
                $return = false;
 
                $frameTitle = $frame->getTitle();
-               $titleText = $frameTitle->getPrefixedText();
-               $arguments = array( $titleText ) + $frame->getArguments();
+               $frameTitleText = $frameTitle->getPrefixedText();
+               $arguments = array( $frameTitleText ) + $frame->getArguments();
                $scope = self::getScope( $frame );
 
                try {
-                       $bytecode = self::getBytecode( $frameTitle, $input, 
$time, $parser );
+                       $bytecode = self::getBytecode( $input, $parser, $frame, 
$frameTitle, $frameTitleText, $time );
                        $result = \PhpTags\Runtime::run( $bytecode, $arguments, 
$scope );
                } catch ( \PhpTags\PhpTagsException $exc ) {
                        // $wgPhpTagsTime += microtime(true) - $time;
@@ -127,40 +102,73 @@
                return \UtfNormal::cleanUp($return);
        }
 
-       private static function getBytecode( $frameTitle, $source, $time, 
$parser ) {
+       /**
+        *
+        * @global int $wgPhpTagsBytecodeExptime
+        * @param string $source
+        * @param Parser $parser
+        * @param PPFrame $frame
+        * @param Title $frameTitle
+        * @param string $frameTitleText
+        * @param int $time
+        * @return array
+        */
+       private static function getBytecode( $source, $parser, $frame, 
$frameTitle, $frameTitleText, $time ) {
                global $wgPhpTagsBytecodeExptime;
-               static $bytecodeCache = array();
-               static $bytecodeLoaded = array();
 
-               $titleText = $frameTitle->getPrefixedText();
                $frameID = $frameTitle->getArticleID();
                $md5Source = md5( $source );
 
-               if ( true === isset( $bytecodeCache[$frameID][$md5Source] ) ) {
-                       return $bytecodeCache[$frameID][$md5Source];
+               self::initialize( $parser, $frame, $frameTitle );
+
+               if ( true === isset( self::$bytecodeCache[$frameID][$md5Source] 
) ) {
+                       return self::$bytecodeCache[$frameID][$md5Source];
                }
 
-               if ( $wgPhpTagsBytecodeExptime > 0 && $frameID > 0 && false === 
isset( $bytecodeLoaded[$frameID] ) ) {
+               if ( $wgPhpTagsBytecodeExptime > 0 && $frameID > 0 && false === 
isset( self::$bytecodeLoaded[$frameID] ) ) {
                        $cache = wfGetCache( CACHE_ANYTHING );
                        $key = wfMemcKey( 'phptags', $frameID, 
PHPTAGS_RUNTIME_RELEASE );
                        $data = $cache->get( $key );
-                       $bytecodeLoaded[$frameID] = true;
+                       self::$bytecodeLoaded[$frameID] = true;
                        if ( false !== $data ) {
                                $arrayData = unserialize( $data );
-                               $bytecodeCache[$frameID] = $arrayData;
-                               if ( true === isset( 
$bytecodeCache[$frameID][$md5Source] ) ) {
-                                       return 
$bytecodeCache[$frameID][$md5Source];
+                               self::$bytecodeCache[$frameID] = $arrayData;
+                               if ( true === isset( 
self::$bytecodeCache[$frameID][$md5Source] ) ) {
+                                       return 
self::$bytecodeCache[$frameID][$md5Source];
                                }
                        }
                }
 
                $compiler = new PhpTags\Compiler();
-               $bytecode = $compiler->compile( $source, $titleText );
-               $bytecodeCache[$frameID][$md5Source] = $bytecode;
-               self::$bytecodeNeedsUpdate[$frameID] =& 
$bytecodeCache[$frameID];
+               $bytecode = $compiler->compile( $source, $frameTitleText );
+               self::$bytecodeCache[$frameID][$md5Source] = $bytecode;
+               self::$bytecodeNeedsUpdate[$frameID] =& 
self::$bytecodeCache[$frameID];
 
                self::$compileTime += $parser->mOutput->getTimeSinceStart( 
'cpu' ) - $time;
                return $bytecode;
+       }
+
+       /**
+        *
+        * @param Parser $parser
+        * @param PPFrame $frame
+        * @throws \PhpTags\PhpTagsException
+        * @return null
+        */
+       private static function initialize( $parser, $frame, $frameTitle ) {
+               global $wgPhpTagsNamespaces, $wgPhpTagsMaxLoops;
+               if ( true !== $wgPhpTagsNamespaces && false === isset( 
$wgPhpTagsNamespaces[$frameTitle->getNamespace()] ) ) {
+                       throw new \PhpTags\PhpTagsException( 
\PhpTags\PhpTagsException::FATAL_DENIED_FOR_NAMESPACE, $frameTitle->getNsText() 
);
+               }
+
+               if ( true === self::$needInitRuntime ) {
+                       \wfRunHooks( 'PhpTagsRuntimeFirstInit' );
+                       \PhpTags\Runtime::$loopsLimit = $wgPhpTagsMaxLoops;
+                       self::$needInitRuntime = false;
+               }
+
+               \PhpTags\Runtime::$transit[PHPTAGS_TRANSIT_PARSER] = $parser;
+               \PhpTags\Runtime::$transit[PHPTAGS_TRANSIT_PPFRAME] = $frame;
        }
 
        public static function updateBytecodeCache() {
@@ -187,29 +195,10 @@
                $cache->delete( $key );
        }
 
-       /**
-        *
-        * @global type $wgPhpTagsNamespaces
-        * @param PPFrame $frame
-        * @return mixed
-        */
-       public static function isBanned( PPFrame $frame ) {
-               global $wgPhpTagsNamespaces;
-               if ( $wgPhpTagsNamespaces !== true && 
!in_array($frame->getTitle()->getNamespace(), $wgPhpTagsNamespaces) ) {
-                       return Html::element(
-                                       'span', array( 'class'=>'error' ),
-                                       wfMessage( 
'phptags-disabled-for-namespace', $frame->getTitle()->getNsText() )->text()
-                               );
-               }
-//             if ( $wgPhpTagsPermittedTime !== true && self::$time >= 
\PhpTags\Runtime::$permittedTime ) {
-//                     return Html::element( 'span', array('class'=>'error'),
-//                             wfMessage( 
'phptags-fatal-error-max-execution-time' )
-//                                     ->numParams( 
\PhpTags\Runtime::$permittedTime )
-//                                     ->params( 
$frame->getTitle()->getPrefixedText() )
-//                                     ->text()
-//                     );
-//             }
-               return false;
+       public static function onParserClearState() {
+               PhpTags\Runtime::reset();
+               self::$bytecodeCache = array();
+               self::$bytecodeLoaded = array();
        }
 
        /**
diff --git a/PhpTags.php b/PhpTags.php
index b03b6f4..e14fac1 100644
--- a/PhpTags.php
+++ b/PhpTags.php
@@ -16,8 +16,8 @@
 }
 
 define( 'PHPTAGS_MAJOR_VERSION', 3 );
-define( 'PHPTAGS_MINOR_VERSION', 0 );
-define( 'PHPTAGS_RELEASE_VERSION', 1 );
+define( 'PHPTAGS_MINOR_VERSION', 1 );
+define( 'PHPTAGS_RELEASE_VERSION', 0 );
 define( 'PHPTAGS_VERSION', PHPTAGS_MAJOR_VERSION . '.' . PHPTAGS_MINOR_VERSION 
. '.' . PHPTAGS_RELEASE_VERSION );
 
 define( 'PHPTAGS_HOOK_RELEASE', 4 );
@@ -61,6 +61,7 @@
 $wgHooks['OutputPageParserOutput'][]   = 'PhpTags::updateBytecodeCache';
 $wgHooks['ArticleDeleteComplete'][]            = 'PhpTags::clearBytecodeCache';
 $wgHooks['PageContentSaveComplete'][]  = 'PhpTags::clearBytecodeCache';
+$wgHooks['ParserClearState'][]                 = 'PhpTags::onParserClearState';
 
 $wgPhpTagsTime = 0;
 /**
@@ -114,11 +115,11 @@
  * $wgExtraNamespaces[NS_PHPTAGS] = "PhpTags";
  * $wgExtraNamespaces[NS_PHPTAGS_TALK] = "PhpTags_Talk";
  *
- * $wgPhpTagsNamespaces = array( NS_PHPTAGS );
+ * $wgPhpTagsNamespaces = array( NS_PHPTAGS => true );
  * $wgNamespaceProtection[NS_PHPTAGS] = array( 'phptags_editor' );
  * $wgGroupPermissions['sysop']['phptags_editor'] = true;
  *
- * @var mixed Namespaces Array of namespaces in which allowed to use the 
extension PhpTags, and if boolean 'true' then it is unlimited namespaces
+ * @var mixed $wgPhpTagsNamespaces Array of namespaces in which allowed to use 
the extension PhpTags, and if boolean 'true' then it is unlimited namespaces
  */
 $wgPhpTagsNamespaces = true; // By default, this is unlimited namespaces
 
diff --git a/includes/PhpTagsException.php b/includes/PhpTagsException.php
index 918867c..b5b0eb8 100644
--- a/includes/PhpTagsException.php
+++ b/includes/PhpTagsException.php
@@ -165,6 +165,9 @@
                        case self::WARNING_EXPECTS_AT_MOST_PARAMETERS:
                                $message = "$arguments[0]() expects at most 
$arguments[1] parameters, $arguments[2] given";
                                break;
+                       case self::FATAL_DENIED_FOR_NAMESPACE:
+                               $message = wfMessage( 
'phptags-disabled-for-namespace', $arguments )->text();
+                               break;
                        default:
                                $message = "Undefined error, code 
{$this->code}";
                                $this->code = self::EXCEPTION_FATAL * 1000;
@@ -247,6 +250,8 @@
        const FATAL_CALL_FUNCTION_ON_NON_OBJECT = 4019; // PHP Fatal error:  
Call to a member function doo() on a non-object
        const FATAL_ACCESS_TO_UNDECLARED_STATIC_PROPERTY = 4020; // PHP Fatal 
error:  Access to undeclared static property: F::$rsrr
 
+       const FATAL_DENIED_FOR_NAMESPACE = 4500;
+
        const EXCEPTION_CATCHABLE_FATAL = 5;
        const FATAL_OBJECT_COULD_NOT_BE_CONVERTED = 5001;  //PHP Catchable 
fatal error:  Object of class stdClass could not be converted to string
 
diff --git a/includes/Runtime.php b/includes/Runtime.php
index f1d0222..ccf6cb0 100644
--- a/includes/Runtime.php
+++ b/includes/Runtime.php
@@ -91,34 +91,20 @@
 
        public static $loopsLimit = 0;
        public static $transit = array();
-//     static public $time = 0;
-//     static public $permittedTime = true;
-//     protected static $startTime = array();
 
        private static $variables = array();
        private static $staticVariables = array();
        private static $globalVariables = array();
 
-       /*public function startTime($scope) {
-               self::$startTime[$scope] = microtime(true);
-               if( isset(self::$time[$scope]) ) {
-                       return $this->checkExceedsTime();
-               }else{
-                       self::$time[$scope] = 0;
-               }
-       }
+       public static function reset() {
+               global $wgPhpTagsMaxLoops;
 
-       public function stopTime($scope) {
-               self::$time[$scope] += microtime(true) - 
self::$startTime[$scope];
+               self::$variables = array();
+               self::$staticVariables = array();
+               self::$globalVariables = array();
+               self::$transit = array();
+               self::$loopsLimit = $wgPhpTagsMaxLoops;
        }
-
-       public function checkExceedsTime() {
-               global $wgFoxway_max_execution_time_for_scope;
-               if( microtime(true) - self::$startTime[$this->scope] + 
self::$time[$this->scope] > $wgFoxway_max_execution_time_for_scope ) {
-                       return new ErrorMessage( __LINE__, null, E_ERROR, 
array( 'foxway-php-fatal-error-max-execution-time-scope', 
$wgFoxway_max_execution_time_for_scope, 
isset($this->args[0])?$this->args[0]:'n\a' ) );
-               }
-               return null;
-       }*/
 
        public static function runSource($code, array $args = array(), $scope = 
'' ) {
                $compiler = new Compiler();

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I879023677e3da72d14c80282ed34914264ee1d37
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/PhpTags
Gerrit-Branch: master
Gerrit-Owner: Pastakhov <[email protected]>

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

Reply via email to