From d213e09a40cb0ee9c94c35b3aecb1814d740184a Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Fri, 18 Apr 2014 11:06:04 +0200 Subject: [PATCH] [ticket/12273] Crawl the phpBB directory for events PHPBB3-12273 --- phpBB/develop/event_exporter.php | 178 +++++++++++++---------- phpBB/develop/export_events_for_wiki.php | 3 +- tests/event/exporter_test.php | 27 ++-- 3 files changed, 122 insertions(+), 86 deletions(-) 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: diff --git a/tests/event/exporter_test.php b/tests/event/exporter_test.php index df5a258bbb..5736476f45 100644 --- a/tests/event/exporter_test.php +++ b/tests/event/exporter_test.php @@ -20,7 +20,7 @@ class phpbb_event_exporter_test extends phpbb_test_case $this->exporter = new \event_exporter(dirname(__FILE__) . '/fixtures/'); } - static public function check_for_events_data() + static public function crawl_php_file_data() { return array( array( @@ -67,15 +67,17 @@ class phpbb_event_exporter_test extends phpbb_test_case } /** - * @dataProvider check_for_events_data + * @dataProvider crawl_php_file_data */ - public function test_check_for_events($file, $expected) + public function test_crawl_php_file($file, $expected) { - $this->exporter->check_for_events($file); - $this->assertEquals($expected, $this->exporter->get_events()); + $this->exporter->crawl_php_file($file); + $events = $this->exporter->get_events(); + $this->assertArrayHasKey('php', $events); + $this->assertEquals($expected, $events['php']); } - static public function check_for_events_throws_data() + static public function crawl_php_file_throws_data() { return array( array('missing_var.test', null), @@ -84,12 +86,12 @@ class phpbb_event_exporter_test extends phpbb_test_case } /** - * @dataProvider check_for_events_throws_data + * @dataProvider crawl_php_file_throws_data */ - public function test_check_for_events_throws($file, $exception_code) + public function test_crawl_php_file_throws($file, $exception_code) { $this->setExpectedException('LogicException', '', $exception_code); - $this->assertNull($this->exporter->check_for_events($file)); + $this->exporter->crawl_php_file($file); } static public function validate_since_data() @@ -663,4 +665,11 @@ class phpbb_event_exporter_test extends phpbb_test_case $this->exporter->set_content($lines); $this->exporter->find_description(); } + + public function test_crawl_phpbb_directory_php() + { + global $phpbb_root_path; + $exporter = new \event_exporter($phpbb_root_path); + $this->assertGreaterThan(0, $exporter->crawl_phpbb_directory_php()); + } }