|
Frequently Asked Questions v2
|
The script /misc/file2slice/importer.php3 contains a function importer, which can import a CSV (comma-separated-values) file into your slice.
The data file should contain field names (any names, not from AA) on the first line, separated by your chosen separator. The next lines contain the items, one item per line, fields separated by the same separator.
E.g.:
AUTHOR,TEXT,EMAIL,LANGUAGE,CATEGORY
Johny Bravo, he is a real good guy, johny@bravo.uk, fr, VIP+fools
France Newman, a noname boy, france.newman@noname.org, en, nonames+fools
Here, the separator is comma, but it may be any other string.
The function trimmes each field from whitespace. It has these parametres:
- sliceID - ID of slice into which you want to import
- fileName - name of the file being imported
- separator - the string which separates the field values
- actions - an array describing how to import - see further
- postedBy - to be inserted into the postedBy and editedBy fields, the SQL user number or LDAP string (for LDAP users)
- fire - if true, writes the data into the database, if false (default), only shows the data in their final shape
- timeLimit - how many seconds may the script work before the server considers it unavailable (default=120)
- statusCode - where to put the item: 1 (default) - Active items, 2 - Hold Bin
- publishDate - if set to 0 (default), fills in the current date, otherwise fills in given value. Use a Unix time-stamp, i.e. number of seconds since the Unix Epoch (January 1 1970 00:00:00 GMT).
- expiryDate - if set to 0 (default), fills in 200 years after publishDate, otherwise fills in given value. Use a Unix time-stamp, i.e. number of seconds since the Unix Epoch (January 1 1970 00:00:00 GMT).
- maxRowLength - max length of one line (default = 50000)
You don't have to set all the parametres - the last one required is postedBy, the next ones have default values.
The "fire" parameter works as a safe mode. You can prepare your data with "fire" set to false and when you are sure that what you see on the screen is what you want to be added to the database (by the StoreItem function), you may set "fire" to true.
Actions
The array "actions" should look like this:
$actions = array(
"created_by......" => array("action" => "store", "from" => "AUTHOR"),
"category.......1" => array("action" => "storeparsemulti", "delimiter" => "+", "from" => "CATEGORY"),
"language.......1" => array("action" => "storetrans", "from" => "LANGUAGE", "flag" => 1, "trans"=>array("en"=>"english","fr"=>"french"))
);
It contains the AA field names followed by how to get their content. You may set flags (e.g. "flag" => 1
means HTML content) with any action.
The actions are as follows:
- store - simply stores the content of "from". Params:
- from - name of a field in the text file
- storeparsemulti - splits content of "from" and stores the result as a multi-value. In our example, Johny Bravo will be in both the categories "VIP" and "fools". Params:
- from - name of a field in the text file
- delimiter - string which separates the values
- storetrans - translates the content and stores it. In our example, France Newman will have "english" as his language. Params:
- from - name of a field in the text file
- trans - array containing translations
- storeasmulti - takes the values of several fields and stores them as one multi-value. Params:
- from - array of fields to be stored together. E.g.
"from" => array ("Name","Middle Name","Surname")
- storemultiasone - takes the values of several fields nad stores them as a single value seperated by the delimiter. Params:
- from - array of fields to be stored together. E.g.
"from" => array ("Name","Middle Name","Surname")
- delimiter - string to put between fields e.g.
"delimiter" => ", "
- bool - stores "1" if the given field contains "1", nothing else. Params:
- from - name of a field in the text file
- web - adds "http://" to the field if not beginning with "http" and not empty. Params:
- from - name of a field in the text file
- storeboolarray - stores constants if the given fields contain "1". Stores the result as one multi-value.
- from - array of fields and assigned constants. E.g.:
"from" => array ("Bool1" => "Bold", "Bool2" => "Italic", "Bool3" => "Underline")
A fully functional example script
<?php
$actions = array(
"category.......1" => array("action" => "storeparsemulti", "delimiter" => ";", "from" => "Category"),
"headline........" => array("action" => "store", "from" => "Title"),
"created_by......" => array("action" => "store", "from" => "Author"),
"full_text......." => array("action" => "store", "from" => "Summary", "flag"=>1)
);
require "./importer.php3";
Importer ( "aa65a21b285c25f9fe0d4b662919b4b2", // slice iD
"./books.txt", // file name
"t", // field separator
$actions,
"uid=ada01,ou=People,ou=AA", // posted by (there should be a number for sql permissions)
true, // write to DB?
600); // time limit in seconds
?>
Last Edit: Aug 17 2011
<aafaq id=1731> ~ToDo: 1731 How to import lots of data into a slice? </aafaq>
This FAQ interface was developed
by Jason at Commons.ca
|
APC
ActionApps is a free software content management system initiated by
the Association for Progressive Communications (APC)
APC - Internet and ICTs for social justice and development |