コード例 #1
0
ファイル: user.php プロジェクト: josf/folkso
/**
 *
 * 
 */
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;
}
コード例 #2
0
ファイル: folksoResponder.php プロジェクト: josf/folkso
 /**
  * Checks to see if this Response should be used to respond to the query $q.
  *
  * If there are 'exclude' parameters, they are checked first. If any
  * of them are present, param_check returns 'false'.
  *
  * Then we check the 'required' parameters in $this->activate_params
  * and returns false if any of them are missing. If one of the
  * 'oneof' fields is present (and assuming conditions in the other
  * arrays are met), 'true' is returned.
  *
  * @param folksoQuery $q
  * @return boolean
  */
 private function param_check(folksoQuery $q)
 {
     if (is_array($this->activate_params['exclude'])) {
         foreach ($this->activate_params['exclude'] as $no) {
             if ($q->is_param($no)) {
                 return false;
             }
         }
     }
     $all_requireds = array();
     foreach (array($this->activate_params['required'], $this->activate_params['required_single'], $this->activate_params['required_multiple']) as $arr) {
         if (is_array($arr)) {
             $all_requireds = array_merge($all_requireds, $arr);
         }
     }
     foreach ($all_requireds as $p) {
         if (!$q->is_param($p)) {
             return false;
         }
     }
     if (is_array($this->activate_params['required_single'])) {
         foreach ($this->activate_params['required_single'] as $p) {
             if (!$q->is_single_param($p)) {
                 return false;
             }
         }
     }
     if (is_array($this->activate_params['required_multiple'])) {
         foreach ($this->activate_params['required_multiple'] as $p) {
             if (!$q->is_multiple_param($p)) {
                 return false;
             }
         }
     }
     $oneof = false;
     if (is_array($this->activate_params['oneof'])) {
         foreach ($this->activate_params['oneof'] as $p) {
             if ($q->is_param($p)) {
                 $oneof = true;
             }
         }
         if (!$oneof) {
             return false;
         }
     }
     return true;
 }
コード例 #3
0
ファイル: tag.php プロジェクト: josf/folkso
/**
 * Retrieves a list of the resources associated with the given tag.
 *
 * Parameters: GET, resources (single param)
 *
 * We might have to think about adding a way to announce whether there
 * is a "next page" or not.
 * 
 * Currently supports xhtml and xml dataformats.
 */
function getTagResources(folksoQuery $q, folksoDBconnect $dbc, folksoSession $fks)
{
    $r = new folksoResponse();
    try {
        $i = new folksoDBinteract($dbc);
        // check to see if tag exists -- can this be done with the main query instead?
        if (!$i->tagp($q->tag)) {
            $r->setError(404, 'Tag not found', $q->tag . " does not exist in the database");
            return $r;
        }
        $querybase = "SELECT\n                 r.uri_raw AS href, r.id AS id, r.title AS title, \n                 CASE \n                   WHEN title IS NULL THEN uri_normal \n                   ELSE title \n                 END AS display\n              FROM resource r\n                 JOIN tagevent ON r.id = tagevent.resource_id\n                 JOIN tag ON tagevent.tag_id = tag.id ";
        // tag by ID
        if (is_numeric($q->tag)) {
            $querybase .= "WHERE tag.id = " . $i->dbescape($q->tag);
        } else {
            $querybase .= "WHERE tag.tagnorm = normalize_tag('" . $i->dbescape($q->tag) . "')";
        }
        $querybase .= " ORDER BY r.visited DESC ";
        //pagination
        if (!$q->is_param('page') || $q->get_param('page') == 1) {
            $querybase .= "  LIMIT 20 ";
        } else {
            $querybase .= "  LIMIT " . $q->get_param('page') * 20 . ",20 ";
        }
        $i->query($querybase);
    } catch (dbException $e) {
        return $r->handleDBexception($e);
    }
    switch ($i->result_status) {
        case 'NOROWS':
            $r->setOk(204, 'No resources associated with  tag');
            $r->t("No resources are currently associated with " . $q->tag);
            return $r;
            break;
        case 'OK':
            $r->setOk(200, 'Tag found');
            break;
            // now we do the rest, assuming all is well.
    }
    $df = new folksoDisplayFactory();
    $dd = $df->ResourceList();
    if ($q->content_type() == 'text/html') {
        $dd->activate_style('xhtml');
    } else {
        $dd->activate_style('xml');
    }
    $r->t($dd->startform());
    while ($row = $i->result->fetch_object()) {
        $r->t($dd->line($row->id, $row->href, array('xml' => $row->display, 'default' => $row->display)));
    }
    $r->t($dd->endform());
    return $r;
}
コード例 #4
0
ファイル: resource.php プロジェクト: josf/folkso
/**
 * Tag cloud.
 *
 * Parameters: GET, folksoclouduri, res.
 *
 * Optional: folksolimit (limit number of tags returned).
 * Optional: folksobypop (pure popularity based cloud).
 * Optional: folksobydate (date based cloud).
 */
function tagCloudLocalPop(folksoQuery $q, folksoDBconnect $dbc, folksoSession $fks)
{
    $r = new folksoResponse();
    try {
        $i = new folksoDBinteract($dbc);
        // check to see if resource is in db.
        if (!$i->resourcep($q->res)) {
            $r->setError(404, 'Resource not found');
            $r->errorBody("Resource not present in database");
            return $r;
        }
        // using the "limit" parameter
        $taglimit = 0;
        if ($q->is_param('limit') && is_numeric($q->get_param('limit'))) {
            $taglimit = $q->get_param('limit');
        }
        $rq = new folksoResQuery();
        if ($q->is_param('bypop')) {
            $sql = $rq->cloud_by_popularity($i->dbescape($q->res), $taglimit);
        } elseif ($q->is_param('bydate')) {
            $sql = $rq->cloud_by_date($i->dbescape($q->res), $taglimit);
        } else {
            $sql = $rq->basic_cloud($i->dbescape($q->res), $taglimit);
        }
        $i->query($sql);
        switch ($i->result_status) {
            case 'NOROWS':
                // probably impossible now
                $r->setOK(204, 'No tags associated with resource');
                return $r;
                break;
            case 'OK':
                if ($i->result->num_rows == 0) {
                    // should this be == 1 instead?
                    $r->setOK(204, 'No tags associated with resource');
                    return $r;
                } else {
                    $r->setOK(200, 'Cloud OK');
                }
                break;
        }
    } catch (dbException $e) {
        $r->handleDBexception($e);
    }
    $df = new folksoDisplayFactory();
    $dd = $df->cloud();
    //  $row1 = $i->result->fetch_object(); // popping the title line
    /** in CLOUDY
     * tagdisplay = r.title
     * tagnorm = r.uri_raw
     * tagid = r.id
     */
    switch ($q->content_type()) {
        case 'html':
            $dd->activate_style('xhtml');
            $r->setType('html');
            $r->t($dd->title($row1->tagdisplay));
            $r->t($dd->startform());
            // <ul>
            while ($row = $i->result->fetch_object()) {
                $r->t($dd->line($row->cloudweight, "resourceview.php?tagthing=" . $row->tagid, $row->tagdisplay) . "\n");
            }
            $r->t($dd->endform());
            return $r;
            break;
        case 'xml':
            $r->setType('xml');
            $dd->activate_style('xml');
            $r->t($dd->startform());
            $r->t($dd->title($q->res));
            while ($row = $i->result->fetch_object()) {
                $link = new folksoTagLink($row->tagnorm);
                $r->t($dd->line($row->tagid, $row->tagdisplay, htmlspecialchars($link->getLink()), $row->cloudweight, $row->tagnorm) . "\n");
            }
            $r->t($dd->endform());
            return $r;
            break;
        default:
            $r->setError(406, "Invalid or missing specified");
            $r->errorBody("Sorry, but you did not give a datatype and we are too lazy " . " right now to supply a default.");
            return $r;
    }
}