diff options
Diffstat (limited to 'www/wiki/extensions/SemanticMediaWiki/src/Utils/CircularReferenceGuard.php')
-rw-r--r-- | www/wiki/extensions/SemanticMediaWiki/src/Utils/CircularReferenceGuard.php | 110 |
1 files changed, 110 insertions, 0 deletions
diff --git a/www/wiki/extensions/SemanticMediaWiki/src/Utils/CircularReferenceGuard.php b/www/wiki/extensions/SemanticMediaWiki/src/Utils/CircularReferenceGuard.php new file mode 100644 index 00000000..26cb35ec --- /dev/null +++ b/www/wiki/extensions/SemanticMediaWiki/src/Utils/CircularReferenceGuard.php @@ -0,0 +1,110 @@ +<?php + +namespace SMW\Utils; + +/** + * @license GNU GPL v2+ + * @since 2.2 + * + * @author mwjames + */ +class CircularReferenceGuard { + + /** + * @var array + */ + private static $circularRefGuard = []; + + /** + * @var string + */ + private $namespace = ''; + + /** + * @var integer + */ + private $maxRecursionDepth = 1; + + /** + * @since 2.2 + * + * @param string $namespace + */ + public function __construct( $namespace = '' ) { + $this->namespace = $namespace; + } + + /** + * @since 2.2 + * + * @param integer $maxRecursionDepth + */ + public function setMaxRecursionDepth( $maxRecursionDepth ) { + $this->maxRecursionDepth = (int)$maxRecursionDepth; + } + + /** + * @since 2.2 + * + * @param string $hash + */ + public function mark( $hash ) { + + if ( !isset( self::$circularRefGuard[$this->namespace][$hash] ) ) { + self::$circularRefGuard[$this->namespace][$hash] = 0; + } + + self::$circularRefGuard[$this->namespace][$hash]++; + } + + /** + * @since 2.2 + * + * @param string $hash + */ + public function unmark( $hash ) { + + if ( isset( self::$circularRefGuard[$this->namespace][$hash] ) && self::$circularRefGuard[$this->namespace][$hash] > 0 ) { + return self::$circularRefGuard[$this->namespace][$hash]--; + } + + unset( self::$circularRefGuard[$this->namespace][$hash] ); + } + + /** + * @since 2.2 + * + * @param string $hash + * + * @return boolean + */ + public function isCircular( $hash ) { + return $this->get( $hash ) > $this->maxRecursionDepth; + } + + /** + * @since 2.2 + * + * @param string $hash + * + * @return integer + */ + public function get( $hash ) { + + if ( isset( self::$circularRefGuard[$this->namespace][$hash] ) ) { + return self::$circularRefGuard[$this->namespace][$hash]; + } + + return 0; + } + + /** + * @since 2.2 + * + * @param string $namespace + */ + public function reset( $namespace ) { + self::$circularRefGuard[$namespace] = []; + } + +} |