Documentation here.';
// Function that will handle the wizard-like behaviour
function dispatch() {
if (empty ($_GET['step']))
$step = 0;
else
$step = (int) $_GET['step'];
// load the header
$this->header();
switch ($step) {
case 0 :
$this->greet();
break;
case 1 :
check_admin_referer('import-upload');
$this->marc_accept_file();
break;
case 2:
$this->marc_parse_file();
break;
case 3:
$this->harvest_import();
break;
case 4:
$this->ktnxbye();
break;
}
// load the footer
$this->footer();
}
function header() {
echo '
';
echo '
'.__('Scriblio Catalog Importer').'
';
}
function footer() {
echo '';
}
function greet() {
$this->activate();
echo '';
echo '
'.__('Howdy! Start here to import MaRC records into Scriblio.').'
';
echo '
'.__('This has not been tested much. Mileage may vary.').'
';
echo '
';
wp_import_upload_form("admin.php?import=$this->importer_code&step=1");
echo '
';
echo '
';
echo '
';
}
function ktnxbye() {
echo '';
echo '
'.__('All done.').'
';
echo '
';
}
function marc_accept_file(){
$prefs = get_option('scrib_marcimporter');
$prefs['scrib_marc-warnings'] = array();
$prefs['scrib_marc-errors'] = array();
$prefs['scrib_marc-record_start'] = 0;
$prefs['scrib_marc-records_harvested'] = 0;
update_option('scrib_marcimporter', $prefs);
$this->marc_options();
}
function marc_options(){
global $file;
if(empty($this->id)){
$file = wp_import_handle_upload();
if ( isset($file['error']) ) {
echo ''.__('Sorry, there has been an error.').'
';
echo '' . $file['error'] . '
';
return;
}
$this->file = $file['file'];
$this->id = (int) $file['id'];
}
$prefs = get_option('scrib_marcimporter');
echo '';
echo '
'.__('MaRC file options.').'
';
echo '
'.__('All Scriblio records have a 'sourceid,' a unique alphanumeric string that's used to avoid creating duplicate records and, in some installations, link back to the source system for current availability information.').'
';
echo '
'.__('The sourceid is made up of two parts: the prefix that you assign, and a ID info from the source record. Many systems assign unique numbers to each record, the challenge is figuring out which field to use.').'
';
echo '
';
echo '
';
}
function marc_parse_file(){
$interval = 2500;
if( empty( $_REQUEST[ 'scrib_marc-record_start' ] ))
$n = 0;
else
$n = (int) $_REQUEST[ 'scrib_marc-record_start' ];
ini_set('memory_limit', '1024M');
set_time_limit(0);
ignore_user_abort(TRUE);
$this->id = (int) $_GET['id'];
$this->file = get_attached_file($this->id);
if(empty($_POST['scrib_marc-sourceprefix']) || empty($_POST['scrib_marc-sourcefield']) || empty($this->file)){
echo ''.__('Sorry, there has been an error.').'
';
echo 'Please complete all fields
';
return;
}
// save these settings so we can try them again later
$prefs = get_option('scrib_marcimporter');
$prefs['scrib_marc-sourceprefix'] = stripslashes($_POST['scrib_marc-sourceprefix']);
$prefs['scrib_marc-sourcefield'] = stripslashes($_POST['scrib_marc-sourcefield']);
update_option('scrib_marcimporter', $prefs);
error_reporting(E_ERROR);
// initialize the marc library
require_once(ABSPATH . PLUGINDIR .'/'. plugin_basename(dirname(__FILE__)) .'/includes/php-marc.php');
$file = new File($this->file);
$prefs['scrib_marc-records_count'] = count($file->raw);
update_option('scrib_marcimporter', $prefs);
if($n > 0 || count($file->raw) > $interval)
$file->raw = array_slice($file->raw, $n, $interval);
if(!empty($_POST['scrib_marc-debug'])){
$record = $file->next();
echo 'The MaRC Record:
';
print_r($record->fields());
echo '
The Tags and Display Record:
';
$test_pancake = $this->marc_parse_record($record->fields());
print_r($test_pancake);
echo '
';
echo 'The Raw Excerpt:
'. $test_pancake['the_excerpt'] .'
';
echo 'The Raw Content:
'. $test_pancake['the_content'] .'
';
echo 'The SourceID: '. $test_pancake['the_sourceid'] .'
';
// bring back that form
echo ''.__('File Options').'
';
echo 'File has '. $prefs['scrib_marc-records_count'] .' records.
';
$this->marc_options();
}else{
// import with status
$count = 0;
echo "Reading the file and parsing ". $file->num_records() ." records. Please be patient.
";
echo '';
while($file->pointer < count($file->raw)){
if($record = $file->next()){
$bibr = &$this->marc_parse_record($record->fields());
echo "- {$bibr['the_title']} {$bibr['the_sourceid']}
";
$count++;
}
}
echo '
';
$prefs['scrib_marc-warnings'] = array_merge($prefs['scrib_marc-warnings'], $file->warn);
$prefs['scrib_marc-errors'] = array_merge($prefs['scrib_marc-errors'], $file->error);
$prefs['scrib_marc-records_harvested'] = $prefs['scrib_marc-records_harvested'] + $count;
update_option('scrib_marcimporter', $prefs);
if(count($file->raw) >= $interval){
$prefs['scrib_marc-record_start'] = $n + $interval;
update_option('scrib_marcimporter', $prefs);
$this->marc_options();
?>
marc_done();
?>
tagno == 100) || ($field->tagno == 110)){
$temp = ereg_replace(',$', '', $field->subfields['a'] .' '. $field->subfields['d']);
$atomic['author'][] = $temp;
}else if($field->tagno == 110){
$temp = $field->subfields['a'];
$atomic['author'][] = $temp;
}else if(($field->tagno > 699) && ($field->tagno < 721)){
$temp = ereg_replace(',$', '', $field->subfields['a'] .' '. $field->subfields['d']);
$atomic['author'][] = $temp;
//Standard Numbers
}else if($field->tagno == 10){
$temp = explode(' ', trim($field->subfields['a']));
$atomic['lccn'][] = ereg_replace('[^0-9]', '', $temp[0]);
}else if($field->tagno == 20){
$temp = trim($field->subfields['a']) . ' ';
$temp = ereg_replace('[^0-9|x|X]', '', strtolower(substr($temp, 0, strpos($temp, ' '))));
$atomic['isbn'][] = $temp;
}else if($field->tagno == 22){
$temp = trim($field->subfields['a']) . ' ';
$temp = ereg_replace('[^0-9|x|X|\-]', '', strtolower(substr($temp, 0, strpos($temp, ' '))));
$atomic['issn'][] = $temp;
//Call Numbers
}else if($field->tagno == 852){ // callnums from InfoCenter
$temp = trim($field->subfields['h']);
$atomic['callnumber'][] = $temp;
$temp = trim($field->subfields['b']);
$atomic['location'][] = $temp;
$atomic['acqdate'][] = $field->subfields[x]{14}.$field->subfields[x]{15}.$field->subfields[x]{16}.$field->subfields[x]{17} .'-'. $field->subfields[x]{18}.$field->subfields[x]{19} .'-'. $field->subfields[x]{20}.$field->subfields[x]{21};
//Titles
}else if($field->tagno == 245){
$temp = ucwords(trim(trim(trim(ereg_replace('/$', '', $field->subfields['a']) .' '. trim(ereg_replace('/$', '', $field->subfields['b']))), ',.;:/')));
$atomic['title'][] = $temp;
$atomic['attribution'][] = trim(trim(trim($field->subfields['c']), ',.;:/'));
}else if($field->tagno == 240){
$temp = ucwords(trim(trim(trim(ereg_replace('/$', '', $field->subfields['a']) .' '. trim(ereg_replace('/$', '', $field->subfields['b']))), ',.;:/')));
$atomic['alttitle'][] = $temp;
}else if(($field->tagno > 719) && ($field->tagno < 741)){
$temp = $field->subfields['a'];
$atomic['alttitle'][] = $field->subfields['a'];
//Dates
}else if($field->tagno == 260){
$temp = str_pad(substr(ereg_replace('[^0-9]', '', $field->subfields['c']), 0, 4), 4 , '5');
$atomic['pubyear'][] = $temp;
}else if($field->tagno == 005){
$atomic['catdate'][] = $field->data{0}.$field->data{1}.$field->data{2}.$field->data{3} .'-'. $field->data{4}.$field->data{5} .'-'. $field->data{6}.$field->data{7};
}else if($field->tagno == 008){
$atomic['pubyear'][] = substr($field->data, 14, 4);
//Subjects
}else if(($field->tagno > 599) && ($field->tagno < 700)){
$atomic['subject'][] = trim(trim(implode(' -- ', $field->subfields)), '.');
if($atomic['subjkey']){
$atomic['subjkey'] = array_unique(array_merge($atomic['subjkey'], array_map($trimmer, array_values($field->subfields))));
}else{
$atomic['subjkey'] = array_map($trimmer, array_values($field->subfields));
}
//URLs
}else if($field->tagno == 856){
unset($temp);
$temp['href'] = $temp['title'] = str_replace(' ', '', $field->subfields['u']);
$temp['title'] = trim(parse_url( $temp['href'] , PHP_URL_HOST), 'www.');
if($field->subfields['3'])
$temp['title'] = $field->subfields['3'];
if($field->subfields['z'])
$temp['title'] = $field->subfields['z'];
$atomic['url'][] = ''. $temp['title'] .'';
//Notes
}else if(($field->tagno > 299) && ($field->tagno < 400)){
$atomic['physdesc'][] = implode(' ', array_values($field->subfields));
}else if(($field->tagno > 399) && ($field->tagno < 500)){
$atomic['title'][] = implode("\n", array_values($field->subfields));
}else if(($field->tagno > 799) && ($field->tagno < 841)){
$atomic['series'][] = implode("\n", array_values($field->subfields));
}else if(($field->tagno > 499) && ($field->tagno < 600)){
$line = implode("\n", array_values($field->subfields));
if($field->tagno == 504)
continue;
if($field->tagno == 505){
$atomic['contents'][] = str_replace(array('> ','> ','> '), '>', ''. str_replace('--', "\n", trim(str_replace(array(' ', ' ', ' '), ' ', $line))) .'');
continue;
}
$atomic['notes'][] = str_replace(' ', ' ', $line);
}
//Format
if((!$atomic['format']) && ($field->tagno > 239) && ($field->tagno < 246)){
$temp = ucwords(strtolower(str_replace('[', '', str_replace(']', '', $field->subfields['h']))));
if(eregi('^book', $temp)){
$format = 'Book';
$formats = 'Books';
}else if(eregi('^micr', $temp)){
$format = 'Microform';
}else if(eregi('^electr', $temp)){
$format = 'Website';
$formats = 'Websites';
}else if(eregi('^vid', $temp)){
$format = 'Video';
}else if(eregi('^motion', $temp)){
$format = 'Video';
}else if(eregi('^audi', $temp)){
$format = 'Audio';
}else if(eregi('^cass', $temp)){
$format = 'Audio';
}else if(eregi('^phono', $temp)){
$format = 'Audio';
}else if(eregi('^record', $temp)){
$format = 'Audio';
}else if(eregi('^sound', $temp)){
$format = 'Audio';
}else if(eregi('^carto', $temp)){
$format = 'Map';
$formats = 'Maps';
}else if(eregi('^map', $temp)){
$format = 'Map';
$formats = 'Maps';
}else if(eregi('^globe', $temp)){
$format = 'Map';
$formats = 'Maps';
}else if($temp){
$format = 'Classroom Material';
//$format = $temp;
}
if(!$formats)
$formats = $format;
if($format){
$atomic['format'][] = $format;
$atomic['formats'][] = $formats;
}
}
}
}
if(!$atomic['format'][0]){
$atomic['format'][0] = 'Book';
}
if(!$atomic['acqdate'])
$atomic['acqdate'] = $atomic['catdate'];
if(!$atomic['catdate'][0])
$atomic['catdate'][0] = '1984-01-01';
if($atomic['pubyear'][0] > (date(Y) + 5))
$atomic['pubyear'][0] = substr($atomic['catdate'][0],0,4);
if($atomic['pubyear'][0]){
$atomic['pubdate'][] = $atomic['pubyear'][0].substr($atomic['catdate'][0],4);
}
if($atomic['alttitle'])
$atomic['title'] = array_unique(array_merge($atomic['title'], $atomic['alttitle']));
$atomic['the_sourceid'] = substr(ereg_replace('[^a-z|0-9]', '', strtolower($_POST['scrib_marc-sourceprefix'])), 0, 2) . trim(eval('return($marcrecord'. str_replace(array('(',')','$'), '', stripslashes($_POST['scrib_marc-sourcefield'])) .');'));
if(!empty($atomic['title']) && !empty($atomic['the_sourceid'])){
$atomic['tags']['subj'] = $atomic['subjkey'];
$atomic['tags']['auth'] = $atomic['author'];
$atomic['tags']['isbn'] = $atomic['isbn'];
$atomic['tags']['title'] = $atomic['title'];
$atomic['tags']['format'] = $atomic['formats'];
$atomic['tags']['pubyear'] = $atomic['pubyear'];
if($sweets = $this->get_sweets($atomic['isbn'])){
if(!empty($sweets['img']));
$atomic['img'] = $sweets['img'];
if(!empty($sweets['summary'])){
$atomic['shortdescription'] = html_entity_decode($sweets['summary']);
}
}
$atomic['the_title'] = $atomic['title'][0];
$atomic['the_pubdate'] = $atomic['pubdate'][0];
$atomic['the_acqdate'] = $atomic['acqdate'][0];
$atomic['the_excerpt'] = $this->the_excerpt($atomic);
$atomic['the_content'] = $this->the_content($atomic);
$this->insert_harvest($atomic);
return($atomic);
}else{
return(FALSE);
}
}
function marc_done(){
$prefs = get_option('scrib_marcimporter');
// click next
echo '';
if(count($prefs['scrib_marc-warnings'])){
echo '
Warnings
';
echo '
bottom ·
errors';
echo '
- ';
echo implode($prefs['scrib_marc-warnings'], '
- ');
echo '
';
}
if(count($prefs['scrib_marc-errors'])){
echo '
Errors
';
echo '
bottom ·
warnings';
echo '
- ';
echo implode($prefs['scrib_marc-errors'], '
- ');
echo '
';
}
echo '
'.__('Processing complete.').'
';
echo '
'. $prefs['scrib_marc-records_harvested'] .' of '. $prefs['scrib_marc-records_count'] .' '.__('records harvested.').' with '. count($prefs['scrib_marc-warnings']) .' warnings and '. count($prefs['scrib_marc-errors']) .' errors.
';
echo '
'.__('Continue to the next step to publish those harvested catalog entries.').'
';
echo '
';
echo '
';
}
function update_record( $sourceid ) {
global $wpdb, $bsuite;
if( !$bsuite->get_lock( 'scrib_passvupd' ))
return( FALSE );
// if ( wp_cache_get( $sourceid, 'scrib_passvupd' ) > time())
// return( FALSE );
$post = $wpdb->get_row('SELECT * FROM '. $this->harvest_table .' WHERE source_id = "'. $wpdb->escape( $sourceid ) .'" AND imported = 0 LIMIT 1');
if( isset( $post->source_id )) {
$post_id = $this->insert_post( unserialize( $post->content ));
if( $post_id )
$wpdb->get_var('UPDATE '. $this->harvest_table .' SET imported = 1 WHERE source_id = "'. $post->source_id .'"');
else
$wpdb->get_var('UPDATE '. $this->harvest_table .' SET imported = -1 WHERE source_id = "'. $post->source_id .'"');
}
// wp_cache_add( $sourceid, time() + 86400, 'scrib_passvupd', 86400 );
}
function harvest_import() {
global $wpdb;
$interval = 50;
if( isset( $_GET[ 'n' ] ) == false ) {
$n = 0;
} else {
$n = intval( $_GET[ 'n' ] );
}
$posts = $wpdb->get_results('SELECT * FROM '. $this->harvest_table .' WHERE imported = 0 LIMIT 0,'. $interval, ARRAY_A);
if( is_array( $posts ) ) {
$count = $wpdb->get_var('SELECT COUNT(*) FROM '. $this->harvest_table .' WHERE imported = 0');
echo "Fetching records in batches of $interval...importing them...making coffee. Please be patient.
";
echo '';
foreach( $posts as $post ) {
$post_id = $this->insert_post(unserialize($post['content']));
if($post_id){
$wpdb->get_var('UPDATE '. $this->harvest_table .' SET imported = 1 WHERE source_id = "'. $post['source_id'] .'"');
echo '- '. get_the_title($post_id) .'
';
}else{
$wpdb->get_var('UPDATE '. $this->harvest_table .' SET imported = -1 WHERE source_id = "'. $post['source_id'] .'"');
}
}
echo '
';
?>
'. ($count - $interval) .' records remain to be imported.';
} else {
echo 'That's all folks. kthnxbye.
';
}
}
function insert_harvest($bibr) {
global $wpdb;
$wpdb->get_results("REPLACE INTO $this->harvest_table
(source_id, harvest_date, imported, content)
VALUES ('". $wpdb->escape($bibr['the_sourceid']) ."', NOW(), 0, '". $wpdb->escape(serialize($bibr)) ."')");
}
function post_exists($sourceid) {
if($post_id = get_objects_in_term( is_term($sourceid), 'sourceid' ))
return($post_id[0]);
// return(FALSE);
}
function get_the_content( $bibr ){
if( function_exists( 'scribimport_content_'.$source ))
return( call_user_func( 'scribimport_content_'.$source, $bibr ));
else if( $bibr['cformatter'] && function_exists( $bibr['cformatter'] ))
return( call_user_func( $bibr['cformatter'], $bibr ));
else
return( $this->the_content( $bibr ));
}
function get_the_excerpt( $bibr ){
if( function_exists( 'scribimport_excerpt_'.$source ))
return( call_user_func( 'scribimport_excerpt_'.$source, $bibr ));
else if( $bibr['eformatter'] && function_exists( $bibr['eformatter'] ))
return( call_user_func( $bibr['eformatter'], $bibr ));
else
return( $this->the_excerpt( $bibr ));
}
function insert_post( $bibr ){
// return(1);
global $wpdb, $bsuite, $scrib;
wp_defer_term_counting( TRUE ); // may improve performance
remove_filter('content_save_pre', array(&$bsuite, 'innerindex_nametags')); // don't build an inner index for catalog records
remove_filter('publish_post', '_publish_post_hook', 5, 1); // avoids pinging links in catalog records
remove_filter('save_post', '_save_post_hook', 5, 2); // don't bother
kses_remove_filters(); // don't kses filter catalog records
define( 'WP_IMPORTING', TRUE ); // may improve performance by preventing exection of some unknown hooks
$source = substr( $bibr['the_sourceid'], 0, 2 );
if($this->post_exists($bibr['the_sourceid']))
$postdata['ID'] = $this->post_exists($bibr['the_sourceid']);
$postdata['post_title'] = $wpdb->escape(str_replace('\"', '"', $bibr['the_title']));
//Start HKUST Customization
// $postdata['post_date'] = $bibr['the_pubdate'];
$postdata['post_date'] = $bibr['the_acqdate'];
//End HKUST Customization
$postdata['post_date_gmt'] = $bibr['the_acqdate'];
$postdata['comment_status'] = get_option('default_comment_status');
$postdata['ping_status'] = get_option('default_pingback_flag');
$postdata['post_status'] = 'publish';
$postdata['post_type'] = 'post';
$postdata['post_author'] = $scrib->options['catalog_author_id'];
$postdata['post_content'] = $wpdb->escape( $this->get_the_content( $bibr ));
$postdata['post_excerpt'] = $wpdb->escape( $this->get_the_excerpt( $bibr ));
if( empty( $postdata['post_title'] ))
return( FALSE );
$post_id = wp_insert_post($postdata); // insert the post
if($post_id){
// $bsuite->searchsmart_upindex($post_id, $bibr['the_content'], $bibr['the_title']); // update the full text index // disabled because bSuite 4 now builds the search index in the background
wp_set_object_terms($post_id, $bibr['the_sourceid'], 'sourceid', TRUE);
if( empty( $bibr['the_category'] ))
$bibr['the_category'] = (int) $scrib->options['catalog_category_id'];
wp_set_object_terms($post_id, $bibr['the_category'], 'category', FALSE);
add_post_meta( $post_id, 'scrib_the_author', $bibr['attribution'][0], TRUE ) or update_post_meta( $post_id, 'scrib_the_author', $bibr['attribution'][0] );
add_post_meta( $post_id, 'scrib_the_author_link', implode( '|', $bibr['author'] ), TRUE ) or update_post_meta( $post_id, 'scrib_the_author_link', implode( '|', $bibr['author'] ) );
if( is_array( $bibr['bibkeys'] ))
foreach( $bibr['bibkeys'] as $temp )
$real_bibkeys[] = key( $temp ) .':'. current( $temp );
add_post_meta( $post_id, 'scrib_the_bibkeys', serialize( $real_bibkeys ), TRUE ) or update_post_meta( $post_id, 'scrib_the_bibkeys', serialize( $real_bibkeys ));
//insert the tags
if(is_array($bibr['tags'])){
$bibr['tags'] = array_filter($bibr['tags']);
foreach(array_keys($bibr['tags']) as $taxonomy){
$bibr['tags'][$taxonomy] = array_filter($bibr['tags'][$taxonomy]);
register_taxonomy( $taxonomy, 'post' );
wp_set_object_terms($post_id, $bibr['tags'][$taxonomy], $taxonomy, TRUE);
}
}
return($post_id);
}
return(FALSE);
}
function get_altisbn($isbn) {
$result = array();
// OCLC's xISBN
// http://www.oclc.org/research/projects/xisbn/
if($xml = file_get_contents('http://labs.oclc.org/xisbn/' . $isbn)){
foreach ($xml->xpath('/idlist/isbn') as $temp)
$isbn[] = (string) $temp;
}
// the first element of the array is always the same as the query ISBN, delete it
array_shift($result);
/*
Also note LibraryThing's thingISBN
http://www.librarything.com/thingology/2006/06/introducing-thingisbn_14.php
'http://www.librarything.com/api/thingISBN/' . $isbn;
*/
return($result);
}
function get_cachejacket( $url, $filename, $dl = TRUE ){
$uploads = wp_upload_dir( 'jckt-'. substr( $filename, 4, 2 ));
if( $uploads['path'] ){
if(!is_dir( $uploads['path'] ))
mkdir( $uploads['path'], 0775, TRUE );
if( $dl )
exec( 'curl -o '. escapeshellarg( $uploads['path'] .'/'. $filename ) .' '. escapeshellarg( $url ));
return( $uploads['url'] .'/'. $filename );
}
return( FALSE );
}
function get_sweets( $bibkeys, $the_title = '', $the_author, $the_sourceid ){
$prefs = get_option('scrib_marcimporter');
foreach( $bibkeys as $bibn )
$bibkey[] = '{"'. key( $bibn ) .'":"'. current( $bibn ) .'"}';
$url = 'http://api.scriblio.net/v01b/enrich/?return=php&query='. urlencode( '{"bibkeys":['. implode( $bibkey, ',' ) .'],"title":"'. str_replace( '"', '\"', $the_title ) .'","author":"'. str_replace( '"', '\"', $the_author ) .'"}' );
//echo ''. $url .'
';
$session = curl_init( $url );
// curl_setopt($session, CURLOPT_HEADER, TRUE);
curl_setopt($session, CURLOPT_RETURNTRANSFER, TRUE);
// there's an ugly hack/work around below. For some reason the API server is returning crunk at the head of the serialized result. I'm removing it with substring, but i should fix it at the source.
$content = unserialize( substr( curl_exec( $session ), 6 ));
curl_close( $session );
//print_r( $content );
if( $content->status == 'ok' ){
if( !isset( $content->image->thumb->url )){
$content->fakejacket->tiny = $content->image->tiny;
$content->image = $content->fakejacket;
$content->image->thumb->url = $this->get_cachejacket( $content->image->thumb->url , $the_sourceid .'_t.jpg' );
$content->image->large = $content->image->thumb;
}else if( $prefs['scrib_marc-cachejacket'] ){
$content->image->thumb->url = $this->get_cachejacket( $content->image->thumb->url , $the_sourceid .'_t.jpg' );
$content->image->large->url = $this->get_cachejacket( $content->image->large->url , $the_sourceid .'_l.jpg' );
}
return( $content );
}
return( FALSE );
}
function get_summarized($text){
// api: http://api.scriblio.net/v01a/summarize/?text=...
// The POST URL and parameters
$request = 'http://api.scriblio.net/v01a/summarize/';
$postargs = 'text='.urlencode($text);
// Get the curl session object
$session = curl_init($request);
// Set the POST options.
curl_setopt ($session, CURLOPT_POST, true);
curl_setopt ($session, CURLOPT_POSTFIELDS, $postargs);
curl_setopt($session, CURLOPT_HEADER, FALSE);
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
// Do the POST and then close the session
$response = curl_exec($session);
curl_close($session);
if(!empty($response))
return($response);
else
return(FALSE);
}
function the_excerpt($bibr){
global $scrib;
$result = '';
if($bibr['img']->thumb->url ){
$result .= '- [scrib_bookjacket]
[/scrib_bookjacket] ';
}else{
$result .= '- [scrib_bookjacket]
[/scrib_bookjacket] ';
}
$result .= 'Attribution
'. array_shift( $bibr['attribution'] ) .' ';
$pubdeets = array();
if( count( $bibr['format'] ))
$pubdeets[] = ''. array_shift( $bibr['format'] ) .'';
if( count( $bibr['edition'] ))
$pubdeets[] = ''. array_shift( $bibr['edition'] ) .'';
if( count( $bibr['publisher'] ))
$pubdeets[] = ''. array_shift( $bibr['publisher'] ) .'';
if( count( $bibr['pubyear'] ))
$pubdeets[] = ''. array_shift( $bibr['pubyear'] ) .'';
if( count( $pubdeets ))
$result .= 'Publication Details
'. implode( ', ', $pubdeets ) .' ';
if(count($bibr['url']) > 0){
$result .= '- ';
$result .= '
Links
';
$result .= '';
foreach($bibr['url'] as $temp){
$result .= '- ' . $temp . '
';
}
$result .= '
';
$result .= ' ';
}
if($bibr['shortdescription'])
$result .= 'Description
'. $bibr['shortdescription'] .' ';
// build a block of tags
$tags = NULL;
if($bibr['subjkey']){
foreach($bibr['subjkey'] as $temp){
$tags[] = '' . $temp . '';
}
}
if($bibr['author']){
foreach($bibr['author'] as $temp){
$tags[] = '' . $temp . '';
}
}
if($tags){
$result .= 'Tags
'. implode(' · ', $tags) .' ';
}
// end tag block
$result .= 'Availability
[scrib_availability sourceid="'. $bibr['the_sourceid'] .'"] ';
$result .='
';
return($result);
}
function the_content($bibr){
global $scrib;
$result = '';
if($bibr['img']->large->url ){
$result .= '- [scrib_bookjacket]
[/scrib_bookjacket] ';
}else{
$result .= '- [scrib_bookjacket]
[/scrib_bookjacket] ';
}
if($bibr['title']){
$result .= '- ';
if( 1 < count($bibr['title'] )){
$result .= '
Titles
';
}else{
$result .= 'Title
';
}
$result .= '';
foreach($bibr['title'] as $temp){
$result .= '- ' . $temp . '
';
}
$result .= '
';
$result .= ' ';
}
$result .= 'Attribution
'. array_shift( $bibr['attribution'] ) .' ';
$pubdeets = array();
if( count( $bibr['format'] ))
$pubdeets[] = ''. array_shift( $bibr['format'] ) .'';
if( count( $bibr['edition'] ))
$pubdeets[] = ''. array_shift( $bibr['edition'] ) .'';
if( count( $bibr['publisher'] ))
$pubdeets[] = ''. array_shift( $bibr['publisher'] ) .'';
if( count( $bibr['pubyear'] ))
$pubdeets[] = ''. array_shift( $bibr['pubyear'] ) .'';
if( count( $pubdeets ))
$result .= 'Publication Details
'. implode( ', ', $pubdeets ) .' ';
$result .= 'Availability
[scrib_availability sourceid="'. $bibr['the_sourceid'] .'"] ';
if( count( $bibr['callnumber'] )){
$result .= '- ';
$result .= '
Call Number
';
$result .= '';
foreach($bibr['callnumber'] as $temp)
$result .= '- ' . $temp . '
';
$result .= '
';
$result .= ' ';
}
if( count( $bibr['deweynumber'] )){
$result .= '- ';
$result .= '
Dewey Number
';
$result .= '';
foreach($bibr['deweynumber'] as $temp)
$result .= '- ' . $temp . '
';
$result .= '
';
$result .= ' ';
}
if( count( $bibr['lcclass'] )){
$result .= '- ';
$result .= '
LC Classification
';
$result .= '';
foreach($bibr['lcclass'] as $temp)
$result .= '- ' . $temp . '
';
$result .= '
';
$result .= ' ';
}
if(count($bibr['url']) > 0){
$result .= '- ';
$result .= '
Links
';
$result .= '';
foreach($bibr['url'] as $temp){
$result .= '- ' . $temp . '
';
}
$result .= '
';
$result .= ' ';
}
if( !empty( $bibr['description'] ))
$result .= 'Description
'. $bibr['description'] .' ';
else if( !empty( $bibr['shortdescription'] ))
$result .= 'Description
'. $bibr['shortdescription'] .' ';
if( count( $bibr['author'] )){
$result .= '- ';
if( 1< count( $bibr['author'] ))
$result .= '
Authors
';
else
$result .= 'Author
';
$result .= '';
foreach($bibr['author'] as $temp)
$result .= '- ' . $temp . '
';
$result .= '
';
$result .= ' ';
}
if( count( $bibr['author_alt'] )){
$result .= '- ';
if( 1< count( $bibr['author_alt'] ))
$result .= '
Aditional Authors
';
else
$result .= 'Aditional Author
';
$result .= '';
foreach($bibr['author_alt'] as $temp)
$result .= '- ' . $temp . '
';
$result .= '
';
$result .= ' ';
}
if( count( $bibr['genre'] )){
$result .= '- ';
$result .= '
Genre
';
$result .= '';
foreach($bibr['genre'] as $temp)
$result .= '- ' . $temp . '
';
$result .= '
';
$result .= ' ';
}
if( count( $bibr['subject'] )){
$result .= '- ';
$result .= '
Subject
';
$result .= '';
foreach( $bibr['subject'] as $temp )
$result .= '- ' . $temp . '
';
$result .= '
';
}
if( count( $bibr['place'] )){
$result .= '- ';
$result .= '
Places in this work
';
$result .= '';
foreach($bibr['place'] as $temp)
$result .= '- ' . $temp . '
';
$result .= '
';
$result .= ' ';
}
if( count( $bibr['time'] )){
$result .= '- ';
$result .= '
Time periods in this work
';
$result .= '';
foreach($bibr['time'] as $temp)
$result .= '- ' . $temp . '
';
$result .= '
';
$result .= ' ';
}
if($bibr['notes']){
$result .= '- ';
$result .= '
Notes
';
$result .= '';
foreach($bibr['notes'] as $temp)
$result .= '- ' . $temp . '
';
$result .= '
';
}
if($bibr['contents']){
$result .= '- ';
$result .= '
Contents
';
$result .= ''. $bibr['contents'][0] .'
';
}
if($bibr['isbn']){
$result .= 'ISBN
';
$result .= '';
foreach($bibr['isbn'] as $temp)
$result .= '- '. strtolower( $temp ) . '
';
$result .= '
';
$result .= ' ';
}
if($bibr['issn']){
$result .= 'ISSN
';
$result .= '';
foreach($bibr['issn'] as $temp)
$result .= '- '. strtolower( $temp ) . '
';
$result .= '
';
$result .= ' ';
}
if($bibr['lccn']){
$result .= 'LCCN
';
$result .= '';
foreach($bibr['lccn'] as $temp)
$result .= '- '. $temp .'
';
$result .= '
';
$result .= ' ';
}
if($bibr['olid']){
$result .= 'Open Library ID
';
$result .= '';
foreach($bibr['olid'] as $temp)
$result .= '- '. $temp .'
';
$result .= '
';
$result .= ' ';
}
/*
if($bibr['tags']){
$result .= 'Tags
';
$result .= '';
foreach($bibr['tags'] as $temp){
$result .= '- [tag]'. strtolower($temp) . '[/tag]
';
}
$result .= '
';
}
if($bibr['asin'])
$result .= 'Search inside
[[searchinside|'. $bibr['asin'] .']] ';
if($bibr['asin'])
$result .= 'Images and reviews
additional catalog content provided by Amazon.com ';
*/
$result .= '
';
return($result);
}
//strip leading and trailing ascii punctuations
function strip_punct($str) {
// props to K. T. Lam of HKUST
//strip html entities, i.e. ;
$htmlentity = '\&\#\d\d\;';
$lead_htmlentity_pattern = '/^'.$htmlentity.'/';
$trail_htmlentity_pattern = '/'.$htmlentity.'$/';
$str = preg_replace($lead_htmlentity_pattern, '', preg_replace($trail_htmlentity_pattern, '', $str));
//strip ASCII punctuations
$puncts = '\s\~\!\@\#\$\%\^\&\*\_\+\`\-\=\{\}\|\[\]\\\:\"\;\'\<\>\?\,\.\/';
$lead_puncts_pattern = '/^['.$puncts.']+/';
$trail_puncts_pattern = '/['.$puncts.']+$/';
$str = preg_replace($trail_puncts_pattern, '', preg_replace($lead_puncts_pattern, '', $str));
//Strip ()
$both_pattern = '/^[\(]([^\(|\)]+)[\)]$/';
$trail_pattern = '/^([^\(]+)[\)]$/';
$lead_pattern = '/^[\(]([^\)]+)$/';
$str = preg_replace($lead_pattern, '\\1', preg_replace($trail_pattern,'\\1', preg_replace($both_pattern, '\\1', $str)));
return ($str);
}
function activate() {
global $wpdb;
$charset_collate = '';
if ( version_compare( mysql_get_server_info(), '4.1.0', '>=' )) {
if ( ! empty( $wpdb->charset ))
$charset_collate = "DEFAULT CHARACTER SET $wpdb->charset";
if ( ! empty( $wpdb->collate ))
$charset_collate .= " COLLATE $wpdb->collate";
}
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
dbDelta("
CREATE TABLE $this->harvest_table (
source_id varchar(50) NOT NULL default '',
harvest_date timestamp NOT NULL default '0000-00-00 00:00:00',
imported tinyint(1) default '0',
content longtext NOT NULL,
PRIMARY KEY (source_id),
KEY imported (imported)
) $charset_collate");
}
// Default constructor
function Scrib_import() {
global $wpdb;
register_activation_hook(__FILE__, array(&$this, 'activate'));
$this->harvest_table = $wpdb->prefix . 'scrib_harvest';
register_taxonomy( 'sourceid', 'post' );
}
}
// Instantiate and register the importer
include_once(ABSPATH . 'wp-admin/includes/import.php');
if(function_exists('register_importer')) {
$scrib_import = new Scrib_import();
register_importer($scrib_import->importer_code, $scrib_import->importer_name, $scrib_import->importer_desc, array (&$scrib_import, 'dispatch'));
}
?>