[ticket/12273] Crawl the phpBB directory for events

PHPBB3-12273
This commit is contained in:
Joas Schilling 2014-04-18 11:06:04 +02:00
parent b831e96aaf
commit d213e09a40
3 changed files with 122 additions and 86 deletions

View file

@ -34,6 +34,7 @@ class event_exporter
{ {
$this->root_path = $phpbb_root_path; $this->root_path = $phpbb_root_path;
$this->events = $this->file_lines = array(); $this->events = $this->file_lines = array();
$this->events['php'] = array();
$this->current_file = $this->current_event = ''; $this->current_file = $this->current_event = '';
$this->current_event_line = 0; $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() public function get_events()
{ {
return $this->events; return $this->events;
@ -130,11 +114,108 @@ class event_exporter
$this->file_lines = $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 * @param $file
* @throws LogicException * @throws LogicException
*/ */
public function check_for_events($file) public function crawl_php_file($file)
{ {
$this->current_file = $file; $this->current_file = $file;
$this->file_lines = array(); $this->file_lines = array();
@ -182,13 +263,13 @@ class event_exporter
$description_line_num = $this->find_description(); $description_line_num = $this->find_description();
$description = substr(trim($this->file_lines[$description_line_num]), strlen('* ')); $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 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, 'event' => $this->current_event,
'file' => $this->current_file, 'file' => $this->current_file,
'arguments' => $arguments, '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 . '"'); 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;
}
} }

View file

@ -54,7 +54,8 @@ switch ($action)
break; break;
case 'php': case 'php':
$exporter->export_from_php(); $exporter->crawl_phpbb_directory_php();
echo $exporter->export_php_events_for_wiki();
break; break;
default: default:

View file

@ -20,7 +20,7 @@ class phpbb_event_exporter_test extends phpbb_test_case
$this->exporter = new \event_exporter(dirname(__FILE__) . '/fixtures/'); $this->exporter = new \event_exporter(dirname(__FILE__) . '/fixtures/');
} }
static public function check_for_events_data() static public function crawl_php_file_data()
{ {
return array( return array(
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->exporter->crawl_php_file($file);
$this->assertEquals($expected, $this->exporter->get_events()); $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( return array(
array('missing_var.test', null), 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->setExpectedException('LogicException', '', $exception_code);
$this->assertNull($this->exporter->check_for_events($file)); $this->exporter->crawl_php_file($file);
} }
static public function validate_since_data() 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->set_content($lines);
$this->exporter->find_description(); $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());
}
} }