/** * @file * This page contains methods that need to be refactored to find a sensible * home. However, no home exists yet or they need to be refactored. * * This is not active code. */ /** * Geocode a string. * * @param string $string * A location string. * * @return array * An array with location information extracted from the string. */ public function geoCodeString($string) { // Geocode the location and parse into structured data for migration. // Geocoder module is not an explicit dependency because most migrations // do not rely on it. It should be disabled after use. if (!empty($string)) { if ($string == 'Washington, D.C.') { // The most common entry, so skip geocoding. $address['locality'] = 'Washington'; $address['administrative_area_level_1'] = 'DC'; $address['country'] = "US"; } elseif (module_exists('geocoder')) { // Note that calling this too many times (as in very large migrations) // may exceed the API request limit for geocoder's source data. $point = geocoder('google', $string); module_load_include('inc', 'migration_tools', 'includes/migration_tools'); try { $address = mt_migrate_convert_geocoded_point_to_address($point); } catch (Exception $e) { watchdog("migration_tools", "The geocoder failed: {$e->getMessage()}"); } if (!$address) { $address['locality'] = ''; $address['administrative_area_level_1'] = ''; $address['country'] = ''; $message = "@fileid Could not look up location because geocoder returned nothing. The API request limit may have been exceeded."; $variables = array('@fileid' => $this->fileId); \MigrationTools\Message::make($address, $variables, WATCHDOG_INFO, 2); } } else { $message = "@fileid Could not look up location because geocoder module is not enabled"; $variables = array('@fileid' => $this->fileId); \MigrationTools\Message::make($address, $variables, WATCHDOG_INFO, 2); } return $address; } } /** * Extracts metadata from pdf file. * * @param string $pdf_file_path * The absolute file path of the pdf on the local system. * * @return array * An associative array of pdf metadata. */ public function getPdfMetadata($pdf_file_path) { try { $pdfinfo_bin = variable_get('pdfinfo_binary', '/home/mt/xpdf/bin64/pdfinfo'); $file_arg = escapeshellarg($pdf_file_path); $command = "$pdfinfo_bin $file_arg"; $output = shell_exec($command); $pdf_info_rows = explode("\n", $output); $pdf_metadata = array(); foreach ($pdf_info_rows as $row) { $columns = explode(':', $row); $pdf_metadata[$columns[0]] = $columns[1]; } return $pdf_metadata; } catch (Exception $e) { $message = t('Could not parse attached PDF for @fileid. Exception thrown: @exception', array('@fileid' => $pdf_file_path, '@exception' => $e->getMessage())); $this->queueMessage($message); watchdog('migration_tools', $message); } } /** * Extracts the contents of a pdf file. * * @param string $pdf_file_path * The absolute file path of the pdf on the local system. * * @return string * The text content of the pdf. * * @throws \XPDF\Exception\BinaryNotFoundException */ public function getPdfContents($pdf_file_path) { try { // Get PDF contents. $pdftotext_bin = variable_get('pdftotext_binary', '/home/mt/xpdf/bin64/pdftotext'); $pdf_parser = XPDF\PdfToText::create(array('pdftotext.binaries' => $pdftotext_bin)); $pdf_contents = $pdf_parser->getText($pdf_file_path); return $pdf_contents; } catch (Exception $e) { $message = t('Could not parse attached PDF for @fileid. Exception thrown: @exception', array('@fileid' => $pdf_file_path, '@exception' => $e->getMessage())); $this->queueMessage($message); watchdog('migration_tools', $message); } } --------------------------------------- /** * Alters language to LANGUAGE_NONE if used in prepare(). * * This is to correct issues with redirects and aliases having languages. * * @param object $entity * Fully loaded entity. */ public function languageSideStep($entity) { // Check language, if other than LANGUAGE_NONE, sidestep it. if ($entity->language !== LANGUAGE_NONE) { // Save the original langage for processing in the complete(). $entity->original_language = $entity->language; $entity->language = LANGUAGE_NONE; } } /** * Alters language on node back to originally specified if used in complete(). * * This is to correct issues with redirects and aliases having languages. * * @param object $entity * Fully loaded entity. */ public function languageReturnStep($entity) { // Check to see if language was sidestepped in prepare(). if (!empty($entity->original_language)) { // Language was sidestepped, so put it back and resave. $entity->language = $entity->original_language; unset($entity->original_language); entity_save('node', $entity); } } ----------------------------------------- /** * Sets the field mapping for an organic group parent using uuid. * * @param string $uuid * The unique identifier for the Organic Group. * * @return string * The entity id of the Organic Group. */ public function addGroupReferenceMapping($uuid) { // Set the Organic Group to which this content belongs. Get the data // here so that it isn't fetched per $row in prepare(). list($og_eid) = array_values(entity_get_id_by_uuid('node', array($uuid))); $this->addFieldMapping('og_group_ref')->defaultValue($og_eid); return $og_eid; }