예제 #1
0
function get_book_page($book_id, $full = false, $override_hidden = false)
{
    if (!$override_hidden) {
        check_book_hidden($book_id);
    }
    $res = sql_pe("SELECT * FROM `books` WHERE `book_id`=? LIMIT 1", array($book_id));
    if (!sizeof($res)) {
        throw new UnexpectedValueException();
    }
    $out = array('id' => $book_id, 'title' => $res[0]['book_name'], 'select' => get_books_for_select(), 'is_wikinews' => $res[0]['parent_id'] == 56, 'is_chaskor_news' => $res[0]['parent_id'] == 226);
    get_book_tags($book_id, $out);
    //sub-books
    foreach (sql_pe("SELECT book_id, book_name FROM books WHERE parent_id=? ORDER BY book_name", array($book_id)) as $r) {
        $out['children'][] = array('id' => $r['book_id'], 'title' => $r['book_name']);
    }
    //parents
    $out['parents'] = array_reverse(get_book_parents($book_id));
    //sentences
    if ($full) {
        $q = "SELECT p.`pos` ppos, par_id, s.sent_id, s.`pos` spos";
        if (user_has_permission(PERM_ADDER)) {
            $q .= ", ss.status";
        }
        $q .= "\nFROM paragraphs p\n            LEFT JOIN sentences s\n            USING (par_id)\n";
        if (user_has_permission(PERM_ADDER)) {
            $q .= "LEFT JOIN sentence_check ss ON (s.sent_id = ss.sent_id AND ss.status=1 AND ss.user_id=" . $_SESSION['user_id'] . ")\n";
        }
        $q .= "WHERE p.book_id = ?\n            ORDER BY p.`pos`, s.`pos`";
        $res = sql_pe($q, array($book_id));
        $res1 = sql_prepare("SELECT tf_id, tf_text FROM tokens WHERE sent_id=? ORDER BY pos");
        foreach ($res as $r) {
            sql_execute($res1, array($r['sent_id']));
            $tokens = array();
            while ($r1 = sql_fetch_array($res1)) {
                $tokens[] = array('text' => $r1['tf_text'], 'id' => $r1['tf_id']);
            }
            $new_a = array('id' => $r['sent_id'], 'pos' => $r['spos'], 'tokens' => $tokens);
            if (user_has_permission(PERM_ADDER)) {
                $new_a['checked'] = $r['status'];
            }
            $out['paragraphs'][$r['ppos']]['sentences'][] = $new_a;
            $out['paragraphs'][$r['ppos']]['id'] = $r['par_id'];
        }
    } else {
        $res = sql_pe("SELECT p.`pos` ppos, s.sent_id, s.`pos` spos FROM paragraphs p LEFT JOIN sentences s ON (p.par_id = s.par_id) WHERE p.book_id = ? ORDER BY p.`pos`, s.`pos`", array($book_id));
        foreach ($res as $r) {
            $r1 = sql_fetch_array(sql_query("SELECT source, SUBSTRING_INDEX(source, ' ', 6) AS `cnt` FROM sentences WHERE sent_id=" . $r['sent_id'] . " LIMIT 1"));
            if ($r1['source'] === $r1['cnt']) {
                $out['paragraphs'][$r['ppos']]['sentences'][] = array('pos' => $r['spos'], 'id' => $r['sent_id'], 'snippet' => $r1['source']);
                continue;
            }
            $snippet = '';
            $r1 = sql_fetch_array(sql_query("SELECT SUBSTRING_INDEX(source, ' ', 3) AS `start` FROM sentences WHERE sent_id=" . $r['sent_id'] . " LIMIT 1"));
            $snippet = $r1['start'];
            if ($snippet) {
                $snippet .= '... ';
            }
            $r1 = sql_fetch_array(sql_query("SELECT SUBSTRING_INDEX(source, ' ', -3) AS `end` FROM sentences WHERE sent_id=" . $r['sent_id'] . " LIMIT 1"));
            $snippet .= $r1['end'];
            $out['paragraphs'][$r['ppos']]['sentences'][] = array('pos' => $r['spos'], 'id' => $r['sent_id'], 'snippet' => $snippet);
        }
    }
    return $out;
}
예제 #2
0
function get_sentence($sent_id)
{
    global $config;
    $r = sql_fetch_array(sql_query("SELECT `check_status`, source FROM sentences WHERE sent_id={$sent_id} LIMIT 1"));
    $out = array('id' => $sent_id, 'next_id' => get_next_sentence_id($sent_id), 'prev_id' => get_previous_sentence_id($sent_id), 'status' => $r['check_status'], 'source' => $r['source']);
    //counting comments
    $r = sql_fetch_array(sql_query("SELECT COUNT(comment_id) comm_cnt FROM sentence_comments WHERE sent_id={$sent_id}"));
    $out['comment_count'] = $r['comm_cnt'];
    //looking for source name
    $r = sql_fetch_array(sql_query("\n        SELECT book_id, old_syntax_moder_id\n        FROM books\n        WHERE book_id = (\n            SELECT book_id\n            FROM paragraphs\n            WHERE par_id = (\n                SELECT par_id\n                FROM sentences\n                WHERE sent_id={$sent_id}\n                LIMIT 1\n            )\n        )\n    "));
    $out['book_id'] = $book_id = $r['book_id'];
    check_book_hidden($book_id);
    $out['syntax_moder_id'] = $r['old_syntax_moder_id'];
    $r = sql_fetch_array(sql_query("\n        SELECT book_name\n        FROM books\n        WHERE book_id = (\n            SELECT parent_id\n            FROM books\n            WHERE book_id = {$book_id}\n            LIMIT 1\n        )\n    "));
    $out['book_name'] = $r['book_name'];
    //looking for url
    $res = sql_query("\n        SELECT tag_name\n        FROM book_tags\n        WHERE book_id = " . $book_id);
    while ($r = sql_fetch_array($res)) {
        if (substr($r['tag_name'], 0, 4) == 'url:') {
            $out['url'] = substr($r['tag_name'], 4);
            break;
        }
    }
    $tf_text = array();
    // TODO we'd better preload all grammemes info to save queries
    $res = sql_query("\n        SELECT tf_id, tf_text, rev_text\n        FROM tokens\n        LEFT JOIN tf_revisions\n            USING (tf_id)\n        WHERE sent_id={$sent_id}\n        AND is_last = 1\n        ORDER BY `pos`\n    ");
    $j = 0;
    //token position, for further highlighting
    while ($r = sql_fetch_array($res)) {
        array_push($tf_text, '<span id="src_token_' . $j++ . '">' . htmlspecialchars($r['tf_text']) . '</span>');
        $parse = new MorphParseSet($r['rev_text']);
        $out['tokens'][] = array('tf_id' => $r['tf_id'], 'tf_text' => $r['tf_text'], 'variants' => $parse->parses);
    }
    $out['fulltext'] = typo_spaces(implode(' ', $tf_text), 1);
    return $out;
}