Ejemplo n.º 1
0
 /**
  * Load a yaml file describing data
  * @param string $file
  * @return array
  */
 protected function loadFile(&$file, $default_path = '')
 {
     if (!preg_match(',[.]yaml$,i', $file) or !($file = $this->findFile($file, $default_path))) {
         return array();
     }
     define('_YAML_EVAL_PHP', false);
     if (!function_exists('yaml_decode')) {
         if (function_exists('include_spip')) {
             include_spip('inc/yaml');
         } else {
             require_once dirname(__FILE__) . '/../inc/yaml.php';
         }
     }
     $dataset = yaml_decode(file_get_contents($file));
     if (is_null($dataset)) {
         $dataset = array();
     }
     #			throw new DomainException('yaml file is empty, unreadable or badly formed: '.$file.var_export($dataset,true));
     // if a php file with same name exists
     // include it as it contains callback functions
     if ($f = preg_replace(',[.]yaml$,i', '.php', $file) and file_exists($f)) {
         $dataset[] = array('require' => $f, 'priority' => -1000);
     }
     return $dataset;
 }
Ejemplo n.º 2
0
function yaml_decode_file($fichier)
{
    $yaml = '';
    $retour = false;
    lire_fichier($fichier, $yaml);
    // Si on recupere bien quelque chose
    if ($yaml) {
        $retour = yaml_decode($yaml);
    }
    return $retour;
}
Ejemplo n.º 3
0
function inc_yaml_to_array_dist($u)
{
    include_spip('inc/yaml');
    if (is_array($yaml = yaml_decode($u))) {
        $tableau = $yaml;
    } else {
        if (is_object($yaml)) {
            $tableau = (array) $yaml;
        }
    }
    return $tableau;
}
function formulaires_importer_champs_extras_traiter_dist()
{
    $res = array('editable' => true);
    $fichier = $_FILES['fichier']['tmp_name'];
    lire_fichier($fichier, $yaml);
    if (!$yaml) {
        $res['message_erreur'] = "Lecture du fichier en erreur.";
        return $res;
    }
    include_spip('inc/yaml');
    $description = yaml_decode($yaml, true);
    if (!$description or !is_array($description)) {
        $res['message_erreur'] = "Pas de champ trouvé dans le fichier.";
        return $res;
    }
    if (iextras_importer_description($description, $message, _request('fusionner') == 'oui')) {
        $res['message_ok'] = $message;
    } else {
        $res['message_erreur'] = $message;
    }
    return $res;
}
Ejemplo n.º 5
0
/**
 * YAML -> tableau
 * @param string $u
 * @return bool|array
 * @throws Exception
 */
function inc_yaml_to_array_dist($u)
{
    include_spip('inc/yaml-mini');
    if (!function_exists("yaml_decode")) {
        throw new Exception('YAML: impossible de trouver la fonction yaml_decode');
        return false;
    }
    return yaml_decode($u);
}
Ejemplo n.º 6
0
function process_commands($cmds)
{
    # load the commands as YAML if not an array
    if (is_string($cmds)) {
        $cmds = yaml_decode($cmds);
    }
    # get the values for which they are not converted
    $no_set_values = yaml_decode($GLOBALS["process_commands_no_set_value"]);
    # get the existing pipe data
    $pipe = @$GLOBALS['pipe_data'];
    # loop through the commands
    for ($i = 0; $i < count($cmds); $i++) {
        $cmd = $cmds[$i];
        if (is_string($cmd)) {
            $cmd = array($cmd => null);
        }
        foreach ($cmd as $func => $opts) {
            # check to see if we are setting a variable
            if (substr($func, 0, 1) == '$') {
                # set the variable as the pipe
                if ($opts == null) {
                    $pipe = value($func);
                } else {
                    $var_name = substr($func, 1);
                    $GLOBALS[$var_name] = $opts;
                }
                continue;
            }
            # special case if the function is 'echo'
            if ($func == 'echo') {
                echo value($opts), "\n";
                continue;
            }
            # check that the function exists
            if (!function_exists($func)) {
                return error($func, "function '{$func}' does not exist");
            }
            # check to see if the input is a string that can be converted to an array
            if (is_string($opts)) {
                $opts = value($opts);
            }
            # re-write variables passed to the array
            if (is_array($opts) && count($opts) > 0) {
                foreach ($opts as $opt => $value) {
                    if ($opt == '=' || !is_string($value)) {
                        continue;
                    }
                    if (!@$no_set_values[$func][$opt]) {
                        $opts[$opt] = value($value);
                    }
                    if (@$GLOBALS['error'] || @$GLOBALS['error_msg']) {
                        return false;
                    }
                }
            }
            # call the function passing the options
            $r = $func($opts, $pipe);
            if ($r === false) {
                return false;
            }
            # check to see if we should set a value upon return
            if (is_array($opts)) {
                if (array_key_exists('=', $opts)) {
                    $var = $opts['='];
                    if (!is_string($var)) {
                        return error($func, "var name to save data to is not a string");
                    }
                    if (substr($var, 0, 1) != '$') {
                        return error($func, "var name '{$var}' for saving does not start with a '\$'");
                    }
                    debug_echo($func, "saving response to var '{$var}'");
                    $var_name = substr($var, 1);
                    $GLOBALS[$var_name] = $r;
                }
            }
            # define the $pipe for the next piped command
            $pipe = $r;
        }
    }
    $GLOBALS['pipe_data'] = $pipe;
    return $r === false ? false : true;
}
function genie_vacarme_numeros_import_dist($t)
{
    spip_log("debut de la fonction d'importation", 'vacarme_numeros_cron');
    include_spip('inc/distant');
    include_spip('inc/yaml');
    $url = 'http://www.vacarme.org/?page=export.yaml';
    $page = recuperer_page($url);
    $f = yaml_decode($page);
    // le taux de TVA utilisé par le plugin Produits
    $tva = lire_config('produits/taxe');
    $data = array();
    $processus = false;
    // pour le dev on met en true
    // $processus = true;
    foreach ($f as $numeros) {
        foreach ($numeros as $numero) {
            if (is_array($numero['sommaire'])) {
                $s = array();
                foreach ($numero['sommaire'] as $sommaire) {
                    $s[] = serialize($sommaire);
                }
                $numero['sommaire'] = serialize($s);
            }
            if (isset($numero['id'])) {
                // Cette rubrique n'a pas déjà été importée ?
                $id = $numero['id'];
                if (!sql_countsel("spip_vacarme_numeros", array("id_rubrique_distante=" . sql_quote($id)))) {
                    // traitement des données
                    $titre = $numero['numero'];
                    $saison = $numero['saison'];
                    $annee = $numero['annee'];
                    if ($numero['prix']) {
                        $prix = round($numero['prix'] / (1 + $tva), 4);
                    }
                    $url = $numero['url'];
                    $logo = $numero['logo'];
                    $sommaire = $numero['sommaire'];
                    // insertion
                    sql_insertq("spip_vacarme_numeros", array('id_rubrique_distante' => $id, 'numero' => $titre, 'saison' => $saison, 'annee' => $annee, 'prix' => $prix, 'url' => $url, 'logo' => $logo, 'sommaire' => $sommaire, 'statut' => 'publie'));
                    spip_log("La rubrique «" . $titre . "» a été importée", "vacarme_numeros_cron");
                    $processus = true;
                }
            }
        }
    }
    // une importation a été faite, les numéros sont enregistrés comme produits.
    if ($processus) {
        // les numeros importés en produits sont placés dans la rubrique 2/secteur 2
        $id_rubrique = 2;
        $id_secteur = 2;
        $row = array();
        // 1- on met les numeros en prop (ie qui viennent d'être importés)
        $result = sql_select("*", "spip_vacarme_numeros", "statut='publie'");
        if ($result) {
            while ($row = sql_fetch($result)) {
                $id_numero = $row['id_numero'];
                $titre = $row['numero'];
                $prix = $row['prix'];
                $saison = $row['saison'];
                $annee = $row['annee'];
                $url = $row['url'];
                $logo = $row['logo'];
                // calcul de la référence du produit à ajouter
                $ref = preg_replace('/(vacarme\\s*)(\\d*)(\\/*)(\\d*)/i', 'v$2', $titre);
                // le sommaire
                $sommaire = unserialize($row['sommaire']);
                // 2- les numéros sont importés dans la table produits
                // test nécessaire (?) car sinon, un numéro est importé plusieurs fois de manière identique. Pas compris pourquoi.
                if (!($test = sql_fetsel("id_produit", "spip_produits", "titre=" . sql_quote($ref)))) {
                    $ladate = date('Y-m-d H:i:s');
                    $id_produit = sql_insertq('spip_produits', array('titre' => $titre, 'id_rubrique' => $id_rubrique, 'id_secteur' => $id_secteur, 'prix_ht' => $prix, 'statut' => 'publie', 'lang' => 'fr', 'date' => $ladate, 'reference' => $ref));
                    if ($id_produit) {
                        // 3- on importe le logo pour l'appliquer au produit
                        if ($logo) {
                            $infos = recuperer_infos_distantes($logo);
                            if ($infos['extension']) {
                                $ext = $infos['extension'];
                                $taille = $infos['taille'];
                                $fichier = $infos['fichier'];
                                $largeur = $infos['largeur'];
                                $hauteur = $infos['hauteur'];
                                // extension autorisee ?
                                $ext_autorisee = sql_fetsel("inclus", "spip_types_documents", "extension=" . sql_quote($ext) . " AND upload='oui'");
                                if ($ext_autorisee) {
                                    $id_document = sql_insertq('spip_documents', array('extension' => $ext, 'date' => $ladate, 'fichier' => $fichier, 'taille' => $taille, 'largeur' => $largeur, 'hauteur' => $hauteur, 'mode' => 'document', 'distant' => 'oui', 'statut' => 'publie', 'date_publication' => $ladate));
                                    sql_insertq('spip_documents_liens', array('id_document' => $id_document, 'id_objet' => $id_produit, 'objet' => 'produit', 'vu' => 'non'));
                                    // on applique éventuellement la fonction de copie locale
                                    // si l'url du fichier est relative ../IMG/distant
                                    $f = sql_fetsel("fichier", "spip_documents", "id_document={$id_document}");
                                    $url_relative = strstr($f['fichier'], '../IMG/');
                                    if ($url_relative) {
                                        include_spip('vacarme_numeros_fonctions');
                                        $traitement = vacarme_numeros_copie_locale($id_document);
                                    }
                                }
                            }
                        }
                        // fin importation logo
                        // 4- le sommaire détaillé d'un numéro
                        sql_insertq('spip_vacarme_numeros_details', array('id_produit' => $id_produit, 'saison' => $saison, 'annee' => $annee, 'url' => $url));
                        if (is_array($sommaire)) {
                            foreach ($sommaire as $data) {
                                $data = unserialize($data);
                                $data_titre = $data['titre'];
                                $data_url = $data['url'];
                                $resume = $data['resume'];
                                sql_insertq('spip_vacarme_numeros_sommaires', array('id_produit' => $id_produit, 'titre' => $data_titre, 'url' => $data_url, 'resume' => $resume));
                            }
                        }
                        // fin sommaire
                    }
                    // fin test produit
                }
                // fin importation produit
                // on dépublie les numéros qui viennent d'être transformés en produits
                sql_updateq('spip_vacarme_numeros', array('statut' => "refuse"), 'id_numero=' . intval($id_numero));
            }
        }
    }
    spip_log("fin de la fonction d'importation", 'vacarme_numeros_cron');
    return 1;
}
Ejemplo n.º 8
0
function yaml_decode_file($yaml_file_path)
{
    return yaml_decode(file_get_contents($yaml_file_path));
}
Ejemplo n.º 9
0
function xml_parse_document($opts, $pipe, $cmd = __FUNCTION__)
{
    # set prefix
    $prefix = 'xml_parse_document';
    # merge opts
    $opts = merge_opts($opts, $pipe, 'document');
    # get document opt
    $document = get_opt($prefix, $opts, 'document');
    if (!check_opt_set_type($cmd, $document, 'document', 'string')) {
        return false;
    }
    # get doc type opt
    $doc_type = get_opt_config_value($prefix, $opts, 'doc_type', 'xml');
    if (!check_opt_set_type($cmd, $doc_type, 'doc_type', 'string')) {
        return false;
    }
    # get logic opt
    $logic = get_opt($prefix, $opts, 'logic');
    if (!check_opt_set_type($cmd, $logic, 'logic', 'string,array')) {
        return false;
    }
    if (is_string($logic)) {
        $logic = @yaml_decode($logic);
        if (!$logic) {
            return error($cmd, "parse logic not valid YAML");
        }
    }
    # get result type opt
    $result_type = get_opt_config_value($prefix, $opts, 'result_type', 'elements');
    if (!check_opt_set_type($cmd, $result_type, 'result_type', 'string')) {
        return false;
    }
    # get merge results opt
    $merge_results = get_opt($prefix, $opts, 'merge_results', false);
    if (!check_opt_set_type($cmd, $merge_results, 'merge_results', 'boolean')) {
        return false;
    }
    # load the document
    $dom = new DOMDocument('1.0');
    @$dom->loadHTML($document);
    #$dom->preserveWhiteSpace = false;
    # check that the document was parsed correctly
    $res = xml_parse_element($dom, $logic);
    if ($res === false) {
        return error($cmd, "could not parse {$type} document");
    }
    # display info about the parsing
    debug_echo($cmd, "{$doc_type} document parsed successfully for {$result_type}");
    if (empty($res)) {
        debug_echo($cmd, "no {$result_type} found while parsing the {$doc_type} document");
    } else {
        debug_echo($cmd, "the following {$result_type} were found in the {$doc_type} document");
        debug_dump_yaml($res, true);
    }
    # return if we are not merging the results
    if (!$merge_results) {
        return $res;
    }
    # merge the results
    $new_res = array();
    $res_count = count($res);
    for ($i = 0; $i < $res_count; $i++) {
        $elt = $res[$i];
        foreach ($elt as $key => $value) {
            if (is_array($value)) {
                $cur_value = @$new_res[$key];
                if ($cur_value) {
                    $value = array_merge($cur_value, $value);
                }
            }
            $new_res[$key] = $value;
        }
    }
    return $new_res;
}
Ejemplo n.º 10
0
function is_yaml_array_of_strings($yaml_str)
{
    if (!is_string($yaml_str)) {
        return false;
    }
    $val = @yaml_decode($yaml_str);
    return is_array_of_strings($val);
}
Ejemplo n.º 11
0
function load_data_from_doc_file($opts, $pipe, $cmd = __FUNCTION__)
{
    # set prefix
    $prefix = $cmd;
    # merge opts
    $opts = merge_opts($opts, $pipe, 'doc_type');
    # get file opt
    $file = get_opt($prefix, $opts, 'file');
    if (!check_opt_set_type($cmd, $file, 'file', 'string')) {
        return false;
    }
    # get defined fields opt
    $defined_fields = get_opt($prefix, $opts, 'defined_fields');
    if (!check_opt_if_set_type($cmd, $defined_fields, 'defined_fields', 'array_of_strings')) {
        return false;
    }
    # get save fields
    $save_fields = get_opt($prefix, $opts, 'save_fields');
    if (!check_opt_if_set_type($cmd, $save_fields, 'save_fields', 'array_of_strings')) {
        return false;
    }
    # get doc type opt
    $doc_type = get_opt($prefix, $opts, 'doc_type', 'json');
    if (!check_opt_set_type($cmd, $doc_type, 'doc_type', 'document_file_type')) {
        return false;
    }
    $doc_type_upper = strtoupper($doc_type);
    # get data structure opt
    $data_structure = get_opt($prefix, $opts, 'data_structure', 'list_of_objects');
    if (!check_opt_set_type($cmd, $data_structure, 'data_structure', 'data_structure_type')) {
        return false;
    }
    # get limit opt
    $limit = get_opt($prefix, $opts, 'limit', 0);
    if (!check_opt_set_type($cmd, $limit, 'limit', 'positive_integer')) {
        return false;
    }
    # get offset opt
    $offset = get_opt($prefix, $opts, 'offset', 0);
    if (!check_opt_set_type($cmd, $offset, 'offset', 'positive_integer')) {
        return false;
    }
    # check the file exists
    if (!file_exists($file)) {
        return error($cmd, "file does not exist : {$file}");
    }
    # display generic info about the data
    $data_structure_str = str_replace('_', ' ', $data_structure);
    debug_echo($cmd, "creating data from {$doc_type_upper} file : {$file}");
    debug_echo($cmd, "source data of type '{$data_structure_str}'");
    # read the file into memory
    $data_str = @file_get_contents($file);
    if (!is_string($data_str)) {
        return error($cmd, "could not read file : {$file}");
    }
    # load data depending on type
    switch ($doc_type) {
        case 'json':
            $data = @json_decode($data_str, true);
            break;
        case 'yaml':
            $data = @yaml_decode($data_str);
            break;
    }
    if (!$data) {
        return error($cmd, "invalid {$doc_type_upper} in file : {$file}");
    }
    $data_count = count($data);
    # set up end point
    if ($limit == 0) {
        $end = $data_count;
    } else {
        $end = $offset + $limit;
    }
    # set up the fields and build data
    $res_data = array();
    switch ($data_structure) {
        case 'list_of_columns':
            # set up fields
            if ($defined_fields) {
                $defined_fields_source = '(from the config)';
            } else {
                $defined_fields = array_shift($data);
                $defined_fields_source = '(from the source file)';
            }
            # set up response field indexes
            if ($save_fields) {
                $res_fields = $save_fields;
                $res_field_count = count($res_fields);
                $res_field_indexes = build_field_indexes($defined_fields, $res_fields, $cmd);
                if ($res_field_indexes === false) {
                    return;
                }
            } else {
                $res_fields = $defined_fields;
            }
            # display info about the fields
            debug_echo($cmd, "defined fields {$defined_fields_source} : ");
            debug_dump_list($defined_fields, true);
            if ($save_fields) {
                debug_echo($cmd, "saved fields :");
                debug_dump_list($save_fields, true);
            }
            # gather the data
            # TODO: change for having offset / limit
            $res_data = array();
            if ($save_fields) {
                $res_data = array();
                for ($i = $offset; $i < $end; $i++) {
                    $line = $data[$i];
                    $new_line = array();
                    for ($j = 0; $j < $res_field_count; $j++) {
                        $new_line[] = $line[$res_field_indexes[$j]];
                    }
                    $res_data[] = $new_line;
                }
            } elseif ($offset == 0 && $limit == 0) {
                $res_data = $data;
            } else {
                $res_data = array();
                for ($i = $offset; $i < $end; $i++) {
                    $res_data[] = $data[$i];
                }
            }
            break;
        case 'list_of_objects':
            $res_data = array();
            if ($save_fields) {
                # set up result fields
                $res_fields = $save_fields;
                $res_fields_count = count($res_fields);
                # display info about what fields will be saved
                debug_echo($cmd, "saved fields :");
                debug_dump_list($res_fields, true);
                # build the data
                for ($i = $offset; $i < $end; $i++) {
                    $obj = $data[$i];
                    $new_line = array();
                    for ($j = 0; $j < $res_fields_count; $j++) {
                        $new_line[] = @$obj[$res_fields[$j]];
                    }
                    $res_data[] = $new_line;
                }
                $res_fields = $save_fields;
            } else {
                # display info about saved files
                debug_echo($cmd, "saving all fields - they will be listed as they are added");
                # build the data
                $res_fields = array();
                $res_fields_count = 0;
                for ($i = $offset; $i < $end; $i++) {
                    $obj = $data[$i];
                    $new_line = array();
                    # save all the existing data in order
                    for ($j = 0; $j < $res_fields_count; $j++) {
                        $new_line[] = @$obj[$res_fields[$j]];
                    }
                    # index and save any new fields
                    $res_fields_added = array();
                    foreach ($obj as $key => $value) {
                        if (in_array($key, $res_fields)) {
                            continue;
                        }
                        $res_fields_added[] = $key;
                        $res_fields[] = $key;
                        $res_fields_count++;
                        $new_line[] = $value;
                    }
                    $res_data[$i] = $new_line;
                    # display list of fields added (if any)
                    if ($res_fields_added) {
                        $line_no = $i + 1;
                        debug_echo($cmd, "the following fields were added on row {$line_no} :");
                        debug_dump_list($res_fields_added, true);
                    }
                }
                # add back in any empty fields
                for ($i = 0; $i < count($res_data); $i++) {
                    $line =& $res_data[$i];
                    $line_fields_count = count($line);
                    if ($line_fields_count == $res_fields_count) {
                        break;
                    }
                    for ($j = $line_fields_count; $j < $res_fields_count; $j++) {
                        $line[] = '';
                    }
                }
            }
            break;
    }
    # detail results
    $line_count = count($res_data);
    debug_echo($cmd, "creation of data from JSON file complete ({$line_count} lines processed)");
    # create result
    return build_result_data($res_fields, $res_data);
}
Ejemplo n.º 12
0
function search_replace_data_by_key_value($opts, $pipe, $cmd = __FUNCTION__)
{
    # set prefix
    $prefix = 'search_replace_data';
    # merge opts
    $opts = merge_opts($opts, $pipe);
    # get fields opt
    $data = get_opt_config_value($prefix, $opts, 'data');
    if (!check_opt_set_type($cmd, $data, 'data', 'array')) {
        return false;
    }
    # get fields opt
    $fields = get_opt_config_value($prefix, $opts, 'fields');
    if (!check_opt_set_type($cmd, $fields, 'fields', 'array_of_strings')) {
        return false;
    }
    # get map opt
    $search = get_opt_config_value($prefix, $opts, 'search');
    if (!check_opt_set_type($cmd, $search, 'search', 'array,yaml_array')) {
        return false;
    }
    if (is_string($search)) {
        $search = yaml_decode($search);
    }
    # get replace opt
    $replace = get_opt_config_value($prefix, $opts, 'replace');
    if (!check_opt_set_type($cmd, $replace, 'replace', 'array,yaml_array')) {
        return false;
    }
    if (is_string($replace)) {
        $replace = yaml_decode($replace);
    }
    # set all the fields
    $search_fields = array_keys($search);
    $value_fields = array_values($search);
    $replace_fields = array_values($replace);
    # set up new fields
    $new_fields = array();
    foreach ($fields as $field) {
        if (in_array($field, $search_fields) || in_array($field, $value_fields)) {
            continue;
        }
        $new_fields[] = $field;
    }
    foreach ($replace_fields as $field) {
        $new_fields[] = $field;
    }
    $new_fields_count = count($new_fields);
    # set up copy fields
    $new_fields_flipped = array_flip($new_fields);
    $copy_fields = array();
    foreach ($fields as $old_index => $field) {
        if (!in_array($field, $new_fields) || in_array($field, $replace_fields)) {
            continue;
        }
        $new_index = $new_fields_flipped[$field];
        $copy_fields[$old_index] = $new_index;
    }
    $copy_fields_count = count($copy_fields);
    # set up search field indexes
    $fields_flipped = array_flip($fields);
    $searches = array();
    foreach ($search as $check_field => $value_field) {
        $check_field_idx = $fields_flipped[$check_field];
        $value_field_idx = $fields_flipped[$value_field];
        $searches[$check_field_idx] = $value_field_idx;
    }
    # set up replaces
    $replaces = array();
    foreach ($replace as $value => $field) {
        $replaces[$value] = $new_fields_flipped[$field];
    }
    # display messages
    debug_echo($cmd, "doing a search-replace by key value using the following pairs (check_field: value_field):");
    debug_dump_yaml($search, true);
    debug_echo($cmd, "the following found replacements were made (checked_value: replacement_field):");
    debug_dump_yaml($replace, true);
    # loop through data
    $new_data = array();
    foreach ($data as $line) {
        # create new line
        $new_line = array();
        for ($i = 0; $i < $new_fields_count; $i++) {
            $new_line[] = null;
        }
        # copy data
        foreach ($copy_fields as $old_index => $new_index) {
            $new_line[$new_index] = $line[$old_index];
        }
        # search for values
        foreach ($searches as $check_field_index => $value_field_index) {
            $check = $line[$check_field_index];
            $replace_field_index = @$replaces[$check];
            if ($replace_field_index === null) {
                continue;
            }
            $new_line[$replace_field_index] = $line[$value_field_index];
        }
        # add the new line to the data
        $new_data[] = $new_line;
    }
    # build res
    $res = array('fields' => $new_fields, 'data' => $new_data);
    return $res;
}
Ejemplo n.º 13
0
function inc_yaml_to_array($u)
{
    include_spip('inc/yaml');
    return yaml_decode($u);
}
Ejemplo n.º 14
0
function merge_relational_data($opts, $pipe, $cmd = __FUNCTION__)
{
    # set prefix
    $prefix = 'merge_relational_data';
    # merge opts
    $opts = merge_opts($opts, $pipe);
    # get data opt
    $data = get_opt_config_value($prefix, $opts, 'data');
    if (!check_opt_set_type($cmd, $data, 'data', 'array')) {
        return false;
    }
    # get fields opt
    $fields = get_opt_config_value($prefix, $opts, 'fields');
    if (!check_opt_set_type($cmd, $fields, 'fields', 'array_of_strings')) {
        return false;
    }
    # get primary key opt
    $primary_key = get_opt_config_value($prefix, $opts, 'primary_key');
    if (!check_opt_set_type($cmd, $primary_key, 'primary_key', 'string')) {
        return false;
    }
    # get check field opt
    $merge_rules = get_opt_config_value($prefix, $opts, 'merge_rules');
    if (!check_opt_set_type($cmd, $merge_rules, 'merge_rules', 'array,yaml_array')) {
        return false;
    }
    if (is_string($merge_rules)) {
        $merge_rules = yaml_decode($merge_rules);
    }
    # set up primary key
    if (!in_array($primary_key, $fields)) {
        return error($cmd, "field '{$primary_key}' does not exist");
    }
    $fields_flipped = array_flip($fields);
    $primary_key_index = $fields_flipped[$primary_key];
    # set up new fields
    $new_fields = array();
    foreach ($fields as $field) {
        $new_fields[] = $field;
    }
    # loop through all the rules
    $rules = array();
    foreach ($merge_rules as $i => $merge_rule) {
        $rule = array();
        # get check field opt
        $check_field = get_opt_config_value($prefix, $merge_rule, 'check_field');
        if (!check_opt_set_type($cmd, $check_field, 'check_field', 'string')) {
            return false;
        }
        # check that the fields exist
        if (!in_array($check_field, $fields)) {
            return error($cmd, "field '{$check_field}' does not exist");
        }
        $check_field_index = $fields_flipped[$check_field];
        $rule[0] = $check_field_index;
        # get value field opt
        $value_field = get_opt_config_value($prefix, $merge_rule, 'value_field');
        if (!check_opt_if_set_type($cmd, $value_field, 'value_field', 'string')) {
            return false;
        }
        # get value field map opt
        $value_field_map = get_opt_config_value($prefix, $merge_rule, 'value_field_map');
        if (!check_opt_if_set_type($cmd, $value_field_map, 'value_field_map', 'array,yaml_array')) {
            return false;
        }
        if ($value_field_map && !is_array($value_field_map)) {
            $value_field_map = yaml_decode($value_field_map);
        }
        if ($value_field === null && $value_field_map === null) {
            return opt_not_set_msg($cmd, 'value_field,value_field_map');
        }
        # check the type of the rule
        if ($value_field !== null) {
            $rule[1] = 'f';
            if (!in_array($value_field, $fields)) {
                return error($cmd, "field '{$value_field}' does not exist");
            }
            $value_field_index = $fields_flipped[$value_field];
            $rule[2] = $value_field_index;
        } else {
            $rule[1] = 'fm';
            # set up value field map
            foreach ($value_field_map as $value => $field) {
                if (!in_array($field, $new_fields)) {
                    $new_fields[] = $field;
                }
            }
            # create the value indexes
            $new_fields_flipped = array_flip($new_fields);
            $values_indexes = array();
            foreach ($value_field_map as $value => $field) {
                $values_indexes[$value] = $new_fields_flipped[$field];
            }
            $rule[2] = $values_indexes;
        }
        # get value opt
        # TODO: value_map
        if (array_key_exists('value', $merge_rule)) {
            $rule[3] = 'v';
            $rule[4] = $merge_rule['value'];
        } elseif (array_key_exists('value_map', $merge_rule)) {
            $value_map = get_opt_config_value($prefix, $merge_rule, 'value_map');
            if (!check_opt_set_type($cmd, $value_map, 'value_map', 'array,yaml_array')) {
                return false;
            }
            if (is_string($value_map)) {
                $value_map = yaml_decode($value_map);
            }
            $rule[3] = 'vm';
            $rule[4] = $value_map;
        } else {
            $rule[3] = 'cv';
        }
        # add rule to rules
        $rules[] = $rule;
    }
    # set up counts
    $data_count = count($data);
    $field_count = count($fields);
    $new_field_count = count($new_fields);
    # set up new data
    $new_data = array();
    $new_data_indexes = array();
    # loop over data and apply rules
    debug_echo($cmd, "merging relational data (this can take some time) ...");
    for ($i = 0; $i < $data_count; $i++) {
        # get line and check if line already exists
        $line = $data[$i];
        $primary_value = $line[$primary_key_index];
        unset($new_line);
        $new_line =& $new_data_indexes[$primary_value];
        # create new line
        if (!$new_line) {
            unset($new_line);
            $new_line = array();
            for ($j = 0; $j < $field_count; $j++) {
                $new_line[$j] = $line[$j];
            }
            for ($j = $field_count; $j < $new_field_count; $j++) {
                $new_line[] = null;
            }
            $new_data[] =& $new_line;
            $new_data_indexes[$primary_value] =& $new_line;
        }
        # loop through the rules to modify the data
        foreach ($rules as $rule) {
            # set up rule parts
            $check_field_index = $rule[0];
            $value_field_type = $rule[1];
            $value_field_index = $rule[2];
            $value_type = $rule[3];
            # intiiate check
            $check_value = $line[$check_field_index];
            if ($check_value === null) {
                continue;
            }
            # get value field
            switch ($value_field_type) {
                case 'fm':
                    $value_field_index = @$value_field_index[$check_value];
                    break;
            }
            if ($value_field_index === null) {
                continue;
            }
            # get value
            switch ($value_type) {
                case 'v':
                    $value = $rule[4];
                    break;
                case 'vm':
                    $value = $rule[4][$check_value];
                    break;
                case 'cv':
                    $value = $check_value;
                    break;
            }
            # set the value
            $new_line[$value_field_index] = $value;
        }
    }
    # build result
    $res = array('data' => $new_data, 'fields' => $new_fields);
    return $res;
}
Ejemplo n.º 15
0
function modify_fields($opts, $pipe, $cmd = __FUNCTION__, $opt_prefix = false)
{
    # set prefix
    $prefix = 'modify_fields';
    # merge opts
    $opts = merge_opts($opts, $pipe);
    # adjust opt prefix
    $cmd = adjust_opt_prefix($cmd, $opts, $opt_prefix, 'add_fields');
    # get data opt
    $data =& get_opt_config_value($prefix, $opts, 'data');
    if (!check_opt_set_type($cmd, $data, 'data', 'array')) {
        return false;
    }
    # get fields opt
    $fields = get_opt_config_value($prefix, $opts, 'fields');
    if (!check_opt_set_type($cmd, $fields, 'fields', 'array_of_strings')) {
        return false;
    }
    # get indexes fields opt
    $indexes =& get_opt_config_value($prefix, $opts, 'indexes');
    if (!check_opt_if_set_type($cmd, $indexes, 'indexes', 'array')) {
        return false;
    }
    # get map fields opt
    $map_fields = get_opt_config_value($prefix, $opts, 'map');
    if (!check_opt_if_set_type($cmd, $map_fields, 'map', 'string,array')) {
        return false;
    }
    if (is_string($map_fields)) {
        $map_fields = yaml_decode($map_fields);
    }
    # get swap fields opt
    $swap_fields = get_opt_config_value($prefix, $opts, 'swap');
    if (!check_opt_if_set_type($cmd, $swap_fields, 'swap', 'string,array')) {
        return false;
    }
    if (is_string($swap_fields)) {
        $swap_fields = yaml_decode($swap_fields);
    }
    # get add fields opt
    $add_fields = get_opt_config_value($prefix, $opts, 'add');
    if (!check_opt_if_set_type($cmd, $add_fields, 'add', 'array')) {
        return false;
    }
    # get delete fields opt
    $delete_fields = get_opt_config_value($prefix, $opts, 'delete');
    if (!check_opt_if_set_type($cmd, $delete_fields, 'delete', 'array_of_strings')) {
        return false;
    }
    # get reorder fields opt
    $reorder_fields = get_opt_config_value($prefix, $opts, 'reorder');
    if (!check_opt_if_set_type($cmd, $reorder_fields, 'reorder', 'array_of_strings')) {
        return false;
    }
    # perform the operations
    if ($map_fields && !map_data_fields($data, $fields, $map_fields, $cmd)) {
        return false;
    }
    if ($swap_fields && !swap_data_fields($fields, $swap_fields, $cmd)) {
        return false;
    }
    if ($add_fields && !add_data_fields($data, $fields, $add_fields, $cmd)) {
        return false;
    }
    if ($delete_fields && !delete_data_fields($data, $fields, $delete_fields, $cmd)) {
        return false;
    }
    if ($reorder_fields && !reorder_data_fields($data, $fields, $reorder_fields, $cmd)) {
        return false;
    }
    # create indexes
    /*
    var_dump ($fields);  
    var_dump ($data[0]);
    exit;
    */
    # set the response
    return build_result_data($fields, $data, $indexes);
}