From 2b7189767f4d5708948f3989df8c3d8084eab87a Mon Sep 17 00:00:00 2001 From: uckelman Date: Sat, 10 Apr 2010 14:43:17 +0000 Subject: Refactored. git-svn-id: https://vassalengine.svn.sourceforge.net/svnroot/vassalengine/site-src/trunk@6637 67b53d14-2c14-4ace-a08f-0dab2b34000c --- src/PhpBB3Lib.php | 42 ++++++++++++++++++++++++------------------ test/PhpBB3LibTest.php | 8 ++++---- 2 files changed, 28 insertions(+), 22 deletions(-) diff --git a/src/PhpBB3Lib.php b/src/PhpBB3Lib.php index ccaa527..5d39ae0 100644 --- a/src/PhpBB3Lib.php +++ b/src/PhpBB3Lib.php @@ -5,6 +5,7 @@ define('IN_PHPBB', true); require_once(__DIR__ . '/PhpBB3Conf.php'); $phpEx = substr(strrchr(__FILE__, '.'), 1); require_once($phpbb_root_path . 'common.' . $phpEx); +require_once($phpbb_root_path . 'includes/functions_posting.' . $phpEx); require_once($phpbb_root_path . 'includes/functions_user.' . $phpEx); @@ -14,15 +15,7 @@ function get_user_id($from) { $sql = 'SELECT user_id FROM ' . USERS_TABLE . ' WHERE user_email = "' . $db->sql_escape($from) . '"'; - $result = $db->sql_query($sql); -// FIXME: what to do if more than one row is returned? - $row = $db->sql_fetchrow($result); - $db->sql_freeresult($result); - - if (!$row) { - trigger_error("Unknown user email: $from", E_USER_ERROR); - } - + $row = get_exactly_one_row($sql); return $row['user_id']; } @@ -42,23 +35,36 @@ function get_user_name($id) { return $names[$id]; } -function get_topic_id($post_id) { +function get_topic_and_forum_ids($post_id) { global $db; -// FIXME: should get topic_id, forum_id at the same time - $sql = 'SELECT topic_id FROM ' . POSTS_TABLE . + $sql = 'SELECT topic_id, forum_id FROM ' . POSTS_TABLE . ' WHERE post_id = "' . $db->sql_escape($post_id) . '"'; + $row = get_exactly_one_row($sql); + return $row; +} + +function get_exactly_one_row($sql) { + global $db; + $result = $db->sql_query($sql); -// FIXME: what to do if more than one row is returned? - $row = $db->sql_fetchrow($result); + + $rows = $db->sql_fetchrowset($result); $db->sql_freeresult($result); - if (!$row) { - trigger_error("Unknown post id: $post_id", E_USER_ERROR); - } + switch (count($rows)) { + case 0: + trigger_error("No rows returned: $sql", E_USER_ERROR); + break; + + case 1: + return $rows[0]; - return $row['topic_id']; + default: + trigger_error("Too many rows returned: $sql", E_USER_ERROR); + break; + } } ?> diff --git a/test/PhpBB3LibTest.php b/test/PhpBB3LibTest.php index 22f6e0e..21fe862 100644 --- a/test/PhpBB3LibTest.php +++ b/test/PhpBB3LibTest.php @@ -67,16 +67,16 @@ EOF; /** * @dataProvider provider_get_topic_id */ - public function test_get_topic_id($post_id, $expected, $ex) { + public function test_get_topic_and_forum_ids($post_id, $expected, $ex) { if ($ex) $this->setExpectedException($ex); - $run = 'get_topic_id(' . $post_id . ')'; + $run = 'get_topic_and_forum_ids(' . $post_id . ')'; $this->assertEquals($expected, $this->exec_kludge($run)); } public function provider_get_topic_id() { return array( - array(0, null, 'PHPUnit_Framework_Error'), - array(2, 2, null ) + array(0, null, 'PHPUnit_Framework_Error'), + array(2, array('topic_id' => 2, 'forum_id' => 2), null) ); } } -- cgit v1.2.3