Beispiel #1
0
 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');
 }
Beispiel #2
0
/**
 *
 * 
 */
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;
}
Beispiel #3
0
function autocomplete(folksoQuery $q, folksoDBconnect $dbc, folksoSession $fks)
{
    $i = new folksoDBinteract($dbc);
    $r = new folksoResponse();
    if ($i->db_error()) {
        $r->dbConnectionError($i->error_info());
        return $r;
    }
    $sql = "SELECT tagdisplay " . "FROM tag " . "WHERE tagnorm like '" . $i->dbescape(strtolower($q->get_param('q'))) . "%'";
    $i->query($sql);
    switch ($i->result_status) {
        case 'DBERR':
            $r->dbQueryError($i->error_info());
            return $r;
            break;
        case 'NOROWS':
            $r->setOk(204, 'No matching tags');
            return $r;
            break;
        case 'OK':
            $r->setOk(200, 'OK I guess');
            while ($row = $i->result->fetch_object()) {
                /** For entirely numeric tags, we enclose them in quotes so that
                    they can be treated as text instead of as ids. **/
                if (is_numeric($row->tagdisplay)) {
                    $r->t('"' . $row->tagdisplay . '"' . "\n");
                } else {
                    $r->t($row->tagdisplay . "\n");
                }
            }
            return $r;
            break;
    }
}
Beispiel #4
0
/**
 * 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;
}
Beispiel #5
0
/**
 * Returns an xml list of resources associated with the same ean-13 as
 * the selected resource
 *
 * Web params: GET, folksores, folksoean13list
 */
function resEans(folksoQuery $q, folksoDBconnect $dbc, folksoSession $fks)
{
    $r = new folksoResponse();
    try {
        $i = new folksoDBinteract($dbc);
        if ($i->db_error()) {
            $r->dbConnectionError($i->error_info());
            return $r;
        }
        $rq = new folksoResQuery();
        $sql = $rq->resEans($i->dbescape($q->res));
        $i->query($sql);
    } catch (dbConnectionException $e) {
        $r->dbConnectionError($e->getMessage());
        return $r;
    } catch (dbQueryException $e) {
        $r->dbQueryError($e->getMessage() . $e->sqlquery);
        return $r;
    }
    switch ($i->result_status) {
        case 'NOROWS':
            $r->setError(404, 'Resource not found', "The requested resource is not present in the database.\n" . " Maybe it  has not been indexed yet, or an erroneous identifier " . " was used. ");
            return $r;
            break;
        case 'OK':
            if ($i->result->num_rows == 1) {
                $r->setError(404, 'No EAN-13 data associated with this resource', "There is no EAN-13 data yet for the resource " . $q->res . ".");
                return $r;
            } else {
                $r->setOk(200, 'EAN-13 data found');
            }
    }
    $title_line = $i->result->fetch_object();
    /**popping the title that
          we are not using, but
          we could if we needed
          too (see note in ResQuery) 
       **/
    $df = new folksoDisplayFactory();
    $dd = $df->associatedEan13resources();
    $dd->activate_style('xml');
    $r->t($dd->startform());
    while ($row = $i->result->fetch_object()) {
        $r->t($dd->line($row->id, $row->url, $row->title));
    }
    $r->t($dd->endform());
    return $r;
}