function testExceptionHandling() { $r = new folksoResponse(); $r->handleDBexception(new dbConnectionException('Something bad happened')); $r->prepareHeaders(); $this->assertEqual($r->status, 500, 'dbConnectionException not handled correctly, no 500'); $r2 = new folksoResponse(); $r2->handleDBexception(new dbQueryException(234, 'select * from peeps', 'Something horrible just happened')); $r2->prepareHeaders(); $this->assertEqual($r->status, 500, 'dbQueryException not producing a 500'); $this->assertPattern('/peeps/', $r2->body(), 'Not getting correct exception information in body for query exception'); $r3 = new folksoResponse(); $r3->handleUserException(new badUseridException('Who are you?')); $this->assertEqual($r3->status, 403, 'Bad userid not returning 403'); }
/** * List of all the tags. */ function allTags(folksoQuery $q, folksoDBconnect $dbc, folksoSession $fks) { $r = new folksoResponse(); try { $i = new folksoDBinteract($dbc); $query = "SELECT t.tagdisplay AS display, t.id AS tagid, \n\t" . "t.tagnorm AS tagnorm, \n\t" . "(SELECT COUNT(*) FROM tagevent te WHERE te.tag_id = t.id) AS popularity \n" . "FROM tag t \n" . " ORDER BY display "; $i->query($query); } catch (dbException $e) { return $r->handleDBexception($e); } $r->setOk(200, 'There they are'); $df = new folksoDisplayFactory(); $dd = $df->TagList(); $dd->activate_style('xml'); $r->t($dd->startform()); while ($row = $i->result->fetch_object()) { $r->t($dd->line($row->tagid, $row->tagnorm, $row->display, $row->popularity, '')); } $r->t($dd->endform()); return $r; }
/** * Web params: POST + note + delete * * "note" must be a numerical note id. */ function rmNote(folksoquery $q, folksoDBconnect $dbc, folksoSession $fks) { $r = new folksoResponse(); $u = $fks->userSession(null, 'folkso', 'redac'); if (!$u instanceof folksoUser || !$u->checkUserRight('folkso', 'redac')) { return $r->unAuthorized($u); } try { $i = new folksoDBinteract($dbc); if (!is_numeric($q->get_param('note'))) { $r->setError(400, 'Bad note argument', $q->get_param('note') . ' is not a number'); return $r; } $sql = "DELETE FROM note WHERE id = " . $q->get_param('note'); $i->query($sql); } catch (dbException $e) { return $r->handleDBexception($e); } $r->setOk(200, 'Deleted'); $r->t("The note " . $q->get_param('note') . " was deleted."); return $r; }
/** * * */ function getUserResByTag(folksoQuery $q, folksoDBconnect $dbc, folksoSession $fks) { $r = new folksoResponse(); try { $u = $fks->userSession(null); if (!$u instanceof folksoUser && !$q->is_param('user')) { return $r->setError(404, 'No user'); } elseif ($q->is_param('user')) { $u = new folksoUser($dbc); // we create a user object anyway $u->setUid($q->get_param('user')); if (!$u->exists($q->get_param('user'))) { return $r->setError(404, 'Missing or invalid user'); } } $i = new folksoDBinteract($dbc); $uq = new folksoUserQuery(); $sql = $uq->resourcesByTag($q->tag, $u->userid); $i->query($sql); /* these are inside the try block because exists() hits the DB */ if ($i->rowCount == 0) { if (isset($u->nick) || $u->exists()) { return $r->setOk(204, 'User has no resources with this tag'); } else { // no longer necessary return $r->setError(404, 'Unknown user'); } } } catch (dbException $e) { return $r->handleDBexception($e); } catch (badUseridException $e) { return $r->handleDBexception($e); // TODO: update this with new class } $r->setOk(200, 'Found'); $df = new folksoDisplayFactory(); if ($q->content_type() == 'json') { $dd = new folksoDataJson('resid', 'url', 'title'); } else { $dd = $df->ResourceList('xml'); } $r->t($dd->startform()); while ($row = $i->result->fetch_object()) { $r->t($dd->line($row->id, htmlspecialchars($row->uri_raw), htmlspecialchars($row->title))); } $r->t($dd->endform()); return $r; }