Running WordPress plugin periodically to import data - wordpress

Here is the situation: I have a program producing .csv files with fields for title and post text (according to http://wordpress.org/extend/plugins/csv-importer/other_notes/). I actually can modify the program's output if it's required.
I can now manually log in into WordPress admin console, go to CSV importer plug-in tab, select file and import articles contained in the file.
However, I would like to run this task automatically -- everything will be running on the server (application producing (CSV) data, application/script importing data), not remote file uploading etc ... any idea how to do it?

You'll want to look into cron and/or WP's own wp_schedule_event.

I believe BlogSense Automation Tools provides a CSV import module that has the capability to be automated. As long as you have new csv files being generated and the titles of the posts are unique then the automation should flow.
Otherwise It will take some significant modification to the plugin; in which you would want to have it try to load a local static csv file with CURL (to replace the need to manually load one from your hard-darve) and then use the internal wordpress cronjob system to execute the process from there on out.
Here's the code I use to execute a wordpress internal cronjob every minute. You could alter the second intervals to something significantly larger.
add_filter('cron_schedules', 'add_per_min');
function add_per_min() {
return array(
'perminute' => array('interval' => 60, 'display' => 'Every Minute'),
);
}
if (!wp_next_scheduled('the_name_of_my_custom_interval')) {
wp_schedule_event(time(), 'perminute', 'the_name_of_my_custom_interval' );
}
add_action('the_name_of_my_custom_interval', 'the_function_to_run_here');

Related

Can I use PowerBI to access SharePoint files, and R to write those files to a local directory (without opening them)?

I have a couple of large .xlsb files in 2FA-protected SharePoint. They refresh periodically, and I'd like to automate the process of pulling them across to a local directory. I can do this in PowerBI already by polling the folder list, filtering to the folder/files that I want, importing them and using an R script to write that to an .rds (it doesn't need to be .rds - any compressed format would do). Here's the code:
let
#"~ Query ~"="",
//Address for the SP folder
SPAddress="https://....sharepoint.com/sites/...",
//Poll the content
Source15 = SharePoint.Files(SPAddress, [ApiVersion=15]),
//... some code to filter the content list down to the 2 .xlsb files I'm interested in - they're listed as nested 'binary' items under column 'Content' within table 'xlsbList'
//R export within an arbitrary 'add column' instruction
ExportRDS = Table.AddColumn(xlsbList, "Export", each R.Execute(
"saveRDS(dataset, file = ""C:/Users/current.user/Desktop/XLSBs/" & [Label] & ".rds"")",[dataset=Excel.Workbook([Content])[Data]{0}]))
However, the files are so large that my login times out before the refresh can complete. I've tried using R's file.copy command instead of saveRDS, to pick up the files as binaries (so PowerBI never has to import them):
R.Execute("file.copy(dataset, ""C:/Users/current.user/Desktop/XLSBs/""),[dataset=[Content]])
with dataset=[Content] instead of dataset=Excel.Workbook([Content])[Data]{0} (which gives me a different error, but in any event would result in the same runtime issues as before) but it tells me The Parameter 'dataset' isn't a Table. Is there a way to reference what PowerBI sees as binary objects, from within nested R (or Python) code so that I can copy them to a local directory without PowerBI importing them as data?
Unfortunately I don't have permissions to set the SharePoint site up for direct access from R/Python, or I'd leave PowerBI out entirely.
Thanks in advance for your help

import .doc files to wordpress

I am modifying a website on wordpress (codex) and I would like to import all the .doc files (about 200) from the old website and make them into posts on the wordpress website.
Any ideas how to do this bulk import or is the only way to copy and paste each one, which will take a long time.
There Is a way to do this - but as far as I know - it is not easy ..
I struggled with it one time - but could achieve it in the end .
The reason is that a WORD *.doc is a complicated format , and even simple Copy&Paste can make problems (as it actually copies also formatting)
You need to use the COM interface in php , turn the DOC to a TXT file or a string , and then create the post with wp_insert_post( $my_post );
so - opening the DOC . (you need to loop for bulk )
$filename="file.doc";
$TXTfilename = $filename . ".txt";
$word = new COM("word.application") or die("Unable to instantiate Word object");
$word->Documents->Open($filename);
$word->Documents[1]->SaveAs($TXTfilename ,2);// '2' for txt format
$word->Documents[1]->Close(false);
$word->Quit();
$word->Release();
$word = NULL;
unset($word);
$my_doc_string = file_get_contents($TXTfilename); //write file
unlink($TXTfilename);
Then, either read a txt file , or if in the same function use the $my_doc_string directly.. :
$title_p = 'mypost no'.$i;
$my_post = array(
'post_title' => $title_p,
'post_content' => $my_doc_string,
'post_status' => 'publish',
'post_author' => 1
);
$post_id = wp_insert_post( $my_post );
wp_insert_post( $my_post );
NOTE : the COM interface , as far as I know , Is available only on the WINDOWS vesion of PHP , and you will also need to have MS WORD installed ...
some more reading for you :
http://php.net/manual/en/book.com.php
http://docstore.mik.ua/orelly/webprog/php/ch15_03.htm
http://www.gsdesign.ro/blog/extracting-text-from-word-documents-in-php-with-com-objects/
EDIT I -
After reading your question again - if you have an OLD website - why do you need the DOC files ? you might be able to do that with an HTML PARSER (if the OLD website has the same content as the DOCs - but in HTML)
EDIT II - unbelievably - today I have stumbled into this plugin, it might be of some help although I did not tried it .
http://wordpress.org/extend/plugins/auto-poster/screenshots/
l downloaded a plugin https://wordpress.org/plugins/mammoth-docx-converter/ that is able to import word documents into wordpress editor for publishing. Format mapping is not 100% but does the basic stuff. All i need to do now is to be able to populate metadata in association with that particular document (custom post) upon import using particular key words from the post Content to asynchronously populate the meta boxes. I am also trying to generate excerpts automatically from a particular paragraph (second paragraph) of the document.I read about wp all import plugin and find it quite close to what i want but cant really figure out how word documents can be imported at the same time automating the operations stated above. I was able to come up with an XML file from source using WRX specification leveraging exported Content from my Website and then uploading again for test purposes but that is not what i really want to achieve. Is there anyway wp all import plugin can be customized to do what i want?
I also find your php approach quite suitable and might be more appropriate for what i need it to do and my goal. Important for me is an automated import of the word document as a post keeping the formatting intact and and then asynchronously populating associated meta data automatically. Do you have any recommendations based on the above php COM Code?

Drupal - Get image from url and import it into node

Im writing a module for drupal, Im trying to create a node from my module, everything is fine , I only have 1 problem with creating an image , The image exist on different server, so I want to grab the page and insert it , I install module http://drupal.org/project/filefield_sources , which has remote option , I search in the module code , I could not find the function that he used for this process, module work very nice from interface , but how i make it do the job from code ? which function should i call and what parameter should i pass .
I'm over Drupal 6.
Hopefully you're using Drupal 7...
The system_retrieve_file() function will download a file from a remote source, copy it from temp to a specified destination and optionally save it to the file_managed table if you want it to be managed.
$managed = TRUE; // Whether or not to create a Drupal file record
$path = system_retrieve_file($url, 'public://my_files/', $managed);
If you want to get the file object immediately after you've done this, the following is the only way I've found so far:
$file = file_load(db_query('SELECT MAX(fid) FROM {file_managed}')->fetchField());
get fid using $path->fid. no need to mysql

Re-processing attached images in drupal 7

I'm trying to import nodes from my forum to drupal 7. Not in bulk, but one by one so that news posts can be created and referenced back to the forum. The kicker is that I'm wanting to bring image attachments across as well...
So far, using the code example here http://drupal.org/node/889058#comment-3709802 things mostly work: Nodes are created, but the images don't go through any validation or processing.
I'd like the attached images to be validated against the rules defined in the content type. in particular the style associated with my image field which resizes them to 600x600.
So, instead of simply creating the nodes programatically with my own form, i decided to modify a "new" node using hook_node_prepare and using the existing form to create new content (based on passed in url args). This works really well and a create form is presented pre-filled with all my data. including the image! very cute.
I expected that i could then hit preview or save and all the validation and resizing would happen to my image, but instead i get the error:
"The file used in the Image field may not be referenced."
The reason for this is that my file doesn't have an entry in the file_usage table.. *le sigh*
so, how do i get to all the nice validation and processing which happens when i manually choose a file to upload? like resizing, an entry in the file_usage table.
The ajax upload function does it, but i can't find the code which is called to do this anywhere in the api.
What file upload / validation functions does Drupal call which i'm not doing?
Anybody have any experience with the file/image api for Drupal 7 who can help me out?
For getting the usage entry (in essence, checking out a file to a specific module so that it doesn't get deleted while its in use) look up the Drupal function 'file_usage_add()'
For validating incoming images, I got this example from user.module (if you're comfortable with PHP, you can always look at the core to see how something is done the 'Drupal way'):
function user_validate_picture(&$form, &$form_state) {
// If required, validate the uploaded picture.
$validators = array(
'file_validate_is_image' => array(),
'file_validate_image_resolution' => array(variable_get('user_picture_dimensions', '85x85')),
'file_validate_size' => array(variable_get('user_picture_file_size', '30') * 1024),
);
// Save the file as a temporary file.
$file = file_save_upload('picture_upload', $validators);
if ($file === FALSE) {
form_set_error('picture_upload', t("Failed to upload the picture image; the %directory directory doesn't exist or is not writable.", array('%directory' => variable_get('user_picture_path', 'pictures'))));
}
elseif ($file !== NULL) {
$form_state['values']['picture_upload'] = $file;
}
}
That function is added to the $form['#validate'] array like so:
$form['#validate'][] = 'user_validate_picture'

How can I associate many existing files with drupal filefield?

I have many mp3 files stored on my server already from a static website, and we're now moving to drupal. I'm going to create a node for each audio file, but I don't want to have to upload each file again. I'd rather copy the files into the drupal files directory where I want them, and then associate the nodes with the appropriate file.
Any ideas on how to accomplish that?
Thanks!
I am not sure if I am going to propose a different approach or if I am about to tell with different words what you already meant with your original question, but as you want the nodes to be the files, I would rather generate the nodes starting from the files, rather than associating existing nodes with existing files.
In generic terms I would do it programmatically: for each existing files in your import directory I would build the $node object and then invoke node_save($node) to store it in Drupal.
Of course in building the $node object you will need to invoke the API function of the module you are using to manage the files. Here's some sample code I wrote to do a similar task. In this scenario I was attaching a product sheet to a product (a node with additional fields), so...
field_sheet was the CCK field for the product sheet in the product node
product was the node type
$sheet_file was the complete reference (path + filename) to the product sheet file.
So the example:
// Load the CCK field
$field = content_fields('field_sheet', 'product');
// Load the appropriate validators
$validators = array_merge(filefield_widget_upload_validators($field));
// Where do we store the files?
$files_path = filefield_widget_file_path($field);
// Create the file object
$file = field_file_save_file($sheet_file, $validators, $files_path);
// Apply the file to the field, this sets the first file only, could be looped
// if there were more files
$node->field_scheda = array(0 => $file);
// The file has been copied in the appropriate directory, so it can be
// removed from the import directory
unlink($sheet_file);
BTW: if you use a library to read MP3 metadata, you could set $node->title and other attributes in a sensible way.
Hope this helps!
The file_import module doesn't do exactly what you want (it creates node attachments instead of nodes), but it would be relatively simple to use that module as guidance along with the Drapal API to do what you want.

Resources