コード例 #1
0
ファイル: functions.php プロジェクト: sdgdsffdsfff/json-db
function sync_affected_fields($table_root, $item_id, $ori_data = null)
{
    $item_id = intval($item_id);
    $affected_files = array();
    //找出sameid的访问路径,只取第一个出现的
    $schema = object_read("{$table_root}/schema.json");
    $fields = @$schema['fields'];
    $sameid_group = null;
    $sameid_key = null;
    $shared_keys = array();
    $rank_keys = array();
    foreach ($fields as $group => $items) {
        foreach ($items as $key => $value) {
            if ($sameid_key === null) {
                if (is_group_key($value)) {
                    $sameid_group = $group;
                    $sameid_key = $key;
                }
            }
            if (is_share_key($schema, $key, $value)) {
                $shared_keys[] = $key;
            }
            if (is_rank_key($schema, $key, $value)) {
                $rank_keys[] = $key;
            }
        }
    }
    //如果没有发现关联id的键,则退出
    if (empty($sameid_key)) {
        return $affected_files;
    }
    //枚举出所有的sameid组成员
    $sameid_objs = array();
    $onebox_list = array();
    $checked_ids = array();
    $uncheck_ids = array($item_id);
    $data_file = "{$table_root}/{$item_id}.json";
    $new_data = object_read($data_file);
    $onebox_list[$item_id] = onebox_object($schema, $data_file, $new_data);
    //需要删除的ID列表
    $rm_ids = remove_ids($ori_data, $new_data, $sameid_group, $sameid_key);
    //枚举
    while (!empty($uncheck_ids)) {
        $id = array_pop($uncheck_ids);
        $data_file = "{$table_root}/{$id}.json";
        $data = object_read($data_file);
        $sameid_objs[$id] = $data;
        $checked_ids[] = $id;
        $oneboxes = @$data[$sameid_group][$sameid_key];
        if (empty($oneboxes)) {
            continue;
        }
        foreach ($oneboxes as $onebox) {
            $new_id = intval(@$onebox['id']);
            if ($new_id === 0) {
                continue;
            }
            if (in_array($new_id, $checked_ids)) {
                continue;
            }
            $uncheck_ids[] = $new_id;
            if (!in_array($new_id, $rm_ids)) {
                $onebox_list[$new_id] = $onebox;
            }
        }
    }
    //更新字段内容
    foreach ($sameid_objs as $id => $data) {
        $need_save = false;
        /************************
        		    维护groud key字段
        		************************/
        //生成新的onebox清单
        $new_oneboxs = array();
        foreach ($onebox_list as $box_id => $onebox) {
            if ($box_id !== $id) {
                $new_oneboxs[] = $onebox;
            }
        }
        //对比新旧onebox
        $old_oneboxs = @$data[$sameid_group][$sameid_key];
        if (empty($old_oneboxs)) {
            $old_oneboxs = array();
        }
        if (!is_same_oneboxs($old_oneboxs, $new_oneboxs)) {
            //更新sameid字段
            $data[$sameid_group][$sameid_key] = $new_oneboxs;
            $need_save = true;
        }
        /************************
        		    维护共享字段
        		************************/
        //处理特殊字段,如共享字段,排行榜字段
        foreach ($data as $group => &$items) {
            foreach ($items as $name => &$value) {
                //处理“共享”字段
                //用新的数据,覆盖所有的share字段
                if (in_array($name, $shared_keys)) {
                    $value = $new_data[$group][$name];
                    $need_save = true;
                }
                //处理“排行”字段
                if (in_array($name, $rank_keys)) {
                    $max_rank = count($sameid_objs);
                    if ($ori_data) {
                        $old_rank = intval(@$ori_data[$group][$name]);
                        if ($old_rank === 0) {
                            $old_rank = 1;
                        }
                    } else {
                        $old_rank = $max_rank;
                    }
                    $new_rank = intval($new_data[$group][$name]);
                    $this_rank = intval($value);
                    if ($new_rank < 1 || $new_rank > $max_rank) {
                        $new_rank = $max_rank;
                    }
                    //这是自己新排行的数据
                    if ($id === $item_id) {
                        if ($this_rank !== $new_rank) {
                            $value = $new_rank;
                            $need_save = true;
                        }
                    } else {
                        $low_rank = min($old_rank, $new_rank);
                        $high_rank = max($old_rank, $new_rank);
                        if ($low_rank !== $high_rank) {
                            if ($this_rank >= $low_rank && $this_rank <= $high_rank) {
                                if ($this_rank === $old_rank) {
                                    $value = $new_rank;
                                } else {
                                    if ($new_rank > $old_rank) {
                                        $value--;
                                    } else {
                                        $value++;
                                    }
                                }
                                $need_save = true;
                            }
                        }
                    }
                }
            }
        }
        //保存
        if ($need_save) {
            $data_file = "{$table_root}/{$id}.json";
            object_save($data_file, $data);
            $affected_files[] = $data_file;
        }
    }
    //更新要删除的onebox字段内容
    foreach ($rm_ids as $id) {
        $data_file = "{$table_root}/{$id}.json";
        $data = object_read($data_file);
        $data[$sameid_group][$sameid_key] = array();
        object_save($data_file, $data);
        $affected_files[] = $data_file;
    }
    return $affected_files;
}
コード例 #2
0
ファイル: name.php プロジェクト: sdgdsffdsfff/json-db
}
$schema = object_read("{$table_root}/schema.json");
if (empty($schema)) {
    jsonp_nocache_exit(['status' => 'error', 'error' => 'not found schema.json']);
}
$mapper = object_read("{$table_root}/mapper.json");
$map_key = mapper_key($req_map_key);
$map_val = @$mapper[$map_key];
if (empty($map_val)) {
    $map_val = strval(intval($req_map_key));
}
$map_file = "{$table_root}/{$map_val}.json";
if (!file_exists($map_file)) {
    jsonp_nocache_exit(['status' => 'error', 'error' => 'not found target file']);
}
$onebox = onebox_object($schema, $map_file);
$res = array();
$res['provider_name'] = '任玩堂游戏数据库';
$res['provider_url'] = 'http://db.appgame.com/';
$res['favicon_url'] = 'http://www.appgame.com/favicon.ico';
$res['ori_url'] = $onebox['url'];
$res['ID'] = $onebox['id'];
$res['title'] = $onebox['title'];
$res['image'] = $onebox['image'];
$res['update_time'] = $onebox['time'];
$res['create_time'] = $onebox['ctime'];
$res['description'] = $onebox['desc'];
$res_type = @$req['type'];
if ($res_type === 'json') {
    $res['status'] = 'ok';
    return jsonp_nocache_exit($res);