Permalink
Browse files

Remove some dead code and update tests to look for latestSession

  • Loading branch information...
birtles committed Mar 19, 2013
1 parent d411994 commit c2e3e83435d69ddf1e20eb9db1743c688f704c7f
Showing with 27 additions and 266 deletions.
  1. +16 −0 wall/lib/db.inc
  2. +6 −0 wall/lib/utils.inc
  3. +0 −262 wall/lib/walls.inc
  4. +2 −1 wall/public/api/doWalls.php
  5. +3 −3 wall/tests/api/TestSessions.php
View
@@ -33,4 +33,20 @@ function getDbConnection() {
}
return $conn;
}
+
+function recordExists($table, $field, $value, $type) {
+ $conn =& getDbConnection();
+ $res =& $conn->query(
+ "SELECT * FROM $table WHERE $field = "
+ . $conn->quote($value, $type)
+ . " LIMIT 1");
+ if (PEAR::isError($res)) {
+ error_log($res->getMessage() . ', ' . $res->getDebugInfo());
+ throw new KeyedException('db-error');
+ }
+ $exists = $res->numRows() > 0;
+ $res->free();
+ return $exists;
+}
+
?>
View
@@ -10,4 +10,10 @@ function wtrim($str) {
return preg_replace('/^[\pZ\pC]+|[\pZ\pC]+$/u', '', $str);
}
+// Convert something that should be numerical to an integer whilst preserving
+// null values
+function toId($idLike) {
+ return is_null($idLike) ? null : intval($idLike);
+}
+
?>
View
@@ -76,204 +76,6 @@ function getWallSummaryForUser($email) {
return $result;
}
-function createWall($params) {
- // Get connection
- $conn =& getDbConnection();
-
- // Validate owner email
- $email = trim(@$params['ownerEmail']);
- if (!strlen($email) || !filter_var($email, FILTER_VALIDATE_EMAIL)) {
- error_log("Bad or missing email when creating wall");
- throw new KeyedException('server-error', 'No email for user');
- }
-
- // Fetch user ID for owner
- $ownerId = getOrCreateUserIdFromEmail($email);
-
- // Validate title
- $title = trim(@$params['title']);
- if (!strlen($title)) {
- error_log("Empty title");
- throw new KeyedException('empty-title');
- }
- // XXX Test this
- if (!isTitleUnique($title)) {
- error_log("Even with title '$title' already exists.");
- throw new KeyedException('duplicate-title');
- }
-
- // Validate ID
- $designId = intval(@$params['design']);
- if ($designId < 1) {
- error_log("Bad design ID: $designId");
- throw new KeyedException('design-not-found', 'Bad design ID');
- }
- if (!isDesignIdValid($designId)) {
- error_log("Could't find design ID " . $designId);
- throw new KeyedException('design-not-found', 'ID not found');
- }
-
- // Get URLs
- $path = getPathForTitle($title);
- $wallUrl = getWallUrlForPath($path);
- $editorUrl = getEditorUrlForPath($path);
-
- $shortUrl = shortenUrl($wallUrl);
- if ($shortUrl == $wallUrl)
- $shortUrl = NULL;
-
- $editorShortUrl = shortenUrl($editorUrl);
- if ($editorShortUrl == $editorUrl)
- $editorShortUrl = NULL;
-
- $date = gmdate('Y-m-d H:i:s');
-
- // Run query
- $query =
- 'INSERT INTO walls'
- . ' (owner, designId, eventName, galleryDisplay, urlPath, shortUrl,'
- . ' editorShortUrl, createDate, modifyDate)'
- . ' VALUES '
- . '(' . $conn->quote($ownerId, 'integer')
- . ',' . $conn->quote($designId, 'integer')
- . ',' . $conn->quote($title, 'text')
- . ',TRUE' // galleryDisplay
- . ',' . $conn->quote($path, 'text')
- . ',' . (($shortUrl) ? $conn->quote($shortUrl, 'text') : 'NULL')
- . ',' . (($editorShortUrl) ? $conn->quote($editorShortUrl, 'text') : 'NULL')
- . ',' . $conn->quote($date, 'timestamp')
- . ',' . $conn->quote($date, 'timestamp')
- . ')';
- $res =& $conn->query($query);
- if (PEAR::isError($res)) {
- error_log($res->getMessage() . ', ' . $res->getDebugInfo());
- throw new KeyedException('db-error');
- }
-
- // Get ID of newly created wall
- $wallId = $conn->lastInsertID('walls', 'wallId');
- if (PEAR::isError($wallId)) {
- error_log($res->getMessage() . ', ' . $res->getDebugInfo());
- throw new KeyedException('db-error');
- }
-
- return intval($wallId);
-}
-
-function getOrCreateUserIdFromEmail($email) {
- $conn =& getDbConnection();
- $res =& $conn->queryOne(
- 'SELECT userId FROM users WHERE email = '
- . $conn->quote($email, 'text')
- . ' LIMIT 1',
- 'integer');
- if (PEAR::isError($res)) {
- error_log($res->getMessage() . ', ' . $res->getDebugInfo());
- throw new KeyedException('db-error');
- }
-
- // If user does not exist, add a record for them
- if ($res >= 1)
- return $res;
-
- $res =& $conn->query('INSERT INTO users (email) VALUES ('
- . $conn->quote($email, 'text')
- . ')');
- if (PEAR::isError($res)) {
- error_log($res->getMessage() . ', ' . $res->getDebugInfo());
- throw new KeyedException('db-error');
- }
-
- $ownerId = $conn->lastInsertID('users', 'userId');
- if (PEAR::isError($ownerId)) {
- error_log($res->getMessage() . ', ' . $res->getDebugInfo());
- throw new KeyedException('db-error');
- }
-
- return $ownerId;
-}
-
-function getWallDetails($wallId, $email) {
- global $config;
- $conn =& getDbConnection();
- $row =& $conn->queryRow(
- 'SELECT walls.designId'
- . ' ,eventName, eventDescr, eventLocation, eventType, eventFinish'
- . ' ,urlPath, shortUrl, editorShortUrl'
- . ' ,walls.duration, galleryDisplay, passcode'
- . ' ,designs.duration AS defaultduration'
- . ' ,users.userId IS NOT NULL AS isOwner'
- . ' ,sessionId, beginDate as sessionStart, endDate as sessionEnd'
- . ' FROM walls'
- . ' INNER JOIN designs ON walls.designId = designs.designId'
- . ' LEFT JOIN users ON'
- . ' walls.owner = users.userId AND'
- . ' users.email = ' . $conn->quote($email, 'text')
- . ' LEFT JOIN sessions ON'
- . ' sessions.sessionId = '
- . ' (SELECT MAX(sessionId) FROM sessions'
- . ' WHERE sessions.wallId = ' . $conn->quote($wallId, 'integer') . ')'
- . ' WHERE walls.wallId = ' . $conn->quote($wallId, 'integer')
- . ' LIMIT 1',
- null,
- MDB2_FETCHMODE_ASSOC
- );
- $conn->disconnect();
-
- if (PEAR::isError($row)) {
- error_log($row->getMessage() . ', ' . $row->getDebugInfo());
- throw new KeyedException('db-error');
- }
- if ($row === null) {
- throw new KeyedException('not-found');
- }
-
- // Check auth
- if (!$row['isowner']) {
- error_log("Access denied to wall (#$wallId) by $email");
- throw new KeyedException('access-denied',
- "$email is not the owner of this wall");
- }
-
- // Wall ID
- $result["wallId"] = $wallId;
-
- // Event information
- $result["name"] = $row['eventname'];
- $result["eventDescr"] = $row['eventdescr'];
- $result["eventLocation"] = $row['eventlocation'];
- $result["eventType"] = $row['eventtype'];
- $result["eventFinish"] = $row['eventfinish'];
-
- // Design
- $result["designId"] = $row['designid'];
-
- // Prepare URLs
- $result["wallUrl"] = getCurrentServer() . '/wall/' . $row['urlpath'];
- $result["wallUrlShort"] = $row['shorturl'];
- $result["editorUrl"] = $config['editor']['url'] . $row['urlpath'];
- $result["editorUrlShort"] = $row['editorshorturl'];
- $result["galleryDisplay"] = $row['gallerydisplay'];
-
- // Session information
- if ($row['sessionid']) {
- $session['id'] = $row['sessionid'];
- $session['start'] = $row['sessionstart'];
- $session['end'] = $row['sessionend'];
- $result['session'] = $session;
- $result['status'] = $row['sessionend'] ? 'finished' : 'running';
- } else {
- $result['session'] = null;
- $result['status'] = 'finished';
- }
-
- $result["duration"] = $row['duration'];
- $result["defaultDuration"] = $row['defaultduration'];
- $result["passcode"] = $row['passcode'] != NULL ? strlen($row['passcode']) : 0;
-
- return $result;
-}
-
function updateWall($wallId, $name, $value, $type) {
$conn =& getDbConnection();
$qvalue = $conn->quote($value, $type);
@@ -365,10 +167,6 @@ function closeLastSession($wallId, $sessionId, $datetime) {
return true;
}
-function toId($idLike) {
- return is_null($idLike) ? null : intval($idLike);
-}
-
// Returns NULL if there is no active session for the wall
function getActiveSessionId($wallId) {
$conn =& getDbConnection();
@@ -413,66 +211,6 @@ function getLatestSession($wallId) {
);
}
-function isTitleUnique($title) {
- return !recordExists('walls', 'eventName', $title, 'text');
-}
-
-function isDesignIdValid($id) {
- return recordExists('designs', 'designId', $id, 'integer');
-}
-
-function getWallUrlForPath($path) {
- $scheme = 'http://';
- $port = intval($_SERVER['SERVER_PORT']) !== 80
- ? ':' . $_SERVER['SERVER_PORT']
- : '';
- $path = '/wall/' . $path;
- $url = $scheme . $_SERVER['SERVER_NAME'] . $port . $path;
- return $url;
-}
-
-function getEditorUrlForPath($path) {
- global $config;
- return $config['editor']['url'] . $path;
-}
-
-function getPathForTitle($title) {
- // Strip leading/trailing space, and
- // convert fullwidth forms to halfwidth
- $path = mb_convert_kana(trim($title), "as", "utf-8");
- // Make lowercase, and
- // convert whitespace to -
- $path = strtr(strtolower($path), " \t", '--');
- // URL encode
- $path = rawurlencode($path);
- // Check for duplicates
- // If we have a clash, just generate a random ID and use that--the user can
- // make it something more sensible later if they wish
- if (!isPathUnique($path))
- $path = rawurlencode(uniqid());
- return $path;
-}
-
-// Input: a URL-encoded path
-function isPathUnique($path) {
- return !recordExists('walls', 'urlPath', $path, 'text');
-}
-
-function recordExists($table, $field, $value, $type) {
- $conn =& getDbConnection();
- $res =& $conn->query(
- "SELECT * FROM $table WHERE $field = "
- . $conn->quote($value, $type)
- . " LIMIT 1");
- if (PEAR::isError($res)) {
- error_log($res->getMessage() . ', ' . $res->getDebugInfo());
- throw new KeyedException('db-error');
- }
- $exists = $res->numRows() > 0;
- $res->free();
- return $exists;
-}
-
function getWallDuration($wallId) {
$conn =& getDbConnection();
$res =& $conn->query(
@@ -46,7 +46,8 @@
// XXX Return the list of walls here
bailWithError('bad-request');
}
- $wall = Walls::getById($wallId, $email);
+ $email = @$_SESSION['email'];
+ $wall = Walls::getById($wallId, $email);
if ($wall === null)
bailWithError('not-found');
@@ -61,7 +61,7 @@ function testCloseSession() {
// Re-fetch wall
$wall = $this->getWall($wall['wallId']);
$this->assertTrue($wall['status'] == 'finished');
- $this->assertTrue($this->isClosedSession($wall['session']),
+ $this->assertTrue($this->isClosedSession($wall['latestSession']),
"Refetched wall session does not appear to be ended.");
// Logout and check it fails
@@ -107,7 +107,7 @@ function testCloseClosedSession() {
// Re-fetch wall (to check we're in a consistent state)
$wall = $this->getWall($wall['wallId']);
$this->assertTrue($wall['status'] == 'finished');
- $this->assertTrue($this->isClosedSession($wall['session']),
+ $this->assertTrue($this->isClosedSession($wall['latestSession']),
"Refetched wall session does not appear to be ended.");
// Tidy up by removing the wall
@@ -139,7 +139,7 @@ function testStartNew() {
// Re-fetch wall
$wall = $this->getWall($wall['wallId']);
$this->assertTrue($wall['status'] == 'running');
- $this->assertTrue($this->isOpenSession($wall['session']),
+ $this->assertTrue($this->isOpenSession($wall['latestSession']),
"Refetched wall session does not appear to be open");
// Logout and check it fails

0 comments on commit c2e3e83

Please sign in to comment.