<commit_before><?php
// (c) Copyright 2002-2012 by authors of the Tiki Wiki CMS Groupware Project
//
// All Rights Reserved. See copyright.txt for details and a complete list of authors.
// Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See license.txt for details.
// $Id$

class WikiPlugin_ParserNegotiator
{
	var $parser;
	var $name;
	var $className;
	var $class;
	var $parserLevel = 0;
	var $argParser;
	var $args;
	var $body;
	var $info;
	var $fingerprint;
	var $page;
	var $exists;
	var $prefs;
	var $parserOption;
	var $index;
	var $key;
	var $result;

	static $pluginIndexes = array();
	static $parserLevels = array();
	static $currentParserLevel = 0;
	static $pluginsAwaitingExecution = array();

	function __construct(& $parser, & $pluginDetails, & $page, & $prefs, & $parserOption)
	{
		$this->parser = & $parser;
		$this->name = strtolower($pluginDetails['name']);
		$this->className = 'WikiPlugin_' . $this->name;

		if (@class_exists($this->className)) {
			$this->class = new $this->className;
		}

		$this->argParser = new WikiParser_PluginArgumentParser;
		$this->args = $this->argParser->parse($pluginDetails['args']);
		$this->body = & $pluginDetails['body'];
		$this->info = $this->info();
		$this->fingerprint = $this->fingerprint();
		$this->page = & $page;
		$this->exists = $this->exists();
		$this->prefs = & $prefs;
		$this->parserOption = & $parserOption;

		$this->index = $this->incrementIndex();

		$this->key = '§' . md5('plugin:' . $this->name . '_' . $this->index) . '§';
	}

	private function incrementIndex()
	{
		if (isset(self::$pluginIndexes[$this->name]) == false) self::$pluginIndexes[$this->name] = 0;

		self::$pluginIndexes[$this->name]++;

		return self::$pluginIndexes[$this->name];
	}

	function execute()
	{
		$output = '';
		if ($this->enabled($output) == false) {
			return $output->toHtml();
		}

		if (empty($this->result) == false) return $this->result;

		if (isset($this->class)) {
			if (isset($this->class->parserLevel)) {
				$this->parserLevel = $this->class->parserLevel;

				if($this->class->parserLevel > self::$currentParserLevel) {
					$this->addWaitingPlugin();
					return $this->key;
				} else {

					$this->applyFilters();
					$button = $this->button(false);
					$this->result = $this->class->exec($this->body, $this->args, $this->index, $this->parser, $button);
					return $this->result;
				}
			}
		}

		$fnName = strtolower('wikiplugin_' .  $this->name);

		if ( $this->exists && function_exists($fnName) ) {

			$this->result = $fnName($this->body, $this->args, $this->index, $this) . $this->button();

			return $this->result;
		}

		return $this->body;
	}

	function toSyntax()
	{
		$source = '{' . (empty($this->body) ? $this->name : TikiLib::strtoupper($this->name) . '(') . ' ';
		$args = '';		// not using http_build_query() as it converts spaces into +
		if (!empty($this->args)) {
			foreach ( $this->args as $argKey => $argValue ) {
				if (is_array($argValue)) {
					if (isset($this->info['params'][$argKey]['separator'])) {
						$sep = $this->info['params'][$argKey]['separator'];
					} else {
						$sep = ',';
					}
					$source .= $argKey.'="'.implode($sep, $argValue).'" ';	// process array
					$args .= $argKey.'='.implode($sep, $argValue).'&';
				} else {
					$source .= $argKey.'="'.$argValue.'" ';
					$args .= $argKey.'='.$argValue.'&';
				}
			}
		}
		if (substr($source, -1) === ' ') {
			$source = substr($source, 0, -1);
		}
		if (!empty($this->body)) {
			$source .= ')}' . $this->body . '{' . TikiLib::strtoupper($this->name) . '}';
		} else {
			$source .= '}';
		}

		$args = rtrim($args, '&');

		return array(
			'plugin' => $source,
			'args' => $args
		);
	}

	private function addWaitingPlugin()<commit_msg>[NEW] Remove the url encoding of arguments from toSyntax, and make urlEncodeArgs method seperate to clean things a little<commit_after><?php
// (c) Copyright 2002-2012 by authors of the Tiki Wiki CMS Groupware Project
//
// All Rights Reserved. See copyright.txt for details and a complete list of authors.
// Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See license.txt for details.
// $Id$

class WikiPlugin_ParserNegotiator
{
	var $parser;
	var $name;
	var $className;
	var $class;
	var $parserLevel = 0;
	var $argParser;
	var $args;
	var $body;
	var $info;
	var $fingerprint;
	var $page;
	var $exists;
	var $prefs;
	var $parserOption;
	var $index;
	var $key;
	var $result;

	static $pluginIndexes = array();
	static $parserLevels = array();
	static $currentParserLevel = 0;
	static $pluginsAwaitingExecution = array();

	function __construct(& $parser, & $pluginDetails, & $page, & $prefs, & $parserOption)
	{
		$this->parser = & $parser;
		$this->name = strtolower($pluginDetails['name']);
		$this->className = 'WikiPlugin_' . $this->name;

		if (@class_exists($this->className)) {
			$this->class = new $this->className;
		}

		$this->argParser = new WikiParser_PluginArgumentParser;
		$this->args = $this->argParser->parse($pluginDetails['args']);
		$this->body = & $pluginDetails['body'];
		$this->info = $this->info();
		$this->fingerprint = $this->fingerprint();
		$this->page = & $page;
		$this->exists = $this->exists();
		$this->prefs = & $prefs;
		$this->parserOption = & $parserOption;

		$this->index = $this->incrementIndex();

		$this->key = '§' . md5('plugin:' . $this->name . '_' . $this->index) . '§';
	}

	private function incrementIndex()
	{
		if (isset(self::$pluginIndexes[$this->name]) == false) self::$pluginIndexes[$this->name] = 0;

		self::$pluginIndexes[$this->name]++;

		return self::$pluginIndexes[$this->name];
	}

	function execute()
	{
		$output = '';
		if ($this->enabled($output) == false) {
			return $output->toHtml();
		}

		if (empty($this->result) == false) return $this->result;

		if (isset($this->class)) {
			if (isset($this->class->parserLevel)) {
				$this->parserLevel = $this->class->parserLevel;

				if($this->class->parserLevel > self::$currentParserLevel) {
					$this->addWaitingPlugin();
					return $this->key;
				} else {

					$this->applyFilters();
					$button = $this->button(false);
					$this->result = $this->class->exec($this->body, $this->args, $this->index, $this->parser, $button);
					return $this->result;
				}
			}
		}

		$fnName = strtolower('wikiplugin_' .  $this->name);

		if ( $this->exists && function_exists($fnName) ) {

			$this->result = $fnName($this->body, $this->args, $this->index, $this) . $this->button();

			return $this->result;
		}

		return $this->body;
	}

	function toSyntax()
	{
		$source = '{' . (empty($this->body) ? $this->name : TikiLib::strtoupper($this->name) . '(') . ' ';
		if (!empty($this->args)) {
			foreach ( $this->args as $argKey => $argValue ) {
				if (is_array($argValue)) {
					if (isset($this->info['params'][$argKey]['separator'])) {
						$sep = $this->info['params'][$argKey]['separator'];
					} else {
						$sep = ',';
					}
					$source .= $argKey.'="'.implode($sep, $argValue).'" ';	// process array
				} else {
					$source .= $argKey.'="'.$argValue.'" ';
				}
			}
		}
		if (substr($source, -1) === ' ') {
			$source = substr($source, 0, -1);
		}
		if (!empty($this->body)) {
			$source .= ')}' . $this->body . '{' . TikiLib::strtoupper($this->name) . '}';
		} else {
			$source .= '}';
		}

		return $source;
	}

	function urlEncodeArgs()
	{
		$args = '';// not using http_build_query() as it converts spaces into +
		if (!empty($this->args)) {
			foreach ( $this->args as $argKey => $argValue ) {
				if (is_array($argValue)) {
					if (isset($this->info['params'][$argKey]['separator'])) {
						$sep = $this->info['params'][$argKey]['separator'];
					} else {
						$sep = ',';
					}
					$args .= $argKey.'='.implode($sep, $argValue).'&';
				} else {
					$args .= $argKey.'='.$argValue.'&';
				}
			}
		}

		$args = rtrim($args, '&');

		return $args;
	}

	private function addWaitingPlugin()
{'fraction_non_alphanumeric': 0.2139322421012562, 'fraction_numerical': 0.003806623524933384, 'mean_word_length': 3.504, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 160, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7544813', 'n_tokens_mistral': 3179, 'n_tokens_neox': 2764, 'n_words': 563}