From 3352d9fd344c14172bd690f20ee50a912032db7f Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Fri, 18 Apr 2014 11:13:02 +0200 Subject: [ticket/12273] Move event exporter to namespace PHPBB3-12273 --- phpBB/phpbb/event/exporter.php | 610 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 610 insertions(+) create mode 100644 phpBB/phpbb/event/exporter.php (limited to 'phpBB/phpbb/event') diff --git a/phpBB/phpbb/event/exporter.php b/phpBB/phpbb/event/exporter.php new file mode 100644 index 0000000000..e3dd85f5ac --- /dev/null +++ b/phpBB/phpbb/event/exporter.php @@ -0,0 +1,610 @@ +root_path = $phpbb_root_path; + $this->events = $this->file_lines = array(); + $this->events['php'] = array(); + $this->current_file = $this->current_event = ''; + $this->current_event_line = 0; + } + + function export_from_eventsmd($filter) + { + $file_content = file_get_contents($this->root_path . 'docs/events.md'); + + $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); + + if ($filter == 'acp' && strpos($event_name, 'acp_') !== 0) continue; + if ($filter == 'styles' && strpos($event_name, 'acp_') === 0) continue; + + list($file_details, $details) = explode("\n* Since: ", $details); + list($version, $explanition) = explode("\n* Purpose: ", $details); + + echo "|- id=\"{$event_name}\"\n"; + echo "| [[#{$event_name}|{$event_name}]] || "; + + if (strpos($file_details, "* Locations:\n + ") === 0) + { + $file_details = substr($file_details, strlen("* Locations:\n + ")); + $files = explode("\n + ", $file_details); + $prosilver = $subsilver2 = $adm = array(); + foreach ($files as $file) + { + if (strpos($file, 'styles/prosilver/template/') === 0) + { + $prosilver[] = substr($file, strlen('styles/prosilver/template/')); + } + if (strpos($file, 'styles/subsilver2/template/') === 0) + { + $subsilver2[] = substr($file, strlen('styles/subsilver2/template/')); + } + if (strpos($file, 'adm/style/') === 0) + { + $adm[] = substr($file, strlen('adm/style/')); + } + } + if ($filter == 'acp') + { + echo implode(', ', $adm); + } + else + { + echo implode(', ', $prosilver) . ' || ' . implode(', ', $subsilver2); + } + } + else if ($filter == 'acp') + { + echo substr($file_details, strlen("* Location: adm/style/")); + } + echo " || {$version} || " . str_replace("\n", ' ', $explanition) . "\n"; + + } + } + + public function get_events() + { + return $this->events; + } + + public function set_current_event($name, $line) + { + $this->current_event = $name; + $this->current_event_line = $line; + } + + public function set_content($content) + { + $this->file_lines = $content; + } + + /** + * Crawl the phpBB/ directory for php events + * @return int The number of events found + */ + public function crawl_phpbb_directory_php() + { + $files = $this->get_recursive_file_list($this->root_path); + $this->events['php'] = array(); + foreach ($files as $file) + { + $this->crawl_php_file($file); + } + ksort($this->events['php']); + + return sizeof($this->events['php']); + } + + /** + * Returns a list of files in $dir + * + * Works recursive with any depth + * + * @param string $dir Directory to go through + * @param string $path Path from root to $dir + * @return array List of files (including directories) + */ + public function get_recursive_file_list($dir, $path = '') + { + try + { + $iterator = new \DirectoryIterator($dir); + } + catch (\Exception $e) + { + return array(); + } + + $files = array(); + foreach ($iterator as $file_info) + { + /** @var \DirectoryIterator $file_info */ + if ($file_info->isDot()) + { + continue; + } + + // Do not scan some directories + if ($file_info->isDir() && ( + ($path == '' && in_array($file_info->getFilename(), array( + 'cache', + 'develop', + 'ext', + 'files', + 'language', + 'store', + 'vendor', + ))) + || ($path == '/includes' && in_array($file_info->getFilename(), array('utf'))) + || ($path == '/phpbb/db/migration' && in_array($file_info->getFilename(), array('data'))) + || ($path == '/phpbb' && in_array($file_info->getFilename(), array('event'))) + )) + { + continue; + } + else if ($file_info->isDir()) + { + $sub_dir = $this->get_recursive_file_list($file_info->getPath() . '/' . $file_info->getFilename(), $path . '/' . $file_info->getFilename()); + foreach ($sub_dir as $file) + { + $files[] = $file_info->getFilename() . '/' . $file; + } + } + else if ($file_info->getExtension() == 'php') + { + $files[] = $file_info->getFilename(); + } + } + + return $files; + } + + /** + * Format the php events as a wiki table + * @return string + */ + public function export_php_events_for_wiki() + { + $wiki_page = ''; + foreach ($this->events['php'] as $event) + { + $wiki_page .= '|- id="' . $event['event'] . '"' . "\n"; + $wiki_page .= '| [[#' . $event['event'] . '|' . $event['event'] . ']] || ' . $event['file'] . ' || ' . implode(', ', $event['arguments']) . ' || ' . $event['since'] . ' || ' . $event['description'] . "\n"; + } + + return $wiki_page; + } + + /** + * @param $file + * @throws \LogicException + */ + public function crawl_php_file($file) + { + $this->current_file = $file; + $this->file_lines = array(); + $content = file_get_contents($this->root_path . $this->current_file); + + if (strpos($content, "dispatcher->trigger_event('") || strpos($content, "dispatcher->dispatch('")) + { + $this->set_content(explode("\n", $content)); + for ($i = 0, $num_lines = sizeof($this->file_lines); $i < $num_lines; $i++) + { + $event_line = false; + $found_trigger_event = strpos($this->file_lines[$i], "dispatcher->trigger_event('"); + $arguments = array(); + if ($found_trigger_event !== false) + { + $event_line = $i; + $this->set_current_event($this->get_trigger_event_name($this->file_lines[$event_line]), $event_line); + + // Find variables of the event + $arguments = $this->get_vars_from_array(); + $doc_vars = $this->get_vars_from_docblock(); + $this->validate_vars_docblock_array($arguments, $doc_vars); + } + else + { + $found_dispatch = strpos($this->file_lines[$i], "dispatcher->dispatch('"); + if ($found_dispatch !== false) + { + $event_line = $i; + $this->set_current_event($this->get_dispatch_name($this->file_lines[$event_line]), $event_line); + } + } + + if ($event_line) + { + // Validate @event + $event_line_num = $this->find_event(); + $this->validate_event($this->current_event, $this->file_lines[$event_line_num]); + + // Validate @since + $since_line_num = $this->find_since(); + $since = $this->validate_since($this->file_lines[$since_line_num]); + + // Find event description line + $description_line_num = $this->find_description(); + $description = substr(trim($this->file_lines[$description_line_num]), strlen('* ')); + + if (isset($this->events['php'][$this->current_event])) + { + throw new \LogicException('The event "' . $this->current_event . '" from file "' . $this->current_file + . '" already exists in file "'. $this->events['php'][$this->current_event]['file'] . '"', 10); + } + + $this->events['php'][$this->current_event] = array( + 'event' => $this->current_event, + 'file' => $this->current_file, + 'arguments' => $arguments, + 'since' => $since, + 'description' => $description, + ); + } + } + } + } + + /** + * Find the name of the event inside the dispatch() line + * + * @param string $event_line + * @return int Absolute line number + * @throws \LogicException + */ + public function get_dispatch_name($event_line) + { + $event_line = ltrim($event_line, "\t"); + + $regex = '#\$([a-z](?:[a-z0-9_]|->)*)'; + $regex .= '->dispatch\('; + $regex .= '\'' . $this->preg_match_event_name() . '\''; + $regex .= '\);#'; + + $match = array(); + preg_match($regex, $event_line, $match); + if (!isset($match[2])) + { + throw new \LogicException('Can not find event name in line "' . $event_line . '" in file "' . $this->current_file . '"', 1); + } + + return $match[2]; + } + + /** + * Find the name of the event inside the trigger_event() line + * + * @param string $event_line + * @return int Absolute line number + * @throws \LogicException + */ + public function get_trigger_event_name($event_line) + { + $event_line = ltrim($event_line, "\t"); + + $regex = '#extract\(\$([a-z](?:[a-z0-9_]|->)*)'; + $regex .= '->trigger_event\('; + $regex .= '\'' . $this->preg_match_event_name() . '\''; + $regex .= ', compact\(\$vars\)\)\);#'; + + $match = array(); + preg_match($regex, $event_line, $match); + if (!isset($match[2])) + { + throw new \LogicException('Can not find event name in line "' . $event_line . '" in file "' . $this->current_file . '"', 1); + } + + return $match[2]; + } + + /** + * Find the name of the event inside the trigger_event() line + * + * @return string Returns a regex match for the event name + */ + protected function preg_match_event_name() + { + return '([a-z][a-z0-9_]*(?:\.[a-z][a-z0-9_]*)+)'; + } + + /** + * Find the $vars array + * + * @return array List of variables + * @throws \LogicException + */ + public function get_vars_from_array() + { + $vars_line = ltrim($this->file_lines[$this->current_event_line - 1], "\t"); + if (strpos($vars_line, "\$vars = array('") !== 0 || substr($vars_line, -3) !== '\');') + { + throw new \LogicException('Can not find "$vars = array();"-line for event "' . $this->current_event . '" in file "' . $this->current_file . '"', 1); + } + + $vars_array = substr($vars_line, strlen("\$vars = array('"), 0 - strlen('\');')); + if ($vars_array === '') + { + throw new \LogicException('Found empty $vars array for event "' . $this->current_event . '" in file "' . $this->current_file . '"', 2); + } + + $vars_array = explode("', '", $vars_array); + + foreach ($vars_array as $var) + { + if (!preg_match('#^([a-zA-Z_][a-zA-Z0-9_]*)$#', $var)) + { + throw new \LogicException('Found invalid var "' . $var . '" in array for event "' . $this->current_event . '" in file "' . $this->current_file . '"', 3); + } + } + + sort($vars_array); + return $vars_array; + } + + /** + * Find the $vars array + * + * @return array List of variables + * @throws \LogicException + */ + public function get_vars_from_docblock() + { + $doc_vars = array(); + $current_doc_line = 1; + $found_comment_end = false; + while (ltrim($this->file_lines[$this->current_event_line - $current_doc_line], "\t") !== '/**') + { + if (ltrim($this->file_lines[$this->current_event_line - $current_doc_line], "\t") === '*/') + { + $found_comment_end = true; + } + + if ($found_comment_end) + { + $var_line = trim($this->file_lines[$this->current_event_line - $current_doc_line]); + $var_line = preg_replace('!\s+!', ' ', $var_line); + if (strpos($var_line, '* @var ') === 0) + { + $doc_line = explode(' ', $var_line, 5); + if (sizeof($doc_line) !== 5) + { + throw new \LogicException('Found invalid line "' . $this->file_lines[$this->current_event_line - $current_doc_line] + . '" for event "' . $this->current_event . '" in file "' . $this->current_file . '"', 1); + } + $doc_vars[] = $doc_line[3]; + } + } + + $current_doc_line++; + if ($current_doc_line > $this->current_event_line) + { + // Reached the start of the file + throw new \LogicException('Can not find end of docblock for event "' . $this->current_event . '" in file "' . $this->current_file . '"', 2); + } + } + + if (empty($doc_vars)) + { + // Reached the start of the file + throw new \LogicException('Can not find @var lines for event "' . $this->current_event . '" in file "' . $this->current_file . '"', 3); + } + + foreach ($doc_vars as $var) + { + if (!preg_match('#^([a-zA-Z_][a-zA-Z0-9_]*)$#', $var)) + { + throw new \LogicException('Found invalid @var "' . $var . '" in docblock for event "' . $this->current_event . '" in file "' . $this->current_file . '"', 4); + } + } + + sort($doc_vars); + return $doc_vars; + } + + /** + * Find the "@since" Information line + * + * @return int Absolute line number + * @throws \LogicException + */ + public function find_since() + { + return $this->find_tag('since', array('event', 'var')); + } + + /** + * Find the "@event" Information line + * + * @return int Absolute line number + */ + public function find_event() + { + return $this->find_tag('event', array()); + } + + /** + * Find a "@*" Information line + * + * @param string $find_tag Name of the tag we are trying to find + * @param array $disallowed_tags List of tags that must not appear between + * the tag and the actual event + * @return int Absolute line number + * @throws \LogicException + */ + public function find_tag($find_tag, $disallowed_tags) + { + $find_tag_line = 0; + $found_comment_end = false; + while (strpos(ltrim($this->file_lines[$this->current_event_line - $find_tag_line], "\t"), '* @' . $find_tag . ' ') !== 0) + { + if ($found_comment_end && ltrim($this->file_lines[$this->current_event_line - $find_tag_line], "\t") === '/**') + { + // Reached the start of this doc block + throw new \LogicException('Can not find @' . $find_tag . ' information for event "' . $this->current_event . '" in file "' . $this->current_file . '"', 1); + } + + foreach ($disallowed_tags as $disallowed_tag) + { + if ($found_comment_end && strpos(ltrim($this->file_lines[$this->current_event_line - $find_tag_line], "\t"), '* @' . $disallowed_tag) === 0) + { + // Found @var after the @since + throw new \LogicException('Found @' . $disallowed_tag . ' information after @' . $find_tag . ' for event "' . $this->current_event . '" in file "' . $this->current_file . '"', 3); + } + } + + if (ltrim($this->file_lines[$this->current_event_line - $find_tag_line], "\t") === '*/') + { + $found_comment_end = true; + } + + $find_tag_line++; + if ($find_tag_line >= $this->current_event_line) + { + // Reached the start of the file + throw new \LogicException('Can not find @' . $find_tag . ' information for event "' . $this->current_event . '" in file "' . $this->current_file . '"', 2); + } + } + + return $this->current_event_line - $find_tag_line; + } + + /** + * Find a "@*" Information line + * + * @return int Absolute line number + * @throws \LogicException + */ + public function find_description() + { + $find_desc_line = 0; + while (ltrim($this->file_lines[$this->current_event_line - $find_desc_line], "\t") !== '/**') + { + $find_desc_line++; + if ($find_desc_line > $this->current_event_line) + { + // Reached the start of the file + throw new \LogicException('Can not find a description for event "' . $this->current_event . '" in file "' . $this->current_file . '"', 1); + } + } + + $find_desc_line = $this->current_event_line - $find_desc_line + 1; + + $desc = trim($this->file_lines[$find_desc_line]); + if (strpos($desc, '* @') === 0 || $desc[0] !== '*' || substr($desc, 1) == '') + { + // First line of the doc block is a @-line, empty or only contains "*" + throw new \LogicException('Can not find a description for event "' . $this->current_event . '" in file "' . $this->current_file . '"', 2); + } + + return $find_desc_line; + } + + /** + * Validate "@since" Information + * + * @param string $line + * @return string + * @throws \LogicException + */ + public function validate_since($line) + { + $since = substr(ltrim($line, "\t"), strlen('* @since ')); + + if ($since !== trim($since)) + { + throw new \LogicException('Invalid @since information for event "' . $this->current_event . '" in file "' . $this->current_file . '"', 1); + } + + $since = ($since === '3.1-A1') ? '3.1.0-a1' : $since; + + if (!preg_match('#^\d+\.\d+\.\d+(?:-(?:a|b|rc|pl)\d+)?$#', $since)) + { + throw new \LogicException('Invalid @since information for event "' . $this->current_event . '" in file "' . $this->current_file . '"', 2); + } + + return $since; + } + + /** + * Validate "@event" Information + * + * @param string $event_name + * @param string $line + * @return string + * @throws \LogicException + */ + public function validate_event($event_name, $line) + { + $event = substr(ltrim($line, "\t"), strlen('* @event ')); + + if ($event !== trim($event)) + { + throw new \LogicException('Invalid @event information for event "' . $event_name . '" in file "' . $this->current_file . '"', 1); + } + + if ($event !== $event_name) + { + throw new \LogicException('Event name does not match @event tag for event "' . $event_name . '" in file "' . $this->current_file . '"', 2); + } + + return $event; + } + + /** + * Validates that two arrays contain the same strings + * + * @param array $vars_array Variables found in the array line + * @param array $vars_docblock Variables found in the doc block + * @return null + * @throws \LogicException + */ + public function validate_vars_docblock_array($vars_array, $vars_docblock) + { + $vars_array = array_unique($vars_array); + $vars_docblock = array_unique($vars_docblock); + $sizeof_vars_array = sizeof($vars_array); + + if ($sizeof_vars_array !== sizeof($vars_docblock) || $sizeof_vars_array !== sizeof(array_intersect($vars_array, $vars_docblock))) + { + throw new \LogicException('$vars array does not match the list of @var tags for event "' . $this->current_event . '" in file "' . $this->current_file . '"'); + } + } +} -- cgit v1.2.1 From 6da52acb3ce849b25adbf0ef9533a0acb7286ddf Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Fri, 18 Apr 2014 12:50:23 +0200 Subject: [ticket/12273] Rename exporter to php_exporter PHPBB3-12273 --- phpBB/phpbb/event/exporter.php | 610 ------------------------------------- phpBB/phpbb/event/php_exporter.php | 559 +++++++++++++++++++++++++++++++++ 2 files changed, 559 insertions(+), 610 deletions(-) delete mode 100644 phpBB/phpbb/event/exporter.php create mode 100644 phpBB/phpbb/event/php_exporter.php (limited to 'phpBB/phpbb/event') diff --git a/phpBB/phpbb/event/exporter.php b/phpBB/phpbb/event/exporter.php deleted file mode 100644 index e3dd85f5ac..0000000000 --- a/phpBB/phpbb/event/exporter.php +++ /dev/null @@ -1,610 +0,0 @@ -root_path = $phpbb_root_path; - $this->events = $this->file_lines = array(); - $this->events['php'] = array(); - $this->current_file = $this->current_event = ''; - $this->current_event_line = 0; - } - - function export_from_eventsmd($filter) - { - $file_content = file_get_contents($this->root_path . 'docs/events.md'); - - $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); - - if ($filter == 'acp' && strpos($event_name, 'acp_') !== 0) continue; - if ($filter == 'styles' && strpos($event_name, 'acp_') === 0) continue; - - list($file_details, $details) = explode("\n* Since: ", $details); - list($version, $explanition) = explode("\n* Purpose: ", $details); - - echo "|- id=\"{$event_name}\"\n"; - echo "| [[#{$event_name}|{$event_name}]] || "; - - if (strpos($file_details, "* Locations:\n + ") === 0) - { - $file_details = substr($file_details, strlen("* Locations:\n + ")); - $files = explode("\n + ", $file_details); - $prosilver = $subsilver2 = $adm = array(); - foreach ($files as $file) - { - if (strpos($file, 'styles/prosilver/template/') === 0) - { - $prosilver[] = substr($file, strlen('styles/prosilver/template/')); - } - if (strpos($file, 'styles/subsilver2/template/') === 0) - { - $subsilver2[] = substr($file, strlen('styles/subsilver2/template/')); - } - if (strpos($file, 'adm/style/') === 0) - { - $adm[] = substr($file, strlen('adm/style/')); - } - } - if ($filter == 'acp') - { - echo implode(', ', $adm); - } - else - { - echo implode(', ', $prosilver) . ' || ' . implode(', ', $subsilver2); - } - } - else if ($filter == 'acp') - { - echo substr($file_details, strlen("* Location: adm/style/")); - } - echo " || {$version} || " . str_replace("\n", ' ', $explanition) . "\n"; - - } - } - - public function get_events() - { - return $this->events; - } - - public function set_current_event($name, $line) - { - $this->current_event = $name; - $this->current_event_line = $line; - } - - public function set_content($content) - { - $this->file_lines = $content; - } - - /** - * Crawl the phpBB/ directory for php events - * @return int The number of events found - */ - public function crawl_phpbb_directory_php() - { - $files = $this->get_recursive_file_list($this->root_path); - $this->events['php'] = array(); - foreach ($files as $file) - { - $this->crawl_php_file($file); - } - ksort($this->events['php']); - - return sizeof($this->events['php']); - } - - /** - * Returns a list of files in $dir - * - * Works recursive with any depth - * - * @param string $dir Directory to go through - * @param string $path Path from root to $dir - * @return array List of files (including directories) - */ - public function get_recursive_file_list($dir, $path = '') - { - try - { - $iterator = new \DirectoryIterator($dir); - } - catch (\Exception $e) - { - return array(); - } - - $files = array(); - foreach ($iterator as $file_info) - { - /** @var \DirectoryIterator $file_info */ - if ($file_info->isDot()) - { - continue; - } - - // Do not scan some directories - if ($file_info->isDir() && ( - ($path == '' && in_array($file_info->getFilename(), array( - 'cache', - 'develop', - 'ext', - 'files', - 'language', - 'store', - 'vendor', - ))) - || ($path == '/includes' && in_array($file_info->getFilename(), array('utf'))) - || ($path == '/phpbb/db/migration' && in_array($file_info->getFilename(), array('data'))) - || ($path == '/phpbb' && in_array($file_info->getFilename(), array('event'))) - )) - { - continue; - } - else if ($file_info->isDir()) - { - $sub_dir = $this->get_recursive_file_list($file_info->getPath() . '/' . $file_info->getFilename(), $path . '/' . $file_info->getFilename()); - foreach ($sub_dir as $file) - { - $files[] = $file_info->getFilename() . '/' . $file; - } - } - else if ($file_info->getExtension() == 'php') - { - $files[] = $file_info->getFilename(); - } - } - - return $files; - } - - /** - * Format the php events as a wiki table - * @return string - */ - public function export_php_events_for_wiki() - { - $wiki_page = ''; - foreach ($this->events['php'] as $event) - { - $wiki_page .= '|- id="' . $event['event'] . '"' . "\n"; - $wiki_page .= '| [[#' . $event['event'] . '|' . $event['event'] . ']] || ' . $event['file'] . ' || ' . implode(', ', $event['arguments']) . ' || ' . $event['since'] . ' || ' . $event['description'] . "\n"; - } - - return $wiki_page; - } - - /** - * @param $file - * @throws \LogicException - */ - public function crawl_php_file($file) - { - $this->current_file = $file; - $this->file_lines = array(); - $content = file_get_contents($this->root_path . $this->current_file); - - if (strpos($content, "dispatcher->trigger_event('") || strpos($content, "dispatcher->dispatch('")) - { - $this->set_content(explode("\n", $content)); - for ($i = 0, $num_lines = sizeof($this->file_lines); $i < $num_lines; $i++) - { - $event_line = false; - $found_trigger_event = strpos($this->file_lines[$i], "dispatcher->trigger_event('"); - $arguments = array(); - if ($found_trigger_event !== false) - { - $event_line = $i; - $this->set_current_event($this->get_trigger_event_name($this->file_lines[$event_line]), $event_line); - - // Find variables of the event - $arguments = $this->get_vars_from_array(); - $doc_vars = $this->get_vars_from_docblock(); - $this->validate_vars_docblock_array($arguments, $doc_vars); - } - else - { - $found_dispatch = strpos($this->file_lines[$i], "dispatcher->dispatch('"); - if ($found_dispatch !== false) - { - $event_line = $i; - $this->set_current_event($this->get_dispatch_name($this->file_lines[$event_line]), $event_line); - } - } - - if ($event_line) - { - // Validate @event - $event_line_num = $this->find_event(); - $this->validate_event($this->current_event, $this->file_lines[$event_line_num]); - - // Validate @since - $since_line_num = $this->find_since(); - $since = $this->validate_since($this->file_lines[$since_line_num]); - - // Find event description line - $description_line_num = $this->find_description(); - $description = substr(trim($this->file_lines[$description_line_num]), strlen('* ')); - - if (isset($this->events['php'][$this->current_event])) - { - throw new \LogicException('The event "' . $this->current_event . '" from file "' . $this->current_file - . '" already exists in file "'. $this->events['php'][$this->current_event]['file'] . '"', 10); - } - - $this->events['php'][$this->current_event] = array( - 'event' => $this->current_event, - 'file' => $this->current_file, - 'arguments' => $arguments, - 'since' => $since, - 'description' => $description, - ); - } - } - } - } - - /** - * Find the name of the event inside the dispatch() line - * - * @param string $event_line - * @return int Absolute line number - * @throws \LogicException - */ - public function get_dispatch_name($event_line) - { - $event_line = ltrim($event_line, "\t"); - - $regex = '#\$([a-z](?:[a-z0-9_]|->)*)'; - $regex .= '->dispatch\('; - $regex .= '\'' . $this->preg_match_event_name() . '\''; - $regex .= '\);#'; - - $match = array(); - preg_match($regex, $event_line, $match); - if (!isset($match[2])) - { - throw new \LogicException('Can not find event name in line "' . $event_line . '" in file "' . $this->current_file . '"', 1); - } - - return $match[2]; - } - - /** - * Find the name of the event inside the trigger_event() line - * - * @param string $event_line - * @return int Absolute line number - * @throws \LogicException - */ - public function get_trigger_event_name($event_line) - { - $event_line = ltrim($event_line, "\t"); - - $regex = '#extract\(\$([a-z](?:[a-z0-9_]|->)*)'; - $regex .= '->trigger_event\('; - $regex .= '\'' . $this->preg_match_event_name() . '\''; - $regex .= ', compact\(\$vars\)\)\);#'; - - $match = array(); - preg_match($regex, $event_line, $match); - if (!isset($match[2])) - { - throw new \LogicException('Can not find event name in line "' . $event_line . '" in file "' . $this->current_file . '"', 1); - } - - return $match[2]; - } - - /** - * Find the name of the event inside the trigger_event() line - * - * @return string Returns a regex match for the event name - */ - protected function preg_match_event_name() - { - return '([a-z][a-z0-9_]*(?:\.[a-z][a-z0-9_]*)+)'; - } - - /** - * Find the $vars array - * - * @return array List of variables - * @throws \LogicException - */ - public function get_vars_from_array() - { - $vars_line = ltrim($this->file_lines[$this->current_event_line - 1], "\t"); - if (strpos($vars_line, "\$vars = array('") !== 0 || substr($vars_line, -3) !== '\');') - { - throw new \LogicException('Can not find "$vars = array();"-line for event "' . $this->current_event . '" in file "' . $this->current_file . '"', 1); - } - - $vars_array = substr($vars_line, strlen("\$vars = array('"), 0 - strlen('\');')); - if ($vars_array === '') - { - throw new \LogicException('Found empty $vars array for event "' . $this->current_event . '" in file "' . $this->current_file . '"', 2); - } - - $vars_array = explode("', '", $vars_array); - - foreach ($vars_array as $var) - { - if (!preg_match('#^([a-zA-Z_][a-zA-Z0-9_]*)$#', $var)) - { - throw new \LogicException('Found invalid var "' . $var . '" in array for event "' . $this->current_event . '" in file "' . $this->current_file . '"', 3); - } - } - - sort($vars_array); - return $vars_array; - } - - /** - * Find the $vars array - * - * @return array List of variables - * @throws \LogicException - */ - public function get_vars_from_docblock() - { - $doc_vars = array(); - $current_doc_line = 1; - $found_comment_end = false; - while (ltrim($this->file_lines[$this->current_event_line - $current_doc_line], "\t") !== '/**') - { - if (ltrim($this->file_lines[$this->current_event_line - $current_doc_line], "\t") === '*/') - { - $found_comment_end = true; - } - - if ($found_comment_end) - { - $var_line = trim($this->file_lines[$this->current_event_line - $current_doc_line]); - $var_line = preg_replace('!\s+!', ' ', $var_line); - if (strpos($var_line, '* @var ') === 0) - { - $doc_line = explode(' ', $var_line, 5); - if (sizeof($doc_line) !== 5) - { - throw new \LogicException('Found invalid line "' . $this->file_lines[$this->current_event_line - $current_doc_line] - . '" for event "' . $this->current_event . '" in file "' . $this->current_file . '"', 1); - } - $doc_vars[] = $doc_line[3]; - } - } - - $current_doc_line++; - if ($current_doc_line > $this->current_event_line) - { - // Reached the start of the file - throw new \LogicException('Can not find end of docblock for event "' . $this->current_event . '" in file "' . $this->current_file . '"', 2); - } - } - - if (empty($doc_vars)) - { - // Reached the start of the file - throw new \LogicException('Can not find @var lines for event "' . $this->current_event . '" in file "' . $this->current_file . '"', 3); - } - - foreach ($doc_vars as $var) - { - if (!preg_match('#^([a-zA-Z_][a-zA-Z0-9_]*)$#', $var)) - { - throw new \LogicException('Found invalid @var "' . $var . '" in docblock for event "' . $this->current_event . '" in file "' . $this->current_file . '"', 4); - } - } - - sort($doc_vars); - return $doc_vars; - } - - /** - * Find the "@since" Information line - * - * @return int Absolute line number - * @throws \LogicException - */ - public function find_since() - { - return $this->find_tag('since', array('event', 'var')); - } - - /** - * Find the "@event" Information line - * - * @return int Absolute line number - */ - public function find_event() - { - return $this->find_tag('event', array()); - } - - /** - * Find a "@*" Information line - * - * @param string $find_tag Name of the tag we are trying to find - * @param array $disallowed_tags List of tags that must not appear between - * the tag and the actual event - * @return int Absolute line number - * @throws \LogicException - */ - public function find_tag($find_tag, $disallowed_tags) - { - $find_tag_line = 0; - $found_comment_end = false; - while (strpos(ltrim($this->file_lines[$this->current_event_line - $find_tag_line], "\t"), '* @' . $find_tag . ' ') !== 0) - { - if ($found_comment_end && ltrim($this->file_lines[$this->current_event_line - $find_tag_line], "\t") === '/**') - { - // Reached the start of this doc block - throw new \LogicException('Can not find @' . $find_tag . ' information for event "' . $this->current_event . '" in file "' . $this->current_file . '"', 1); - } - - foreach ($disallowed_tags as $disallowed_tag) - { - if ($found_comment_end && strpos(ltrim($this->file_lines[$this->current_event_line - $find_tag_line], "\t"), '* @' . $disallowed_tag) === 0) - { - // Found @var after the @since - throw new \LogicException('Found @' . $disallowed_tag . ' information after @' . $find_tag . ' for event "' . $this->current_event . '" in file "' . $this->current_file . '"', 3); - } - } - - if (ltrim($this->file_lines[$this->current_event_line - $find_tag_line], "\t") === '*/') - { - $found_comment_end = true; - } - - $find_tag_line++; - if ($find_tag_line >= $this->current_event_line) - { - // Reached the start of the file - throw new \LogicException('Can not find @' . $find_tag . ' information for event "' . $this->current_event . '" in file "' . $this->current_file . '"', 2); - } - } - - return $this->current_event_line - $find_tag_line; - } - - /** - * Find a "@*" Information line - * - * @return int Absolute line number - * @throws \LogicException - */ - public function find_description() - { - $find_desc_line = 0; - while (ltrim($this->file_lines[$this->current_event_line - $find_desc_line], "\t") !== '/**') - { - $find_desc_line++; - if ($find_desc_line > $this->current_event_line) - { - // Reached the start of the file - throw new \LogicException('Can not find a description for event "' . $this->current_event . '" in file "' . $this->current_file . '"', 1); - } - } - - $find_desc_line = $this->current_event_line - $find_desc_line + 1; - - $desc = trim($this->file_lines[$find_desc_line]); - if (strpos($desc, '* @') === 0 || $desc[0] !== '*' || substr($desc, 1) == '') - { - // First line of the doc block is a @-line, empty or only contains "*" - throw new \LogicException('Can not find a description for event "' . $this->current_event . '" in file "' . $this->current_file . '"', 2); - } - - return $find_desc_line; - } - - /** - * Validate "@since" Information - * - * @param string $line - * @return string - * @throws \LogicException - */ - public function validate_since($line) - { - $since = substr(ltrim($line, "\t"), strlen('* @since ')); - - if ($since !== trim($since)) - { - throw new \LogicException('Invalid @since information for event "' . $this->current_event . '" in file "' . $this->current_file . '"', 1); - } - - $since = ($since === '3.1-A1') ? '3.1.0-a1' : $since; - - if (!preg_match('#^\d+\.\d+\.\d+(?:-(?:a|b|rc|pl)\d+)?$#', $since)) - { - throw new \LogicException('Invalid @since information for event "' . $this->current_event . '" in file "' . $this->current_file . '"', 2); - } - - return $since; - } - - /** - * Validate "@event" Information - * - * @param string $event_name - * @param string $line - * @return string - * @throws \LogicException - */ - public function validate_event($event_name, $line) - { - $event = substr(ltrim($line, "\t"), strlen('* @event ')); - - if ($event !== trim($event)) - { - throw new \LogicException('Invalid @event information for event "' . $event_name . '" in file "' . $this->current_file . '"', 1); - } - - if ($event !== $event_name) - { - throw new \LogicException('Event name does not match @event tag for event "' . $event_name . '" in file "' . $this->current_file . '"', 2); - } - - return $event; - } - - /** - * Validates that two arrays contain the same strings - * - * @param array $vars_array Variables found in the array line - * @param array $vars_docblock Variables found in the doc block - * @return null - * @throws \LogicException - */ - public function validate_vars_docblock_array($vars_array, $vars_docblock) - { - $vars_array = array_unique($vars_array); - $vars_docblock = array_unique($vars_docblock); - $sizeof_vars_array = sizeof($vars_array); - - if ($sizeof_vars_array !== sizeof($vars_docblock) || $sizeof_vars_array !== sizeof(array_intersect($vars_array, $vars_docblock))) - { - throw new \LogicException('$vars array does not match the list of @var tags for event "' . $this->current_event . '" in file "' . $this->current_file . '"'); - } - } -} diff --git a/phpBB/phpbb/event/php_exporter.php b/phpBB/phpbb/event/php_exporter.php new file mode 100644 index 0000000000..51fe878d41 --- /dev/null +++ b/phpBB/phpbb/event/php_exporter.php @@ -0,0 +1,559 @@ +root_path = $phpbb_root_path; + $this->events = $this->file_lines = array(); + $this->current_file = $this->current_event = ''; + $this->current_event_line = 0; + } + + public function get_events() + { + return $this->events; + } + + public function set_current_event($name, $line) + { + $this->current_event = $name; + $this->current_event_line = $line; + } + + public function set_content($content) + { + $this->file_lines = $content; + } + + /** + * Crawl the phpBB/ directory for php events + * @return int The number of events found + */ + public function crawl_phpbb_directory_php() + { + $files = $this->get_recursive_file_list($this->root_path); + $this->events = array(); + foreach ($files as $file) + { + $this->crawl_php_file($file); + } + ksort($this->events); + + return sizeof($this->events); + } + + /** + * Returns a list of files in $dir + * + * Works recursive with any depth + * + * @param string $dir Directory to go through + * @param string $path Path from root to $dir + * @return array List of files (including directories) + */ + public function get_recursive_file_list($dir, $path = '') + { + try + { + $iterator = new \DirectoryIterator($dir); + } + catch (\Exception $e) + { + return array(); + } + + $files = array(); + foreach ($iterator as $file_info) + { + /** @var \DirectoryIterator $file_info */ + if ($file_info->isDot()) + { + continue; + } + + // Do not scan some directories + if ($file_info->isDir() && ( + ($path == '' && in_array($file_info->getFilename(), array( + 'cache', + 'develop', + 'ext', + 'files', + 'language', + 'store', + 'vendor', + ))) + || ($path == '/includes' && in_array($file_info->getFilename(), array('utf'))) + || ($path == '/phpbb/db/migration' && in_array($file_info->getFilename(), array('data'))) + || ($path == '/phpbb' && in_array($file_info->getFilename(), array('event'))) + )) + { + continue; + } + else if ($file_info->isDir()) + { + $sub_dir = $this->get_recursive_file_list($file_info->getPath() . '/' . $file_info->getFilename(), $path . '/' . $file_info->getFilename()); + foreach ($sub_dir as $file) + { + $files[] = $file_info->getFilename() . '/' . $file; + } + } + else if ($file_info->getExtension() == 'php') + { + $files[] = $file_info->getFilename(); + } + } + + return $files; + } + + /** + * Format the php events as a wiki table + * @return string + */ + public function export_events_for_wiki() + { + $wiki_page = '= PHP Events (Hook Locations) =' . "\n"; + $wiki_page .= '{| class="sortable zebra" cellspacing="0" cellpadding="5"' . "\n"; + $wiki_page .= '! Identifier !! Placement !! Arguments !! Added in Release !! Explanation' . "\n"; + foreach ($this->events as $event) + { + $wiki_page .= '|- id="' . $event['event'] . '"' . "\n"; + $wiki_page .= '| [[#' . $event['event'] . '|' . $event['event'] . ']] || ' . $event['file'] . ' || ' . implode(', ', $event['arguments']) . ' || ' . $event['since'] . ' || ' . $event['description'] . "\n"; + } + $wiki_page .= '|}' . "\n"; + + return $wiki_page; + } + + /** + * @param $file + * @throws \LogicException + */ + public function crawl_php_file($file) + { + $this->current_file = $file; + $this->file_lines = array(); + $content = file_get_contents($this->root_path . $this->current_file); + + if (strpos($content, "dispatcher->trigger_event('") || strpos($content, "dispatcher->dispatch('")) + { + $this->set_content(explode("\n", $content)); + for ($i = 0, $num_lines = sizeof($this->file_lines); $i < $num_lines; $i++) + { + $event_line = false; + $found_trigger_event = strpos($this->file_lines[$i], "dispatcher->trigger_event('"); + $arguments = array(); + if ($found_trigger_event !== false) + { + $event_line = $i; + $this->set_current_event($this->get_trigger_event_name($this->file_lines[$event_line]), $event_line); + + // Find variables of the event + $arguments = $this->get_vars_from_array(); + $doc_vars = $this->get_vars_from_docblock(); + $this->validate_vars_docblock_array($arguments, $doc_vars); + } + else + { + $found_dispatch = strpos($this->file_lines[$i], "dispatcher->dispatch('"); + if ($found_dispatch !== false) + { + $event_line = $i; + $this->set_current_event($this->get_dispatch_name($this->file_lines[$event_line]), $event_line); + } + } + + if ($event_line) + { + // Validate @event + $event_line_num = $this->find_event(); + $this->validate_event($this->current_event, $this->file_lines[$event_line_num]); + + // Validate @since + $since_line_num = $this->find_since(); + $since = $this->validate_since($this->file_lines[$since_line_num]); + + // Find event description line + $description_line_num = $this->find_description(); + $description = substr(trim($this->file_lines[$description_line_num]), strlen('* ')); + + if (isset($this->events[$this->current_event])) + { + throw new \LogicException('The event "' . $this->current_event . '" from file "' . $this->current_file + . '" already exists in file "'. $this->events[$this->current_event]['file'] . '"', 10); + } + + $this->events[$this->current_event] = array( + 'event' => $this->current_event, + 'file' => $this->current_file, + 'arguments' => $arguments, + 'since' => $since, + 'description' => $description, + ); + } + } + } + } + + /** + * Find the name of the event inside the dispatch() line + * + * @param string $event_line + * @return int Absolute line number + * @throws \LogicException + */ + public function get_dispatch_name($event_line) + { + $event_line = ltrim($event_line, "\t"); + + $regex = '#\$([a-z](?:[a-z0-9_]|->)*)'; + $regex .= '->dispatch\('; + $regex .= '\'' . $this->preg_match_event_name() . '\''; + $regex .= '\);#'; + + $match = array(); + preg_match($regex, $event_line, $match); + if (!isset($match[2])) + { + throw new \LogicException('Can not find event name in line "' . $event_line . '" in file "' . $this->current_file . '"', 1); + } + + return $match[2]; + } + + /** + * Find the name of the event inside the trigger_event() line + * + * @param string $event_line + * @return int Absolute line number + * @throws \LogicException + */ + public function get_trigger_event_name($event_line) + { + $event_line = ltrim($event_line, "\t"); + + $regex = '#extract\(\$([a-z](?:[a-z0-9_]|->)*)'; + $regex .= '->trigger_event\('; + $regex .= '\'' . $this->preg_match_event_name() . '\''; + $regex .= ', compact\(\$vars\)\)\);#'; + + $match = array(); + preg_match($regex, $event_line, $match); + if (!isset($match[2])) + { + throw new \LogicException('Can not find event name in line "' . $event_line . '" in file "' . $this->current_file . '"', 1); + } + + return $match[2]; + } + + /** + * Returns a regex match for the event name + * + * @return string + */ + protected function preg_match_event_name() + { + return '([a-z][a-z0-9_]*(?:\.[a-z][a-z0-9_]*)+)'; + } + + /** + * Find the $vars array + * + * @return array List of variables + * @throws \LogicException + */ + public function get_vars_from_array() + { + $vars_line = ltrim($this->file_lines[$this->current_event_line - 1], "\t"); + if (strpos($vars_line, "\$vars = array('") !== 0 || substr($vars_line, -3) !== '\');') + { + throw new \LogicException('Can not find "$vars = array();"-line for event "' . $this->current_event . '" in file "' . $this->current_file . '"', 1); + } + + $vars_array = substr($vars_line, strlen("\$vars = array('"), 0 - strlen('\');')); + if ($vars_array === '') + { + throw new \LogicException('Found empty $vars array for event "' . $this->current_event . '" in file "' . $this->current_file . '"', 2); + } + + $vars_array = explode("', '", $vars_array); + + foreach ($vars_array as $var) + { + if (!preg_match('#^([a-zA-Z_][a-zA-Z0-9_]*)$#', $var)) + { + throw new \LogicException('Found invalid var "' . $var . '" in array for event "' . $this->current_event . '" in file "' . $this->current_file . '"', 3); + } + } + + sort($vars_array); + return $vars_array; + } + + /** + * Find the $vars array + * + * @return array List of variables + * @throws \LogicException + */ + public function get_vars_from_docblock() + { + $doc_vars = array(); + $current_doc_line = 1; + $found_comment_end = false; + while (ltrim($this->file_lines[$this->current_event_line - $current_doc_line], "\t") !== '/**') + { + if (ltrim($this->file_lines[$this->current_event_line - $current_doc_line], "\t") === '*/') + { + $found_comment_end = true; + } + + if ($found_comment_end) + { + $var_line = trim($this->file_lines[$this->current_event_line - $current_doc_line]); + $var_line = preg_replace('!\s+!', ' ', $var_line); + if (strpos($var_line, '* @var ') === 0) + { + $doc_line = explode(' ', $var_line, 5); + if (sizeof($doc_line) !== 5) + { + throw new \LogicException('Found invalid line "' . $this->file_lines[$this->current_event_line - $current_doc_line] + . '" for event "' . $this->current_event . '" in file "' . $this->current_file . '"', 1); + } + $doc_vars[] = $doc_line[3]; + } + } + + $current_doc_line++; + if ($current_doc_line > $this->current_event_line) + { + // Reached the start of the file + throw new \LogicException('Can not find end of docblock for event "' . $this->current_event . '" in file "' . $this->current_file . '"', 2); + } + } + + if (empty($doc_vars)) + { + // Reached the start of the file + throw new \LogicException('Can not find @var lines for event "' . $this->current_event . '" in file "' . $this->current_file . '"', 3); + } + + foreach ($doc_vars as $var) + { + if (!preg_match('#^([a-zA-Z_][a-zA-Z0-9_]*)$#', $var)) + { + throw new \LogicException('Found invalid @var "' . $var . '" in docblock for event "' . $this->current_event . '" in file "' . $this->current_file . '"', 4); + } + } + + sort($doc_vars); + return $doc_vars; + } + + /** + * Find the "@since" Information line + * + * @return int Absolute line number + * @throws \LogicException + */ + public function find_since() + { + return $this->find_tag('since', array('event', 'var')); + } + + /** + * Find the "@event" Information line + * + * @return int Absolute line number + */ + public function find_event() + { + return $this->find_tag('event', array()); + } + + /** + * Find a "@*" Information line + * + * @param string $find_tag Name of the tag we are trying to find + * @param array $disallowed_tags List of tags that must not appear between + * the tag and the actual event + * @return int Absolute line number + * @throws \LogicException + */ + public function find_tag($find_tag, $disallowed_tags) + { + $find_tag_line = 0; + $found_comment_end = false; + while (strpos(ltrim($this->file_lines[$this->current_event_line - $find_tag_line], "\t"), '* @' . $find_tag . ' ') !== 0) + { + if ($found_comment_end && ltrim($this->file_lines[$this->current_event_line - $find_tag_line], "\t") === '/**') + { + // Reached the start of this doc block + throw new \LogicException('Can not find @' . $find_tag . ' information for event "' . $this->current_event . '" in file "' . $this->current_file . '"', 1); + } + + foreach ($disallowed_tags as $disallowed_tag) + { + if ($found_comment_end && strpos(ltrim($this->file_lines[$this->current_event_line - $find_tag_line], "\t"), '* @' . $disallowed_tag) === 0) + { + // Found @var after the @since + throw new \LogicException('Found @' . $disallowed_tag . ' information after @' . $find_tag . ' for event "' . $this->current_event . '" in file "' . $this->current_file . '"', 3); + } + } + + if (ltrim($this->file_lines[$this->current_event_line - $find_tag_line], "\t") === '*/') + { + $found_comment_end = true; + } + + $find_tag_line++; + if ($find_tag_line >= $this->current_event_line) + { + // Reached the start of the file + throw new \LogicException('Can not find @' . $find_tag . ' information for event "' . $this->current_event . '" in file "' . $this->current_file . '"', 2); + } + } + + return $this->current_event_line - $find_tag_line; + } + + /** + * Find a "@*" Information line + * + * @return int Absolute line number + * @throws \LogicException + */ + public function find_description() + { + $find_desc_line = 0; + while (ltrim($this->file_lines[$this->current_event_line - $find_desc_line], "\t") !== '/**') + { + $find_desc_line++; + if ($find_desc_line > $this->current_event_line) + { + // Reached the start of the file + throw new \LogicException('Can not find a description for event "' . $this->current_event . '" in file "' . $this->current_file . '"', 1); + } + } + + $find_desc_line = $this->current_event_line - $find_desc_line + 1; + + $desc = trim($this->file_lines[$find_desc_line]); + if (strpos($desc, '* @') === 0 || $desc[0] !== '*' || substr($desc, 1) == '') + { + // First line of the doc block is a @-line, empty or only contains "*" + throw new \LogicException('Can not find a description for event "' . $this->current_event . '" in file "' . $this->current_file . '"', 2); + } + + return $find_desc_line; + } + + /** + * Validate "@since" Information + * + * @param string $line + * @return string + * @throws \LogicException + */ + public function validate_since($line) + { + $since = substr(ltrim($line, "\t"), strlen('* @since ')); + + if ($since !== trim($since)) + { + throw new \LogicException('Invalid @since information for event "' . $this->current_event . '" in file "' . $this->current_file . '"', 1); + } + + $since = ($since === '3.1-A1') ? '3.1.0-a1' : $since; + + if (!preg_match('#^\d+\.\d+\.\d+(?:-(?:a|b|rc|pl)\d+)?$#', $since)) + { + throw new \LogicException('Invalid @since information for event "' . $this->current_event . '" in file "' . $this->current_file . '"', 2); + } + + return $since; + } + + /** + * Validate "@event" Information + * + * @param string $event_name + * @param string $line + * @return string + * @throws \LogicException + */ + public function validate_event($event_name, $line) + { + $event = substr(ltrim($line, "\t"), strlen('* @event ')); + + if ($event !== trim($event)) + { + throw new \LogicException('Invalid @event information for event "' . $event_name . '" in file "' . $this->current_file . '"', 1); + } + + if ($event !== $event_name) + { + throw new \LogicException('Event name does not match @event tag for event "' . $event_name . '" in file "' . $this->current_file . '"', 2); + } + + return $event; + } + + /** + * Validates that two arrays contain the same strings + * + * @param array $vars_array Variables found in the array line + * @param array $vars_docblock Variables found in the doc block + * @return null + * @throws \LogicException + */ + public function validate_vars_docblock_array($vars_array, $vars_docblock) + { + $vars_array = array_unique($vars_array); + $vars_docblock = array_unique($vars_docblock); + $sizeof_vars_array = sizeof($vars_array); + + if ($sizeof_vars_array !== sizeof($vars_docblock) || $sizeof_vars_array !== sizeof(array_intersect($vars_array, $vars_docblock))) + { + throw new \LogicException('$vars array does not match the list of @var tags for event "' . $this->current_event . '" in file "' . $this->current_file . '"'); + } + } +} -- cgit v1.2.1 From 17a1ed6fb59e36dcefa43d9755a69ab146e11576 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Fri, 18 Apr 2014 12:51:13 +0200 Subject: [ticket/12273] Move MD Exporter to separate file PHPBB3-12273 --- phpBB/phpbb/event/md_exporter.php | 223 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 223 insertions(+) create mode 100644 phpBB/phpbb/event/md_exporter.php (limited to 'phpBB/phpbb/event') diff --git a/phpBB/phpbb/event/md_exporter.php b/phpBB/phpbb/event/md_exporter.php new file mode 100644 index 0000000000..6a8a9b1adb --- /dev/null +++ b/phpBB/phpbb/event/md_exporter.php @@ -0,0 +1,223 @@ +root_path = $phpbb_root_path; + $this->events = array(); + $this->filter = $this->current_event = ''; + } + + public function get_events() + { + return $this->events; + } + + /** + * @param string $md_file + * @param string $filter + * @return int Number of events found + * @throws \LogicException + */ + public function crawl_eventsmd($md_file, $filter) + { + $file_content = file_get_contents($this->root_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) continue; + if ($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 === 'acp') + { + $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('Found invalid event name "' . $event_name . '"'); + } + } + + /** + * Validate "Since" Information + * + * @param string $since + * @return string + * @throws \LogicException + */ + public function validate_since($since) + { + $since = ($since === '3.1-A1') ? '3.1.0-a1' : $since; + + if (!preg_match('#^\d+\.\d+\.\d+(?:-(?:a|b|rc|pl)\d+)?$#', $since)) + { + throw new \LogicException('Invalid since information 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->root_path . $file)) + { + throw new \LogicException('File "' . $file . '" not found for event "' . $this->current_event . '"', 2); + } + + 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 . '" found for event "' . $this->current_event . '"', 2); + } + } + } + else if ($this->filter == 'adm') + { + $files_list['adm'][] = substr($file_details, strlen("* Location: adm/style/")); + } + else + { + throw new \LogicException('Invalid file list found for event "' . $this->current_event . '"', 2); + } + + return $files_list; + } +} -- cgit v1.2.1 From 9ba9a024d6a72eb81d77347eb866e73556b81051 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Fri, 18 Apr 2014 13:06:13 +0200 Subject: [ticket/12273] Make event exporter compatible with php 5.3.3 PHPBB3-12273 --- phpBB/phpbb/event/php_exporter.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'phpBB/phpbb/event') diff --git a/phpBB/phpbb/event/php_exporter.php b/phpBB/phpbb/event/php_exporter.php index 51fe878d41..eb9e8f72b3 100644 --- a/phpBB/phpbb/event/php_exporter.php +++ b/phpBB/phpbb/event/php_exporter.php @@ -134,7 +134,7 @@ class php_exporter $files[] = $file_info->getFilename() . '/' . $file; } } - else if ($file_info->getExtension() == 'php') + else if (substr($file_info->getFilename(), -4) == '.php') { $files[] = $file_info->getFilename(); } -- cgit v1.2.1 From 1a913d6e0f9f1e0f5df2acbfdc5a5f43a0c83be0 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Fri, 18 Apr 2014 14:05:38 +0200 Subject: [ticket/12273] Do not use Inline control structures PHPBB3-12273 --- phpBB/phpbb/event/md_exporter.php | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) (limited to 'phpBB/phpbb/event') diff --git a/phpBB/phpbb/event/md_exporter.php b/phpBB/phpbb/event/md_exporter.php index 6a8a9b1adb..44b990c5ed 100644 --- a/phpBB/phpbb/event/md_exporter.php +++ b/phpBB/phpbb/event/md_exporter.php @@ -59,7 +59,10 @@ class md_exporter foreach ($events as $event) { // Last row of the file - if (strpos($event, "\n===\n") === false) continue; + if (strpos($event, "\n===\n") === false) + { + continue; + } list($event_name, $details) = explode("\n===\n", $event, 2); $this->validate_event_name($event_name); @@ -70,8 +73,11 @@ class md_exporter throw new \LogicException('The event "' . $this->current_event . '" is defined multiple times'); } - if ($this->filter == 'adm' && strpos($this->current_event, 'acp_') !== 0) continue; - if ($this->filter == 'styles' && strpos($this->current_event, 'acp_') === 0) continue; + 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); -- cgit v1.2.1 From 8ddc9ff185527128d74b618107edde2f57637dde Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Sun, 20 Apr 2014 14:15:54 +0200 Subject: [ticket/12273] Allow multiple $vars lines PHPBB3-12273 --- phpBB/phpbb/event/php_exporter.php | 36 +++++++++++++++++++++++++++--------- 1 file changed, 27 insertions(+), 9 deletions(-) (limited to 'phpBB/phpbb/event') diff --git a/phpBB/phpbb/event/php_exporter.php b/phpBB/phpbb/event/php_exporter.php index eb9e8f72b3..9044168980 100644 --- a/phpBB/phpbb/event/php_exporter.php +++ b/phpBB/phpbb/event/php_exporter.php @@ -302,25 +302,43 @@ class php_exporter */ public function get_vars_from_array() { - $vars_line = ltrim($this->file_lines[$this->current_event_line - 1], "\t"); - if (strpos($vars_line, "\$vars = array('") !== 0 || substr($vars_line, -3) !== '\');') + $vars_array_line = 1; + $found_vars_array = false; + $vars_array = array(); + while (ltrim($this->file_lines[$this->current_event_line - $vars_array_line], "\t") !== '*/') { - throw new \LogicException('Can not find "$vars = array();"-line for event "' . $this->current_event . '" in file "' . $this->current_file . '"', 1); + $line = ltrim($this->file_lines[$this->current_event_line - $vars_array_line], "\t"); + $match = array(); + preg_match('#^\$vars (?:\+)?= array\(\'([a-zA-Z0-9_\' ,]+)\'\);$#', $line, $match); + + if (isset($match[1])) + { + $found_vars_array = true; + if (strlen($match[1]) > 90) + { + throw new \LogicException('Should use multiple lines for $vars definition' + . ' for event "' . $this->current_event . '" in file "' . $this->current_file . '"', 1); + } + $vars_array = array_merge($vars_array, explode("', '", $match[1])); + } + + $vars_array_line++; + if ($this->current_event_line - $vars_array_line === 0) + { + throw new \LogicException('Can not find "$vars = array();"-line for event "' . $this->current_event . '" in file "' . $this->current_file . '"', 2); + } } - $vars_array = substr($vars_line, strlen("\$vars = array('"), 0 - strlen('\');')); - if ($vars_array === '') + if (!$found_vars_array) { - throw new \LogicException('Found empty $vars array for event "' . $this->current_event . '" in file "' . $this->current_file . '"', 2); + throw new \LogicException('Can not find "$vars = array();"-line for event "' . $this->current_event . '" in file "' . $this->current_file . '"', 3); } - $vars_array = explode("', '", $vars_array); - foreach ($vars_array as $var) { if (!preg_match('#^([a-zA-Z_][a-zA-Z0-9_]*)$#', $var)) { - throw new \LogicException('Found invalid var "' . $var . '" in array for event "' . $this->current_event . '" in file "' . $this->current_file . '"', 3); + throw new \LogicException('Found invalid var "' . $var . '" in array for event "' . $this->current_event . '" in file "' . $this->current_file . '"', 4); } } -- cgit v1.2.1 From b83a555da5df5b6916e4ae15aee10815bdac65d5 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Sun, 20 Apr 2014 14:28:09 +0200 Subject: [ticket/12273] Move phpBB test to new file and use a data provider PHPBB3-12273 --- phpBB/phpbb/event/php_exporter.php | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) (limited to 'phpBB/phpbb/event') diff --git a/phpBB/phpbb/event/php_exporter.php b/phpBB/phpbb/event/php_exporter.php index 9044168980..1e680ec896 100644 --- a/phpBB/phpbb/event/php_exporter.php +++ b/phpBB/phpbb/event/php_exporter.php @@ -163,7 +163,8 @@ class php_exporter } /** - * @param $file + * @param string $file + * @return int Number of events found in this file * @throws \LogicException */ public function crawl_php_file($file) @@ -171,6 +172,7 @@ class php_exporter $this->current_file = $file; $this->file_lines = array(); $content = file_get_contents($this->root_path . $this->current_file); + $num_events_found = 0; if (strpos($content, "dispatcher->trigger_event('") || strpos($content, "dispatcher->dispatch('")) { @@ -227,9 +229,12 @@ class php_exporter 'since' => $since, 'description' => $description, ); + $num_events_found++; } } } + + return $num_events_found; } /** -- cgit v1.2.1 From 6849e8b36f5d097a413c87a1321a09b9209a9d71 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Sun, 20 Apr 2014 14:57:18 +0200 Subject: [ticket/12273] Add file line to exception message PHPBB3-12273 --- phpBB/phpbb/event/php_exporter.php | 73 +++++++++++++++++++++++--------------- 1 file changed, 44 insertions(+), 29 deletions(-) (limited to 'phpBB/phpbb/event') diff --git a/phpBB/phpbb/event/php_exporter.php b/phpBB/phpbb/event/php_exporter.php index 1e680ec896..88301f86f6 100644 --- a/phpBB/phpbb/event/php_exporter.php +++ b/phpBB/phpbb/event/php_exporter.php @@ -185,7 +185,7 @@ class php_exporter if ($found_trigger_event !== false) { $event_line = $i; - $this->set_current_event($this->get_trigger_event_name($this->file_lines[$event_line]), $event_line); + $this->set_current_event($this->get_trigger_event_name($event_line), $event_line); // Find variables of the event $arguments = $this->get_vars_from_array(); @@ -198,7 +198,7 @@ class php_exporter if ($found_dispatch !== false) { $event_line = $i; - $this->set_current_event($this->get_dispatch_name($this->file_lines[$event_line]), $event_line); + $this->set_current_event($this->get_dispatch_name($event_line), $event_line); } } @@ -240,13 +240,14 @@ class php_exporter /** * Find the name of the event inside the dispatch() line * - * @param string $event_line + * @param int $event_line * @return int Absolute line number * @throws \LogicException */ public function get_dispatch_name($event_line) { - $event_line = ltrim($event_line, "\t"); + $event_text_line = $this->file_lines[$event_line]; + $event_text_line = ltrim($event_text_line, "\t"); $regex = '#\$([a-z](?:[a-z0-9_]|->)*)'; $regex .= '->dispatch\('; @@ -254,10 +255,11 @@ class php_exporter $regex .= '\);#'; $match = array(); - preg_match($regex, $event_line, $match); + preg_match($regex, $event_text_line, $match); if (!isset($match[2])) { - throw new \LogicException('Can not find event name in line "' . $event_line . '" in file "' . $this->current_file . '"', 1); + throw new \LogicException("Can not find event name in line '{$event_text_line}' " + . "in file '{$this->current_file}:{$event_line}'", 1); } return $match[2]; @@ -266,13 +268,14 @@ class php_exporter /** * Find the name of the event inside the trigger_event() line * - * @param string $event_line + * @param int $event_line * @return int Absolute line number * @throws \LogicException */ public function get_trigger_event_name($event_line) { - $event_line = ltrim($event_line, "\t"); + $event_text_line = $this->file_lines[$event_line]; + $event_text_line = ltrim($event_text_line, "\t"); $regex = '#extract\(\$([a-z](?:[a-z0-9_]|->)*)'; $regex .= '->trigger_event\('; @@ -280,10 +283,11 @@ class php_exporter $regex .= ', compact\(\$vars\)\)\);#'; $match = array(); - preg_match($regex, $event_line, $match); + preg_match($regex, $event_text_line, $match); if (!isset($match[2])) { - throw new \LogicException('Can not find event name in line "' . $event_line . '" in file "' . $this->current_file . '"', 1); + throw new \LogicException("Can not find event name in line '{$event_text_line}' " + . "in file '{$this->current_file}:{$event_line}'", 1); } return $match[2]; @@ -322,7 +326,7 @@ class php_exporter if (strlen($match[1]) > 90) { throw new \LogicException('Should use multiple lines for $vars definition' - . ' for event "' . $this->current_event . '" in file "' . $this->current_file . '"', 1); + . "for event '{$this->current_event}' in file '{$this->current_file}:{$this->current_event_line}'", 3); } $vars_array = array_merge($vars_array, explode("', '", $match[1])); } @@ -330,20 +334,20 @@ class php_exporter $vars_array_line++; if ($this->current_event_line - $vars_array_line === 0) { - throw new \LogicException('Can not find "$vars = array();"-line for event "' . $this->current_event . '" in file "' . $this->current_file . '"', 2); + throw new \LogicException("Can not find '\$vars = array();'-line for event '{$this->current_event}' in file '{$this->current_file}:{$this->current_event_line}'", 2); } } if (!$found_vars_array) { - throw new \LogicException('Can not find "$vars = array();"-line for event "' . $this->current_event . '" in file "' . $this->current_file . '"', 3); + throw new \LogicException("Can not find '\$vars = array();'-line for event '{$this->current_event}' in file '{$this->current_file}:{$this->current_event_line}'", 3); } foreach ($vars_array as $var) { if (!preg_match('#^([a-zA-Z_][a-zA-Z0-9_]*)$#', $var)) { - throw new \LogicException('Found invalid var "' . $var . '" in array for event "' . $this->current_event . '" in file "' . $this->current_file . '"', 4); + throw new \LogicException("Found invalid var '{$var}' in array for event '{$this->current_event}' in file '{$this->current_file}:{$this->current_event_line}'", 4); } } @@ -378,8 +382,8 @@ class php_exporter $doc_line = explode(' ', $var_line, 5); if (sizeof($doc_line) !== 5) { - throw new \LogicException('Found invalid line "' . $this->file_lines[$this->current_event_line - $current_doc_line] - . '" for event "' . $this->current_event . '" in file "' . $this->current_file . '"', 1); + throw new \LogicException("Found invalid line '{$this->file_lines[$this->current_event_line - $current_doc_line]}'" + . "for event '{$this->current_event}' in file '{$this->current_file}:{$this->current_event_line}'", 1); } $doc_vars[] = $doc_line[3]; } @@ -389,21 +393,22 @@ class php_exporter if ($current_doc_line > $this->current_event_line) { // Reached the start of the file - throw new \LogicException('Can not find end of docblock for event "' . $this->current_event . '" in file "' . $this->current_file . '"', 2); + throw new \LogicException("Can not find end of docblock for event '{$this->current_event}' in file '{$this->current_file}:{$this->current_event_line}'", 2); } } if (empty($doc_vars)) { // Reached the start of the file - throw new \LogicException('Can not find @var lines for event "' . $this->current_event . '" in file "' . $this->current_file . '"', 3); + throw new \LogicException("Can not find @var lines for event '{$this->current_event}' in file '{$this->current_file}:{$this->current_event_line}'", 3); } foreach ($doc_vars as $var) { if (!preg_match('#^([a-zA-Z_][a-zA-Z0-9_]*)$#', $var)) { - throw new \LogicException('Found invalid @var "' . $var . '" in docblock for event "' . $this->current_event . '" in file "' . $this->current_file . '"', 4); + throw new \LogicException("Found invalid @var '{$var}' in docblock for event " + . "'{$this->current_event}' in file '{$this->current_file}:{$this->current_event_line}'", 4); } } @@ -450,7 +455,8 @@ class php_exporter if ($found_comment_end && ltrim($this->file_lines[$this->current_event_line - $find_tag_line], "\t") === '/**') { // Reached the start of this doc block - throw new \LogicException('Can not find @' . $find_tag . ' information for event "' . $this->current_event . '" in file "' . $this->current_file . '"', 1); + throw new \LogicException("Can not find '@{$find_tag}' information for event " + . "'{$this->current_event}' in file '{$this->current_file}:{$this->current_event_line}'", 1); } foreach ($disallowed_tags as $disallowed_tag) @@ -458,7 +464,8 @@ class php_exporter if ($found_comment_end && strpos(ltrim($this->file_lines[$this->current_event_line - $find_tag_line], "\t"), '* @' . $disallowed_tag) === 0) { // Found @var after the @since - throw new \LogicException('Found @' . $disallowed_tag . ' information after @' . $find_tag . ' for event "' . $this->current_event . '" in file "' . $this->current_file . '"', 3); + throw new \LogicException("Found '@{$disallowed_tag}' information after '@{$find_tag}' for event " + . "'{$this->current_event}' in file '{$this->current_file}:{$this->current_event_line}'", 3); } } @@ -471,7 +478,8 @@ class php_exporter if ($find_tag_line >= $this->current_event_line) { // Reached the start of the file - throw new \LogicException('Can not find @' . $find_tag . ' information for event "' . $this->current_event . '" in file "' . $this->current_file . '"', 2); + throw new \LogicException("Can not find '@{$find_tag}' information for event " + . "'{$this->current_event}' in file '{$this->current_file}:{$this->current_event_line}'", 2); } } @@ -493,7 +501,8 @@ class php_exporter if ($find_desc_line > $this->current_event_line) { // Reached the start of the file - throw new \LogicException('Can not find a description for event "' . $this->current_event . '" in file "' . $this->current_file . '"', 1); + throw new \LogicException("Can not find a description for event " + . "'{$this->current_event}' in file '{$this->current_file}:{$this->current_event_line}'", 1); } } @@ -503,7 +512,8 @@ class php_exporter if (strpos($desc, '* @') === 0 || $desc[0] !== '*' || substr($desc, 1) == '') { // First line of the doc block is a @-line, empty or only contains "*" - throw new \LogicException('Can not find a description for event "' . $this->current_event . '" in file "' . $this->current_file . '"', 2); + throw new \LogicException("Can not find a description for event " + . "'{$this->current_event}' in file '{$this->current_file}:{$this->current_event_line}'", 2); } return $find_desc_line; @@ -522,14 +532,16 @@ class php_exporter if ($since !== trim($since)) { - throw new \LogicException('Invalid @since information for event "' . $this->current_event . '" in file "' . $this->current_file . '"', 1); + throw new \LogicException("Invalid '@since' information for event " + . "'{$this->current_event}' in file '{$this->current_file}:{$this->current_event_line}'", 1); } $since = ($since === '3.1-A1') ? '3.1.0-a1' : $since; if (!preg_match('#^\d+\.\d+\.\d+(?:-(?:a|b|rc|pl)\d+)?$#', $since)) { - throw new \LogicException('Invalid @since information for event "' . $this->current_event . '" in file "' . $this->current_file . '"', 2); + throw new \LogicException("Invalid '@since' information for event " + . "'{$this->current_event}' in file '{$this->current_file}:{$this->current_event_line}'", 2); } return $since; @@ -549,12 +561,14 @@ class php_exporter if ($event !== trim($event)) { - throw new \LogicException('Invalid @event information for event "' . $event_name . '" in file "' . $this->current_file . '"', 1); + throw new \LogicException("Invalid '@event' information for event " + . "'{$this->current_event}' in file '{$this->current_file}:{$this->current_event_line}'", 1); } if ($event !== $event_name) { - throw new \LogicException('Event name does not match @event tag for event "' . $event_name . '" in file "' . $this->current_file . '"', 2); + throw new \LogicException("Event name does not match '@event' tag for event" + . "'{$this->current_event}' in file '{$this->current_file}:{$this->current_event_line}'", 2); } return $event; @@ -576,7 +590,8 @@ class php_exporter if ($sizeof_vars_array !== sizeof($vars_docblock) || $sizeof_vars_array !== sizeof(array_intersect($vars_array, $vars_docblock))) { - throw new \LogicException('$vars array does not match the list of @var tags for event "' . $this->current_event . '" in file "' . $this->current_file . '"'); + throw new \LogicException("\$vars array does not match the list of '@var' tags for event" + . "'{$this->current_event}' in file '{$this->current_file}:{$this->current_event_line}'"); } } } -- cgit v1.2.1 From 08cce5fba548374c8c067ee8bed8aa6d388367e5 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Sun, 20 Apr 2014 14:58:24 +0200 Subject: [ticket/12273] Remove duplicated code PHPBB3-12273 --- phpBB/phpbb/event/php_exporter.php | 49 +++++++++++++------------------------- 1 file changed, 16 insertions(+), 33 deletions(-) (limited to 'phpBB/phpbb/event') diff --git a/phpBB/phpbb/event/php_exporter.php b/phpBB/phpbb/event/php_exporter.php index 88301f86f6..c076d7567d 100644 --- a/phpBB/phpbb/event/php_exporter.php +++ b/phpBB/phpbb/event/php_exporter.php @@ -185,7 +185,7 @@ class php_exporter if ($found_trigger_event !== false) { $event_line = $i; - $this->set_current_event($this->get_trigger_event_name($event_line), $event_line); + $this->set_current_event($this->get_event_name($event_line, false), $event_line); // Find variables of the event $arguments = $this->get_vars_from_array(); @@ -198,7 +198,7 @@ class php_exporter if ($found_dispatch !== false) { $event_line = $i; - $this->set_current_event($this->get_dispatch_name($event_line), $event_line); + $this->set_current_event($this->get_event_name($event_line, true), $event_line); } } @@ -241,46 +241,29 @@ class php_exporter * Find the name of the event inside the dispatch() line * * @param int $event_line + * @param bool $is_dispatch Do we look for dispatch() or trigger_event() ? * @return int Absolute line number * @throws \LogicException */ - public function get_dispatch_name($event_line) + public function get_event_name($event_line, $is_dispatch) { $event_text_line = $this->file_lines[$event_line]; $event_text_line = ltrim($event_text_line, "\t"); - $regex = '#\$([a-z](?:[a-z0-9_]|->)*)'; - $regex .= '->dispatch\('; - $regex .= '\'' . $this->preg_match_event_name() . '\''; - $regex .= '\);#'; - - $match = array(); - preg_match($regex, $event_text_line, $match); - if (!isset($match[2])) + if ($is_dispatch) { - throw new \LogicException("Can not find event name in line '{$event_text_line}' " - . "in file '{$this->current_file}:{$event_line}'", 1); + $regex = '#\$([a-z](?:[a-z0-9_]|->)*)'; + $regex .= '->dispatch\('; + $regex .= '\'' . $this->preg_match_event_name() . '\''; + $regex .= '\);#'; + } + else + { + $regex = '#extract\(\$([a-z](?:[a-z0-9_]|->)*)'; + $regex .= '->trigger_event\('; + $regex .= '\'' . $this->preg_match_event_name() . '\''; + $regex .= ', compact\(\$vars\)\)\);#'; } - - return $match[2]; - } - - /** - * Find the name of the event inside the trigger_event() line - * - * @param int $event_line - * @return int Absolute line number - * @throws \LogicException - */ - public function get_trigger_event_name($event_line) - { - $event_text_line = $this->file_lines[$event_line]; - $event_text_line = ltrim($event_text_line, "\t"); - - $regex = '#extract\(\$([a-z](?:[a-z0-9_]|->)*)'; - $regex .= '->trigger_event\('; - $regex .= '\'' . $this->preg_match_event_name() . '\''; - $regex .= ', compact\(\$vars\)\)\);#'; $match = array(); preg_match($regex, $event_text_line, $match); -- cgit v1.2.1 From bc1ab3dc79b63091c82a29f5c5175558423ac8cd Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Sun, 20 Apr 2014 15:11:23 +0200 Subject: [ticket/12273] Fix missing space in some Exceptions PHPBB3-12273 --- phpBB/phpbb/event/php_exporter.php | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) (limited to 'phpBB/phpbb/event') diff --git a/phpBB/phpbb/event/php_exporter.php b/phpBB/phpbb/event/php_exporter.php index c076d7567d..3956be600b 100644 --- a/phpBB/phpbb/event/php_exporter.php +++ b/phpBB/phpbb/event/php_exporter.php @@ -218,8 +218,9 @@ class php_exporter if (isset($this->events[$this->current_event])) { - throw new \LogicException('The event "' . $this->current_event . '" from file "' . $this->current_file - . '" already exists in file "'. $this->events[$this->current_event]['file'] . '"', 10); + throw new \LogicException("The event '{$this->current_event}' from file " + . "'{$this->current_file}:{$event_line_num}' already exists in file " + . "'{$this->events[$this->current_event]['file']}'", 10); } $this->events[$this->current_event] = array( @@ -308,7 +309,7 @@ class php_exporter $found_vars_array = true; if (strlen($match[1]) > 90) { - throw new \LogicException('Should use multiple lines for $vars definition' + throw new \LogicException('Should use multiple lines for $vars definition ' . "for event '{$this->current_event}' in file '{$this->current_file}:{$this->current_event_line}'", 3); } $vars_array = array_merge($vars_array, explode("', '", $match[1])); @@ -365,7 +366,7 @@ class php_exporter $doc_line = explode(' ', $var_line, 5); if (sizeof($doc_line) !== 5) { - throw new \LogicException("Found invalid line '{$this->file_lines[$this->current_event_line - $current_doc_line]}'" + throw new \LogicException("Found invalid line '{$this->file_lines[$this->current_event_line - $current_doc_line]}' " . "for event '{$this->current_event}' in file '{$this->current_file}:{$this->current_event_line}'", 1); } $doc_vars[] = $doc_line[3]; @@ -550,7 +551,7 @@ class php_exporter if ($event !== $event_name) { - throw new \LogicException("Event name does not match '@event' tag for event" + throw new \LogicException("Event name does not match '@event' tag for event " . "'{$this->current_event}' in file '{$this->current_file}:{$this->current_event_line}'", 2); } @@ -573,7 +574,7 @@ class php_exporter if ($sizeof_vars_array !== sizeof($vars_docblock) || $sizeof_vars_array !== sizeof(array_intersect($vars_array, $vars_docblock))) { - throw new \LogicException("\$vars array does not match the list of '@var' tags for event" + throw new \LogicException("\$vars array does not match the list of '@var' tags for event " . "'{$this->current_event}' in file '{$this->current_file}:{$this->current_event_line}'"); } } -- cgit v1.2.1 From c7dcc6d7007d1500fc5007b2b33916e9f20f137b Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Sun, 20 Apr 2014 15:48:04 +0200 Subject: [ticket/12273] Verify that the events are still in the named files PHPBB3-12273 --- phpBB/phpbb/event/md_exporter.php | 53 +++++++++++++++++++++++++++++++-------- 1 file changed, 43 insertions(+), 10 deletions(-) (limited to 'phpBB/phpbb/event') diff --git a/phpBB/phpbb/event/md_exporter.php b/phpBB/phpbb/event/md_exporter.php index 44b990c5ed..8714bdb2f7 100644 --- a/phpBB/phpbb/event/md_exporter.php +++ b/phpBB/phpbb/event/md_exporter.php @@ -36,6 +36,7 @@ class md_exporter { $this->root_path = $phpbb_root_path; $this->events = array(); + $this->events_by_file = array(); $this->filter = $this->current_event = ''; } @@ -70,7 +71,7 @@ class md_exporter if (isset($this->events[$this->current_event])) { - throw new \LogicException('The event "' . $this->current_event . '" is defined multiple times'); + throw new \LogicException("The event '{$this->current_event}' is defined multiple times"); } if (($this->filter == 'adm' && strpos($this->current_event, 'acp_') !== 0) @@ -93,6 +94,11 @@ class md_exporter ); } + foreach ($this->events_by_file as $file => $events) + { + $this->validate_events_for_file($file, $events); + } + return sizeof($this->events); } @@ -147,7 +153,7 @@ class md_exporter { if (!preg_match('#^([a-z][a-z0-9]*(?:_[a-z][a-z0-9]*)+)$#', $event_name)) { - throw new \LogicException('Found invalid event name "' . $event_name . '"'); + throw new \LogicException("Invalid event name '{$event_name}'"); } } @@ -164,7 +170,7 @@ class md_exporter if (!preg_match('#^\d+\.\d+\.\d+(?:-(?:a|b|rc|pl)\d+)?$#', $since)) { - throw new \LogicException('Invalid since information for event "' . $this->current_event . '"'); + throw new \LogicException("Invalid since information found for event '{$this->current_event}'"); } return $since; @@ -192,10 +198,6 @@ class md_exporter $files = explode("\n + ", $file_details); foreach ($files as $file) { - if (!file_exists($this->root_path . $file)) - { - throw new \LogicException('File "' . $file . '" not found for event "' . $this->current_event . '"', 2); - } if (($this->filter !== 'adm') && strpos($file, 'styles/prosilver/template/') === 0) { @@ -211,19 +213,50 @@ class md_exporter } else { - throw new \LogicException('Invalid file "' . $file . '" found for event "' . $this->current_event . '"', 2); + 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') { - $files_list['adm'][] = substr($file_details, strlen("* Location: adm/style/")); + $file = substr($file_details, strlen('* Location: ')); + $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); + throw new \LogicException("Invalid file list found for event '{$this->current_event}'", 2); } return $files_list; } + + /** + * Validates whether a list of events is named in $file + * + * @param string $file + * @param array $events + * @return null + * @throws \LogicException + */ + public function validate_events_for_file($file, array $events) + { + if (!file_exists($this->root_path . $file)) + { + $event_list = implode("', '", $events); + throw new \LogicException("File '{$file}' not found for event '{$event_list}'", 1); + } + + $file_content = file_get_contents($this->root_path . $file); + foreach ($events as $event) + { + if (strpos($file_content, '') === false) + { + throw new \LogicException("Event '{$event}' not found in file '{$file}'", 2); + } + } + } } -- cgit v1.2.1 From f676f50c22a2e8d4a6c5671c5cf2f9f7372ed40d Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Sun, 20 Apr 2014 15:55:24 +0200 Subject: [ticket/12273] Only check selected files PHPBB3-12273 --- phpBB/phpbb/event/md_exporter.php | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) (limited to 'phpBB/phpbb/event') diff --git a/phpBB/phpbb/event/md_exporter.php b/phpBB/phpbb/event/md_exporter.php index 8714bdb2f7..089115b91f 100644 --- a/phpBB/phpbb/event/md_exporter.php +++ b/phpBB/phpbb/event/md_exporter.php @@ -198,7 +198,6 @@ class md_exporter $files = explode("\n + ", $file_details); foreach ($files as $file) { - if (($this->filter !== 'adm') && strpos($file, 'styles/prosilver/template/') === 0) { $files_list['prosilver'][] = substr($file, strlen('styles/prosilver/template/')); @@ -253,9 +252,13 @@ class md_exporter $file_content = file_get_contents($this->root_path . $file); foreach ($events as $event) { - if (strpos($file_content, '') === false) + if (($this->filter !== 'adm') && strpos($file, 'adm/style/') !== 0 + || ($this->filter === 'adm') && strpos($file, 'adm/style/') === 0) { - throw new \LogicException("Event '{$event}' not found in file '{$file}'", 2); + if (strpos($file_content, '') === false) + { + throw new \LogicException("Event '{$event}' not found in file '{$file}'", 2); + } } } } -- cgit v1.2.1 From 7dc32a45e5c43f29b19b8a8138b515c9e09d23de Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Sun, 20 Apr 2014 17:00:18 +0200 Subject: [ticket/12273] Verify the result of the .md file to the source PHPBB3-12273 --- phpBB/phpbb/event/md_exporter.php | 166 ++++++++++++++++++++++++++++++++++---- 1 file changed, 151 insertions(+), 15 deletions(-) (limited to 'phpBB/phpbb/event') diff --git a/phpBB/phpbb/event/md_exporter.php b/phpBB/phpbb/event/md_exporter.php index 089115b91f..8dba963f11 100644 --- a/phpBB/phpbb/event/md_exporter.php +++ b/phpBB/phpbb/event/md_exporter.php @@ -45,6 +45,54 @@ class md_exporter return $this->events; } + /** + * @param string $md_file + * @param string $filter + * @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->root_path . 'adm/style/', '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 + * @param string $filter + * @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->root_path . 'styles/' . $style . '/template/', + '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 * @param string $filter @@ -94,11 +142,6 @@ class md_exporter ); } - foreach ($this->events_by_file as $file => $events) - { - $this->validate_events_for_file($file, $events); - } - return sizeof($this->events); } @@ -233,33 +276,126 @@ class md_exporter return $files_list; } + public function crawl_file_for_events($file) + { + if (!file_exists($this->root_path . $file)) + { + throw new \LogicException("File '{$file}' does not exist", 1); + } + + $event_list = array(); + $file_content = file_get_contents($this->root_path . $file); + + $events = explode('', $event, 2); + $event_list[] = $event_name; + } + + return $event_list; + } + /** - * Validates whether a list of events is named in $file + * Validates whether all events from $file are in the md file and vice-versa * * @param string $file * @param array $events * @return null * @throws \LogicException */ - public function validate_events_for_file($file, array $events) + public function validate_events_from_file($file, array $events) { - if (!file_exists($this->root_path . $file)) + if (empty($this->events_by_file[$file]) && empty($events)) { - $event_list = implode("', '", $events); - throw new \LogicException("File '{$file}' not found for event '{$event_list}'", 1); + return true; + } + 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); } - $file_content = file_get_contents($this->root_path . $file); + $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 (($this->filter !== 'adm') && strpos($file, 'adm/style/') !== 0 - || ($this->filter === 'adm') && strpos($file, 'adm/style/') === 0) + if (!in_array($event, $this->events_by_file[$file])) { - if (strpos($file_content, '') === false) + $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 + * @param string $path Path from root to $dir + * @return array List of files (including directories) + */ + public function get_recursive_file_list($dir, $path = '') + { + try + { + $iterator = new \DirectoryIterator($dir); + } + catch (\Exception $e) + { + return array(); + } + + $files = array(); + foreach ($iterator as $file_info) + { + /** @var \DirectoryIterator $file_info */ + if ($file_info->isDot()) + { + continue; + } + + // Do not scan some directories + if ($file_info->isDir()) + { + $sub_dir = $this->get_recursive_file_list($file_info->getPath() . '/' . $file_info->getFilename(), $path . '/' . $file_info->getFilename()); + foreach ($sub_dir as $file) { - throw new \LogicException("Event '{$event}' not found in file '{$file}'", 2); + $files[] = $file_info->getFilename() . '/' . $file; } } + else if (substr($file_info->getFilename(), -5) == '.html') + { + $files[] = $file_info->getFilename(); + } } + + return $files; } } -- cgit v1.2.1 From a2c3b2534a3a187a7ccc3e4b86ba69735c4b8b1a Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Sun, 20 Apr 2014 17:08:06 +0200 Subject: [ticket/12273] Fix method name of test and fix undefined index PHPBB3-12273 --- phpBB/phpbb/event/md_exporter.php | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to 'phpBB/phpbb/event') diff --git a/phpBB/phpbb/event/md_exporter.php b/phpBB/phpbb/event/md_exporter.php index 8dba963f11..708900c693 100644 --- a/phpBB/phpbb/event/md_exporter.php +++ b/phpBB/phpbb/event/md_exporter.php @@ -241,6 +241,11 @@ class md_exporter $files = explode("\n + ", $file_details); foreach ($files as $file) { + if (!file_exists($this->root_path . $file)) + { + 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/')); @@ -312,6 +317,12 @@ class md_exporter { 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]); -- cgit v1.2.1 From b0a9acd8ff953736d6aeb39095aca278c0869563 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Thu, 24 Apr 2014 17:14:12 +0200 Subject: [ticket/12273] Use array_merge instead of += PHPBB3-12273 --- phpBB/phpbb/event/php_exporter.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'phpBB/phpbb/event') diff --git a/phpBB/phpbb/event/php_exporter.php b/phpBB/phpbb/event/php_exporter.php index 3956be600b..8de3051d9b 100644 --- a/phpBB/phpbb/event/php_exporter.php +++ b/phpBB/phpbb/event/php_exporter.php @@ -302,17 +302,17 @@ class php_exporter { $line = ltrim($this->file_lines[$this->current_event_line - $vars_array_line], "\t"); $match = array(); - preg_match('#^\$vars (?:\+)?= array\(\'([a-zA-Z0-9_\' ,]+)\'\);$#', $line, $match); + preg_match('#^\$vars = (array_merge\(\$vars, )?array\(\'([a-zA-Z0-9_\' ,]+)\'\)(?(1)\));$#', $line, $match); - if (isset($match[1])) + if (isset($match[2])) { $found_vars_array = true; - if (strlen($match[1]) > 90) + if (strlen($match[2]) > 90) { throw new \LogicException('Should use multiple lines for $vars definition ' . "for event '{$this->current_event}' in file '{$this->current_file}:{$this->current_event_line}'", 3); } - $vars_array = array_merge($vars_array, explode("', '", $match[1])); + $vars_array = array_merge($vars_array, explode("', '", $match[2])); } $vars_array_line++; -- cgit v1.2.1 From 142fe1a0e57797aa77059edda42ebedd91b11397 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Fri, 25 Apr 2014 11:45:54 +0200 Subject: [ticket/12273] Use multiline arrays instead of array_merge() PHPBB3-12273 --- phpBB/phpbb/event/php_exporter.php | 84 ++++++++++++++++++++++++++------------ 1 file changed, 58 insertions(+), 26 deletions(-) (limited to 'phpBB/phpbb/event') diff --git a/phpBB/phpbb/event/php_exporter.php b/phpBB/phpbb/event/php_exporter.php index 8de3051d9b..2a69f15802 100644 --- a/phpBB/phpbb/event/php_exporter.php +++ b/phpBB/phpbb/event/php_exporter.php @@ -295,48 +295,80 @@ class php_exporter */ public function get_vars_from_array() { - $vars_array_line = 1; - $found_vars_array = false; - $vars_array = array(); - while (ltrim($this->file_lines[$this->current_event_line - $vars_array_line], "\t") !== '*/') + $line = ltrim($this->file_lines[$this->current_event_line - 1], "\t"); + if ($line === ');') { - $line = ltrim($this->file_lines[$this->current_event_line - $vars_array_line], "\t"); - $match = array(); - preg_match('#^\$vars = (array_merge\(\$vars, )?array\(\'([a-zA-Z0-9_\' ,]+)\'\)(?(1)\));$#', $line, $match); + $vars_array = $this->get_vars_from_multi_line_array(); + } + else + { + $vars_array = $this->get_vars_from_single_line_array($line); + } - if (isset($match[2])) + foreach ($vars_array as $var) + { + if (!preg_match('#^([a-zA-Z_][a-zA-Z0-9_]*)$#', $var)) { - $found_vars_array = true; - if (strlen($match[2]) > 90) - { - throw new \LogicException('Should use multiple lines for $vars definition ' - . "for event '{$this->current_event}' in file '{$this->current_file}:{$this->current_event_line}'", 3); - } - $vars_array = array_merge($vars_array, explode("', '", $match[2])); + throw new \LogicException("Found invalid var '{$var}' in array for event '{$this->current_event}' in file '{$this->current_file}:{$this->current_event_line}'", 3); } + } + + sort($vars_array); + return $vars_array; + } + + /** + * Find the variables in single line array + * + * @param string $line + * @return array List of variables + * @throws \LogicException + */ + public function get_vars_from_single_line_array($line, $throw_multiline = true) + { + $match = array(); + preg_match('#^\$vars = array\(\'([a-zA-Z0-9_\' ,]+)\'\);$#', $line, $match); - $vars_array_line++; - if ($this->current_event_line - $vars_array_line === 0) + if (isset($match[1])) + { + $vars_array = explode("', '", $match[1]); + if ($throw_multiline && sizeof($vars_array) > 6) { - throw new \LogicException("Can not find '\$vars = array();'-line for event '{$this->current_event}' in file '{$this->current_file}:{$this->current_event_line}'", 2); + throw new \LogicException('Should use multiple lines for $vars definition ' + . "for event '{$this->current_event}' in file '{$this->current_file}:{$this->current_event_line}'", 2); } + return $vars_array; } - - if (!$found_vars_array) + else { - throw new \LogicException("Can not find '\$vars = array();'-line for event '{$this->current_event}' in file '{$this->current_file}:{$this->current_event_line}'", 3); + throw new \LogicException("Can not find '\$vars = array();'-line for event '{$this->current_event}' in file '{$this->current_file}:{$this->current_event_line}'", 1); } + } - foreach ($vars_array as $var) + /** + * Find the variables in single line array + * + * @param string $line + * @return array List of variables + * @throws \LogicException + */ + public function get_vars_from_multi_line_array() + { + $current_vars_line = 2; + $var_lines = array(); + while (ltrim($this->file_lines[$this->current_event_line - $current_vars_line], "\t") !== '$vars = array(') { - if (!preg_match('#^([a-zA-Z_][a-zA-Z0-9_]*)$#', $var)) + $var_lines[] = substr(trim($this->file_lines[$this->current_event_line - $current_vars_line]), 0, -1); + + $current_vars_line++; + if ($current_vars_line > $this->current_event_line) { - throw new \LogicException("Found invalid var '{$var}' in array for event '{$this->current_event}' in file '{$this->current_file}:{$this->current_event_line}'", 4); + // Reached the start of the file + throw new \LogicException("Can not find end of \$vars array for event '{$this->current_event}' in file '{$this->current_file}:{$this->current_event_line}'", 2); } } - sort($vars_array); - return $vars_array; + return $this->get_vars_from_single_line_array('$vars = array(' . implode(", ", $var_lines) . ');', false); } /** -- cgit v1.2.1 From 48278f122cf9a2b23c39eccdd998b0c9b0552b2a Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Fri, 25 Apr 2014 12:31:57 +0200 Subject: [ticket/12273] Do not allow 3.1-A1 version PHPBB3-12273 --- phpBB/phpbb/event/php_exporter.php | 18 +++++------------- 1 file changed, 5 insertions(+), 13 deletions(-) (limited to 'phpBB/phpbb/event') diff --git a/phpBB/phpbb/event/php_exporter.php b/phpBB/phpbb/event/php_exporter.php index 2a69f15802..ac88ffa8b5 100644 --- a/phpBB/phpbb/event/php_exporter.php +++ b/phpBB/phpbb/event/php_exporter.php @@ -544,23 +544,15 @@ class php_exporter */ public function validate_since($line) { - $since = substr(ltrim($line, "\t"), strlen('* @since ')); - - if ($since !== trim($since)) - { - throw new \LogicException("Invalid '@since' information for event " - . "'{$this->current_event}' in file '{$this->current_file}:{$this->current_event_line}'", 1); - } - - $since = ($since === '3.1-A1') ? '3.1.0-a1' : $since; - - if (!preg_match('#^\d+\.\d+\.\d+(?:-(?:a|b|rc|pl)\d+)?$#', $since)) + $match = array(); + preg_match('#^\* @since (\d+\.\d+\.\d+(?:-(?:a|b|rc|pl)\d+)?)$#', ltrim($line, "\t"), $match); + if (!isset($match[1])) { throw new \LogicException("Invalid '@since' information for event " - . "'{$this->current_event}' in file '{$this->current_file}:{$this->current_event_line}'", 2); + . "'{$this->current_event}' in file '{$this->current_file}:{$this->current_event_line}'"); } - return $since; + return $match[1]; } /** -- cgit v1.2.1 From 3bcda97e28f096558baa46be63e8bb40f43c59c8 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Fri, 25 Apr 2014 12:37:39 +0200 Subject: [ticket/12273] Sort arguments alphabetically before exporting PHPBB3-12273 --- phpBB/phpbb/event/php_exporter.php | 1 + 1 file changed, 1 insertion(+) (limited to 'phpBB/phpbb/event') diff --git a/phpBB/phpbb/event/php_exporter.php b/phpBB/phpbb/event/php_exporter.php index ac88ffa8b5..a92bd2671d 100644 --- a/phpBB/phpbb/event/php_exporter.php +++ b/phpBB/phpbb/event/php_exporter.php @@ -223,6 +223,7 @@ class php_exporter . "'{$this->events[$this->current_event]['file']}'", 10); } + sort($arguments); $this->events[$this->current_event] = array( 'event' => $this->current_event, 'file' => $this->current_file, -- cgit v1.2.1 From 18be18e9989dcd48b662c5bb2bd128ca947f68d3 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Fri, 25 Apr 2014 20:26:47 +0200 Subject: [ticket/12273] Do not allow 3.1-A1 for template events PHPBB3-12273 --- phpBB/phpbb/event/md_exporter.php | 2 -- 1 file changed, 2 deletions(-) (limited to 'phpBB/phpbb/event') diff --git a/phpBB/phpbb/event/md_exporter.php b/phpBB/phpbb/event/md_exporter.php index 708900c693..4940094e77 100644 --- a/phpBB/phpbb/event/md_exporter.php +++ b/phpBB/phpbb/event/md_exporter.php @@ -209,8 +209,6 @@ class md_exporter */ public function validate_since($since) { - $since = ($since === '3.1-A1') ? '3.1.0-a1' : $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}'"); -- cgit v1.2.1 From 6aa8596d4dc206797e4740465e3035c65bcdba3f Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Sat, 26 Apr 2014 16:30:19 +0200 Subject: [ticket/12273] Fix doc blocks PHPBB3-12273 --- phpBB/phpbb/event/md_exporter.php | 20 +++++++++++++++----- phpBB/phpbb/event/php_exporter.php | 21 ++++++++++++++++++++- 2 files changed, 35 insertions(+), 6 deletions(-) (limited to 'phpBB/phpbb/event') diff --git a/phpBB/phpbb/event/md_exporter.php b/phpBB/phpbb/event/md_exporter.php index 4940094e77..540ec9a0d7 100644 --- a/phpBB/phpbb/event/md_exporter.php +++ b/phpBB/phpbb/event/md_exporter.php @@ -40,6 +40,11 @@ class md_exporter $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; @@ -47,7 +52,6 @@ class md_exporter /** * @param string $md_file - * @param string $filter * @return int Number of events found * @throws \LogicException */ @@ -67,7 +71,6 @@ class md_exporter /** * @param string $md_file - * @param string $filter * @return int Number of events found * @throws \LogicException */ @@ -279,6 +282,13 @@ class md_exporter 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->root_path . $file)) @@ -294,8 +304,8 @@ class md_exporter array_shift($events); foreach ($events as $event) { - list($event_name, $null) = explode(' -->', $event, 2); - $event_list[] = $event_name; + $event = explode(' -->', $event, 2); + $event_list[] = array_shift($event); } return $event_list; @@ -306,7 +316,7 @@ class md_exporter * * @param string $file * @param array $events - * @return null + * @return true * @throws \LogicException */ public function validate_events_from_file($file, array $events) diff --git a/phpBB/phpbb/event/php_exporter.php b/phpBB/phpbb/event/php_exporter.php index a92bd2671d..41058216e8 100644 --- a/phpBB/phpbb/event/php_exporter.php +++ b/phpBB/phpbb/event/php_exporter.php @@ -46,17 +46,35 @@ class php_exporter $this->current_event_line = 0; } + /** + * Get the list of all events + * + * @return array Array with events: name => details + */ public function get_events() { return $this->events; } + /** + * Set current event data + * + * @param string $name Name of the current event (used for error messages) + * @param int $line Line where the current event is placed in + * @return null + */ public function set_current_event($name, $line) { $this->current_event = $name; $this->current_event_line = $line; } + /** + * Set the content of this file + * + * @param array $content Array with the lines of the file + * @return null + */ public function set_content($content) { $this->file_lines = $content; @@ -322,6 +340,8 @@ class php_exporter * Find the variables in single line array * * @param string $line + * @param bool $throw_multiline Throw an exception when there are too + * many arguments in one line. * @return array List of variables * @throws \LogicException */ @@ -349,7 +369,6 @@ class php_exporter /** * Find the variables in single line array * - * @param string $line * @return array List of variables * @throws \LogicException */ -- cgit v1.2.1 From 7a44f66448fcdc53ed3c371c25317e553217eec8 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Sat, 26 Apr 2014 16:59:18 +0200 Subject: [ticket/12273] Use RecursiveDirectoryIterator in md_exporter PHPBB3-12273 --- phpBB/phpbb/event/md_exporter.php | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) (limited to 'phpBB/phpbb/event') diff --git a/phpBB/phpbb/event/md_exporter.php b/phpBB/phpbb/event/md_exporter.php index 540ec9a0d7..cb14ff5778 100644 --- a/phpBB/phpbb/event/md_exporter.php +++ b/phpBB/phpbb/event/md_exporter.php @@ -377,14 +377,21 @@ class md_exporter * Works recursive with any depth * * @param string $dir Directory to go through - * @param string $path Path from root to $dir * @return array List of files (including directories) */ - public function get_recursive_file_list($dir, $path = '') + public function get_recursive_file_list($dir) { try { - $iterator = new \DirectoryIterator($dir); + $iterator = new \RecursiveIteratorIterator( + new \phpbb\recursive_dot_prefix_filter_iterator( + new \RecursiveDirectoryIterator( + $dir, + \FilesystemIterator::SKIP_DOTS + ) + ), + \RecursiveIteratorIterator::SELF_FIRST + ); } catch (\Exception $e) { @@ -394,24 +401,17 @@ class md_exporter $files = array(); foreach ($iterator as $file_info) { - /** @var \DirectoryIterator $file_info */ - if ($file_info->isDot()) + /** @var \RecursiveDirectoryIterator $file_info */ + if ($file_info->isDir()) { continue; } - // Do not scan some directories - if ($file_info->isDir()) - { - $sub_dir = $this->get_recursive_file_list($file_info->getPath() . '/' . $file_info->getFilename(), $path . '/' . $file_info->getFilename()); - foreach ($sub_dir as $file) - { - $files[] = $file_info->getFilename() . '/' . $file; - } - } - else if (substr($file_info->getFilename(), -5) == '.html') + $relative_path = $iterator->getInnerIterator()->getSubPathname(); + + if (substr($relative_path, -5) == '.html') { - $files[] = $file_info->getFilename(); + $files[] = str_replace(DIRECTORY_SEPARATOR, '/', $relative_path); } } -- cgit v1.2.1 From 927c219b1fc1cc69f724827771d567384b2cda8c Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Sat, 26 Apr 2014 18:04:47 +0200 Subject: [ticket/12273] Use RecursiveDirectoryIterator with filter in php_exporter PHPBB3-12273 --- phpBB/phpbb/event/php_exporter.php | 56 ++++++---------------- .../event/recursive_event_filter_iterator.php | 45 +++++++++++++++++ 2 files changed, 59 insertions(+), 42 deletions(-) create mode 100644 phpBB/phpbb/event/recursive_event_filter_iterator.php (limited to 'phpBB/phpbb/event') diff --git a/phpBB/phpbb/event/php_exporter.php b/phpBB/phpbb/event/php_exporter.php index 41058216e8..8e4a9d399d 100644 --- a/phpBB/phpbb/event/php_exporter.php +++ b/phpBB/phpbb/event/php_exporter.php @@ -100,17 +100,22 @@ class php_exporter /** * Returns a list of files in $dir * - * Works recursive with any depth - * * @param string $dir Directory to go through - * @param string $path Path from root to $dir - * @return array List of files (including directories) + * @return array List of files (including the path) */ - public function get_recursive_file_list($dir, $path = '') + public function get_recursive_file_list($dir) { try { - $iterator = new \DirectoryIterator($dir); + $iterator = new \RecursiveIteratorIterator( + new \phpbb\event\recursive_event_filter_iterator( + new \RecursiveDirectoryIterator( + $dir, + \FilesystemIterator::SKIP_DOTS + ) + ), + \RecursiveIteratorIterator::LEAVES_ONLY + ); } catch (\Exception $e) { @@ -120,42 +125,9 @@ class php_exporter $files = array(); foreach ($iterator as $file_info) { - /** @var \DirectoryIterator $file_info */ - if ($file_info->isDot()) - { - continue; - } - - // Do not scan some directories - if ($file_info->isDir() && ( - ($path == '' && in_array($file_info->getFilename(), array( - 'cache', - 'develop', - 'ext', - 'files', - 'language', - 'store', - 'vendor', - ))) - || ($path == '/includes' && in_array($file_info->getFilename(), array('utf'))) - || ($path == '/phpbb/db/migration' && in_array($file_info->getFilename(), array('data'))) - || ($path == '/phpbb' && in_array($file_info->getFilename(), array('event'))) - )) - { - continue; - } - else if ($file_info->isDir()) - { - $sub_dir = $this->get_recursive_file_list($file_info->getPath() . '/' . $file_info->getFilename(), $path . '/' . $file_info->getFilename()); - foreach ($sub_dir as $file) - { - $files[] = $file_info->getFilename() . '/' . $file; - } - } - else if (substr($file_info->getFilename(), -4) == '.php') - { - $files[] = $file_info->getFilename(); - } + /** @var \RecursiveDirectoryIterator $file_info */ + $relative_path = $iterator->getInnerIterator()->getSubPathname(); + $files[] = str_replace(DIRECTORY_SEPARATOR, '/', $relative_path); } return $files; diff --git a/phpBB/phpbb/event/recursive_event_filter_iterator.php b/phpBB/phpbb/event/recursive_event_filter_iterator.php new file mode 100644 index 0000000000..c520bdae4a --- /dev/null +++ b/phpBB/phpbb/event/recursive_event_filter_iterator.php @@ -0,0 +1,45 @@ +current()); + $filename = $this->current()->getFilename(); + + return (substr($relative_path, -4) === '.php' || $this->current()->isDir()) + && $filename[0] !== '.' + && strpos($relative_path, 'phpBB/cache/') !== 0 + && strpos($relative_path, 'phpBB/develop/') !== 0 + && strpos($relative_path, 'phpBB/ext/') !== 0 + && strpos($relative_path, 'phpBB/files/') !== 0 + && strpos($relative_path, 'phpBB/includes/utf/') !== 0 + && strpos($relative_path, 'phpBB/language/') !== 0 + && strpos($relative_path, 'phpBB/phpbb/db/migration/data/') !== 0 + && strpos($relative_path, 'phpBB/phpbb/event/') !== 0 + && strpos($relative_path, 'phpBB/store/') !== 0 + && strpos($relative_path, 'phpBB/vendor/') !== 0 + ; + } +} -- cgit v1.2.1 From dbac4bb5c07973a14469169a6bcc2feeb024306b Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Sun, 27 Apr 2014 23:51:06 +0200 Subject: [ticket/12273] Add root path to recursive_event_filter_iterator PHPBB3-12273 --- phpBB/phpbb/event/php_exporter.php | 3 +- .../event/recursive_event_filter_iterator.php | 43 +++++++++++++++++----- 2 files changed, 35 insertions(+), 11 deletions(-) (limited to 'phpBB/phpbb/event') diff --git a/phpBB/phpbb/event/php_exporter.php b/phpBB/phpbb/event/php_exporter.php index 8e4a9d399d..e95426244b 100644 --- a/phpBB/phpbb/event/php_exporter.php +++ b/phpBB/phpbb/event/php_exporter.php @@ -112,7 +112,8 @@ class php_exporter new \RecursiveDirectoryIterator( $dir, \FilesystemIterator::SKIP_DOTS - ) + ), + $dir ), \RecursiveIteratorIterator::LEAVES_ONLY ); diff --git a/phpBB/phpbb/event/recursive_event_filter_iterator.php b/phpBB/phpbb/event/recursive_event_filter_iterator.php index c520bdae4a..970c4bbd04 100644 --- a/phpBB/phpbb/event/recursive_event_filter_iterator.php +++ b/phpBB/phpbb/event/recursive_event_filter_iterator.php @@ -20,6 +20,29 @@ namespace phpbb\event; */ class recursive_event_filter_iterator extends \RecursiveFilterIterator { + protected $root_path; + + /** + * Construct + * + * @param \RecursiveIterator $iterator + * @param string $root_path + */ + public function __construct(\RecursiveIterator $iterator, $root_path) + { + $this->root_path = str_replace(DIRECTORY_SEPARATOR, '/', $root_path); + parent::__construct($iterator); + } + + /** + * Return the inner iterator's children contained in a recursive_event_filter_iterator + * + * @return recursive_event_filter_iterator + */ + public function getChildren() { + return new self($this->getInnerIterator()->getChildren(), $this->root_path); + } + /** * {@inheritDoc} */ @@ -30,16 +53,16 @@ class recursive_event_filter_iterator extends \RecursiveFilterIterator return (substr($relative_path, -4) === '.php' || $this->current()->isDir()) && $filename[0] !== '.' - && strpos($relative_path, 'phpBB/cache/') !== 0 - && strpos($relative_path, 'phpBB/develop/') !== 0 - && strpos($relative_path, 'phpBB/ext/') !== 0 - && strpos($relative_path, 'phpBB/files/') !== 0 - && strpos($relative_path, 'phpBB/includes/utf/') !== 0 - && strpos($relative_path, 'phpBB/language/') !== 0 - && strpos($relative_path, 'phpBB/phpbb/db/migration/data/') !== 0 - && strpos($relative_path, 'phpBB/phpbb/event/') !== 0 - && strpos($relative_path, 'phpBB/store/') !== 0 - && strpos($relative_path, 'phpBB/vendor/') !== 0 + && strpos($relative_path, $this->root_path . 'cache/') !== 0 + && strpos($relative_path, $this->root_path . 'develop/') !== 0 + && strpos($relative_path, $this->root_path . 'ext/') !== 0 + && strpos($relative_path, $this->root_path . 'files/') !== 0 + && strpos($relative_path, $this->root_path . 'includes/utf/') !== 0 + && strpos($relative_path, $this->root_path . 'language/') !== 0 + && strpos($relative_path, $this->root_path . 'phpbb/db/migration/data/') !== 0 + && strpos($relative_path, $this->root_path . 'phpbb/event/') !== 0 + && strpos($relative_path, $this->root_path . 'store/') !== 0 + && strpos($relative_path, $this->root_path . 'vendor/') !== 0 ; } } -- cgit v1.2.1 From 35f8b7f5ea606fd59ccfa4600e806b37e170c63a Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Sun, 27 Apr 2014 23:54:12 +0200 Subject: [ticket/12273] Fix table header for adm events PHPBB3-12273 --- phpBB/phpbb/event/md_exporter.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'phpBB/phpbb/event') diff --git a/phpBB/phpbb/event/md_exporter.php b/phpBB/phpbb/event/md_exporter.php index cb14ff5778..068207d08a 100644 --- a/phpBB/phpbb/event/md_exporter.php +++ b/phpBB/phpbb/event/md_exporter.php @@ -154,7 +154,7 @@ class md_exporter */ public function export_events_for_wiki() { - if ($this->filter === 'acp') + if ($this->filter === 'adm') { $wiki_page = '= ACP Template Events =' . "\n"; $wiki_page .= '{| class="zebra sortable" cellspacing="0" cellpadding="5"' . "\n"; -- cgit v1.2.1 From bf8b9f01437b4b6bff3e27291eafcd73dbcbdb0c Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Mon, 28 Apr 2014 00:06:14 +0200 Subject: [ticket/12273] Remove old parameter from function call PHPBB3-12273 --- phpBB/phpbb/event/md_exporter.php | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) (limited to 'phpBB/phpbb/event') diff --git a/phpBB/phpbb/event/md_exporter.php b/phpBB/phpbb/event/md_exporter.php index 068207d08a..415f001ac1 100644 --- a/phpBB/phpbb/event/md_exporter.php +++ b/phpBB/phpbb/event/md_exporter.php @@ -59,7 +59,7 @@ class md_exporter { $this->crawl_eventsmd($md_file, 'adm'); - $file_list = $this->get_recursive_file_list($this->root_path . 'adm/style/', 'adm/style/'); + $file_list = $this->get_recursive_file_list($this->root_path . 'adm/style/'); foreach ($file_list as $file) { $file_name = 'adm/style/' . $file; @@ -82,8 +82,7 @@ class md_exporter foreach ($styles as $style) { $file_list = $this->get_recursive_file_list( - $this->root_path . 'styles/' . $style . '/template/', - 'styles/' . $style . '/template/' + $this->root_path . 'styles/' . $style . '/template/' ); foreach ($file_list as $file) -- cgit v1.2.1 From ed669982c2a8225e262d57b60f5654658799becd Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Mon, 28 Apr 2014 21:03:59 +0200 Subject: [ticket/12273] Allow to filter events for extensions PHPBB3-12273 --- phpBB/phpbb/event/md_exporter.php | 48 ++++++++++++++++++++++++++------------ phpBB/phpbb/event/php_exporter.php | 26 ++++++++++++++------- 2 files changed, 51 insertions(+), 23 deletions(-) (limited to 'phpBB/phpbb/event') diff --git a/phpBB/phpbb/event/md_exporter.php b/phpBB/phpbb/event/md_exporter.php index 415f001ac1..574827ac44 100644 --- a/phpBB/phpbb/event/md_exporter.php +++ b/phpBB/phpbb/event/md_exporter.php @@ -17,7 +17,10 @@ namespace phpbb\event; */ class md_exporter { - /** @var string */ + /** @var string Path where we look for files*/ + protected $path; + + /** @var string phpBB Root Path */ protected $root_path; /** @var string */ @@ -51,15 +54,22 @@ class md_exporter } /** - * @param string $md_file + * @param string $md_file Relative from phpBB root + * @param mixed $extension String 'vendor/ext' to filter, null for phpBB core * @return int Number of events found * @throws \LogicException */ - public function crawl_phpbb_directory_adm($md_file) + public function crawl_phpbb_directory_adm($md_file, $extension = null) { - $this->crawl_eventsmd($md_file, 'adm'); + $this->path = $this->root_path; + if ($extension) + { + $this->path .= 'ext/' . $extension . '/'; + } - $file_list = $this->get_recursive_file_list($this->root_path . 'adm/style/'); + $this->crawl_eventsmd($md_file, 'adm', $extension); + + $file_list = $this->get_recursive_file_list($this->path . 'adm/style/'); foreach ($file_list as $file) { $file_name = 'adm/style/' . $file; @@ -70,19 +80,26 @@ class md_exporter } /** - * @param string $md_file + * @param string $md_file Relative from phpBB root + * @param mixed $extension String 'vendor/ext' to filter, null for phpBB core * @return int Number of events found * @throws \LogicException */ - public function crawl_phpbb_directory_styles($md_file) + public function crawl_phpbb_directory_styles($md_file, $extension = null) { - $this->crawl_eventsmd($md_file, 'styles'); + $this->path = $this->root_path; + if ($extension) + { + $this->path .= 'ext/' . $extension . '/'; + } + + $this->crawl_eventsmd($md_file, 'styles', $extension); $styles = array('prosilver', 'subsilver2'); foreach ($styles as $style) { $file_list = $this->get_recursive_file_list( - $this->root_path . 'styles/' . $style . '/template/' + $this->path . 'styles/' . $style . '/template/' ); foreach ($file_list as $file) @@ -96,12 +113,13 @@ class md_exporter } /** - * @param string $md_file - * @param string $filter + * @param string $md_file Relative from phpBB root + * @param string $filter Should be 'styles' or 'adm' + * @param mixed $extension String 'vendor/ext' to filter, null for phpBB core * @return int Number of events found * @throws \LogicException */ - public function crawl_eventsmd($md_file, $filter) + public function crawl_eventsmd($md_file, $filter, $extension = null) { $file_content = file_get_contents($this->root_path . $md_file); $this->filter = $filter; @@ -241,7 +259,7 @@ class md_exporter $files = explode("\n + ", $file_details); foreach ($files as $file) { - if (!file_exists($this->root_path . $file)) + if (!file_exists($this->path . $file)) { throw new \LogicException("Invalid file '{$file}' not found for event '{$this->current_event}'", 1); } @@ -290,13 +308,13 @@ class md_exporter */ public function crawl_file_for_events($file) { - if (!file_exists($this->root_path . $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->root_path . $file); + $file_content = file_get_contents($this->path . $file); $events = explode('