Skip to content

Instantly share code, notes, and snippets.

@typhonius
Created June 27, 2013 14:26
Show Gist options
  • Save typhonius/5876854 to your computer and use it in GitHub Desktop.
Save typhonius/5876854 to your computer and use it in GitHub Desktop.
<?php
function custom_import_import_files_batch_files(&$context) {
db_set_active('import');
// Build the total import count.
// need to query D5 db
// mysql> select nr.body from node_revisions nr join node n on n.nid = nr.nid where n.type = 'homepage' and nr.body like '%flipperpage%';
if (empty($context['sandbox'])) {
$query = db_select('node_revisions', 'nr');
$query->join('node', 'n', 'nr.vid = n.vid');
$query->condition('n.type', 'homepage');
$query->condition('nr.body', '%flipperpage%', 'LIKE');
$query->addExpression('COUNT(*)', 'count');
$max = $query->execute()->fetchField();
$context['sandbox'] = array(
'progress' => 0,
'current_node' => 0,
'max' => $max,
);
}
$limit = 5; // Number of nodes to process at a time.
$query = db_select('node_revisions', 'nr');
$query->join('node', 'n', 'nr.vid = n.vid');
$query->condition('n.type', 'homepage');
$query->condition('nr.body', '%flipperpage%', 'LIKE');
$query->orderBy('n.nid', 'asc');
$query->fields('nr', array('body'));
$query->fields('n');
$query->condition('n.nid', $context['sandbox']['current_node'], '>');
$query->range(0, $limit);
$result = $query->execute();
db_set_active('default'); // Switch back.
foreach ($result as $row) {
$urls = custom_import_import_files_grep_url($row->body);
// Get rid of the regex match from the first element before iterating
array_shift($urls);
foreach ($urls[0] as $match) {
$dl = drupal_http_request('http://flipperpage.com/pdf/' . $match);
$pdf = file_save_data($dl->data, 'public://' . $match . '.pdf');
drupal_chmod($pdf->uri, 0664);
// Imagick can fall over sometimes when handled streams so we give
// the full path
$path = drupal_realpath($pdf->uri);
// Convert pdf to image for the front page
// if (class_exists("Imagick")) {
// TODO clean up the /tmp and make it drupal temp dir etc
$image = new Imagick();
// Set ratio and source for page 1
$image->setResolution(100, 100);
$image->readImage($path . '[0]');
$image->setImageFormat('png');
$image->writeImage('/tmp/' . $pdf->filename . '.png');
$image->clear();
$image->destroy();
$im = new stdClass();
$im->uri = '/tmp/' . $pdf->filename . '.png';
$im->status = 1;
$im = file_move($im, 'public://');
// Have to set display to TRUE or it'll fail
// and have to ensure the file objects are arrays
// so when it's processed by Drupal it doesn't fail
$apdf = (array) $pdf;
$apdf['display'] = 1;
$aim = (array) $im;
$aim['display'] = 1;
$node = new stdClass();
$node->type = 'edition';
$node->title = $row->title;
$node->language = LANGUAGE_NONE;
$node->changed = $row->changed;
$node->created = $row->created;
$node->uid = 1;
$node->status = 1;
$node->field_file['und'][] = $apdf;
$node->field_image['und'][] = $aim;
$term = taxonomy_get_term_by_name($node->title, 'edition');
foreach ($term as $term_tid) {
// because we use the vocab id we are pretty safe to
// assume there is only one term
$tid = $term_tid->tid;
}
if ($tid) {
$node->field_edition['und'][] = array(
'tid' => $tid,
);
}
node_save($node);
}
$context['sandbox']['progress'] ++;
$context['message'] = t('Importing node @title', array('@title' => $row->title));
$context['sandbox']['current_node'] = $row->nid;
}
if ($context['sandbox']['progress'] != $context['sandbox']['max']) {
$context['finished'] = $context['sandbox']['progress'] / $context['sandbox']['max'];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment