From 22de4a5927eccf05d614a6da7f6eb6f993838487 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Thu, 17 Apr 2014 15:00:14 +0200 Subject: [ticket/12273] Add a basic set of tests for the exporter PHPBB3-12273 --- phpBB/develop/event_exporter.php | 424 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 424 insertions(+) create mode 100644 phpBB/develop/event_exporter.php (limited to 'phpBB/develop') diff --git a/phpBB/develop/event_exporter.php b/phpBB/develop/event_exporter.php new file mode 100644 index 0000000000..6277433423 --- /dev/null +++ b/phpBB/develop/event_exporter.php @@ -0,0 +1,424 @@ +root_path = $phpbb_root_path; + } + + 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"; + + } + } + + function export_from_php() + { + $files = $this->get_file_list($this->root_path); + $events = array(); + foreach ($files as $file) + { + $file_events = $this->check_for_events($file); + if (!empty($file_events)) + { + $events = array_merge($events, $file_events); + } + } + ksort($events); + + foreach ($events as $event) + { + echo '|- id="' . $event['event'] . '"' . "\n"; + echo '| [[#' . $event['event'] . '|' . $event['event'] . ']] || ' . $event['file'] . ' || ' . implode(', ', $event['arguments']) . ' || ' . $event['since'] . ' || ' . $event['description'] . "\n"; + } + } + + public function check_for_events($file) + { + $events = array(); + $content = file_get_contents($this->root_path . $file); + + if (strpos($content, "phpbb_dispatcher->trigger_event('") || strpos($content, "phpbb_dispatcher->dispatch('")) + { + $lines = explode("\n", $content); + for ($i = 0, $num_lines = sizeof($lines); $i < $num_lines; $i++) + { + $event_line = 0; + $found_trigger_event = strpos($lines[$i], "phpbb_dispatcher->trigger_event('"); + if ($found_trigger_event !== false) + { + $event_line = $i; + $event_name = $lines[$event_line]; + $event_name = substr($event_name, $found_trigger_event + strlen("phpbb_dispatcher->trigger_event('")); + $event_name = substr($event_name, 0, strpos($event_name, "'")); + + $current_line = trim($lines[$event_line]); + $arguments = array(); + $found_inline_array = strpos($current_line, "', compact(array('"); + if ($found_inline_array !== false) + { + $varsarray = substr($current_line, $found_inline_array + strlen("', compact(array('"), -6); + $arguments = explode("', '", $varsarray); + } + + if (empty($arguments)) + { + // Find $vars array lines + $find_varsarray_line = 1; + while (strpos($lines[$event_line - $find_varsarray_line], "\$vars = array('") === false) + { + $find_varsarray_line++; + + if ($find_varsarray_line > min(50, $event_line)) + { + throw new LogicException('Can not find "$vars = array()"-line for event "' . $event_name . '" in file "' . $file . '"'); + } + } + $varsarray = substr(trim($lines[$event_line - $find_varsarray_line]), strlen("\$vars = array('"), -3); + $arguments = explode("', '", $varsarray); + } + + // Validate $vars array with @var + $find_vars_line = 3; + $doc_vars = array(); + while (strpos(trim($lines[$event_line - $find_vars_line]), '*') === 0) + { + $var_line = trim($lines[$event_line - $find_vars_line]); + $var_line = preg_replace('!\s+!', ' ', $var_line); + if (strpos($var_line, '* @var ') === 0) + { + $doc_line = explode(' ', $var_line); + if (isset($doc_line[3])) + { + $doc_vars[] = $doc_line[3]; + } + } + $find_vars_line++; + } + if (sizeof($arguments) !== sizeof($doc_vars) && array_intersect($arguments, $doc_vars)) + { + throw new LogicException('$vars array does not match the list of @var tags for event "' . $event_name . '" in file "' . $file . '"'); + } + } + $found_dispatch = strpos($lines[$i], "phpbb_dispatcher->dispatch('"); + if ($found_dispatch !== false) + { + $event_line = $i; + $event_name = $lines[$event_line]; + $event_name = substr($event_name, $found_dispatch + strlen("phpbb_dispatcher->dispatch('")); + $event_name = substr($event_name, 0, strpos($event_name, "'")); + $arguments = array(); + } + + if ($event_line) + { + // Validate @event + $event_line_num = $this->find_event($file, $event_name, $lines, $event_line); + $this->validate_event($file, $event_name, $lines[$event_line_num]); + + // Validate @since + $since_line_num = $this->find_since($file, $event_name, $lines, $event_line); + $since = $this->validate_since($file, $event_name, $lines[$since_line_num]); + + // Find event description line + $description_line_num = $this->find_description($file, $event_name, $lines, $event_line); + $description = substr(trim($lines[$description_line_num]), strlen('* ')); + + $events[$event_name] = array( + 'event' => $event_name, + 'file' => $file, + 'arguments' => $arguments, + 'since' => $since, + 'description' => $description, + ); + } + } + } + + return $events; + } + + /** + * Find the "@since" Information line + * + * @param string $file + * @param string $event_name + * @param string $lines + * @param int $event_line Index of the event call in $lines + * @return int Absolute line number + */ + public function find_since($file, $event_name, $lines, $event_line) + { + return $this->find_tag($file, $event_name, $lines, $event_line, 'since', array('event', 'var')); + } + + /** + * Find the "@event" Information line + * + * @param string $file + * @param string $event_name + * @param string $lines + * @param int $event_line Index of the event call in $lines + * @return int Absolute line number + */ + public function find_event($file, $event_name, $lines, $event_line) + { + return $this->find_tag($file, $event_name, $lines, $event_line, 'event', array()); + } + + /** + * Find a "@*" Information line + * + * @param string $file + * @param string $event_name + * @param string $lines + * @param int $event_line Index of the event call in $lines + * @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 + */ + public function find_tag($file, $event_name, $lines, $event_line, $find_tag, $disallowed_tags) + { + $find_tag_line = 0; + $found_comment_end = false; + while (strpos(ltrim($lines[$event_line - $find_tag_line], "\t"), '* @' . $find_tag . ' ') !== 0) + { + if ($found_comment_end && ltrim($lines[$event_line - $find_tag_line], "\t") === '/**') + { + // Reached the start of this doc block + throw new LogicException('Can not find @' . $find_tag . ' information for event "' . $event_name . '" in file "' . $file . '"', 1); + } + + foreach ($disallowed_tags as $disallowed_tag) + { + if ($found_comment_end && strpos(ltrim($lines[$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 "' . $event_name . '" in file "' . $file . '"', 3); + } + } + + if (ltrim($lines[$event_line - $find_tag_line], "\t") === '*/') + { + $found_comment_end = true; + } + + $find_tag_line++; + if ($find_tag_line >= $event_line) + { + // Reached the start of the file + throw new LogicException('Can not find @' . $find_tag . ' information for event "' . $event_name . '" in file "' . $file . '"', 2); + } + } + + return $event_line - $find_tag_line; + } + + /** + * Find a "@*" Information line + * + * @param string $file + * @param string $event_name + * @param string $lines + * @param int $event_line Index of the event call in $lines + * @return int Absolute line number + */ + public function find_description($file, $event_name, $lines, $event_line) + { + $find_desc_line = 0; + while (ltrim($lines[$event_line - $find_desc_line], "\t") !== '/**') + { + $find_desc_line++; + if ($find_desc_line > $event_line) + { + // Reached the start of the file + throw new LogicException('Can not find a description for event "' . $event_name . '" in file "' . $file . '"', 1); + } + } + + $find_desc_line = $event_line - $find_desc_line + 1; + + $desc = trim($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 "' . $event_name . '" in file "' . $file . '"', 2); + } + + return $find_desc_line; + } + + /** + * Validate "@since" Information + * + * @param string $file + * @param string $event_name + * @param string $line + * @return string + */ + public function validate_since($file, $event_name, $line) + { + $since = substr(ltrim($line, "\t"), strlen('* @since ')); + + if ($since !== trim($since)) + { + throw new LogicException('Invalid @since information for event "' . $event_name . '" in file "' . $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 "' . $event_name . '" in file "' . $file . '"', 2); + } + + return $since; + } + + /** + * Validate "@event" Information + * + * @param string $file + * @param string $event_name + * @param string $line + * @return string + */ + public function validate_event($file, $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 "' . $file . '"', 1); + } + + if ($event !== $event_name) + { + throw new LogicException('Event name does not match @event tag for event "' . $event_name . '" in file "' . $file . '"', 2); + } + + return $event; + } + + /** + * Returns a list of files in that directory + * + * Works recursive with any depth + * + * @param string $dir Directory to go through + * @return array List of files (including directories from within $dir + */ + function get_file_list($dir, $path = '') + { + try + { + $iterator = new \DirectoryIterator($dir); + } + catch (Exception $e) + { + return array(); + } + + $files = array(); + foreach ($iterator as $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_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(); + } + } + + return $files; + } +} -- cgit v1.2.1 From 6c57edf3a3f9ff260ef0b1d5dbe7db004dd9a51d Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Thu, 17 Apr 2014 15:03:48 +0200 Subject: [ticket/12273] Use the new class PHPBB3-12273 --- phpBB/develop/export_events_for_wiki.php | 280 +------------------------------ 1 file changed, 5 insertions(+), 275 deletions(-) (limited to 'phpBB/develop') diff --git a/phpBB/develop/export_events_for_wiki.php b/phpBB/develop/export_events_for_wiki.php index 3021b64e05..0c4f14a4b4 100644 --- a/phpBB/develop/export_events_for_wiki.php +++ b/phpBB/develop/export_events_for_wiki.php @@ -13,6 +13,7 @@ if (php_sapi_name() != 'cli') $phpEx = substr(strrchr(__FILE__, '.'), 1); $phpbb_root_path = __DIR__ . '/../'; +require __DIR__ . '/event_exporter.' . $phpEx; function usage() { @@ -29,278 +30,6 @@ function usage() exit(2); } -function export_from_eventsmd($phpbb_root_path, $filter) -{ - $file_content = file_get_contents($phpbb_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"; - - } -} - -function export_from_php($phpbb_root_path) -{ - $files = get_file_list($phpbb_root_path); - $events = array(); - foreach ($files as $file) - { - $file_events = check_for_events($phpbb_root_path, $file); - if (!empty($file_events)) - { - $events = array_merge($events, $file_events); - } - } - - ksort($events); - - foreach ($events as $event) - { - echo '|- id="' . $event['event'] . '"' . "\n"; - echo '| [[#' . $event['event'] . '|' . $event['event'] . ']] || ' . $event['file'] . ' || ' . implode(', ', $event['arguments']) . ' || ' . $event['since'] . ' || ' . $event['description'] . "\n"; - } -} - -function check_for_events($phpbb_root_path, $file) -{ - $events = array(); - $content = file_get_contents($phpbb_root_path . $file); - - if (strpos($content, "phpbb_dispatcher->trigger_event('") || strpos($content, "phpbb_dispatcher->dispatch('")) - { - $lines = explode("\n", $content); - for ($i = 0, $num_lines = sizeof($lines); $i < $num_lines; $i++) - { - $event_line = 0; - $found_trigger_event = strpos($lines[$i], "phpbb_dispatcher->trigger_event('"); - if ($found_trigger_event !== false) - { - $event_line = $i; - $event_name = $lines[$event_line]; - $event_name = substr($event_name, $found_trigger_event + strlen("phpbb_dispatcher->trigger_event('")); - $event_name = substr($event_name, 0, strpos($event_name, "'")); - - $current_line = trim($lines[$event_line]); - $arguments = array(); - $found_inline_array = strpos($current_line, "', compact(array('"); - if ($found_inline_array !== false) - { - $varsarray = substr($current_line, $found_inline_array + strlen("', compact(array('"), -6); - $arguments = explode("', '", $varsarray); - } - - if (empty($arguments)) - { - // Find $vars array lines - $find_varsarray_line = 1; - while (strpos($lines[$event_line - $find_varsarray_line], "vars = array('") === false) - { - $find_varsarray_line++; - - if ($find_varsarray_line > min(50, $event_line)) - { - throw new LogicException('Can not find "$vars = array()"-line for event "' . $event_name . '" in file "' . $file . '"'); - } - } - $varsarray = substr(trim($lines[$event_line - $find_varsarray_line]), strlen("\$vars = array('"), -3); - $arguments = explode("', '", $varsarray); - } - - // Validate $vars array with @var - $find_vars_line = 3; - $doc_vars = array(); - while (strpos(trim($lines[$event_line - $find_vars_line]), '*') === 0) - { - $var_line = trim($lines[$event_line - $find_vars_line]); - $var_line = preg_replace('!\s+!', ' ', $var_line); - if (strpos($var_line, '* @var ') === 0) - { - $doc_line = explode(' ', $var_line); - if (isset($doc_line[3])) - { - $doc_vars[] = $doc_line[3]; - } - } - $find_vars_line++; - } - if (sizeof($arguments) !== sizeof($doc_vars) && array_intersect($arguments, $doc_vars)) - { - throw new LogicException('$vars array does not match the list of @var tags for event "' . $event_name . '" in file "' . $file . '"'); - } - } - $found_dispatch = strpos($lines[$i], "phpbb_dispatcher->dispatch('"); - if ($found_dispatch !== false) - { - $event_line = $i; - $event_name = $lines[$event_line]; - $event_name = substr($event_name, $found_dispatch + strlen("phpbb_dispatcher->dispatch('")); - $event_name = substr($event_name, 0, strpos($event_name, "'")); - $arguments = array(); - } - - if ($event_line) - { - // Validate @event name - $find_event_line = 1; - while (strpos($lines[$event_line - $find_event_line], '* @event ') === false) - { - $find_event_line++; - - if ($find_event_line > min(50, $event_line)) - { - throw new LogicException('Can not find @event tag for event "' . $event_name . '" in file "' . $file . '"'); - } - } - $event_name_tag = substr(trim($lines[$event_line - $find_event_line]), strlen('* @event ')); - if ($event_name_tag !== $event_name) - { - throw new LogicException('Event name does not match @event tag for event "' . $event_name . '" in file "' . $file . '"'); - } - - // Find @since - $find_since_line = 1; - while (strpos($lines[$event_line - $find_since_line], '* @since ') === false) - { - $find_since_line++; - - if ($find_since_line > min(50, $event_line)) - { - throw new LogicException('Can not find @since tag for event "' . $event_name . '" in file "' . $file . '"'); - } - } - $since = substr(trim($lines[$event_line - $find_since_line]), strlen('* @since ')); - $since = ($since == '3.1-A1') ? '3.1.0-a1' : $since; - - // Find event description line - $find_description_line = 3; - while (strpos(trim($lines[$event_line - $find_description_line]), '*') === 0) - { - $find_description_line++; - - if ($find_description_line > min(50, $event_line)) - { - throw new LogicException('Can not find description-line for event "' . $event_name . '" in file "' . $file . '"'); - } - } - $description = substr(trim($lines[$event_line - $find_description_line + 1]), strlen('* ')); - - $events[$event_name] = array( - 'event' => $event_name, - 'file' => $file, - 'arguments' => $arguments, - 'since' => $since, - 'description' => $description, - ); - } - } - } - - return $events; -} - -/** -* Returns a list of files in that directory -* -* Works recursive with any depth -* -* @param string $dir Directory to go through -* @return array List of files (including directories from within $dir -*/ -function get_file_list($dir, $path = '') -{ - try - { - $iterator = new \DirectoryIterator($dir); - } - catch (Exception $e) - { - return array(); - } - - $files = array(); - foreach ($iterator as $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 = get_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(); - } - } - - return $files; -} - function validate_argument_count($arguments, $count) { if ($arguments <= $count) @@ -312,19 +41,20 @@ function validate_argument_count($arguments, $count) validate_argument_count($argc, 1); $action = $argv[1]; +$exporter = new \event_exporter($phpbb_root_path); switch ($action) { case 'acp': - export_from_eventsmd($phpbb_root_path, 'acp'); + $exporter->export_from_eventsmd('acp'); break; case 'styles': - export_from_eventsmd($phpbb_root_path, 'styles'); + $exporter->export_from_eventsmd('styles'); break; case 'php': - export_from_php($phpbb_root_path); + $exporter->export_from_php(); break; default: -- cgit v1.2.1 From 5387511f89dbb8e7a8edb70b90f5f510aad8779b Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Thu, 17 Apr 2014 16:55:36 +0200 Subject: [ticket/12273] Grab name of events with a function PHPBB3-12273 --- phpBB/develop/event_exporter.php | 115 +++++++++++++++++++++++++++------------ 1 file changed, 80 insertions(+), 35 deletions(-) (limited to 'phpBB/develop') diff --git a/phpBB/develop/event_exporter.php b/phpBB/develop/event_exporter.php index 6277433423..3b4f054ad0 100644 --- a/phpBB/develop/event_exporter.php +++ b/phpBB/develop/event_exporter.php @@ -105,45 +105,26 @@ class event_exporter $events = array(); $content = file_get_contents($this->root_path . $file); - if (strpos($content, "phpbb_dispatcher->trigger_event('") || strpos($content, "phpbb_dispatcher->dispatch('")) + if (strpos($content, "dispatcher->trigger_event('") || strpos($content, "dispatcher->dispatch('")) { $lines = explode("\n", $content); for ($i = 0, $num_lines = sizeof($lines); $i < $num_lines; $i++) { $event_line = 0; - $found_trigger_event = strpos($lines[$i], "phpbb_dispatcher->trigger_event('"); + $found_trigger_event = strpos($lines[$i], "dispatcher->trigger_event('"); if ($found_trigger_event !== false) { $event_line = $i; - $event_name = $lines[$event_line]; - $event_name = substr($event_name, $found_trigger_event + strlen("phpbb_dispatcher->trigger_event('")); - $event_name = substr($event_name, 0, strpos($event_name, "'")); - - $current_line = trim($lines[$event_line]); - $arguments = array(); - $found_inline_array = strpos($current_line, "', compact(array('"); - if ($found_inline_array !== false) - { - $varsarray = substr($current_line, $found_inline_array + strlen("', compact(array('"), -6); - $arguments = explode("', '", $varsarray); - } + $event_name = $this->get_trigger_event_name($file, $lines[$event_line]); - if (empty($arguments)) + // Find $vars array lines + $vars_line = ltrim($lines[$event_line - 1], "\t"); + if (strpos($vars_line, "\$vars = array('") !== 0) { - // Find $vars array lines - $find_varsarray_line = 1; - while (strpos($lines[$event_line - $find_varsarray_line], "\$vars = array('") === false) - { - $find_varsarray_line++; - - if ($find_varsarray_line > min(50, $event_line)) - { - throw new LogicException('Can not find "$vars = array()"-line for event "' . $event_name . '" in file "' . $file . '"'); - } - } - $varsarray = substr(trim($lines[$event_line - $find_varsarray_line]), strlen("\$vars = array('"), -3); - $arguments = explode("', '", $varsarray); + throw new LogicException('Can not find "$vars = array()"-line for event "' . $event_name . '" in file "' . $file . '"'); } + $varsarray = substr($vars_line, strlen("\$vars = array('"), 0 - strlen('\');')); + $arguments = explode("', '", $varsarray); // Validate $vars array with @var $find_vars_line = 3; @@ -167,14 +148,16 @@ class event_exporter throw new LogicException('$vars array does not match the list of @var tags for event "' . $event_name . '" in file "' . $file . '"'); } } - $found_dispatch = strpos($lines[$i], "phpbb_dispatcher->dispatch('"); - if ($found_dispatch !== false) + + if (!$event_line) { - $event_line = $i; - $event_name = $lines[$event_line]; - $event_name = substr($event_name, $found_dispatch + strlen("phpbb_dispatcher->dispatch('")); - $event_name = substr($event_name, 0, strpos($event_name, "'")); - $arguments = array(); + $found_dispatch = strpos($lines[$i], "dispatcher->dispatch('"); + if ($found_dispatch !== false) + { + $event_line = $i; + $event_name = $this->get_dispatch_name($file, $lines[$event_line]); + $arguments = array(); + } } if ($event_line) @@ -205,6 +188,68 @@ class event_exporter return $events; } + /** + * Find the name of the event inside the dispatch() line + * + * @param string $file + * @param string $event_line + * @return int Absolute line number + */ + public function get_dispatch_name($file, $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 "' . $file . '"', 1); + } + + return $match[2]; + } + + /** + * Find the name of the event inside the trigger_event() line + * + * @param string $file + * @param string $event_line + * @return int Absolute line number + */ + public function get_trigger_event_name($file, $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 "' . $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 "@since" Information line * -- cgit v1.2.1 From 4a3756741c9d1689430c557166a43d79739e98fe Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Thu, 17 Apr 2014 18:21:18 +0200 Subject: [ticket/12273] Move grabbing the $vars array line to a method PHPBB3-12273 --- phpBB/develop/event_exporter.php | 60 ++++++++++++++++++++++++++++++---------- 1 file changed, 45 insertions(+), 15 deletions(-) (limited to 'phpBB/develop') diff --git a/phpBB/develop/event_exporter.php b/phpBB/develop/event_exporter.php index 3b4f054ad0..644063df2b 100644 --- a/phpBB/develop/event_exporter.php +++ b/phpBB/develop/event_exporter.php @@ -110,21 +110,15 @@ class event_exporter $lines = explode("\n", $content); for ($i = 0, $num_lines = sizeof($lines); $i < $num_lines; $i++) { - $event_line = 0; + $event_line = false; $found_trigger_event = strpos($lines[$i], "dispatcher->trigger_event('"); if ($found_trigger_event !== false) { $event_line = $i; $event_name = $this->get_trigger_event_name($file, $lines[$event_line]); - // Find $vars array lines - $vars_line = ltrim($lines[$event_line - 1], "\t"); - if (strpos($vars_line, "\$vars = array('") !== 0) - { - throw new LogicException('Can not find "$vars = array()"-line for event "' . $event_name . '" in file "' . $file . '"'); - } - $varsarray = substr($vars_line, strlen("\$vars = array('"), 0 - strlen('\');')); - $arguments = explode("', '", $varsarray); + // Find $vars array + $arguments = $this->get_vars_from_array($file, $event_name, $lines, $event_line); // Validate $vars array with @var $find_vars_line = 3; @@ -143,13 +137,13 @@ class event_exporter } $find_vars_line++; } + if (sizeof($arguments) !== sizeof($doc_vars) && array_intersect($arguments, $doc_vars)) { throw new LogicException('$vars array does not match the list of @var tags for event "' . $event_name . '" in file "' . $file . '"'); } } - - if (!$event_line) + else { $found_dispatch = strpos($lines[$i], "dispatcher->dispatch('"); if ($found_dispatch !== false) @@ -250,12 +244,48 @@ class event_exporter return '([a-z][a-z0-9_]*(?:\.[a-z][a-z0-9_]*)+)'; } + /** + * Find the $vars array + * + * @param string $file + * @param string $event_name + * @param array $lines + * @param int $event_line Index of the event call in $lines + * @return array List of variables + */ + public function get_vars_from_array($file, $event_name, $lines, $event_line) + { + $vars_line = ltrim($lines[$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 "' . $event_name . '" in file "' . $file . '"', 1); + } + + $vars_array = substr($vars_line, strlen("\$vars = array('"), 0 - strlen('\');')); + if ($vars_array === '') + { + throw new LogicException('Found empty $vars array for event "' . $event_name . '" in file "' . $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 "' . $event_name . '" in file "' . $file . '"', 3); + } + } + + return $vars_array; + } + /** * Find the "@since" Information line * * @param string $file * @param string $event_name - * @param string $lines + * @param array $lines * @param int $event_line Index of the event call in $lines * @return int Absolute line number */ @@ -269,7 +299,7 @@ class event_exporter * * @param string $file * @param string $event_name - * @param string $lines + * @param array $lines * @param int $event_line Index of the event call in $lines * @return int Absolute line number */ @@ -283,7 +313,7 @@ class event_exporter * * @param string $file * @param string $event_name - * @param string $lines + * @param array $lines * @param int $event_line Index of the event call in $lines * @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 @@ -332,7 +362,7 @@ class event_exporter * * @param string $file * @param string $event_name - * @param string $lines + * @param array $lines * @param int $event_line Index of the event call in $lines * @return int Absolute line number */ -- cgit v1.2.1 From 91deb4419b3f7c93288e969cf97fb9e1541c2ecf Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Fri, 18 Apr 2014 00:31:06 +0200 Subject: [ticket/12273] Add more tests for the event exporter PHPBB3-12273 --- phpBB/develop/event_exporter.php | 112 ++++++++++++++++++++++++++++++--------- 1 file changed, 88 insertions(+), 24 deletions(-) (limited to 'phpBB/develop') diff --git a/phpBB/develop/event_exporter.php b/phpBB/develop/event_exporter.php index 644063df2b..c17e714764 100644 --- a/phpBB/develop/event_exporter.php +++ b/phpBB/develop/event_exporter.php @@ -117,31 +117,10 @@ class event_exporter $event_line = $i; $event_name = $this->get_trigger_event_name($file, $lines[$event_line]); - // Find $vars array + // Find variables of the event $arguments = $this->get_vars_from_array($file, $event_name, $lines, $event_line); - - // Validate $vars array with @var - $find_vars_line = 3; - $doc_vars = array(); - while (strpos(trim($lines[$event_line - $find_vars_line]), '*') === 0) - { - $var_line = trim($lines[$event_line - $find_vars_line]); - $var_line = preg_replace('!\s+!', ' ', $var_line); - if (strpos($var_line, '* @var ') === 0) - { - $doc_line = explode(' ', $var_line); - if (isset($doc_line[3])) - { - $doc_vars[] = $doc_line[3]; - } - } - $find_vars_line++; - } - - if (sizeof($arguments) !== sizeof($doc_vars) && array_intersect($arguments, $doc_vars)) - { - throw new LogicException('$vars array does not match the list of @var tags for event "' . $event_name . '" in file "' . $file . '"'); - } + $doc_vars = $this->get_vars_from_docblock($file, $event_name, $lines, $event_line); + $this->validate_vars_docblock_array($file, $event_name, $arguments, $doc_vars); } else { @@ -277,9 +256,73 @@ class event_exporter } } + sort($vars_array); return $vars_array; } + /** + * Find the $vars array + * + * @param string $file + * @param string $event_name + * @param array $lines + * @param int $event_line Index of the event call in $lines + * @return array List of variables + */ + public function get_vars_from_docblock($file, $event_name, $lines, $event_line) + { + $doc_vars = array(); + $current_doc_line = 1; + $found_comment_end = false; + while (ltrim($lines[$event_line - $current_doc_line], "\t") !== '/**') + { + if (ltrim($lines[$event_line - $current_doc_line], "\t") === '*/') + { + $found_comment_end = true; + } + + if ($found_comment_end) + { + $var_line = trim($lines[$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 "' . $lines[$event_line - $current_doc_line] + . '" for event "' . $event_name . '" in file "' . $file . '"', 1); + } + $doc_vars[] = $doc_line[3]; + } + } + + $current_doc_line++; + if ($current_doc_line > $event_line) + { + // Reached the start of the file + throw new LogicException('Can not find end of docblock for event "' . $event_name . '" in file "' . $file . '"', 2); + } + } + + if (empty($doc_vars)) + { + // Reached the start of the file + throw new LogicException('Can not find @var lines for event "' . $event_name . '" in file "' . $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 "' . $event_name . '" in file "' . $file . '"', 4); + } + } + + sort($doc_vars); + return $doc_vars; + } + /** * Find the "@since" Information line * @@ -443,6 +486,27 @@ class event_exporter return $event; } + /** + * Validates that two arrays contain the same strings + * + * @param string $file + * @param string $event_name + * @param array $vars_array Variables found in the array line + * @param array $vars_docblock Variables found in the doc block + * @return null + */ + public function validate_vars_docblock_array($file, $event_name, $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 "' . $event_name . '" in file "' . $file . '"'); + } + } + /** * Returns a list of files in that directory * -- cgit v1.2.1 From b831e96aafc4b8c2bc883dd47c4f2452f329e5b2 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Fri, 18 Apr 2014 02:08:23 +0200 Subject: [ticket/12273] Use class properties instead of parameters PHPBB3-12273 --- phpBB/develop/event_exporter.php | 237 +++++++++++++++++++++------------------ 1 file changed, 128 insertions(+), 109 deletions(-) (limited to 'phpBB/develop') diff --git a/phpBB/develop/event_exporter.php b/phpBB/develop/event_exporter.php index c17e714764..a84b18db26 100644 --- a/phpBB/develop/event_exporter.php +++ b/phpBB/develop/event_exporter.php @@ -12,12 +12,30 @@ class event_exporter /** @var string */ protected $root_path; + /** @var string */ + protected $current_file; + + /** @var string */ + protected $current_event; + + /** @var int */ + protected $current_event_line; + + /** @var array */ + protected $events; + + /** @var array */ + protected $file_lines; + /** * @param string $phpbb_root_path */ public function __construct($phpbb_root_path) { $this->root_path = $phpbb_root_path; + $this->events = $this->file_lines = array(); + $this->current_file = $this->current_event = ''; + $this->current_event_line = 0; } function export_from_eventsmd($filter) @@ -82,53 +100,70 @@ class event_exporter function export_from_php() { $files = $this->get_file_list($this->root_path); - $events = array(); + $this->events = array(); foreach ($files as $file) { - $file_events = $this->check_for_events($file); - if (!empty($file_events)) - { - $events = array_merge($events, $file_events); - } + $this->check_for_events($file); } - ksort($events); + ksort($this->events); - foreach ($events as $event) + foreach ($this->events as $event) { echo '|- id="' . $event['event'] . '"' . "\n"; echo '| [[#' . $event['event'] . '|' . $event['event'] . ']] || ' . $event['file'] . ' || ' . implode(', ', $event['arguments']) . ' || ' . $event['since'] . ' || ' . $event['description'] . "\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; + } + + /** + * @param $file + * @throws LogicException + */ public function check_for_events($file) { - $events = array(); - $content = file_get_contents($this->root_path . $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('")) { - $lines = explode("\n", $content); - for ($i = 0, $num_lines = sizeof($lines); $i < $num_lines; $i++) + $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($lines[$i], "dispatcher->trigger_event('"); + $found_trigger_event = strpos($this->file_lines[$i], "dispatcher->trigger_event('"); if ($found_trigger_event !== false) { $event_line = $i; - $event_name = $this->get_trigger_event_name($file, $lines[$event_line]); + $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($file, $event_name, $lines, $event_line); - $doc_vars = $this->get_vars_from_docblock($file, $event_name, $lines, $event_line); - $this->validate_vars_docblock_array($file, $event_name, $arguments, $doc_vars); + $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($lines[$i], "dispatcher->dispatch('"); + $found_dispatch = strpos($this->file_lines[$i], "dispatcher->dispatch('"); if ($found_dispatch !== false) { $event_line = $i; - $event_name = $this->get_dispatch_name($file, $lines[$event_line]); + $this->set_current_event($this->get_dispatch_name($this->file_lines[$event_line]), $event_line); $arguments = array(); } } @@ -136,20 +171,26 @@ class event_exporter if ($event_line) { // Validate @event - $event_line_num = $this->find_event($file, $event_name, $lines, $event_line); - $this->validate_event($file, $event_name, $lines[$event_line_num]); + $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($file, $event_name, $lines, $event_line); - $since = $this->validate_since($file, $event_name, $lines[$since_line_num]); + $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($file, $event_name, $lines, $event_line); - $description = substr(trim($lines[$description_line_num]), strlen('* ')); + $description_line_num = $this->find_description(); + $description = substr(trim($this->file_lines[$description_line_num]), strlen('* ')); - $events[$event_name] = array( - 'event' => $event_name, - 'file' => $file, + 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, @@ -157,18 +198,16 @@ class event_exporter } } } - - return $events; } /** * Find the name of the event inside the dispatch() line * - * @param string $file * @param string $event_line * @return int Absolute line number + * @throws LogicException */ - public function get_dispatch_name($file, $event_line) + public function get_dispatch_name($event_line) { $event_line = ltrim($event_line, "\t"); @@ -181,7 +220,7 @@ class event_exporter preg_match($regex, $event_line, $match); if (!isset($match[2])) { - throw new LogicException('Can not find event name in line "' . $event_line . '" in file "' . $file . '"', 1); + throw new LogicException('Can not find event name in line "' . $event_line . '" in file "' . $this->current_file . '"', 1); } return $match[2]; @@ -190,11 +229,11 @@ class event_exporter /** * Find the name of the event inside the trigger_event() line * - * @param string $file * @param string $event_line * @return int Absolute line number + * @throws LogicException */ - public function get_trigger_event_name($file, $event_line) + public function get_trigger_event_name($event_line) { $event_line = ltrim($event_line, "\t"); @@ -207,7 +246,7 @@ class event_exporter preg_match($regex, $event_line, $match); if (!isset($match[2])) { - throw new LogicException('Can not find event name in line "' . $event_line . '" in file "' . $file . '"', 1); + throw new LogicException('Can not find event name in line "' . $event_line . '" in file "' . $this->current_file . '"', 1); } return $match[2]; @@ -226,24 +265,21 @@ class event_exporter /** * Find the $vars array * - * @param string $file - * @param string $event_name - * @param array $lines - * @param int $event_line Index of the event call in $lines * @return array List of variables + * @throws LogicException */ - public function get_vars_from_array($file, $event_name, $lines, $event_line) + public function get_vars_from_array() { - $vars_line = ltrim($lines[$event_line - 1], "\t"); + $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 "' . $event_name . '" in file "' . $file . '"', 1); + 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 "' . $event_name . '" in file "' . $file . '"', 2); + throw new LogicException('Found empty $vars array for event "' . $this->current_event . '" in file "' . $this->current_file . '"', 2); } $vars_array = explode("', '", $vars_array); @@ -252,7 +288,7 @@ class event_exporter { if (!preg_match('#^([a-zA-Z_][a-zA-Z0-9_]*)$#', $var)) { - throw new LogicException('Found invalid var "' . $var . '" in array for event "' . $event_name . '" in file "' . $file . '"', 3); + throw new LogicException('Found invalid var "' . $var . '" in array for event "' . $this->current_event . '" in file "' . $this->current_file . '"', 3); } } @@ -263,59 +299,56 @@ class event_exporter /** * Find the $vars array * - * @param string $file - * @param string $event_name - * @param array $lines - * @param int $event_line Index of the event call in $lines * @return array List of variables + * @throws LogicException */ - public function get_vars_from_docblock($file, $event_name, $lines, $event_line) + public function get_vars_from_docblock() { $doc_vars = array(); $current_doc_line = 1; $found_comment_end = false; - while (ltrim($lines[$event_line - $current_doc_line], "\t") !== '/**') + while (ltrim($this->file_lines[$this->current_event_line - $current_doc_line], "\t") !== '/**') { - if (ltrim($lines[$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($lines[$event_line - $current_doc_line]); + $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 "' . $lines[$event_line - $current_doc_line] - . '" for event "' . $event_name . '" in file "' . $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 . '"', 1); } $doc_vars[] = $doc_line[3]; } } $current_doc_line++; - if ($current_doc_line > $event_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 "' . $event_name . '" in file "' . $file . '"', 2); + 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 "' . $event_name . '" in file "' . $file . '"', 3); + 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 "' . $event_name . '" in file "' . $file . '"', 4); + throw new LogicException('Found invalid @var "' . $var . '" in docblock for event "' . $this->current_event . '" in file "' . $this->current_file . '"', 4); } } @@ -326,109 +359,96 @@ class event_exporter /** * Find the "@since" Information line * - * @param string $file - * @param string $event_name - * @param array $lines - * @param int $event_line Index of the event call in $lines * @return int Absolute line number + * @throws LogicException */ - public function find_since($file, $event_name, $lines, $event_line) + public function find_since() { - return $this->find_tag($file, $event_name, $lines, $event_line, 'since', array('event', 'var')); + return $this->find_tag('since', array('event', 'var')); } /** * Find the "@event" Information line * - * @param string $file - * @param string $event_name - * @param array $lines - * @param int $event_line Index of the event call in $lines * @return int Absolute line number */ - public function find_event($file, $event_name, $lines, $event_line) + public function find_event() { - return $this->find_tag($file, $event_name, $lines, $event_line, 'event', array()); + return $this->find_tag('event', array()); } /** * Find a "@*" Information line * - * @param string $file - * @param string $event_name - * @param array $lines - * @param int $event_line Index of the event call in $lines * @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($file, $event_name, $lines, $event_line, $find_tag, $disallowed_tags) + public function find_tag($find_tag, $disallowed_tags) { $find_tag_line = 0; $found_comment_end = false; - while (strpos(ltrim($lines[$event_line - $find_tag_line], "\t"), '* @' . $find_tag . ' ') !== 0) + while (strpos(ltrim($this->file_lines[$this->current_event_line - $find_tag_line], "\t"), '* @' . $find_tag . ' ') !== 0) { - if ($found_comment_end && ltrim($lines[$event_line - $find_tag_line], "\t") === '/**') + 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 "' . $event_name . '" in file "' . $file . '"', 1); + 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($lines[$event_line - $find_tag_line], "\t"), '* @' . $disallowed_tag) === 0) + 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 "' . $event_name . '" in file "' . $file . '"', 3); + throw new LogicException('Found @' . $disallowed_tag . ' information after @' . $find_tag . ' for event "' . $this->current_event . '" in file "' . $this->current_file . '"', 3); } } - if (ltrim($lines[$event_line - $find_tag_line], "\t") === '*/') + if (ltrim($this->file_lines[$this->current_event_line - $find_tag_line], "\t") === '*/') { $found_comment_end = true; } $find_tag_line++; - if ($find_tag_line >= $event_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 "' . $event_name . '" in file "' . $file . '"', 2); + throw new LogicException('Can not find @' . $find_tag . ' information for event "' . $this->current_event . '" in file "' . $this->current_file . '"', 2); } } - return $event_line - $find_tag_line; + return $this->current_event_line - $find_tag_line; } /** * Find a "@*" Information line * - * @param string $file - * @param string $event_name - * @param array $lines - * @param int $event_line Index of the event call in $lines * @return int Absolute line number + * @throws LogicException */ - public function find_description($file, $event_name, $lines, $event_line) + public function find_description() { $find_desc_line = 0; - while (ltrim($lines[$event_line - $find_desc_line], "\t") !== '/**') + while (ltrim($this->file_lines[$this->current_event_line - $find_desc_line], "\t") !== '/**') { $find_desc_line++; - if ($find_desc_line > $event_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 "' . $event_name . '" in file "' . $file . '"', 1); + throw new LogicException('Can not find a description for event "' . $this->current_event . '" in file "' . $this->current_file . '"', 1); } } - $find_desc_line = $event_line - $find_desc_line + 1; + $find_desc_line = $this->current_event_line - $find_desc_line + 1; - $desc = trim($lines[$find_desc_line]); + $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 "' . $event_name . '" in file "' . $file . '"', 2); + throw new LogicException('Can not find a description for event "' . $this->current_event . '" in file "' . $this->current_file . '"', 2); } return $find_desc_line; @@ -437,25 +457,24 @@ class event_exporter /** * Validate "@since" Information * - * @param string $file - * @param string $event_name * @param string $line * @return string + * @throws LogicException */ - public function validate_since($file, $event_name, $line) + public function validate_since($line) { $since = substr(ltrim($line, "\t"), strlen('* @since ')); if ($since !== trim($since)) { - throw new LogicException('Invalid @since information for event "' . $event_name . '" in file "' . $file . '"', 1); + 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 "' . $event_name . '" in file "' . $file . '"', 2); + throw new LogicException('Invalid @since information for event "' . $this->current_event . '" in file "' . $this->current_file . '"', 2); } return $since; @@ -464,23 +483,23 @@ class event_exporter /** * Validate "@event" Information * - * @param string $file * @param string $event_name * @param string $line * @return string + * @throws LogicException */ - public function validate_event($file, $event_name, $line) + 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 "' . $file . '"', 1); + 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 "' . $file . '"', 2); + throw new LogicException('Event name does not match @event tag for event "' . $event_name . '" in file "' . $this->current_file . '"', 2); } return $event; @@ -489,13 +508,12 @@ class event_exporter /** * Validates that two arrays contain the same strings * - * @param string $file - * @param string $event_name * @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($file, $event_name, $vars_array, $vars_docblock) + public function validate_vars_docblock_array($vars_array, $vars_docblock) { $vars_array = array_unique($vars_array); $vars_docblock = array_unique($vars_docblock); @@ -503,7 +521,7 @@ class event_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 "' . $event_name . '" in file "' . $file . '"'); + throw new LogicException('$vars array does not match the list of @var tags for event "' . $this->current_event . '" in file "' . $this->current_file . '"'); } } @@ -513,6 +531,7 @@ class event_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 from within $dir */ function get_file_list($dir, $path = '') -- cgit v1.2.1 From d213e09a40cb0ee9c94c35b3aecb1814d740184a Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Fri, 18 Apr 2014 11:06:04 +0200 Subject: [ticket/12273] Crawl the phpBB directory for events PHPBB3-12273 --- phpBB/develop/event_exporter.php | 178 ++++++++++++++++++------------- phpBB/develop/export_events_for_wiki.php | 3 +- 2 files changed, 104 insertions(+), 77 deletions(-) (limited to 'phpBB/develop') diff --git a/phpBB/develop/event_exporter.php b/phpBB/develop/event_exporter.php index a84b18db26..223f9318ba 100644 --- a/phpBB/develop/event_exporter.php +++ b/phpBB/develop/event_exporter.php @@ -34,6 +34,7 @@ class event_exporter { $this->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; } @@ -97,23 +98,6 @@ class event_exporter } } - function export_from_php() - { - $files = $this->get_file_list($this->root_path); - $this->events = array(); - foreach ($files as $file) - { - $this->check_for_events($file); - } - ksort($this->events); - - foreach ($this->events as $event) - { - echo '|- id="' . $event['event'] . '"' . "\n"; - echo '| [[#' . $event['event'] . '|' . $event['event'] . ']] || ' . $event['file'] . ' || ' . implode(', ', $event['arguments']) . ' || ' . $event['since'] . ' || ' . $event['description'] . "\n"; - } - } - public function get_events() { return $this->events; @@ -130,11 +114,108 @@ class event_exporter $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 check_for_events($file) + public function crawl_php_file($file) { $this->current_file = $file; $this->file_lines = array(); @@ -182,13 +263,13 @@ class event_exporter $description_line_num = $this->find_description(); $description = substr(trim($this->file_lines[$description_line_num]), strlen('* ')); - if (isset($this->events[$this->current_event])) + 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[$this->current_event]['file'] . '"', 10); + . '" already exists in file "'. $this->events['php'][$this->current_event]['file'] . '"', 10); } - $this->events[$this->current_event] = array( + $this->events['php'][$this->current_event] = array( 'event' => $this->current_event, 'file' => $this->current_file, 'arguments' => $arguments, @@ -524,59 +605,4 @@ class event_exporter throw new LogicException('$vars array does not match the list of @var tags for event "' . $this->current_event . '" in file "' . $this->current_file . '"'); } } - - /** - * Returns a list of files in that directory - * - * 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 from within $dir - */ - function get_file_list($dir, $path = '') - { - try - { - $iterator = new \DirectoryIterator($dir); - } - catch (Exception $e) - { - return array(); - } - - $files = array(); - foreach ($iterator as $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_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(); - } - } - - return $files; - } } diff --git a/phpBB/develop/export_events_for_wiki.php b/phpBB/develop/export_events_for_wiki.php index 0c4f14a4b4..0019fbaa44 100644 --- a/phpBB/develop/export_events_for_wiki.php +++ b/phpBB/develop/export_events_for_wiki.php @@ -54,7 +54,8 @@ switch ($action) break; case 'php': - $exporter->export_from_php(); + $exporter->crawl_phpbb_directory_php(); + echo $exporter->export_php_events_for_wiki(); break; default: -- cgit v1.2.1 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/develop/event_exporter.php | 608 ------------------------------- phpBB/develop/export_events_for_wiki.php | 4 +- 2 files changed, 2 insertions(+), 610 deletions(-) delete mode 100644 phpBB/develop/event_exporter.php (limited to 'phpBB/develop') diff --git a/phpBB/develop/event_exporter.php b/phpBB/develop/event_exporter.php deleted file mode 100644 index 223f9318ba..0000000000 --- a/phpBB/develop/event_exporter.php +++ /dev/null @@ -1,608 +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('"); - 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); - $arguments = array(); - } - } - - 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/develop/export_events_for_wiki.php b/phpBB/develop/export_events_for_wiki.php index 0019fbaa44..ff64dc493e 100644 --- a/phpBB/develop/export_events_for_wiki.php +++ b/phpBB/develop/export_events_for_wiki.php @@ -13,7 +13,7 @@ if (php_sapi_name() != 'cli') $phpEx = substr(strrchr(__FILE__, '.'), 1); $phpbb_root_path = __DIR__ . '/../'; -require __DIR__ . '/event_exporter.' . $phpEx; +require __DIR__ . '/../phpbb/event/exporter.' . $phpEx; function usage() { @@ -41,7 +41,7 @@ function validate_argument_count($arguments, $count) validate_argument_count($argc, 1); $action = $argv[1]; -$exporter = new \event_exporter($phpbb_root_path); +$exporter = new \phpbb\event\exporter($phpbb_root_path); switch ($action) { -- cgit v1.2.1 From 3566325874a7d6da00ec7cd0e95543db34e1e811 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Fri, 18 Apr 2014 12:51:45 +0200 Subject: [ticket/12273] Fix export script PHPBB3-12273 --- phpBB/develop/export_events_for_wiki.php | 59 ++++++++++++++++++++++++-------- 1 file changed, 44 insertions(+), 15 deletions(-) (limited to 'phpBB/develop') diff --git a/phpBB/develop/export_events_for_wiki.php b/phpBB/develop/export_events_for_wiki.php index ff64dc493e..69b09ff136 100644 --- a/phpBB/develop/export_events_for_wiki.php +++ b/phpBB/develop/export_events_for_wiki.php @@ -13,20 +13,23 @@ if (php_sapi_name() != 'cli') $phpEx = substr(strrchr(__FILE__, '.'), 1); $phpbb_root_path = __DIR__ . '/../'; -require __DIR__ . '/../phpbb/event/exporter.' . $phpEx; function usage() { echo "Usage: export_events_for_wiki.php COMMAND\n"; echo "\n"; + echo "all:\n"; + echo " Generate the complete wikipage for https://wiki.phpbb.com/Event_List\n"; + echo "\n"; + echo "php:\n"; + echo " Generate the PHP event section of Event_List\n"; + echo "\n"; echo "acp:\n"; - echo " Export all events for files in the acp style.\n"; + echo " Generate the ACP Template event section of Event_List\n"; echo "\n"; echo "styles:\n"; - echo " Export all events for files in the prosilver and subsilver2 styles.\n"; + echo " Generate the Styles Template event section of Event_List\n"; echo "\n"; - echo "php:\n"; - echo " Export all events for php-files.\n"; exit(2); } @@ -41,22 +44,48 @@ function validate_argument_count($arguments, $count) validate_argument_count($argc, 1); $action = $argv[1]; -$exporter = new \phpbb\event\exporter($phpbb_root_path); +require __DIR__ . '/../phpbb/event/php_exporter.' . $phpEx; +require __DIR__ . '/../phpbb/event/md_exporter.' . $phpEx; switch ($action) { - case 'acp': - $exporter->export_from_eventsmd('acp'); - break; - - case 'styles': - $exporter->export_from_eventsmd('styles'); - break; + case 'all': + echo '__FORCETOC__' . "\n"; case 'php': + $exporter = new \phpbb\event\php_exporter($phpbb_root_path); $exporter->crawl_phpbb_directory_php(); - echo $exporter->export_php_events_for_wiki(); - break; + echo $exporter->export_events_for_wiki(); + + if ($action === 'php') + { + break; + } + echo "\n"; + // no break; + + case 'styles': + $exporter = new \phpbb\event\md_exporter($phpbb_root_path); + $exporter->crawl_eventsmd('docs/events.md', 'styles'); + echo $exporter->export_events_for_wiki(); + + if ($action === 'styles') + { + break; + } + echo "\n"; + // no break; + + case 'adm': + $exporter = new \phpbb\event\md_exporter($phpbb_root_path); + $exporter->crawl_eventsmd('docs/events.md', 'adm'); + echo $exporter->export_events_for_wiki(); + + if ($action === 'all') + { + echo "\n" . '[[Category:Events and Listeners]]' . "\n"; + } + break; default: usage(); -- cgit v1.2.1 From 58892555299ad9df5bdd2c5401a854068860e68d Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Sun, 20 Apr 2014 17:01:00 +0200 Subject: [ticket/12273] Test source files and compare with events.md PHPBB3-12273 --- phpBB/develop/export_events_for_wiki.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'phpBB/develop') diff --git a/phpBB/develop/export_events_for_wiki.php b/phpBB/develop/export_events_for_wiki.php index 69b09ff136..cc4aa4444f 100644 --- a/phpBB/develop/export_events_for_wiki.php +++ b/phpBB/develop/export_events_for_wiki.php @@ -24,7 +24,7 @@ function usage() echo "php:\n"; echo " Generate the PHP event section of Event_List\n"; echo "\n"; - echo "acp:\n"; + echo "adm:\n"; echo " Generate the ACP Template event section of Event_List\n"; echo "\n"; echo "styles:\n"; @@ -66,7 +66,7 @@ switch ($action) case 'styles': $exporter = new \phpbb\event\md_exporter($phpbb_root_path); - $exporter->crawl_eventsmd('docs/events.md', 'styles'); + $exporter->crawl_phpbb_directory_styles('docs/events.md'); echo $exporter->export_events_for_wiki(); if ($action === 'styles') @@ -78,7 +78,7 @@ switch ($action) case 'adm': $exporter = new \phpbb\event\md_exporter($phpbb_root_path); - $exporter->crawl_eventsmd('docs/events.md', 'adm'); + $exporter->crawl_phpbb_directory_adm('docs/events.md'); echo $exporter->export_events_for_wiki(); if ($action === 'all') -- cgit v1.2.1 From e934cefe809b91b80db509916a0be4b64a3ad2a5 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Sun, 27 Apr 2014 23:50:02 +0200 Subject: [ticket/12273] Fix missing classes in export_events_for_wiki.php PHPBB3-12273 --- phpBB/develop/export_events_for_wiki.php | 2 ++ 1 file changed, 2 insertions(+) (limited to 'phpBB/develop') diff --git a/phpBB/develop/export_events_for_wiki.php b/phpBB/develop/export_events_for_wiki.php index cc4aa4444f..2a8e4533e5 100644 --- a/phpBB/develop/export_events_for_wiki.php +++ b/phpBB/develop/export_events_for_wiki.php @@ -46,6 +46,8 @@ validate_argument_count($argc, 1); $action = $argv[1]; require __DIR__ . '/../phpbb/event/php_exporter.' . $phpEx; require __DIR__ . '/../phpbb/event/md_exporter.' . $phpEx; +require __DIR__ . '/../phpbb/event/recursive_event_filter_iterator.' . $phpEx; +require __DIR__ . '/../phpbb/recursive_dot_prefix_filter_iterator.' . $phpEx; switch ($action) { -- cgit v1.2.1 From 2928effb3cd10114b71305e5c07f6c03584a4fe1 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Mon, 28 Apr 2014 21:04:36 +0200 Subject: [ticket/12273] Update exporter to allow specifying an extension PHPBB3-12273 --- phpBB/develop/export_events_for_wiki.php | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-) (limited to 'phpBB/develop') diff --git a/phpBB/develop/export_events_for_wiki.php b/phpBB/develop/export_events_for_wiki.php index 2a8e4533e5..61d02059fd 100644 --- a/phpBB/develop/export_events_for_wiki.php +++ b/phpBB/develop/export_events_for_wiki.php @@ -16,19 +16,24 @@ $phpbb_root_path = __DIR__ . '/../'; function usage() { - echo "Usage: export_events_for_wiki.php COMMAND\n"; + echo "Usage: export_events_for_wiki.php COMMAND [EXTENSION]\n"; echo "\n"; - echo "all:\n"; - echo " Generate the complete wikipage for https://wiki.phpbb.com/Event_List\n"; + echo "COMMAND:\n"; + echo " all:\n"; + echo " Generate the complete wikipage for https://wiki.phpbb.com/Event_List\n"; echo "\n"; - echo "php:\n"; - echo " Generate the PHP event section of Event_List\n"; + echo " php:\n"; + echo " Generate the PHP event section of Event_List\n"; echo "\n"; - echo "adm:\n"; - echo " Generate the ACP Template event section of Event_List\n"; + echo " adm:\n"; + echo " Generate the ACP Template event section of Event_List\n"; echo "\n"; - echo "styles:\n"; - echo " Generate the Styles Template event section of Event_List\n"; + echo " styles:\n"; + echo " Generate the Styles Template event section of Event_List\n"; + echo "\n"; + echo "EXTENSION (Optional):\n"; + echo " If not given, only core events will be exported.\n"; + echo " Otherwise only events from the extension will be exported.\n"; echo "\n"; exit(2); } @@ -44,6 +49,7 @@ function validate_argument_count($arguments, $count) validate_argument_count($argc, 1); $action = $argv[1]; +$extension = isset($argv[2]) ? $argv[2] : null; require __DIR__ . '/../phpbb/event/php_exporter.' . $phpEx; require __DIR__ . '/../phpbb/event/md_exporter.' . $phpEx; require __DIR__ . '/../phpbb/event/recursive_event_filter_iterator.' . $phpEx; @@ -56,7 +62,7 @@ switch ($action) case 'php': $exporter = new \phpbb\event\php_exporter($phpbb_root_path); - $exporter->crawl_phpbb_directory_php(); + $exporter->crawl_phpbb_directory_php($extension); echo $exporter->export_events_for_wiki(); if ($action === 'php') @@ -68,7 +74,7 @@ switch ($action) case 'styles': $exporter = new \phpbb\event\md_exporter($phpbb_root_path); - $exporter->crawl_phpbb_directory_styles('docs/events.md'); + $exporter->crawl_phpbb_directory_styles('docs/events.md', $extension); echo $exporter->export_events_for_wiki(); if ($action === 'styles') @@ -80,7 +86,7 @@ switch ($action) case 'adm': $exporter = new \phpbb\event\md_exporter($phpbb_root_path); - $exporter->crawl_phpbb_directory_adm('docs/events.md'); + $exporter->crawl_phpbb_directory_adm('docs/events.md', $extension); echo $exporter->export_events_for_wiki(); if ($action === 'all') -- cgit v1.2.1 From 7f90ff70acd036a022db924bb8d0e0d3f64b8ff8 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Mon, 28 Apr 2014 23:39:37 +0200 Subject: [ticket/12273] Move $extension to constructor so the path is always set PHPBB3-12273 --- phpBB/develop/export_events_for_wiki.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'phpBB/develop') diff --git a/phpBB/develop/export_events_for_wiki.php b/phpBB/develop/export_events_for_wiki.php index 61d02059fd..f4ebb42cf4 100644 --- a/phpBB/develop/export_events_for_wiki.php +++ b/phpBB/develop/export_events_for_wiki.php @@ -61,8 +61,8 @@ switch ($action) echo '__FORCETOC__' . "\n"; case 'php': - $exporter = new \phpbb\event\php_exporter($phpbb_root_path); - $exporter->crawl_phpbb_directory_php($extension); + $exporter = new \phpbb\event\php_exporter($phpbb_root_path, $extension); + $exporter->crawl_phpbb_directory_php(); echo $exporter->export_events_for_wiki(); if ($action === 'php') @@ -73,8 +73,8 @@ switch ($action) // no break; case 'styles': - $exporter = new \phpbb\event\md_exporter($phpbb_root_path); - $exporter->crawl_phpbb_directory_styles('docs/events.md', $extension); + $exporter = new \phpbb\event\md_exporter($phpbb_root_path, $extension); + $exporter->crawl_phpbb_directory_styles('docs/events.md'); echo $exporter->export_events_for_wiki(); if ($action === 'styles') @@ -85,8 +85,8 @@ switch ($action) // no break; case 'adm': - $exporter = new \phpbb\event\md_exporter($phpbb_root_path); - $exporter->crawl_phpbb_directory_adm('docs/events.md', $extension); + $exporter = new \phpbb\event\md_exporter($phpbb_root_path, $extension); + $exporter->crawl_phpbb_directory_adm('docs/events.md'); echo $exporter->export_events_for_wiki(); if ($action === 'all') -- cgit v1.2.1 From b39b0369aa0ac6853bde0504259166f570beb983 Mon Sep 17 00:00:00 2001 From: Patrick Webster Date: Sun, 3 Nov 2013 21:58:05 -0600 Subject: [feature/sqlite3] Add support for SQLite 3 Minimum version requirement is 3.6.15 as that's what ships with PHP 5.3.0 when support for SQLite 3 was added. PHPBB3-9728 --- phpBB/develop/add_permissions.php | 1 + phpBB/develop/create_schema_files.php | 1 + 2 files changed, 2 insertions(+) (limited to 'phpBB/develop') diff --git a/phpBB/develop/add_permissions.php b/phpBB/develop/add_permissions.php index 449d931507..688e228729 100644 --- a/phpBB/develop/add_permissions.php +++ b/phpBB/develop/add_permissions.php @@ -376,6 +376,7 @@ function mass_auth($ug_type, $forum_id, $ug_id, $acl_list, $setting) case 'mssql': case 'sqlite': + case 'sqlite3': $sql = implode(' UNION ALL ', preg_replace('#^(.*?)$#', 'SELECT \1', $sql_subary)); break; diff --git a/phpBB/develop/create_schema_files.php b/phpBB/develop/create_schema_files.php index 9ab8bd193f..d89de2cee3 100644 --- a/phpBB/develop/create_schema_files.php +++ b/phpBB/develop/create_schema_files.php @@ -70,6 +70,7 @@ foreach ($supported_dbms as $dbms) case 'mysql_41': case 'firebird': case 'sqlite': + case 'sqlite3': fwrite($fp, "# DO NOT EDIT THIS FILE, IT IS GENERATED\n"); fwrite($fp, "#\n"); fwrite($fp, "# To change the contents of this file, edit\n"); -- cgit v1.2.1