Skip to content
This repository was archived by the owner on Apr 23, 2020. It is now read-only.

Create.EN

Ivan Dudarev edited this page Oct 10, 2017 · 5 revisions

Documentation / Create database

language: EN RU

Use Converter class for create database

  1. Prepare files describing the intervals and registers from which you are going to compile a binary database. These files should look like a relational database table.

    for example

    Intervals file /path/to/cvs/networks.csv

    first_ip,last_ip,register_id
    "0.0.0.0","63.255.255.255",89
    "64.0.0.0","127.255.255.255",192
    "128.0.0.0","191.255.255.255",2
    "192.0.0.0","255.255.255.255",17
    

    Register file /path/to/cvs/info.csv.

    Unused row. e.g. copiryght. Second row is a header, also unused.
    id,interval_num,data1,data2
    2,3,"some info 1","some info 2"
    17,4,"some info 3","some info 4"
    89,1,"some info 5","some info 6"
    192,2,"some info 7","some info 8"
    34,"unused row","some info 9","some info 10"
    
  2. Set creation time.

    /**
     * $time - time in unixstamp format.
     */
    $converter->setTime(1507638600); // 2017/10/10 15:30:00
  3. Initialization converter

    $tmpDir = 'path/to/dir/for/temporary/files';
    $converter = new \Ddrv\Iptool\Converter($tmpDir);
  4. Set author information

    /**
     * $author can be a string no longer than 64 characters.
     */
    $author = 'Name Surname';
    $converter->setAuthor($author);
  5. Set license of database

    /**
     * $license may be the name of a public license or the text of a license. The length is unlimited.
     */
    $license = 'MIT';
    $converter->setLicense($license);
  6. Add prepared files. Use the addCSV() method with parameters:

    • unique key for file. Required;
    • path to csv file. Required;
    • count ignored first row (default 0);
    • CSV file ecnoding (default UTF-8);
    • delimeter of CSV (default ,);
    • enclosure of CSV (default ");
    • escape of CSV (default ).
    $converter->addCSV('infoCSV','/path/to/cvs/info.csv',2);
    $converter->addCSV('networksCSV','/path/to/cvs/networks.csv',1);
  7. Define format of registers

    $info = array(
        'interval' => array(
            /**
             * Type can be:
             * small - for integer from -128 to 127;
             * int - integer;
             * long - long integer;
             * float - float;
             * double - double (use it for coordinats);
             * string - string.
             */
            'type' => 'int',
            /**
             * The column number from which to take the parameter value. The account is from 0
             */
            'column' => 1,
        ),
        'caption' => array(
            'type' => 'string',
            'column' => 2,
            /**
             * For string type you can add a parameter transform. It can be:
             * low - converts a string to lowercase;
             * up - converts a string to uppercase;
             * none - leave the string as is. Default.
             */
            'transform' => 'low',
        ),
        'extendedInfo' => array(
            'type' => 'string',
            'column' => 3,
        ),
    );
  8. Add definite registers. Use the addRegister() method with parameters:

    • Register name. Required;
    • unique key of CSV file (from method addCSV). Required;
    • number of ID column. The account is from 0;
    • definite format of register.
    $converter->addRegister('info','infoCSV',0, $info);
  9. Define format of intervals

    $networks = array(
        /**
         * key is a name register
         * value ia number of column with ID of register
         */
        'info' => 2,
    );
  10. Add definite intervals. Use the addNetworks() method with parameters:

    • unique key of intervals CSV file (from method addCSV). Required;
    • format IP address in CSV file. May be:
      • ip (for example, 123.123.123.123);
      • long (for example 1361051648);
      • inetnum (for example 1.0.0.0/24).
    • column of first IP adderss. The account is from 0;
    • column of last IP adderss. The account is from 0;
    • definite format of intervals.
    $converter->addNetworks('networksCSV', 'ip', 0, 1, $networks);
  11. Run compile database

    $errors = $converter->getErrors();
    if (!$errors) {
        $dbFile = 'path/to/created/database.file';
        $converter->create($dbFile);
    } else {
        print_r($errors);
    }

    Wait and use!

See examples

Clone this wiki locally