aboutsummaryrefslogtreecommitdiffstats
path: root/phpBB/phpbb/event/md_exporter.php
blob: f7021875f3e6460b4ec0d02d20b02ad4da94d580 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
p4DOE1On..?b\	~LcƙYH<#SV46iORt('Y{iyI뿦Qۓ?mV$B26 i)*B"`lve(Ҕ"ݯU:J*T,-T]{^yʃۦb*	i#8nRvOI`|wKXudܷW9+@ļ<|4Bu"^PWvuZ:
;s1Tbt
h4
ovgM>%Rқkf?vD^* c'WϿJC~С[cs'l163LmV-_I~E5'%#WSŝU@M
A\@(d;g\o>(@;] pH
O=J竪7[ބnylͦnhwnԣʇ* kp6KPj<>?YO%\ԩ{r`롍ʘd]nȉ,edjrHkDU+LYZZla*hljJ?u58E	ڐl'j
(
+
v`.1M
k0	̀B36?i3YhUHmXc#M-\AGIx6$4SzCDtyX
M̓oBk%\BV)%~ֈ%m*
zwΝ=:s^'P9۱Zt4x
&`#rB<7g ;2
>1ʆ~v(LI#G/_2ɸvŮ/DC*nL&
1>Ly}TE@2NQRtFI@K.; mI6gni.H@!-kZ3k}p5#4:Uݐb64}.lev'H/B"
Jُydxb76T^EBk'_lY9Z/	":;L.?U5,nPͻ
eNN14-1_w:ТTBN2GOO?@ *L2o&}a,|s
%wA{d]MX(\	sM¡^~Xhe*@g$ө85P1gkVyӕOWBoZjDΖ+μR_\ȯATsG1GYӖ"YŹY+7؀Tөvlƫߣ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WӾ<	dNgEu~lq
1x3`i^ZaB#&=4*
I
'XfeRtƑdv+8(o#?څAtg408
ac1N"+Gl}k&e1X
ׇlV'`.ЋIQu2Z	?D^~dh	-n1Q,/3}莩1&/3ޥ5
B*j{	$+wT8勉dVk~D0bJ3CE
C	{XόDK+Ay[/^\7'4OOV`wMͮM~Y06h@!l`x`1>p̘“WBؖ0f) YA`*'&F$HN\S!EFRrLf2M6E/5:X{N[=XO׋f3	uQ™ڨTz2ΰ	3)grC	8;p
<?php
/**
*
* This file is part of the phpBB Forum Software package.
*
* @copyright (c) phpBB Limited <https://www.phpbb.com>
* @license GNU General Public License, version 2 (GPL-2.0)
*
* For full copyright and license information, please see
* the docs/CREDITS.txt file.
*
*/

namespace phpbb\event;

/**
* Crawls through a markdown file and grabs all events
*/
class md_exporter
{
	/** @var string Path where we look for files*/
	protected $path;

	/** @var string phpBB Root Path */
	protected $root_path;

	/** @var string */
	protected $filter;

	/** @var string */
	protected $current_event;

	/** @var array */
	protected $events;

	/**
	* @param string $phpbb_root_path
	* @param mixed $extension	String 'vendor/ext' to filter, null for phpBB core
	*/
	public function __construct($phpbb_root_path, $extension = null)
	{
		$this->root_path = $phpbb_root_path;
		$this->path = $this->root_path;
		if ($extension)
		{
			$this->path .= 'ext/' . $extension . '/';
		}

		$this->events = array();
		$this->events_by_file = array();
		$this->filter = $this->current_event = '';
	}

	/**
	* Get the list of all events
	*
	* @return array		Array with events: name => details
	*/
	public function get_events()
	{
		return $this->events;
	}

	/**
	* @param string $md_file	Relative from phpBB root
	* @return int		Number of events found
	* @throws \LogicException
	*/
	public function crawl_phpbb_directory_adm($md_file)
	{
		$this->crawl_eventsmd($md_file, 'adm');

		$file_list = $this->get_recursive_file_list($this->path  . 'adm/style/');
		foreach ($file_list as $file)
		{
			$file_name = 'adm/style/' . $file;
			$this->validate_events_from_file($file_name, $this->crawl_file_for_events($file_name));
		}

		return sizeof($this->events);
	}

	/**
	* @param string $md_file	Relative from phpBB root
	* @return int		Number of events found
	* @throws \LogicException
	*/
	public function crawl_phpbb_directory_styles($md_file)
	{
		$this->crawl_eventsmd($md_file, 'styles');

		$styles = array('prosilver', 'subsilver2');
		foreach ($styles as $style)
		{
			$file_list = $this->get_recursive_file_list(
				$this->path . 'styles/' . $style . '/template/'
			);

			foreach ($file_list as $file)
			{
				$file_name = 'styles/' . $style . '/template/' . $file;
				$this->validate_events_from_file($file_name, $this->crawl_file_for_events($file_name));
			}
		}

		return sizeof($this->events);
	}

	/**
	* @param string $md_file	Relative from phpBB root
	* @param string $filter		Should be 'styles' or 'adm'
	* @return int		Number of events found
	* @throws \LogicException
	*/
	public function crawl_eventsmd($md_file, $filter)
	{
		if (!file_exists($this->path . $md_file))
		{
			throw new \LogicException("The event docs file '{$md_file}' could not be found");
		}

		$file_content = file_get_contents($this->path . $md_file);
		$this->filter = $filter;

		$events = explode("\n\n", $file_content);
		foreach ($events as $event)
		{
			// Last row of the file
			if (strpos($event, "\n===\n") === false)
			{
				continue;
			}

			list($event_name, $details) = explode("\n===\n", $event, 2);
			$this->validate_event_name($event_name);
			$this->current_event = $event_name;

			if (isset($this->events[$this->current_event]))
			{
				throw new \LogicException("The event '{$this->current_event}' is defined multiple times");
			}

			if (($this->filter == 'adm' && strpos($this->current_event, 'acp_') !== 0)
				|| ($this->filter == 'styles' && strpos($this->current_event, 'acp_') === 0))
			{
				continue;
			}

			list($file_details, $details) = explode("\n* Since: ", $details, 2);
			list($since, $description) = explode("\n* Purpose: ", $details, 2);

			$files = $this->validate_file_list($file_details);
			$since = $this->validate_since($since);

			$this->events[$event_name] = array(
				'event'			=> $this->current_event,
				'files'			=> $files,
				'since'			=> $since,
				'description'	=> $description,
			);
		}

		return sizeof($this->events);
	}

	/**
	* Format the php events as a wiki table
	* @return string		Number of events found
	*/
	public function export_events_for_wiki()
	{
		if ($this->filter === 'adm')
		{
			$wiki_page = '= ACP Template Events =' . "\n";
			$wiki_page .= '{| class="zebra sortable" cellspacing="0" cellpadding="5"' . "\n";
			$wiki_page .= '! Identifier !! Placement !! Added in Release !! Explanation' . "\n";
		}
		else
		{
			$wiki_page = '= Template Events =' . "\n";
			$wiki_page .= '{| class="zebra sortable" cellspacing="0" cellpadding="5"' . "\n";
			$wiki_page .= '! Identifier !! Prosilver Placement (If applicable) !! Subsilver Placement (If applicable) !! Added in Release !! Explanation' . "\n";
		}

		foreach ($this->events as $event_name => $event)
		{
			$wiki_page .= "|- id=\"{$event_name}\"\n";
			$wiki_page .= "| [[#{$event_name}|{$event_name}]] || ";

			if ($this->filter === 'adm')
			{
				$wiki_page .= implode(', ', $event['files']['adm']);
			}
			else
			{
				$wiki_page .= implode(', ', $event['files']['prosilver']) . ' || ' . implode(', ', $event['files']['subsilver2']);
			}

			$wiki_page .= " || {$event['since']} || " . str_replace("\n", ' ', $event['description']) . "\n";
		}
		$wiki_page .= '|}' . "\n";

		return $wiki_page;
	}

	/**
	* Validates a template event name
	*
	* @param $event_name
	* @return null
	* @throws \LogicException
	*/
	public function validate_event_name($event_name)
	{
		if (!preg_match('#^([a-z][a-z0-9]*(?:_[a-z][a-z0-9]*)+)$#', $event_name))
		{
			throw new \LogicException("Invalid event name '{$event_name}'");
		}
	}

	/**
	* Validate "Since" Information
	*
	* @param string $since
	* @return string
	* @throws \LogicException
	*/
	public function validate_since($since)
	{
		if (!preg_match('#^\d+\.\d+\.\d+(?:-(?:a|b|RC|pl)\d+)?$#', $since))
		{
			throw new \LogicException("Invalid since information found for event '{$this->current_event}'");
		}

		return $since;
	}

	/**
	* Validate the files list
	*
	* @param string $file_details
	* @return array
	* @throws \LogicException
	*/
	public function validate_file_list($file_details)
	{
		$files_list = array(
			'prosilver'		=> array(),
			'subsilver2'	=> array(),
			'adm'			=> array(),
		);

		// Multi file list
		if (strpos($file_details, "* Locations:\n    + ") === 0)
		{
			$file_details = substr($file_details, strlen("* Locations:\n    + "));
			$files = explode("\n    + ", $file_details);
			foreach ($files as $file)
			{
				if (!file_exists($this->path . $file) || substr($file, -5) !== '.html')
				{
					throw new \LogicException("Invalid file '{$file}' not found for event '{$this->current_event}'", 1);
				}

				if (($this->filter !== 'adm') && strpos($file, 'styles/prosilver/template/') === 0)
				{
					$files_list['prosilver'][] = substr($file, strlen('styles/prosilver/template/'));
				}
				else if (($this->filter !== 'adm') && strpos($file, 'styles/subsilver2/template/') === 0)
				{
					$files_list['subsilver2'][] = substr($file, strlen('styles/subsilver2/template/'));
				}
				else if (($this->filter === 'adm') && strpos($file, 'adm/style/') === 0)
				{
					$files_list['adm'][] = substr($file, strlen('adm/style/'));
				}
				else
				{
					throw new \LogicException("Invalid file '{$file}' not found for event '{$this->current_event}'", 2);
				}

				$this->events_by_file[$file][] = $this->current_event;
			}
		}
		else if ($this->filter == 'adm')
		{
			$file = substr($file_details, strlen('* Location: '));
			if (!file_exists($this->path . $file) || substr($file, -5) !== '.html')
			{
				throw new \LogicException("Invalid file '{$file}' not found for event '{$this->current_event}'", 1);
			}

			$files_list['adm'][] =  substr($file, strlen('adm/style/'));

			$this->events_by_file[$file][] = $this->current_event;
		}
		else
		{
			throw new \LogicException("Invalid file list found for event '{$this->current_event}'", 2);
		}

		return $files_list;
	}

	/**
	* Get all template events in a template file
	*
	* @param string $file
	* @return array
	* @throws \LogicException
	*/
	public function crawl_file_for_events($file)
	{
		if (!file_exists($this->path . $file))
		{
			throw new \LogicException("File '{$file}' does not exist", 1);
		}

		$event_list = array();
		$file_content = file_get_contents($this->path . $file);

		$events = explode('<!-- EVENT ', $file_content);
		// Remove the code before the first event
		array_shift($events);
		foreach ($events as $event)
		{
			$event = explode(' -->', $event, 2);
			$event_list[] = array_shift($event);
		}

		return $event_list;
	}

	/**
	* Validates whether all events from $file are in the md file and vice-versa
	*
	* @param string $file
	* @param array $events
	* @return true
	* @throws \LogicException
	*/
	public function validate_events_from_file($file, array $events)
	{
		if (empty($this->events_by_file[$file]) && empty($events))
		{
			return true;
		}
		else if (empty($this->events_by_file[$file]))
		{
			$event_list = implode("', '", $events);
			throw new \LogicException("File '{$file}' should not contain events, but contains: "
				. "'{$event_list}'", 1);
		}
		else if (empty($events))
		{
			$event_list = implode("', '", $this->events_by_file[$file]);
			throw new \LogicException("File '{$file}' contains no events, but should contain: "
				. "'{$event_list}'", 1);
		}

		$missing_events_from_file = array();
		foreach ($this->events_by_file[$file] as $event)
		{
			if (!in_array($event, $events))
			{
				$missing_events_from_file[] = $event;
			}
		}

		if (!empty($missing_events_from_file))
		{
			$event_list = implode("', '", $missing_events_from_file);
			throw new \LogicException("File '{$file}' does not contain events: '{$event_list}'", 2);
		}

		$missing_events_from_md = array();
		foreach ($events as $event)
		{
			if (!in_array($event, $this->events_by_file[$file]))
			{
				$missing_events_from_md[] = $event;
			}
		}

		if (!empty($missing_events_from_md))
		{
			$event_list = implode("', '", $missing_events_from_md);
			throw new \LogicException("File '{$file}' contains additional events: '{$event_list}'", 3);
		}

		return true;
	}

	/**
	* Returns a list of files in $dir
	*
	* Works recursive with any depth
	*
	* @param	string	$dir	Directory to go through
	* @return	array	List of files (including directories)
	*/
	public function get_recursive_file_list($dir)
	{
		try
		{
			$iterator = new \RecursiveIteratorIterator(
				new \phpbb\recursive_dot_prefix_filter_iterator(
					new \RecursiveDirectoryIterator(
						$dir,
						\FilesystemIterator::SKIP_DOTS
					)
				),
				\RecursiveIteratorIterator::SELF_FIRST
			);
		}
		catch (\Exception $e)
		{
			return array();
		}

		$files = array();
		foreach ($iterator as $file_info)
		{
			/** @var \RecursiveDirectoryIterator $file_info */
			if ($file_info->isDir())
			{
				continue;
			}

			$relative_path = $iterator->getInnerIterator()->getSubPathname();

			if (substr($relative_path, -5) == '.html')
			{
				$files[] = str_replace(DIRECTORY_SEPARATOR, '/', $relative_path);
			}
		}

		return $files;
	}
}