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; } }
function testBodyFuncs() { $r = new folksoResponse(); $r->t('Hello'); $this->assertTrue(is_string($r->body())); $this->assertEqual($r->body(), 'Hello', "Not returning body() correctly"); $r->t('there'); $this->assertEqual($r->body(), "Hello\nthere", "Newline insertion fail"); }
/** * 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; }
/** * 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; }
/** * * */ 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; }