<commit_before><?php
// (c) Copyright 2002-2016 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 Search_Formatter_Builder
{
	private $parser;
	private $paginationArguments;

	private $formatterPlugin;
	private $subFormatters = array();
	private $customFilters = array();
	private $alternateOutput;
	private $id;
	private $count;
	private $tsOn;
	private $tsettings;
	private $actions;
	private $isDownload;
	private $downloadName;

	function __construct()
	{
		$this->parser = new WikiParser_PluginArgumentParser;
		$this->paginationArguments = array(
			'offset_arg' => 'offset',
			'max' => 50,
		);
		$this->actions = array();
		$this->isDownload = false;
	}

	function setPaginationArguments($arguments)
	{
		$this->paginationArguments = $arguments;
	}

	function setFormatterPlugin(Search_Formatter_Plugin_Interface $plugin)
	{
		$this->formatterPlugin = $plugin;
	}

	function setActions($actions) {
		$this->actions = $actions;
	}

	function setDownload($isDownload) {
		$this->isDownload = $isDownload;
	}

	function apply($matches)
	{
		foreach ($matches as $match) {
			$name = $match->getName();

			if ($name == 'output') {
				$this->handleOutput($match);
			}

			if ($name == 'format') {
				$this->handleFormat($match);
			}

			if ($name == 'alternate') {
				$this->handleAlternate($match);
			}

			if ($name == 'tablesorter') {
				$this->handleTablesorter($match);
			}

			if ($name == 'filter') {
				$this->handleFilter($match);
			}

		}
	}

	function getFormatter()
	{
		$plugin = $this->formatterPlugin;
		if (! $plugin) {
			$plugin = new Search_Formatter_Plugin_WikiTemplate("* {display name=title format=objectlink}\n");
		}

		$formatter = Search_Formatter_Factory::newFormatter($plugin);

		if ($this->alternateOutput > '') {
			$formatter->setAlternateOutput($this->alternateOutput);
		} else {
			$formatter->setAlternateOutput('^' . tra('No results for query.') . '^');
		}

		foreach ($this->subFormatters as $name => $plugin) {
			$formatter->addSubFormatter($name, $plugin);
		}

		foreach ($this->customFilters as $filter) {
			$formatter->addCustomFilter($filter);
		}

		return $formatter;
	}

	private function handleFormat($match)
	{
		$arguments = $this->parser->parse($match->getArguments());

		if (isset($arguments['name'])) {
			$plugin = new Search_Formatter_Plugin_WikiTemplate($match->getBody());
			$plugin->setRaw(! empty($arguments['mode']) && $arguments['mode'] == 'raw');
			$this->subFormatters[$arguments['name']] = $plugin;
		}
	}

	private function handleFilter($match)
	{
		$arguments = $this->parser->parse($match->getArguments());

		if (isset($arguments['editable'], $arguments['field'])) {
			$this->customFilters[] = array(
				'field' => $arguments['field'],
				'mode' => $arguments['editable']
			);
		}
	}

	private function handleAlternate($match)
	{<commit_msg>[ENH] add ability to specify format for csv download option in Plugin ListExecute that is different than the html output<commit_after><?php
// (c) Copyright 2002-2016 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 Search_Formatter_Builder
{
	private $parser;
	private $paginationArguments;

	private $formatterPlugin;
	private $subFormatters = array();
	private $customFilters = array();
	private $alternateOutput;
	private $id;
	private $count;
	private $tsOn;
	private $tsettings;
	private $actions;
	private $isDownload;
	private $downloadName;

	function __construct()
	{
		$this->parser = new WikiParser_PluginArgumentParser;
		$this->paginationArguments = array(
			'offset_arg' => 'offset',
			'max' => 50,
		);
		$this->actions = array();
		$this->isDownload = false;
	}

	function setPaginationArguments($arguments)
	{
		$this->paginationArguments = $arguments;
	}

	function setFormatterPlugin(Search_Formatter_Plugin_Interface $plugin)
	{
		$this->formatterPlugin = $plugin;
	}

	function setActions($actions) {
		$this->actions = $actions;
	}

	function setDownload($isDownload) {
		$this->isDownload = $isDownload;
	}

	function apply($matches)
	{
		foreach ($matches as $match) {
			$name = $match->getName();

			if ($name == 'output') {
				$this->handleOutput($match);
			}

			if ($name == 'format') {
				$this->handleFormat($match);
			}

			if ($name == 'alternate') {
				$this->handleAlternate($match);
			}

			if ($name == 'tablesorter') {
				$this->handleTablesorter($match);
			}

			if ($name == 'filter') {
				$this->handleFilter($match);
			}

		}
	}

	function getFormatter()
	{
		$plugin = $this->formatterPlugin;
		if (! $plugin) {
			$plugin = new Search_Formatter_Plugin_WikiTemplate("* {display name=title format=objectlink}\n");
		}

		$formatter = Search_Formatter_Factory::newFormatter($plugin);

		if ($this->alternateOutput > '') {
			$formatter->setAlternateOutput($this->alternateOutput);
		} else {
			$formatter->setAlternateOutput('^' . tra('No results for query.') . '^');
		}

		foreach ($this->subFormatters as $name => $plugin) {
			$formatter->addSubFormatter($name, $plugin);
		}

		foreach ($this->customFilters as $filter) {
			$formatter->addCustomFilter($filter);
		}

		return $formatter;
	}

	private function handleFormat($match)
	{
		$arguments = $this->parser->parse($match->getArguments());

		if (isset($arguments['mode']) && $arguments['mode'] == 'download' && !$this->isDownload) {
			return;
		}

		if (isset($arguments['name'])) {
			$plugin = new Search_Formatter_Plugin_WikiTemplate($match->getBody());
			$plugin->setRaw(! empty($arguments['mode']) && $arguments['mode'] == 'raw');
			$this->subFormatters[$arguments['name']] = $plugin;
		}
	}

	private function handleFilter($match)
	{
		$arguments = $this->parser->parse($match->getArguments());

		if (isset($arguments['editable'], $arguments['field'])) {
			$this->customFilters[] = array(
				'field' => $arguments['field'],
				'mode' => $arguments['editable']
			);
		}
	}

	private function handleAlternate($match)
	{
{'fraction_non_alphanumeric': 0.16581551228854133, 'fraction_numerical': 0.0031918289179699967, 'mean_word_length': 4.374785591766724, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 82, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 17, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3731127', 'n_tokens_mistral': 2247, 'n_tokens_neox': 1952, 'n_words': 423}