restore prod / thesaurus : delete or replace candidates

This commit is contained in:
jygaulier
2012-10-31 20:38:19 +01:00
parent 0a84e02106
commit 8beeaebdab
2 changed files with 63 additions and 302 deletions

View File

@@ -1,4 +1,3 @@
function thesau_show() function thesau_show()
{ {
if(p4.thesau.currentWizard == "???") // first show of thesaurus if(p4.thesau.currentWizard == "???") // first show of thesaurus
@@ -213,27 +212,16 @@ function T_filter_delayed2(f, delay, mode)
// ====================================================================================================== // ======================================================================================================
function T_replaceCandidates_OK(dlgnode) function T_replaceCandidates_OK(dlgnode)
{ {
$("#THPD_confirm_replace_dlg_msg").html("{% trans 'prod::thesaurusTab:dlg:Remplacement en cours.' %}"); $(dlgnode).dialog().html("{% trans 'prod::thesaurusTab:dlg:Remplacement en cours.' %}");
// 3 cases
// the new term already exists (possibly many times) in the thesaurus
// the new term already exists in the candidates
// the new term is brand new
// the simpliest solution is to replace the terms and let the indexer work
var parms = { var parms = {
url: "/xmlhttp/replacecandidate.j.php", url: "/xmlhttp/replacecandidate.j.php",
data: { data: {
"sbid" : trees.C._toReplace.sbas "id[]" : trees.C._toReplace.sbas + "." + trees.C._toReplace.cid
, "cid" : trees.C._toReplace.cid , "t" : trees.C._toReplace.replaceby
, "t" : trees.C._toReplace.replaceby , "debug" : '0'
// , "debug" : '1'
}, },
async: false, async: false,
cache: false, cache: false,
@@ -243,7 +231,21 @@ function T_replaceCandidates_OK(dlgnode)
{ {
trees.C._toReplace = null; trees.C._toReplace = null;
thesauShowWizard("wiz_0", false); thesauShowWizard("wiz_0", false);
$(dlgnode).dialog("close"); $(dlgnode).dialog("close");
var msg = $.sprintf("{% trans 'prod::thesaurusTab:dlg: %s record(s) updated' %}", result.nRecsUpdated);
if(result.msg != '')
{
msg = result.msg + '\n' + msg;
}
alert(msg);
for(i in result.ctermsDeleted)
{
var cid = "#CX_P\\." + result.ctermsDeleted[i].replace(new RegExp("\\.", "g"), "\\."); // escape les '.' pour jquery
$(cid).remove();
}
}, },
_ret: null // private alchemy _ret: null // private alchemy
}; };
@@ -254,7 +256,7 @@ function T_replaceCandidates_OK(dlgnode)
function T_acceptCandidates_OK(dlgnode) function T_acceptCandidates_OK(dlgnode)
{ {
$("#THPD_confirm_accept_dlg_msg").html("{% trans 'prod::thesaurusTab:dlg:Acceptation en cours.' %}"); $(dlgnode).dialog().html("{% trans 'prod::thesaurusTab:dlg:Acceptation en cours.' %}");
var t_ids = []; var t_ids = [];
var dst = trees.C._toAccept.dst.split('.'); var dst = trees.C._toAccept.dst.split('.');
@@ -264,20 +266,19 @@ function T_acceptCandidates_OK(dlgnode)
same_sbas = true; same_sbas = true;
// obviously the candidates and the target already complies (same sbas, good tbranch) // obviously the candidates and the target already complies (same sbas, good tbranch)
trees.C._selInfos.sel.each( trees.C._selInfos.sel.each(
function() function()
{ {
var x = this.getAttribute('id').split('.'); var x = this.getAttribute('id').split('.');
x.shift(); x.shift();
if(x.shift() != sbid) if(x.shift() != sbid)
same_sbas = false; same_sbas = false;
t_ids.push(x.join('.')); t_ids.push(x.join('.'));
} }
); );
if(!same_sbas) if(!same_sbas)
return; return;
var parms = { var parms = {
url: "/xmlhttp/acceptcandidates.j.php", url: "/xmlhttp/acceptcandidates.j.php",
data: { data: {
@@ -319,8 +320,6 @@ function T_acceptCandidates_OK(dlgnode)
} }
, "json"); , "json");
} }
// $("#THPD_confirm_accept_dlg_msg").dialog("close");
}, },
error:function(){}, error:function(){},
timeout:function(){}, timeout:function(){},
@@ -331,22 +330,22 @@ function T_acceptCandidates_OK(dlgnode)
} }
function C_deleteCandidates_OK() function C_deleteCandidates_OK(dlgnode)
{ {
$("#THPD_confirm_del_dlg_msg").html("{% trans 'prod::thesaurusTab:dlg:Suppression en cours.' %}"); $(dlgnode).dialog().html("{% trans 'prod::thesaurusTab:dlg:Suppression en cours.' %}");
var t_ids = []; var t_ids = [];
var lisel = trees.C.tree.find("LI .selected"); var lisel = trees.C.tree.find("LI .selected");
trees.C.tree.find("LI .selected").each( trees.C.tree.find("LI .selected").each(
function() function()
{ {
var x = this.getAttribute('id').split('.'); var x = this.getAttribute('id').split('.');
x.shift(); x.shift();
t_ids.push(x.join('.')); t_ids.push(x.join('.'));
} }
); );
var parms = { var parms = {
url:"/xmlhttp/deletecandidates.j.php", url:"/xmlhttp/replacecandidate.j.php",
data:{"id[]":t_ids}, data:{"id[]":t_ids},
async:false, async:false,
cache:false, cache:false,
@@ -354,12 +353,20 @@ function C_deleteCandidates_OK()
timeout:10*60*1000, // 10 minutes ! timeout:10*60*1000, // 10 minutes !
success: function(result, textStatus) success: function(result, textStatus)
{ {
for(i in result) $(dlgnode).dialog("close");
var msg = $.sprintf("{% trans 'prod::thesaurusTab:dlg: %s record(s) updated' %}", result.nRecsUpdated);
if(result.msg != '')
{ {
var cid = "#CX_P\\." + result[i].replace(new RegExp("\\.", "g"), "\\."); // escape les '.' pour jquery msg = result.msg + '\n' + msg;
}
alert(msg);
for(i in result.ctermsDeleted)
{
var cid = "#CX_P\\." + result.ctermsDeleted[i].replace(new RegExp("\\.", "g"), "\\."); // escape les '.' pour jquery
$(cid).remove(); $(cid).remove();
} }
$("#THPD_confirm_del_dlg").dialog("close");
}, },
_ret: null _ret: null
}; };
@@ -1138,15 +1145,14 @@ function startThesaurus(){
buttons:{ buttons:{
"{% trans 'boutton::ok' %}":function() "{% trans 'boutton::ok' %}":function()
{ {
C_deleteCandidates_OK(); C_deleteCandidates_OK(this);
}, },
"{% trans 'boutton::annuler' %}":function() "{% trans 'boutton::annuler' %}":function()
{ {
$(this).dialog("close"); $(this).dialog("close");
} }
} }
} });
);
$("#THPD_confirm_accept_dlg").dialog( { $("#THPD_confirm_accept_dlg").dialog( {
@@ -1165,8 +1171,7 @@ function startThesaurus(){
$(this).dialog("close"); $(this).dialog("close");
} }
} }
} });
);
$("#THPD_confirm_replace_dlg").dialog( { $("#THPD_confirm_replace_dlg").dialog( {
@@ -1185,8 +1190,7 @@ function startThesaurus(){
$(this).dialog("close"); $(this).dialog("close");
} }
} }
} });
);
trees.T.tree.contextMenu( trees.T.tree.contextMenu(
@@ -1341,6 +1345,16 @@ function startThesaurus(){
// glue selection info to the tree // glue selection info to the tree
trees.C._selInfos = {'sel':lisel, 'field':field, 'sbas':sbas, 'n':lisel.length} ; trees.C._selInfos = {'sel':lisel, 'field':field, 'sbas':sbas, 'n':lisel.length} ;
// $(this.menu).find('.context-menu-item')[{{ thesau_languages|length }}].addClass('context-menu-item-disabled');
if(lisel.length == 1)
{
$(this.menu).find('.context-menu-item').eq({{ thesau_languages|length }}).removeClass('context-menu-item-disabled');
}
else
{
$(this.menu).find('.context-menu-item').eq({{ thesau_languages|length }}).addClass('context-menu-item-disabled');
}
} }
else else
{ {

View File

@@ -1,253 +0,0 @@
<?php
/*
* This file is part of Phraseanet
*
* (c) 2005-2012 Alchemy
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
*
* @license http://opensource.org/licenses/gpl-3.0 GPLv3
* @link www.phraseanet.com
*/
require_once __DIR__ . "/../../lib/bootstrap.php";
$request = http_request::getInstance();
$parm = $request->get_parms(
'id', 'debug'
);
phrasea::headers(200, true, 'application/json', 'UTF-8', false);
define('SEARCH_REPLACE_MAXREC', 25);
$tsbas = array();
$ret = array(
'maxRecsUpdatable'=>SEARCH_REPLACE_MAXREC,
'nRecsToUpdate'=>0,
'nRecsUpdated'=>0,
'msg'=>''
);
foreach ($parm['id'] as $id) {
$id = explode('.', $id);
$sbas_id = array_shift($id);
if ( ! array_key_exists('b' . $sbas_id, $tsbas)) {
$tsbas['b' . $sbas_id] = array(
'sbas_id' => (int) $sbas_id,
'tids' => array(),
'domct' => null,
'tvals' => array(),
'lid' => '',
'trids' => array()
);
}
$tsbas['b' . $sbas_id]['tids'][] = implode('.', $id);
}
if ($parm['debug']) {
var_dump($tsbas);
}
$appbox = \appbox::get_instance(\bootstrap::getCore());
// first, count the number of records to update
foreach ($tsbas as $ksbas=>$sbas) {
/* @var $databox databox */
try {
$databox = $appbox->get_databox($sbas['sbas_id']);
$connbas = $databox->get_connection();
// $domth = $databox->get_dom_thesaurus();
$tsbas[$ksbas]['domct'] = $databox->get_dom_cterms();
} catch (Exception $e) {
continue;
}
if ( ! $tsbas[$ksbas]['domct']) {
continue;
}
$lid = '';
$xpathct = new DOMXPath($tsbas[$ksbas]['domct']);
foreach ($sbas['tids'] as $tid) {
$xp = '//te[@id="' . $tid . '"]/sy';
$nodes = $xpathct->query($xp);
if ($nodes->length == 1) {
$sy = $term = $nodes->item(0);
$syid = str_replace('.', 'd', $sy->getAttribute('id')) . 'd';
$lid .= ( $lid ? ',' : '') . "'" . $syid . "'";
$field = $sy->parentNode->parentNode->getAttribute('field');
if ( ! array_key_exists($field, $tsbas[$ksbas]['tvals'])) {
$tsbas[$ksbas]['tvals'][$field] = array();
}
$tsbas[$ksbas]['tvals'][$field][] = $sy;
}
}
if ($lid == '') {
// no cterm was found
continue;
}
$tsbas[$ksbas]['lid'] = $lid;
// count records
$sql = 'SELECT DISTINCT record_id AS r'
.' FROM thit WHERE value IN (' . $lid . ') ORDER BY record_id';
$stmt = $connbas->prepare($sql);
$stmt->execute();
if ($parm['debug']) {
printf("(%d) sql: \n", __LINE__);
var_dump($sql);
}
$tsbas[$ksbas]['trids'] = $stmt->fetchAll(PDO::FETCH_COLUMN, 0);
$stmt->closeCursor();
$ret['nRecsToUpdate'] += count($tsbas[$ksbas]['trids']);
}
if ($parm['debug']) {
printf("(%d) nRecsToUpdate = %d \ntsbas: \n", __LINE__, $ret['nRecsToUpdate']);
print_r($tsbas);
}
if($ret['nRecsToUpdate'] < SEARCH_REPLACE_MAXREC)
{
$unicode = new unicode;
foreach ($tsbas as $sbas) {
/* @var $databox databox */
try {
$databox = $appbox->get_databox($sbas['sbas_id']);
$connbas = $databox->get_connection();
} catch (Exception $e) {
continue;
}
// delete the branch from the cterms
if ($parm['debug']) {
printf("cterms before :\n%s \n", $sbas['domct']->saveXML());
}
foreach($sbas['tvals'] as $tval) {
foreach($tval as $sy) {
// remove candidate from cterms
$te = $sy->parentNode;
$te->parentNode->removeChild($te);
}
}
if ($parm['debug']) {
printf("cterms after :\n%s \n", $sbas['domct']->saveXML());
}
if ( ! $parm['debug']) {
$databox->saveCterms($sbas['domct']);
}
// fix caption of records
foreach ($sbas['trids'] as $rid) {
if ($parm['debug']) {
printf("(%d) ======== working on record_id = %d ======= \n", __LINE__, $rid);
}
try {
$record = $databox->get_record($rid);
$metadatask = array(); // datas to keep
$metadatasd = array(); // datas to delete
/* @var $field caption_field */
foreach ($record->get_caption()->get_fields(null, true) as $field) {
$meta_struct_id = $field->get_meta_struct_id();
if ($parm['debug']) {
printf("(%d) field '%s' meta_struct_id=%s \n", __LINE__, $field->get_name(), $meta_struct_id);
}
/* @var $v caption_Field_Value */
$fname = $field->get_name();
if(!array_key_exists($fname, $sbas['tvals'])) {
foreach ($field->get_values() as $v) {
if ($parm['debug']) {
printf("(%d) ...v = '%s' (meta_id=%s) keep \n", __LINE__, $v->getValue(), $v->getId());
}
$metadatask[] = array(
'meta_struct_id' => $meta_struct_id,
'meta_id' => $v->getId(),
'value' => $v->getValue()
);
}
}
else {
foreach ($field->get_values() as $v) {
$keep = true;
$vtxt = $unicode->remove_indexer_chars($v->getValue());
foreach($sbas['tvals'][$fname] as $sy) {
if ($sy->getAttribute('w') == $vtxt) {
$keep = false;
}
}
if ($parm['debug']) {
printf("(%d) ...v = '%s' (meta_id=%s) %s \n", __LINE__, $v->getValue(), $v->getId(), ($keep ? '' : '!!! drop !!!'));
}
if ($keep) {
$metadatask[] = array(
'meta_struct_id' => $meta_struct_id,
'meta_id' => $v->getId(),
'value' => $v->getValue()
);
} else {
$metadatasd[] = array(
'meta_struct_id' => $meta_struct_id,
'meta_id' => $v->getId(),
'value' => ''
);
}
}
}
}
if ($parm['debug']) {
printf("(%d) metadatas: \n", __LINE__);
var_dump($metadatasd);
}
if(count($metadatasd) > 0) {
if ( ! $parm['debug']) {
// foreach (array('idx', 'prop', 'thit') as $t) {
// $sql = 'DELETE FROM ' . $t . ' WHERE record_id = :record_id';
// $stmt = $connbas->prepare($sql);
// $stmt->execute(array(':record_id' => $rid));
// $stmt->closeCursor();
// }
$record->set_metadatas($metadatasd, true);
$ret['nRecsUpdated']++;
}
}
} catch (\Exception $e) {
continue;
}
}
}
}
else {
// too many records to update
$ret['msg'] = 'too many records to update';
}
/**
* @todo respecter les droits d'editing par collections
*/
print(p4string::jsonencode($ret));