Exemplo n.º 1
0
function ftp_get_files_in_dir($opts, $pipe, $cmd = __FUNCTION__)
{
    # set prefix
    $prefix = $cmd;
    # merge opts
    $opts = merge_opts_for_output($pipe, $opts);
    # get local dir opt
    $local_dir = get_opt_config_value($prefix, $opts, 'local_dir');
    if (!check_opt_if_set_type($cmd, $local_dir, 'local_dir', 'string')) {
        return false;
    }
    # scan dir
    $res = ftp_scandir($opts, null, $cmd);
    if ($res === false) {
        return false;
    }
    # set the local dir
    $res['local_dir'] = $local_dir;
    # get files
    return ftp_get_files($res, $opts);
}
Exemplo n.º 2
0
function clean_data($opts, $pipe, $cmd = __FUNCTION__)
{
    # set prefix
    $prefix = 'clean_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 old value opt
    $old_value = get_opt_config_value($prefix, $opts, 'old_value');
    # get new value opt
    $new_value = get_opt_config_value($prefix, $opts, 'new_value');
    # set up counts
    $data_count = count($data);
    $field_count = count($fields);
    # messages
    debug_echo($cmd, "cleaning data (old value : {$old_value}, new value : {$new_value})");
    # loop through data
    for ($i = 0; $i < $data_count; $i++) {
        unset($line);
        $line =& $data[$i];
        for ($j = 0; $j < $field_count; $j++) {
            if ($line[$j] === $old_value) {
                $line[$j] = $new_value;
            }
        }
    }
    # set up result
    $res = array('data' => $data, 'fields' => $fields);
    return $res;
}
Exemplo n.º 3
0
function modify_rows($opts, $pipe, $cmd = __FUNCTION__)
{
    # set prefix
    $prefix = 'modify_rows';
    # 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
    $res_fields = get_opt_config_value($prefix, $opts, 'fields');
    if (!check_opt_set_type($cmd, $res_fields, 'fields', 'array_of_strings')) {
        return false;
    }
    # get limit opt
    $limit = get_opt_config_value($prefix, $opts, 'limit');
    if (!check_opt_set_type($cmd, $limit, 'limit', 'integer')) {
        return false;
    }
    # get limit opt
    $offset = get_opt_config_value($prefix, $opts, 'offset', 0);
    if (!check_opt_set_type($cmd, $offset, 'offset', 'integer')) {
        return false;
    }
    # display message
    debug_echo($cmd, "modifying rows (limit = {$limit}, offset = {$offset})");
    # set up end
    $end = $offset + $limit;
    # process the data
    $res_data = array();
    for ($i = $offset; $i < $end; $i++) {
        $res_data[] = $data[$i];
    }
    # build response object
    return build_result_data($res_fields, $res_data);
}
Exemplo n.º 4
0
function build_source_data($prefix, $opts, $cmd)
{
    # get data opt
    $data =& get_opt_config_value($prefix, $opts, 'data');
    if (!check_opt_if_set_type($cmd, $data, 'type', 'array')) {
        return false;
    }
    # get fields opt
    $fields =& get_opt_config_value($prefix, $opts, 'fields');
    if (!check_opt_if_set_type($cmd, $fields, 'type', 'array_of_strings')) {
        return false;
    }
    # get indexes opt
    $indexes =& get_opt_config_value($prefix, $opts, 'indexes');
    if (!check_opt_if_set_type($cmd, $indexes, 'type', 'array')) {
        return false;
    }
    # check if data and fields are set
    if (!is_array($data) || !is_array($fields)) {
        return false;
    }
    return build_result_data($fields, $data, $indexes);
}
Exemplo n.º 5
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;
}
Exemplo n.º 6
0
function process_data($opts, $pipe, $cmd = __FUNCTION__, $opt_prefix = false)
{
    # set prefix
    $prefix = 'process_data';
    # merge opts
    $opts = merge_opts($opts, $pipe);
    # adjust opt prefix
    $cmd = adjust_opt_prefix($cmd, $opts, $opt_prefix, 'edit');
    # get debug opt
    $debug = get_opt_config_value($prefix, $opts, 'debug', false);
    if (!check_opt_set_type($cmd, $debug, 'debug', 'boolean')) {
        return false;
    }
    # get type opt
    $join_type = get_opt_config_value($prefix, $opts, 'join_type', 'outer');
    if (!check_opt_set_type($cmd, $join_type, 'type', 'data_join_type')) {
        return false;
    }
    # get sources opt
    $sources = get_opt($prefix, $opts, 'sources');
    if (!check_opt_if_set_type($cmd, $sources, 'sources', 'array')) {
        return false;
    }
    if (!$sources) {
        $source = build_source_data($prefix, $opts, $cmd);
        if (!$source) {
            return error($cmd, "sources opt is not set and the data and fields opts are not set either, so can't be used to create one");
        }
        $sources = array($source);
    }
    # get sources values
    foreach ($sources as $i => $val) {
        $sources[$i] = value($val);
    }
    # get create_indexes opt
    $create_indexes = get_opt($prefix, $opts, 'create_indexes');
    if (!check_opt_if_set_type($cmd, $create_indexes, 'create_indexes', 'array_of_strings')) {
        return false;
    }
    # get join field opt
    $primary_key = get_opt_config_value($prefix, $opts, 'primary_key', 'guid');
    if (!check_opt_set_type($cmd, $primary_key, 'primary_key', 'string')) {
        return false;
    }
    # get mawk rules opt
    $mawk = get_opt_config_value($prefix, $opts, 'mawk');
    if (!check_opt_set_type($cmd, $mawk, 'mawk', 'string')) {
        return false;
    }
    # get update frequency opt
    $update_frequency = get_opt_config_value($prefix, $opts, 'update_frequency', 0);
    if (!check_opt_set_type($cmd, $update_frequency, 'update_frequency', 'integer')) {
        return false;
    }
    # create fields and data arrays
    $fields_arr = array();
    $data_arr = array();
    /*
      for ($i=0; $i<count ($sources); $i++) {
      
        $source_no = $i+1;
      
        $fields = $sources[$i]['fields'];
        
        if (!$fields)
          return  error ($cmd, "no fields defined for source $source_no");
      
        $fields_arr[] = $fields;
        
        if ($i == 0) {
        
          $data_arr[] = $sources[$i]['data'];
        
        } else {
        
          $data = $sources[$i]['indexes'][$primary_key];
          
          if ($data === null) {
          
            return  error ($cmd, "index on primary key '$primary_key' does not exis for source $source_no");   # TODO: auto-create
          }
          
          $data_arr[] = $data;
        }
      }*/
    $sources_count = count($sources);
    for ($i = 0; $i < $sources_count; $i++) {
        $source_no = $i + 1;
        # add fields
        $fields = $sources[$i]['fields'];
        if (!$fields) {
            return error($cmd, "no fields defined for source {$source_no}");
        }
        if ($sources_count > 1) {
            if (!in_array($primary_key, $fields)) {
                return error($cmd, "primary key '{$primary_key}' not defined for source {$source_no}");
            }
        }
        $fields_arr[] = $fields;
        # add data
        $data = $sources[$i]['data'];
        if (!$data) {
            return error($cmd, "no data defined for source {$source_no}");
        }
        $data_arr[] = $data;
    }
    # display info about the join, if there is one
    if (count($fields_arr) > 1) {
        debug_echo($cmd, "performing {$join_type} join on primary key / field '{$primary_key}'");
    }
    for ($i = 1; $i <= count($fields_arr); $i++) {
        debug_echo($cmd, "fields for data source {$i} :");
        debug_dump_list($fields_arr[$i - 1], true);
    }
    # parse the line rules
    $parsed_mawk = mawk_parse_code($mawk, $fields_arr, $cmd);
    if (is_int($parsed_mawk)) {
        return error($cmd, "code line {$i} of the mawk has an invalid syntax");
    }
    $parsed_mawk_code = $parsed_mawk['code'];
    # set up indexes
    $indexes_arr = $parsed_mawk['indexes'];
    if (count($fields_arr) > 1) {
        $r = mawk_add_primary_index($primary_key, $indexes_arr, $fields_arr, $cmd);
        if ($r === false) {
            return false;
        }
    }
    # display info about the line rules
    debug_echo($cmd, "the following mawk code will be used :");
    debug_echo_txt(mawk_clean_code($mawk), true);
    if ($debug) {
        debug_echo($cmd, "which translates to the following parsed code :");
        debug_dump_yaml($parsed_mawk_code, true);
        if ($indexes_arr) {
            debug_echo($cmd, "and includes the following indexes :");
            debug_dump_yaml($indexes_arr, true);
        }
    }
    /*
    if (count ($fields_arr) > 1) {
      $primary_indexes = mawk_get_indexes ($primary_key, $fields_arr, $cmd);
        
        if ($primary_indexes === false)
          return  false;
          
      } else {
        $primary_indexes = array ();
      }
    
      # set up create indexes
         
      $new_indexes = mawk_get_indexes ($create_indexes, $fields_arr, $cmd);
    if ($new_indexes === false)
        return  false;
    */
    # set up discard
    if ($join_type == 'inner') {
        $inner_join = true;
    } else {
        $inner_join = false;
    }
    # run mawk code
    try {
        debug_echo($cmd, "processing the data (this can take some time depending on the inputs) ...");
        $mawk_res = mawk_process_data($parsed_mawk_code, $fields_arr, $data_arr, $indexes_arr, $update_frequency, $inner_join, $cmd, $debug);
    } catch (MawkError $e) {
        $msg = $e->getMessage();
        return error($cmd, $msg);
    }
    return $mawk_res;
}
Exemplo n.º 7
0
function load_data_from_lengths_file($opts, $pipe, $cmd = __FUNCTION__)
{
    # set prefix
    $prefix = 'load_data_from_lengths_file';
    # merge opts
    $opts = merge_opts($opts, $pipe, 'file');
    # 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,yaml_array_of_strings')) {
        return false;
    }
    if (is_string($defined_fields)) {
        $defined_fields = yaml_decode($defined_fields);
    }
    # 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,yaml_array_of_strings')) {
        return false;
    }
    if (is_string($save_fields)) {
        $save_fields = yaml_decode($save_fields);
    }
    # get splits opt
    $splits = get_opt($prefix, $opts, 'splits');
    if (!check_opt_if_set_type($cmd, $splits, 'splits', 'array_of_integers,yaml_array_of_integers')) {
        return false;
    }
    if (is_string($splits)) {
        $splits = yaml_decode($splits);
    }
    # get lengths opt
    $lengths = get_opt($prefix, $opts, 'lengths');
    if (!check_opt_if_set_type($cmd, $lengths, 'lengths', 'array_of_integers,yaml_array_of_integers')) {
        return false;
    }
    if (is_string($lengths)) {
        $lengths = yaml_decode($lengths);
    }
    # get fields lengths opt
    $fields_lengths = get_opt($prefix, $opts, 'fields_lengths');
    if (!check_opt_if_set_type($cmd, $fields_lengths, 'fields_lengths', 'array,yaml_array')) {
        return false;
    }
    if (is_string($fields_lengths)) {
        $fields_lengths = yaml_decode($fields_lengths);
    }
    # get fields splits opt
    $fields_splits = get_opt($prefix, $opts, 'fields_splits');
    if (!check_opt_if_set_type($cmd, $fields_splits, 'fields_splits', 'array,yaml_array')) {
        return false;
    }
    if (is_string($fields_splits)) {
        $fields_splits = yaml_decode($fields_splits);
    }
    # get trim opt (i.e. whether the values should have leading and trailing whitespace removed)
    $trim = get_opt_config_value($prefix, $opts, 'trim', true);
    if (!check_opt_set_type($cmd, $trim, 'trim', 'boolean')) {
        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 file exists
    if (!file_exists($file)) {
        return error($cmd, "file does not exist : {$file}");
    }
    # open file
    $file_handle = fopen($file, 'r');
    if (!$file_handle) {
        return error($cmd, "cannot read file : {$file}");
    }
    debug_echo($cmd, "creating data from column file : {$file}");
    # check for splits/lengths fields
    if ($fields_splits) {
        $defined_fields = array_keys($fields_splits);
        $splits = array_values($fields_splits);
    } elseif ($fields_lengths) {
        $defined_fields = array_keys($fields_lengths);
        $lengths = array_values($fields_lengths);
    }
    # check for lengths or splits
    if ($lengths) {
        $total = 0;
        $splits = array();
        foreach ($lengths as $length) {
            $total += $length;
            $splits[] = $total;
        }
    } elseif (!$splits) {
        return opt_not_set_msg($cmd, "lengths,splits,fields_lengths,fields_splits");
    }
    # set up splits
    $split_count = count($splits);
    # get the fields from list or file
    if ($defined_fields) {
        $defined_fields_source = '(from the config)';
        $field_count = count($defined_fields);
        if ($field_count != $split_count) {
            return error($cmd, "the field count ({$field_count}) is not the same as the split count ({$split_count})");
        }
    } else {
        $defined_fields = array();
        $line = fgets($file_handle);
        for ($i = 0; $i < $split_count; $i++) {
            $min = $splits[$i];
            if ($i == 0) {
                $min = 0;
                $len = $splits[0];
            } else {
                $min = $splits[$i - 1];
                $len = $splits[$i] - $min;
            }
            $val = substr($line, $min, $len);
            if ($trim) {
                $val = trim($val);
            }
            $defined_fields[] = $val;
        }
        $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
    debug_echo($cmd, "splits used for dividing data : " . json_encode($splits));
    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);
    }
    # parse the offset
    for ($off_i = 0; $off_i < $offset; $off_i++) {
        if (feof($file_handle)) {
            break;
        }
        fgets($file_handle);
    }
    # notice about offset reaching
    if ($offset > 0 && !feof($file_handle)) {
        notice($cmd, "offset ({$offset}) reached");
    }
    # loop over lines to split the file
    $limit_i = 0;
    $res_data = array();
    if ($save_fields) {
        while ($line = fgets($file_handle)) {
            # parse all the fields
            $line_data = array();
            for ($i = 0; $i < $split_count; $i++) {
                $min = $splits[$i];
                if ($i == 0) {
                    $min = 0;
                    $len = $splits[0];
                } else {
                    $min = $splits[$i - 1];
                    $len = $splits[$i] - $min;
                }
                $val = substr($line, $min, $len);
                if ($trim) {
                    $val = trim($val);
                }
                $line_data[] = $val;
            }
            # save just the save fields
            $new_line = array();
            for ($i = 0; $i < $res_field_count; $i++) {
                $new_line[] = $line_data[$res_field_indexes[$i]];
            }
            $res_data[] = $new_line;
            # load_data_line ()
            # check the limit
            if ($limit != 0) {
                $limit_i++;
                if ($limit_i >= $limit) {
                    notice($cmd, "limit ({$limit}) reached");
                    break;
                }
            }
        }
    } else {
        while ($line = fgets($file_handle)) {
            # parse all the fields
            $line_data = array();
            for ($i = 0; $i < $split_count; $i++) {
                $min = $splits[$i];
                if ($i == 0) {
                    $min = 0;
                    $len = $splits[0];
                } else {
                    $min = $splits[$i - 1];
                    $len = $splits[$i] - $min;
                }
                $val = substr($line, $min, $len);
                if ($trim) {
                    $val = trim($val);
                }
                $line_data[] = $val;
            }
            $res_data[] = $line_data;
            # load_data_line ()
            # check the limit
            if ($limit != 0) {
                $limit_i++;
                if ($limit_i >= $limit) {
                    notice($cmd, "limit ({$limit}) reached");
                    break;
                }
            }
        }
    }
    # display info about reaching the end of file
    if (feof($file_handle)) {
        $total_lines = $off_i + $limit_i;
        notice($cmd, "end of file reached after {$total_lines} lines of data (offset = {$offset}, limit = {$limit})");
    }
    fclose($file_handle);
    # detail results
    $line_count = count($res_data);
    debug_echo($cmd, "creation of data from column file complete ({$line_count} lines processed)");
    # create result
    return build_result_data($res_fields, $res_data);
}
Exemplo n.º 8
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;
}
Exemplo n.º 9
0
function curl_use_checked_saved_file($opts, $pipe, $cmd = __FUNCTION__, $prefix = 'curl')
{
    # merge opts
    $opts = merge_opts($opts, $pipe, "use_saved_file");
    # get use downloaded file opt
    $use_saved_file = get_opt_config_value($prefix, $opts, "use_saved_file", true);
    if (!check_opt_set_type($cmd, $use_saved_file, "use_saved_file", "boolean")) {
        return false;
    }
    # reset use download file opt
    $old_use_saved_file = get_config_value("{$prefix}_old_use_saved_file");
    set_config_value("{$prefix}_use_saved_file", $old_use_saved_file);
    # check to see if should use downloaded file or not
    if (!$use_saved_file) {
        return $pipe;
    }
    # restore http request execution config
    $execute = get_config_value("{$prefix}_old_execute");
    set_config_value("{$prefix}_execute", $execute);
    # get checked downloaded file opt
    $file = get_config_value("{$prefix}_checked_saved_file");
    if (!check_opt_if_set_type($cmd, $file, "{$prefix}_checked_saved_file", "string")) {
        return false;
    }
    # if file does not exist just pipe
    if (!$file) {
        return $pipe;
    }
    # reset config and return
    set_config_value("{$prefix}_checked_saved_file", null);
    return array("file" => $file, "response_file" => $file);
}
Exemplo n.º 10
0
function http_use_checked_saved_file($opts, $pipe, $cmd = __FUNCTION__)
{
    # set prefix
    $prefix = 'http';
    # merge opts
    $opts = merge_opts($opts, $pipe, 'use_saved_file');
    # get use downloaded file opt
    $use_saved_file = get_opt_config_value($prefix, $opts, 'use_saved_file', true);
    if (!check_opt_set_type($cmd, $use_saved_file, 'use_saved_file', 'boolean')) {
        return false;
    }
    # reset use download file opt
    $old_use_saved_file = get_config_value('http_old_use_saved_file');
    set_config_value('http_use_saved_file', $old_use_saved_file);
    # check to see if should use downloaded file or not
    if (!$use_saved_file) {
        return $pipe;
    }
    # restore http request execution config
    $http_execute = get_config_value('http_old_execute');
    set_config_value('http_execute', $http_execute);
    # get checked downloaded file opt
    $file = get_config_value('http_checked_saved_file');
    if (!check_opt_if_set_type($cmd, $file, 'http_checked_saved_file', 'string')) {
        return false;
    }
    # if file does not exist just pipe
    if (!$file) {
        return $pipe;
    }
    # reset config and return
    set_config_value('http_checked_saved_file', null);
    return array('file' => $file, 'response_file' => $file);
}
Exemplo n.º 11
0
function split_file_on_cols($opts, $pipe)
{
    # set prefix
    $prefix = 'split_file_on_cols';
    # merge opts
    $opts = merge_opts($opts, $pipe, 'file');
    # get file to split
    $file = get_opt($prefix, $opts, 'file');
    if (!$file) {
        return error("No file specified to split");
    }
    # get split
    $splits = get_opt($prefix, $opts, 'split');
    if (!$splits) {
        return error("No columns split specified to split file '{$file}'");
    }
    # check that the split is valid (i.e. an array of integers)
    if (!is_array($splits)) {
        return error("File split on columns does not have a split that is an array");
    }
    for ($i = 0; $i < count($splits); $i++) {
        $val = $splits[$i];
        if (!is_integer($val)) {
            $i++;
            $type = gettype($val);
            return error("Value at position '{$i}' of split for file '{$file}' is not an integer (is of type '{$type}')");
        }
    }
    # get trim (i.e. whether the values should have leading and trailing whitespace removed)
    $trim = get_opt_config_value($prefix, $opts, 'trim', true);
    # check file exists
    if (!is_file($file)) {
        return error("File '{$file}' does not exist");
    }
    # get lines
    $lines = file($file);
    # loop over lines to split the file
    $data = array();
    $total_splits = count($splits);
    $max_split_index = $total_splits - 1;
    foreach ($lines as $line) {
        $line_data = array();
        for ($i = 0; $i < $total_splits; $i++) {
            $min = $splits[$i];
            if ($i == 0) {
                $min = 0;
                $len = $splits[0];
            } else {
                $min = $splits[$i - 1];
                $len = $splits[$i] - $min;
            }
            $val = substr($line, $min, $len);
            if ($trim) {
                $val = trim($val);
            }
            $line_data[] = $val;
        }
        $data[] = $line_data;
    }
    # return data depending on whether called internally or not
    if ($pipe === false) {
        return $data;
    }
    $res = array('data' => $data, 'lines' => count($lines));
    return $res;
}
Exemplo n.º 12
0
function backup_file($opts, $pipe = null, $cmd = __FUNCTION__)
{
    # set prefix
    $prefix = 'file_backup';
    # merge opts
    $opts = merge_opts($opts, $pipe, 'file');
    # get backup file opt
    $backup_file = get_opt($prefix, $opts, 'backup_file', true);
    if (!check_opt_set_type($cmd, $backup_file, 'backup_file', 'boolean')) {
        return false;
    }
    if ($backup_file === false) {
        return true;
    }
    # get file opt
    $file = get_opt($prefix, $opts, 'file');
    if (!check_opt_set_type($cmd, $file, 'file', 'string')) {
        return false;
    }
    # get suffix opt
    $suffix = get_opt_config_value($prefix, $opts, 'suffix', '.bak.');
    if (!check_opt_set_type($cmd, $suffix, 'suffix', 'string')) {
        return false;
    }
    # create new path
    $index = 1;
    do {
        $new_file = "{$file}{$suffix}{$index}";
        if (!is_file($new_file)) {
            break;
        }
        $index++;
    } while (true);
    # move file
    debug_echo($cmd, "backing up file : {$file} => {$new_file}");
    if (!rename($file, $new_file)) {
        return error($cmd, "could not rename file : {$file} => {$new_file}");
    }
    return array('file' => $new_file);
}
Exemplo n.º 13
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;
}
Exemplo n.º 14
0
function http_finalize_parse_forms($prefix, $func_args, $cmd, $debug)
{
    # get opts and curlopts
    $opts =& $func_args['opts'];
    $res =& $func_args['res'];
    # get parse response hidden inputs opt
    $parse_response_form = get_opt_config_value($prefix, $opts, 'parse_response_form', false);
    if (!check_opt_set_type($cmd, $parse_response_form, 'parse_response_form', 'boolean')) {
        return false;
    }
    # get response form_name opt
    $response_form_name = get_opt_config_value($prefix, $opts, 'response_form_name');
    if (!check_opt_if_set_type($cmd, $response_form_name, 'response_form_name', 'string')) {
        return false;
    }
    if ($parse_response_form) {
        debug_echo($cmd, "parsing the response body for form data");
        return http_parse_form($prefix, $opts, $res, $response_form_name, $cmd, $debug);
    }
    return true;
}
Exemplo 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);
}