Exemplo n.º 1
0
            // only base rules, not child rules
            if (isset($rule->rule->next_rule)) {
                unset($rules[$rule->rule->next_rule]);
            }
        }
        foreach ($rules as $rule) {
            print "fill queue ";
            enqueue($rule->set, $rule->id);
        }
    }
} else {
    $next = mysql_fetch_object($nextq);
    $rules = get_rules($next->set);
    $rule = $rules[$next->rule];
    $data = strlen($next->data) > 0 ? json_decode(stripslashes($next->data)) : false;
    parse_rule($rules, $rule, $data);
    $query = mysql_query("UPDATE parse_queue SET scraped = 1 WHERE `id` = " . $next->id);
}
function enqueue($set, $rule, $data = false)
{
    print "enqueue {$set} {$rule} {$data}<br>";
    $query = "INSERT INTO parse_queue SET `scraped` = 0, `set` = {$set}, `rule` = {$rule}";
    if ($data) {
        $query .= ", `data` = '" . addslashes(json_encode($data)) . "'";
    }
    mysql_query($query) or die(mysql_error());
}
function in_queue($set, $rule, $data)
{
    $query = mysql_query("SELECT * FROM parse_queue WHERE `set` = {$set} AND  `rule` = {$rule} AND `data` = '" . addslashes(json_encode($data)) . "'");
    return (bool) mysql_num_rows($query);
Exemplo n.º 2
0
/**
 * Build a SQL query from a view rule.
 * @param $rule is the rule as raw text.
 * @param $selection designate what field to get from SQL table. Warning: it is not escaped.
 * @param $limit specifies the maximum number of items to return. Ignored if negative.
 * @param $offset specifies the offset to apply for limit. Ignored if negative.
 * @return a PDO-ready query and the binding array.
 *
 * TODO: Refactor me!!!
 */
function rule2sql($rule, $selection = '*', $limit = -1, $offset = -1)
{
    $limit = (int) $limit;
    $ast = parse_rule($rule);
    $ast[] = array('by', 'by', '');
    // Hacky
    $query = "SELECT {$selection} FROM entries E";
    $bindings = array();
    $subquery = '';
    $state = 'select';
    foreach ($ast as $word) {
        $prefix = strtolower($word[1]);
        $tag = $word[2];
        switch ($state) {
            case 'select':
                switch ($prefix) {
                    case '+':
                    case '-':
                        $subquery = append_selection_query($prefix, $tag, $subquery, $bindings);
                        break;
                        // Go to next parsing state
                    // Go to next parsing state
                    case 'by':
                        if ($subquery != '') {
                            $query .= " WHERE {$subquery}";
                        }
                        $subquery = '';
                        $state = 'order';
                        break;
                    default:
                        throw new ParseError("Unknown prefix `{$prefix}`");
                }
                break;
            case 'order':
                $bind = false;
                switch ($tag) {
                    case '$pubDate':
                        $tag_count = "E.pubDate";
                        break;
                    default:
                        $tag_id = "(SELECT id FROM tags WHERE name = ?)";
                        $tag_count = "(SELECT COUNT(*) FROM tags_entries WHERE tag_id = {$tag_id} AND entry_id = E.id)";
                        $bind = true;
                }
                switch ($prefix) {
                    case '+':
                        if ($subquery != '') {
                            $subquery .= ", ";
                        }
                        $subquery .= "{$tag_count} ASC";
                        if ($bind) {
                            $bindings[] = $tag;
                        }
                        break;
                    case '-':
                        if ($subquery != '') {
                            $subquery .= ", ";
                        }
                        $subquery .= "{$tag_count} DESC";
                        if ($bind) {
                            $bindings[] = $tag;
                        }
                        break;
                    case 'by':
                        if ($subquery != '') {
                            $query .= " ORDER BY {$subquery}";
                        }
                        $status = 'done';
                        break;
                    default:
                        throw new ParseError("Unknown prefix `{$word['1']}`");
                }
                break;
        }
    }
    if ($limit >= 0) {
        $query .= " LIMIT {$limit}";
    }
    if ($offset >= 0) {
        $query .= " OFFSET {$offset}";
    }
    return array($query, $bindings);
}