Example #1
0
function split_token($token_id, $num)
{
    //$num is the number of characters (in the beginning) that should become a separate token
    check_permission(PERM_ADDER);
    if (is_token_covered_by_ne_markup($token_id)) {
        throw new Exception("Cannot split token under NE markup");
    }
    if (!$token_id || !$num) {
        throw new UnexpectedValueException();
    }
    $res = sql_pe("SELECT tf_text, sent_id, pos FROM tokens WHERE tf_id=? LIMIT 1", array($token_id));
    if (sizeof($res) == 0) {
        throw new Exception();
    }
    $r = $res[0];
    $text1 = trim(mb_substr($r['tf_text'], 0, $num));
    $text2 = trim(mb_substr($r['tf_text'], $num));
    if (!$text1 || !$text2) {
        throw new Exception();
    }
    sql_begin();
    //create revset
    $revset_id = create_revset("Token {$token_id} (<" . $r['tf_text'] . ">) split to <{$text1}> and <{$text2}>");
    $token_for_form2tf = str_replace('ё', 'е', mb_strtolower($text1));
    //update other tokens in the sentence
    sql_query("UPDATE tokens SET pos=pos+1 WHERE sent_id = " . $r['sent_id'] . " AND pos > " . $r['pos']);
    //create new token and parse
    sql_pe("INSERT INTO tokens VALUES(NULL, ?, ?, ?)", array($r['sent_id'], $r['pos'] + 1, $text2));
    $parse1 = new MorphParseSet(false, $text1);
    $parse2 = new MorphParseSet(false, $text2);
    create_tf_revision($revset_id, sql_insert_id(), $parse2->to_xml());
    //update old token and parse
    sql_pe("DELETE FROM form2tf WHERE tf_id=?", array($token_id));
    sql_pe("UPDATE tokens SET tf_text=? WHERE tf_id=? LIMIT 1", array($text1, $token_id));
    sql_pe("INSERT INTO form2tf VALUES(?, ?)", array($token_for_form2tf, $token_id));
    create_tf_revision($revset_id, $token_id, $parse1->to_xml());
    //dropping sentence status
    $res = sql_pe("SELECT sent_id FROM tokens WHERE tf_id=? LIMIT 1", array($token_id));
    $sent_id = $res[0]['sent_id'];
    sql_query("UPDATE sentences SET check_status='0' WHERE sent_id={$sent_id} LIMIT 1");
    sql_query("DELETE FROM sentence_check WHERE sent_id={$sent_id}");
    sql_commit();
    $res = sql_query("SELECT book_id FROM paragraphs WHERE par_id = (SELECT par_id FROM sentences WHERE sent_id={$sent_id} LIMIT 1)");
    $r = sql_fetch_array($res);
    return array($r['book_id'], $sent_id);
}
Example #2
0
function update_pending_token($token_id, $rev_id, $revset_id = 0, $smart = false)
{
    check_permission(PERM_DICT);
    if (!check_safe_token_update($token_id, $rev_id)) {
        throw new Exception("Update forbidden");
    }
    // ok, now we can safely update
    $res = sql_pe("SELECT tf_text FROM tokens WHERE tf_id=? LIMIT 1", array($token_id));
    $token_text = $res[0]['tf_text'];
    $res = sql_pe("SELECT rev_text FROM tf_revisions WHERE tf_id=? AND is_last=1 LIMIT 1", array($token_id));
    $previous_rev = $res[0]['rev_text'];
    if ($smart) {
        $parse = new MorphParseSet($previous_rev);
        smart_update_pending_token($parse, $rev_id);
    } else {
        $parse = new MorphParseSet(false, $token_text);
    }
    $new_rev = $parse->to_xml();
    // do nothing if nothing changed
    if ($previous_rev == $new_rev) {
        forget_pending_token($token_id, $rev_id);
        return true;
    }
    sql_begin();
    if (!$revset_id) {
        $revset_id = create_revset("Update tokens from dictionary");
    }
    create_tf_revision($revset_id, $token_id, $new_rev);
    forget_pending_token($token_id, $rev_id);
    delete_samples_by_token_id($token_id);
    sql_commit();
}
Example #3
0
function sentence_save($sent_id)
{
    check_permission(PERM_DISAMB);
    if (!$sent_id) {
        throw new UnexpectedValueException();
    }
    $flag = $_POST['var_flag'];
    //what morphovariants are checked as possible (array of arrays)
    $dict = $_POST['dict_flag'];
    //whether this token has been reloaded from the dictionary (array)
    $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    ");
    while ($r = sql_fetch_array($res)) {
        $tokens[$r['tf_id']] = array($r['tf_text'], $r['rev_text']);
    }
    $matches = array();
    $all_changes = array();
    if (count($flag) != count($tokens)) {
        throw new Exception();
    }
    sql_begin();
    foreach ($tokens as $tf_id => $v) {
        list($tf_text, $base_xml) = $v;
        //substitute the last revision's xml for one from dictionary if relevant
        if (isset($dict[$tf_id]) && $dict[$tf_id] == 1) {
            $parse = new MorphParseSet(false, $tf_text, false, true);
        } else {
            $parse = new MorphParseSet($base_xml);
        }
        if (sizeof($parse->parses) == 0) {
            throw new Exception();
        }
        // flags quantity check
        if (sizeof($parse->parses) != sizeof($flag[$tf_id])) {
            throw new Exception();
        }
        // XXX this is bad since order of parse selection from db
        //    is not guaranteed to be consistent
        $parse->filter_by_parse_index(prepare_parse_indices($flag[$tf_id]));
        $new_xml = $parse->to_xml();
        if ($base_xml != $new_xml) {
            //something's changed
            array_push($all_changes, array($tf_id, $new_xml));
        }
    }
    if (count($all_changes) > 0) {
        $revset_id = create_revset($_POST['comment']);
        foreach ($all_changes as $v) {
            create_tf_revision($revset_id, $v[0], $v[1]);
        }
    }
    sql_query("UPDATE sentences SET check_status='1' WHERE sent_id={$sent_id} LIMIT 1");
    sql_commit();
}
Example #4
0
function addtext_add($text, $sentences, $book_id, $par_num)
{
    check_permission(PERM_ADDER);
    if (!$text || !$book_id || !$par_num) {
        throw new UnexpectedValueException();
    }
    if (sizeof(sql_pe("SELECT book_id FROM books WHERE parent_id=?", array($book_id))) > 0) {
        throw new UnexpectedValueException("Can't add paragraphs to a text having subtexts");
    }
    sql_begin();
    $revset_id = create_revset();
    $sent_count = 0;
    $pars = split2paragraphs($text);
    // move the following paragraphs
    sql_query("UPDATE paragraphs SET pos=pos+" . sizeof($pars) . " WHERE book_id = {$book_id} AND pos >= {$par_num}");
    $par_ins = sql_prepare("INSERT INTO `paragraphs` VALUES(NULL, ?, ?)");
    $sent_ins = sql_prepare("INSERT INTO `sentences` VALUES(NULL, ?, ?, ?, 0)");
    $token_ins = sql_prepare("INSERT INTO `tokens` VALUES(NULL, ?, ?, ?)");
    foreach ($pars as $par) {
        //adding a paragraph
        sql_execute($par_ins, array($book_id, $par_num++));
        $par_id = sql_insert_id();
        $sent_num = 1;
        $sents = split2sentences($par);
        foreach ($sents as $sent) {
            if (!preg_match('/\\S/', $sent)) {
                continue;
            }
            //adding a sentence
            sql_execute($sent_ins, array($par_id, $sent_num++, trim($sent)));
            $sent_id = sql_insert_id();
            sql_query("INSERT INTO sentence_authors VALUES({$sent_id}, " . $_SESSION['user_id'] . ", " . time() . ")");
            $token_num = 1;
            $tokens = explode('^^', $sentences[$sent_count++]);
            foreach ($tokens as $token) {
                if (trim($token) === '') {
                    continue;
                }
                //adding a textform
                sql_execute($token_ins, array($sent_id, $token_num++, trim($token)));
                $tf_id = sql_insert_id();
                //adding a revision
                $parse = new MorphParseSet(false, trim($token));
                create_tf_revision($revset_id, $tf_id, $parse->to_xml());
            }
        }
    }
    sql_commit();
}