function getWritableChunks($wootId = NULL, $restrictToCurrent = false)
{
/* Given a wootId to which the user has write-access,
* return the ids of the chunks to which the user can write.
* If restrictToCurrent is true, then the chunks should be restricted
* to those which have not been superseded by newer versions.
* If the wootId is omitted, then the chunks may be sourced from any woot.
*/
$restriction = is_admin() ? "1 " : "(wprm_UGrpID=" . get_user_id() . " or wprm_GroupID in (" . join(",", get_group_ids()) . ",-1)) and wprm_Type='RW' ";
if (!$restrictToCurrent) {
return mysql__select_array(PERMISSION_TABLE, "wprm_ChunkID", $restriction . ($wootId ? " and chunk_WootID={$wootId}" : ""));
} else {
return mysql__select_array(CHUNK_TABLE . " left join " . PERMISSION_TABLE . " on chunk_ID=wprm_ChunkID", "wprm_ChunkID", "{$restriction} and chunk_IsLatest" . ($wootId ? " and chunk_WootID={$wootId}" : "") . " and wprm_ChunkID is not null");
}
}
function doTagInsertion($bkm_ID)
{
global $usrID;
//translate bmkID to record IT
$res = mysql_query("select bkm_recID from usrBookmarks where bkm_ID={$bkm_ID}");
$rec_id = mysql_fetch_row($res);
$rec_id = $rec_id[0] ? $rec_id[0] : null;
if (!$rec_id) {
return "";
}
$tags = mysql__select_array("usrRecTagLinks, usrTags", "tag_Text", "rtl_RecID={$rec_id} and tag_ID=rtl_TagID and tag_UGrpID={$usrID} order by rtl_Order, rtl_ID");
$tagString = join(",", $tags);
// if the tags to insert is the same as the existing tags (in order) Nothing to do
if (mb_strtolower(trim($_POST["tagString"]), 'UTF-8') == mb_strtolower(trim($tagString), 'UTF-8')) {
return;
}
// create array of tags to be linked
$tags = array_filter(array_map("trim", explode(",", str_replace("\\", "/", $_POST["tagString"]))));
// replace backslashes with forwardslashes
//create a map of this user's personal tags to tagIDs
$kwd_map = mysql__select_assoc("usrTags", "trim(lower(tag_Text))", "tag_ID", "tag_UGrpID={$usrID} and tag_Text in (\"" . join("\",\"", array_map("mysql_real_escape_string", $tags)) . "\")");
$tag_ids = array();
foreach ($tags as $tag) {
$tag = preg_replace('/\\s+/', ' ', trim($tag));
$tag = mb_strtolower($tag, 'UTF-8');
if (@$kwd_map[$tag]) {
// tag exist get it's id
$tag_id = $kwd_map[$tag];
} else {
// no existing tag so add it and get it's id
$query = "insert into usrTags (tag_Text, tag_UGrpID) values (\"" . mysql_real_escape_string($tag) . "\", {$usrID})";
mysql_query($query);
$tag_id = mysql_insert_id();
}
array_push($tag_ids, $tag_id);
}
// Delete all personal tags for this bookmark's record
mysql_query("delete usrRecTagLinks from usrRecTagLinks, usrTags where rtl_RecID = {$rec_id} and tag_ID=rtl_TagID and tag_UGrpID = {$usrID}");
if (count($tag_ids) > 0) {
$query = "";
for ($i = 0; $i < count($tag_ids); ++$i) {
if ($query) {
$query .= ", ";
}
$query .= "({$rec_id}, " . ($i + 1) . ", " . $tag_ids[$i] . ")";
}
$query = "insert into usrRecTagLinks (rtl_RecID, rtl_Order, rtl_TagID) values " . $query;
mysql_query($query);
}
// return new tag string
$tags = mysql__select_array("usrRecTagLinks, usrTags", "tag_Text", "rtl_RecID = {$rec_id} and tag_ID=rtl_TagID and tag_UGrpID={$usrID} order by rtl_Order, rtl_ID");
return join(",", $tags);
}
function validateImport($params)
{
global $system;
//get rectype to import
$rty_ID = @$params['sa_rectype'];
$currentSeqIndex = @$params['seq_index'];
if (intval($rty_ID) < 1) {
$system->addError(HEURIST_INVALID_REQUEST, 'Record type not defined or wrong value');
return false;
}
$imp_session = getImportSession($params['imp_ID']);
if (is_bool($imp_session) && !$imp_session) {
return false;
//error - can not get import session
}
//add result of validation to session
$imp_session['validation'] = array("count_update" => 0, "count_insert" => 0, "count_update_rows" => 0, "count_insert_rows" => 0, "count_error" => 0, "error" => array(), "recs_insert" => array(), "recs_update" => array());
//get rectype to import
$id_field = @$params['recid_field'];
//record ID field is always defined explicitly
$ignore_insert = @$params['ignore_insert'] == 1;
//ignore new records
if (@$imp_session['columns'][substr($id_field, 6)] == null) {
$system->addError(HEURIST_INVALID_REQUEST, 'Identification field is not defined');
return false;
}
$import_table = $imp_session['import_table'];
$cnt_update_rows = 0;
$cnt_insert_rows = 0;
$mapping_params = @$params['mapping'];
$mapping = array();
// fieldtype => fieldname in import table
$sel_query = array();
if (is_array($mapping_params) && count($mapping_params) > 0) {
foreach ($mapping_params as $index => $field_type) {
$field_name = "field_" . $index;
$mapping[$field_type] = $field_name;
$imp_session['validation']['mapped_fields'][$field_name] = $field_type;
//all mapped fields - they will be used in validation query
array_push($sel_query, $field_name);
}
} else {
$system->addError(HEURIST_INVALID_REQUEST, 'Mapping is not defined');
return false;
}
$mysqli = $system->get_mysqli();
$cnt_recs_insert_nonexist_id = 0;
// validate selected record ID field
// in case id field is not created on match step (it is from original set of columns)
// we have to verify that its values are valid
if (FALSE && !@$imp_session['indexes'][$id_field]) {
//find recid with different rectype
$query = "select imp_id, " . implode(",", $sel_query) . ", " . $id_field . " from " . $import_table . " left join Records on rec_ID=" . $id_field . " where rec_RecTypeID<>" . $rty_ID;
// TODO: I'm not sure whether message below has been correctly interpreted
$wrong_records = getWrongRecords($query, $imp_session, "Your input data contain record IDs in the selected ID column for existing records which are not numeric IDs. " . "The import cannot proceed until this is corrected.", "Incorrect record types", $id_field);
if (is_array($wrong_records) && count($wrong_records) > 0) {
$wrong_records['validation']['mapped_fields'][$id_field] = 'id';
$imp_session = $wrong_records;
} else {
if ($wrong_records === false) {
return $wrong_records;
}
}
if (!$ignore_insert) {
//WARNING - it ignores possible multivalue index field
//find record ID that do not exist in HDB - to insert
$query = "select count(imp_id) " . " from " . $import_table . " left join Records on rec_ID=" . $id_field . " where " . $id_field . ">0 and rec_ID is null";
$row = mysql__select_array($mysqli, $query);
if ($row && $row[0] > 0) {
$cnt_recs_insert_nonexist_id = $row[0];
}
}
}
// find records to update
$select_query = "SELECT count(DISTINCT " . $id_field . ") FROM " . $import_table . " left join Records on rec_ID=" . $id_field . " WHERE rec_ID is not null and " . $id_field . ">0";
$cnt = mysql__select_value($mysqli, $select_query);
if ($cnt > 0) {
$imp_session['validation']['count_update'] = $cnt;
$imp_session['validation']['count_update_rows'] = $cnt;
/*
//find first 100 records to preview
$select_query = "SELECT ".$id_field.", imp_id, ".implode(",",$sel_query)
." FROM ".$import_table
." left join Records on rec_ID=".$id_field
." WHERE rec_ID is not null and ".$id_field.">0"
." ORDER BY ".$id_field." LIMIT 5000";
$imp_session['validation']['recs_update'] = mysql__select_all($mysqli, $select_query, false);
*/
$imp_session['validation']['recs_update'] = array();
}
if (!$ignore_insert) {
// find records to insert
$select_query = "SELECT count(DISTINCT " . $id_field . ") FROM " . $import_table . " WHERE " . $id_field . "<0";
//$id_field." is null OR ".
$cnt1 = mysql__select_value($mysqli, $select_query);
$select_query = "SELECT count(*) FROM " . $import_table . ' WHERE ' . $id_field . ' IS NULL';
//$id_field." is null OR ".
$cnt2 = mysql__select_value($mysqli, $select_query);
if ($cnt1 + $cnt2 > 0) {
$imp_session['validation']['count_insert'] = $cnt1 + $cnt2;
//.........这里部分代码省略.........
}
function is_admin()
{
return false;
}
function is_logged_in()
{
return false;
}
$ss_id = 0;
} else {
// loggin required entry
$ss_id = 0;
require_once dirname(__FILE__) . '/../../common/connect/applyCredentials.php';
}
$ACCESSABLE_OWNER_IDS = mysql__select_array('sysUsrGrpLinks left join sysUGrps grp on grp.ugr_ID=ugl_GroupID', 'ugl_GroupID', 'ugl_UserID=' . get_user_id() . ' and grp.ugr_Type != "user" order by ugl_GroupID');
if (is_logged_in()) {
array_push($ACCESSABLE_OWNER_IDS, get_user_id());
if (!in_array(0, $ACCESSABLE_OWNER_IDS)) {
array_push($ACCESSABLE_OWNER_IDS, 0);
}
}
//----------------------------------------------------------------------------//
// Traversal functions
// The aim here is to bundle all the queries for each level of relationships
// into one query, rather than doing them all recursively.
//----------------------------------------------------------------------------//
/**
* findPointers - Helper function that finds recIDs of record pointer details for all records in a given set of recIDs
* which can be filtered to a set of rectypes
* @author Stephen White derived from original work by Kim Jackson
function getDetailRollbacks($rec_id, $version)
{
$potential_updates = array();
$potential_deletes = array();
$updates = array();
$inserts = array();
$deletes = array();
$ard_ids = getAffectedDetails($rec_id, $version);
foreach ($ard_ids as $ard_id) {
$deltas = getDetailHistory($ard_id, $version);
if (count($deltas) === 0) {
// this detail didn't exist before or at the target version
// delete it if it exists
array_push($potential_deletes, $ard_id);
} else {
$latest = $deltas[0];
if ($latest["ard_Value"] || $latest["ard_UploadedFileID"] || $latest["ard_Geo"]) {
// an insert or update
array_push($potential_updates, $latest);
} else {
// a delete
// this shouldn't be possible - if a detail was deleted before the target version,
// it would not be in the list returned by getAffectedDetails()
array_push($potential_deletes, $ard_id);
}
}
}
$current_details = mysql__select_array("recDetails", "dtl_ID", "dtl_RecID = {$rec_id}");
foreach ($potential_deletes as $potential_delete) {
if (in_array($potential_delete, $current_details)) {
array_push($deletes, $potential_delete);
}
}
foreach ($potential_updates as $potential_update) {
if (in_array($potential_update["ard_ID"], $current_details)) {
// check if the current value is actually the same
// (this would happen if the detail has been changed, and changed back)
$ard_id = $potential_update["ard_ID"];
$ard_val = $potential_update["ard_Value"];
$ard_file_id = $potential_update["ard_UploadedFileID"];
$ard_geo = $potential_update["ard_Geo"];
$res = mysql_query("\n\t\t\t\tselect dtl_ID\n\t\t\t\tfrom recDetails\n\t\t\t\twhere dtl_ID = {$ard_id}\n\t\t\t\tand dtl_Value " . ($ard_val ? "= '" . mysql_real_escape_string($ard_val) . "'" : "is null") . "\n\t\t\t\tand dtl_UploadedFileID " . ($ard_file_id ? "= {$ard_file_id}" : "is null") . "\n\t\t\t\tand AsWKT(dtl_Geo) " . ($ard_geo ? "= '{$ard_geo}'" : "is null"));
if (mysql_num_rows($res) == 0) {
array_push($updates, $potential_update);
}
} else {
array_push($inserts, $potential_update);
}
}
return array("updates" => $updates, "inserts" => $inserts, "deletes" => $deletes);
}
请发表评论