Skip to content

Commit

Permalink
(migration) keep deleted lemmata in the same table; interface changes…
Browse files Browse the repository at this point in the history
… to distinguish them in history and on the editor page
grandsbor committed Feb 16, 2015
1 parent 55a3bfc commit 6428f69
Showing 8 changed files with 89 additions and 18 deletions.
2 changes: 1 addition & 1 deletion ajax/dict_substring_search.php
Original file line number Diff line number Diff line change
@@ -2,7 +2,7 @@
require_once('../lib/header_ajax.php');
header('Content-type: text/html; charset=utf-8');

$res = sql_pe("SELECT DISTINCT dl.lemma_id, dl.lemma_text, SUBSTR(grammems, 7, 4) AS gr FROM form2lemma fl LEFT JOIN dict_lemmata dl ON (fl.lemma_id=dl.lemma_id) WHERE fl.form_text like ? order by dl.lemma_text limit 10", array($_GET['q'] . '%'));
$res = sql_pe("SELECT DISTINCT dl.lemma_id, dl.lemma_text, SUBSTR(grammems, 7, 4) AS gr FROM form2lemma fl LEFT JOIN dict_lemmata dl ON (fl.lemma_id=dl.lemma_id) WHERE dl.deleted=0 AND fl.form_text like ? order by dl.lemma_text limit 10", array($_GET['q'] . '%'));
foreach ($res as $line) {
echo $line['lemma_text'] . '|' . $line['gr'] . '|' . $line['lemma_id'] ."\n";
}
2 changes: 1 addition & 1 deletion ajax/lemma_search.php
Original file line number Diff line number Diff line change
@@ -2,7 +2,7 @@
require_once('../lib/header_ajax.php');
require_once('../lib/lib_dict.php');
echo '<?xml version="1.0" encoding="utf-8" standalone="yes"?><lemmata>';
$res = sql_pe("SELECT lemma_id FROM dict_lemmata WHERE lemma_text=?", array($_GET['q']));
$res = sql_pe("SELECT lemma_id FROM dict_lemmata WHERE lemma_text=? AND deleted=0", array($_GET['q']));
foreach ($res as $r) {
echo '<lemma id="'.$r['lemma_id'].'"/>';
}
32 changes: 23 additions & 9 deletions lib/lib_dict.php
Original file line number Diff line number Diff line change
@@ -8,7 +8,7 @@ function get_dict_stats() {
$out = array();
$r = sql_fetch_array(sql_query("SELECT COUNT(*) AS cnt_g FROM `gram`"));
$out['cnt_g'] = $r['cnt_g'];
$r = sql_fetch_array(sql_query("SELECT COUNT(*) AS cnt_l FROM `dict_lemmata`"));
$r = sql_fetch_array(sql_query("SELECT COUNT(*) AS cnt_l FROM `dict_lemmata` WHERE deleted=0"));
$out['cnt_l'] = $r['cnt_l'];
$r = sql_fetch_array(sql_query("SELECT COUNT(*) AS cnt_f FROM `form2lemma`"));
$out['cnt_f'] = $r['cnt_f'];
@@ -22,7 +22,7 @@ function get_dict_search_results($get) {
$out = array();
$find_pos = sql_prepare("SELECT SUBSTR(grammems, 7, 4) AS gr FROM form2lemma WHERE lemma_id = ? LIMIT 1");
if (isset($get['search_lemma'])) {
$res = sql_pe("SELECT lemma_id FROM `dict_lemmata` WHERE `lemma_text`= ?", array($get['search_lemma']));
$res = sql_pe("SELECT lemma_id FROM `dict_lemmata` WHERE deleted=0 AND `lemma_text`= ?", array($get['search_lemma']));
$count = sizeof($res);
$out['lemma']['count'] = $count;
if ($count == 0)
@@ -34,7 +34,7 @@ function get_dict_search_results($get) {
}
}
elseif (isset($get['search_form'])) {
$res = sql_pe("SELECT DISTINCT dl.lemma_id, dl.lemma_text FROM `form2lemma` fl LEFT JOIN `dict_lemmata` dl ON (fl.lemma_id=dl.lemma_id) WHERE fl.`form_text`= ?", array($get['search_form']));
$res = sql_pe("SELECT DISTINCT dl.lemma_id, dl.lemma_text FROM `form2lemma` fl LEFT JOIN `dict_lemmata` dl ON (fl.lemma_id=dl.lemma_id) WHERE deleted=0 AND fl.`form_text`= ?", array($get['search_form']));
$count = sizeof($res);
$out['form']['count'] = $count;
if ($count == 0)
@@ -184,7 +184,7 @@ function parse_dict_rev($text) {
return $parsed;
}
function get_word_paradigm($lemma) {
$res = sql_pe("SELECT rev_text FROM dict_revisions LEFT JOIN dict_lemmata USING (lemma_id) WHERE lemma_text=? ORDER BY rev_id DESC LIMIT 1", array($lemma));
$res = sql_pe("SELECT rev_text FROM dict_revisions LEFT JOIN dict_lemmata USING (lemma_id) WHERE deleted=0 AND lemma_text=? ORDER BY rev_id DESC LIMIT 1", array($lemma));
if (sizeof($res) == 0)
return false;
$r = $res[0];
@@ -404,9 +404,18 @@ function get_top_absent_words() {
function get_lemma_editor($id) {
$out = array('lemma' => array('id' => $id), 'errata' => array());
if ($id == -1) return $out;
$res = sql_pe("SELECT l.`lemma_text`, d.`rev_id`, d.`rev_text` FROM `dict_lemmata` l LEFT JOIN `dict_revisions` d ON (l.lemma_id = d.lemma_id) WHERE l.`lemma_id`=? ORDER BY d.rev_id DESC LIMIT 1", array($id));
$res = sql_pe("
SELECT l.`lemma_text`, l.deleted, d.`rev_id`, d.`rev_text`
FROM `dict_lemmata` l
LEFT JOIN `dict_revisions` d
ON (l.lemma_id = d.lemma_id)
WHERE l.`lemma_id`=?
ORDER BY d.rev_id DESC
LIMIT 1
", array($id));
$arr = parse_dict_rev($res[0]['rev_text']);
$out['lemma']['text'] = $arr['lemma']['text'];
$out['deleted'] = $res[0]['deleted'];
$out['lemma']['text'] = $res[0]['lemma_text'];
$out['lemma']['grms'] = implode(', ', $arr['lemma']['grm']);
foreach ($arr['forms'] as $farr) {
$out['forms'][] = array('text' => $farr['text'], 'grms' => implode(', ', $farr['grm']));
@@ -472,7 +481,7 @@ function dict_add_lemma($array) {
}
sql_begin();
//new lemma in dict_lemmata
sql_pe("INSERT INTO dict_lemmata VALUES(NULL, ?)", array(mb_strtolower($lemma_text)));
sql_pe("INSERT INTO dict_lemmata VALUES(NULL, ?, 0)", array(mb_strtolower($lemma_text)));
$lemma_id = sql_insert_id();
//array -> xml
$new_xml = make_dict_xml($lemma_text, $lemma_gram_new, $new_paradigm);
@@ -522,6 +531,12 @@ function dict_save($array) {
if ($array['lemma_id'] == -1) {
return dict_add_lemma($array);
}

// lemma might have been deleted, it is not editable then
$r = sql_fetch_array(sql_query("SELECT deleted FROM dict_lemmata WHERE lemma_id = ".$array['lemma_id']." LIMIT 1"));
if ($r['deleted'])
throw new Exception("This lemma is not editable");

$lemma_text = trim($array['lemma_text']);
if (!$lemma_text)
throw new UnexpectedValueException();
@@ -637,8 +652,7 @@ function del_lemma($id) {
//delete forms from form2lemma
sql_pe("DELETE FROM `form2lemma` WHERE lemma_id=?", array($id));
//delete lemma
sql_pe("INSERT INTO dict_lemmata_deleted (SELECT * FROM dict_lemmata WHERE lemma_id=? LIMIT 1)", array($id));
sql_pe("DELETE FROM dict_lemmata WHERE lemma_id=? LIMIT 1", array($id));
sql_pe("UPDATE dict_lemmata SET deleted=1 WHERE lemma_id=? LIMIT 1", array($id));
sql_commit();
}
function del_link($link_id, $revset_id=0) {
3 changes: 2 additions & 1 deletion lib/lib_history.php
Original file line number Diff line number Diff line change
@@ -94,7 +94,8 @@ function dict_history($lemma_id, $skip = 0) {
'lemma_text' => $r['lemma_text'],
'lemma2_id' => $r['lemma2_id'],
'lemma2_text' => $r['lemma2_text'],
'is_link' => $r['is_link']
'is_link' => $r['is_link'],
'is_lemma_deleted' => $r['deleted']
);
}
return $out;
52 changes: 52 additions & 0 deletions migrations/20150216164636_undelete_lemmata.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

use Phinx\Migration\AbstractMigration;

class UndeleteLemmata extends AbstractMigration
{
/**
* Migrate Up.
*/
public function up()
{
$this->execute("
ALTER TABLE dict_lemmata
ADD COLUMN `deleted` tinyint(3) unsigned not null;
");
$this->execute("
ALTER TABLE dict_lemmata
ADD INDEX(`deleted`)
");
$this->execute("
INSERT INTO dict_lemmata (
SELECT lemma_id, lemma_text, 1
FROM dict_lemmata_deleted
)
");
$this->dropTable('dict_lemmata_deleted');
}

/**
* Migrate Down.
*/
public function down()
{
$this->table('dict_lemmata_deleted', array('id' => false))
->addColumn('lemma_id', 'integer')
->addColumn('lemma_text', 'string', array('limit' => 50))
->save();
$this->execute("
INSERT INTO dict_lemmata_deleted (
SELECT lemma_id, lemma_text
FROM dict_lemmata
WHERE deleted=1
)
");
$this->execute("
DELETE FROM dict_lemmata
WHERE deleted=1
");
$this->table('dict_lemmata')->removeIndex(array('deleted'));
$this->table('dict_lemmata')->removeColumn('deleted');
}
}
2 changes: 1 addition & 1 deletion scripts/stats/update_stats.pl
Original file line number Diff line number Diff line change
@@ -163,7 +163,7 @@ sub words_in_file {
return $sc->fetchrow_hashref()->{'cnt'};
};
$func->{'total_lemmata'} = sub {
my $sc = $dbh->prepare("SELECT COUNT(*) AS cnt FROM dict_lemmata");
my $sc = $dbh->prepare("SELECT COUNT(*) AS cnt FROM dict_lemmata WHERE deleted=0");
$sc->execute();
return $sc->fetchrow_hashref()->{'cnt'};
};
2 changes: 1 addition & 1 deletion templates/dict/history.tpl
Original file line number Diff line number Diff line change
@@ -19,7 +19,7 @@
{if $h.is_link && isset($smarty.get.lemma_id) && $smarty.get.lemma_id != $h.lemma_id}
<a href="dict.php?act=edit&amp;id={$h.lemma2_id}">{$h.lemma2_text}</a>
{else}
<a href="dict.php?act=edit&amp;id={$h.lemma_id}">{$h.lemma_text}</a>
<a href="dict.php?act=edit&amp;id={$h.lemma_id}" {if $h.is_lemma_deleted}class="bgpink"{/if}>{$h.lemma_text}</a>
{/if}
</td>
<td>
12 changes: 8 additions & 4 deletions templates/dict/lemma_edit.tpl
Original file line number Diff line number Diff line change
@@ -36,7 +36,11 @@
{/if}
<a href="#" onclick="$(this).closest('form').submit()">&lt;&lt;&nbsp;к поиску</a>
</form></p-->
{if $editor.deleted}
<h1 class="bgpink">Лемма &laquo;{$editor.lemma.text|htmlspecialchars}&raquo; удалена</h1>
{else}
<h1>Лемма &laquo;{$editor.lemma.text|htmlspecialchars}&raquo;</h1>
{/if}
<ul class="breadcrumb">
<li><a href="{$web_prefix}/dict.php">Словарь</a> <span class="divider">/</span></li>
<li><a href="{$web_prefix}/dict.php">Поиск</a> <span class="divider">/</span></li>
@@ -74,7 +78,7 @@
<input type="text" name="lemma_text" value="{$editor.lemma.text|htmlspecialchars}">
<input type="text" name="lemma_gram"{if !$user_permission_dict}readonly="readonly"{/if} value="{$editor.lemma.grms|htmlspecialchars}" size="40">
<button class="btn" type="button" onClick="location.href='dict_history.php?lemma_id={$editor.lemma.id}'" >История</button>
{if $user_permission_dict}<button type="button" class="btn" onClick="if (confirm('Вы уверены?')) location.href='dict.php?act=del_lemma&lemma_id={$editor.lemma.id}'" >Удалить лемму</button>{/if}
{if $user_permission_dict && !$editor.deleted}<button type="button" class="btn" onClick="if (confirm('Вы уверены?')) location.href='dict.php?act=del_lemma&lemma_id={$editor.lemma.id}'" >Удалить лемму</button>{/if}
{else}
<input type="text" name="lemma_text" id="lemma_txt" value="{$smarty.get.text}"/>
<input type="text"name="lemma_gram" id="lemma_gr" placeholder="граммемы" size="40"/><br/>
@@ -91,11 +95,11 @@
<td><input type='text' name='form_gram[]' {if !$user_permission_dict}readonly="readonly"{/if} size='40' value="{$form.grms|htmlspecialchars}"/>
</tr>
{/foreach}
{if $user_permission_dict}
{if $user_permission_dict && !$editor.deleted}
<tr><td>&nbsp;<td><a id="add_form_link" class="pseudo" href="#">Добавить ещё одну форму</a></tr>
{/if}
</table><br/>
{if $user_permission_dict}
{if $user_permission_dict && !$editor.deleted}
Комментарий к правке:<br/>
<input name='comment' type='text' size='60'/><br/>
<input type="button" class='btn btn-primary' onclick="submit_with_readonly_check($(this).closest('form'))" value="Сохранить"/>&nbsp;&nbsp;
@@ -104,7 +108,7 @@
</form>
{/strip}
<p><b>Связи</b></p>
{if $user_permission_dict}
{if $user_permission_dict && !$editor.deleted}
<p><a href="#" class="pseudo" onclick="$('#add_link').show(); return false">Добавить связь</a></p>
<form id="add_link" method='post' class='hidden-block' action='?act=add_link'>
<input type='hidden' name='from_id' value='{$editor.lemma.id}'/>

0 comments on commit 6428f69

Please sign in to comment.