/** * {@inheritdoc} * * Generate the configuration rankings. */ public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) { $return = array(); foreach ($row->getSource() as $name => $rank) { if (substr($name, 0, 10) == 'node_rank_' && $rank) { $return[substr($name, 10)] = $rank; } } return $return; }
/** * Posts are either type recpie or restaurant_review. * Recpies contain <div class="recipe">, everything else is considered type * restaurant_review. */ public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) { $src = $row->getSource(); if (isset($src['category']) && !empty($src['category'])) { $cats = $src['category']; if (is_array($cats) && in_array('References', $cats)) { $value = 'project'; } } return is_string($value) ? $value : NULL; }
public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) { $value = strtotime($value); if (!$value || $value < 0) { $value = strtotime($row->getSource()['wp:post_date']); } if (!$value || $value < 0) { $value = time(); } return $value; }
public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) { $wp_uri = $row->getSource()['wp:attachment_url']; $search = '/wp-content/uploads/'; $uploads_dir_pos = strpos($wp_uri, $search); if ($uploads_dir_pos !== false) { return 'public://' . substr($wp_uri, $uploads_dir_pos + strlen($search)); } else { // hmm. return 'public://' . basename($wp_uri); } }
/** * {@inheritdoc} */ public function prepareRow(Row $row) { $bundle = $row->getSourceProperty('type'); // Pick up simple CCK fields. $cck_table = "content_type_{$bundle}"; if ($this->tableExists($cck_table)) { $query = $this->select($cck_table, 'f')->condition('vid', $row->getSourceProperty('vid')); // The main column for the field should be rendered with the field name, // not the column name (e.g., field_foo rather than field_foo_value). $field_info = $this->getSourceFieldInfo($bundle); foreach ($field_info as $field_name => $info) { if (isset($info['columns']) && !$info['multiple'] && $info['db_storage']) { $i = 0; $data = FALSE; foreach ($info['columns'] as $display_name => $column_name) { if ($i++ == 0) { $query->addField('f', $column_name, $field_name); } else { // The database API won't allow colons in column aliases, so we // will accept the default alias, and fix up the field names later. // Remember how to translate the field names. if ($info['type'] == 'filefield' && (strpos($display_name, ':list') || strpos($display_name, ':description'))) { if (!$data) { //$this->fileDataFields[] = $field_name . '_data'; $query->addField('f', $field_name . '_data'); $data = TRUE; } } else { $query->addField('f', $column_name); } } } } } if ($results = $query->execute()->fetchAssoc()) { $source = $row->getSource(); // We diff the results because the extra will be all the field columns. $new_fields = array_diff($results, $source); foreach ($new_fields as $key => $value) { $row->setSourceProperty($key, $value); } } } // Handle fields that have their own table. foreach ($this->getSourceFieldInfo($bundle) as $field_name => $field_info) { if ($field_info['multiple'] || !$field_info['db_storage']) { // Select the data. $table = "content_{$field_name}"; $field_query = $this->select($table, 't')->condition('vid', $row->getSourceProperty('vid')); if ($field_info['multiple']) { $field_query->addField('t', 'delta'); } $data = FALSE; foreach ($field_info['columns'] as $display_name => $column_name) { // The database API won't allow colons in column aliases, so we // will accept the default alias, and fix up the field names later. // Remember how to translate the field names. if ($field_info['type'] == 'filefield' && (strpos($display_name, ':list') || strpos($display_name, ':description'))) { if (!$data) { //$this->fileDataFields[] = $field_name . '_data'; $field_query->addField('t', $field_name . '_data'); $data = TRUE; } } else { $field_query->addField('t', $column_name); } } if ($field_info['multiple']) { foreach ($field_query->execute() as $field_row) { foreach ($field_info['columns'] as $display_name => $column_name) { list(, $column) = explode(':', $display_name); $property_path = $field_name . Row::PROPERTY_SEPARATOR . $field_row['delta'] . Row::PROPERTY_SEPARATOR . $column; $row->setSourceProperty($property_path, $field_row[$column_name]); } } } else { if ($field_row = $field_query->execute()->fetchAssoc()) { foreach ($field_info['columns'] as $display_name => $column_name) { $row->setSourceProperty(str_replace(':', Row::PROPERTY_SEPARATOR, $display_name), $field_row[$column_name]); } } } } } parent::prepareRow($row); }
/** * Tests hashing. */ public function testHashing() { $row = new Row($this->testValues, $this->testSourceIds); $this->assertSame('', $row->getHash(), 'No hash at creation'); $row->rehash(); $this->assertSame($this->testHash, $row->getHash(), 'Correct hash.'); $row->rehash(); $this->assertSame($this->testHash, $row->getHash(), 'Correct hash even doing it twice.'); // Set the map to needs update. $test_id_map = array('original_hash' => '', 'hash' => '', 'source_row_status' => MigrateIdMapInterface::STATUS_NEEDS_UPDATE); $row->setIdMap($test_id_map); $this->assertTrue($row->needsUpdate()); $row->rehash(); $this->assertSame($this->testHash, $row->getHash(), 'Correct hash even if id_mpa have changed.'); $row->setSourceProperty('title', 'new title'); $row->rehash(); $this->assertSame($this->testHashMod, $row->getHash(), 'Hash changed correctly.'); // Check hash calculation algorithm. $hash = hash('sha256', serialize($row->getSource())); $this->assertSame($hash, $row->getHash()); // Check length of generated hash used for mapping schema. $this->assertSame(64, strlen($row->getHash())); // Set the map to successfully imported. $test_id_map = array('original_hash' => '', 'hash' => '', 'source_row_status' => MigrateIdMapInterface::STATUS_IMPORTED); $row->setIdMap($test_id_map); $this->assertFalse($row->needsUpdate()); // Set the same hash value and ensure it was not changed. $random = $this->randomMachineName(); $test_id_map = array('original_hash' => $random, 'hash' => $random, 'source_row_status' => MigrateIdMapInterface::STATUS_NEEDS_UPDATE); $row->setIdMap($test_id_map); $this->assertFalse($row->changed()); // Set different has values to ensure it is marked as changed. $test_id_map = array('original_hash' => $this->randomMachineName(), 'hash' => $this->randomMachineName(), 'source_row_status' => MigrateIdMapInterface::STATUS_NEEDS_UPDATE); $row->setIdMap($test_id_map); $this->assertTrue($row->changed()); }
/** * {@inheritdoc} */ public function import(Row $row, array $old_destination_id_values = array()) { $source = $row->getSource(); $this->tempStore->setWithExpire($source['uuid'], $source, $this->expire); return array($this->entityIdKey => $source[$this->entityIdKey]); }
protected function transformIngredients($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) { // WP uses newlines and transforms to <br>s at render time, but Drupal WYSIWYG adds <p>s on [enter]. // Solution to get desired spacing of ingredients (1/line) is to make them css-styled <li>s. // Thus, locate each ingredient in WP text and transform to list item. if ($ingredients_ix = strpos($value, 'Ingredients:')) { $intro = substr($value, 0, $ingredients_ix); $rest_lines = explode("\n", substr($value, $ingredients_ix)); /* Ingredients lists are charactarized by 4 or more adjacent lines of average length <= 50, * with at most one outlier of > 75, all terminated by a blank line. */ $lengths = []; $three_before_had_content = false; $two_before_had_content = false; $last_had_content = false; $list_start_ix = false; for ($i = 1; $i <= 55 && $list_start_ix === false; $i++) { $lengths[$i] = strlen($rest_lines[$i]); if ($lengths[$i] > 75) { $lengths[$i] = 25; $outliers[$i] = 1; } else { $outliers[$i] = 0; } if (trim($rest_lines[$i]) != '') { if ($i >= 3) { $avg = ($lengths[$i] + $lengths[$i - 1] + $lengths[$i - 2] + $lengths[$i - 3]) / 4; $outliers_count = $outliers[$i] + $outliers[$i - 1] + $outliers[$i - 2] + $outliers[$i - 3]; } else { $avg = 0; } if ($last_had_content && $two_before_had_content && $three_before_had_content && $outliers_count <= 1 && $avg > 0 && $avg <= 50) { // we've found the start of the ingredients, two lines before here. $list_start_ix = $i - 3; } $three_before_had_content = $two_before_had_content; $two_before_had_content = $last_had_content; $last_had_content = true; } else { $three_before_had_content = $two_before_had_content; $two_before_had_content = $last_had_content; $last_had_content = false; } } if ($list_start_ix !== false) { // now find the end, first blank line $list_end_ix = false; for ($i = $list_start_ix + 1; $i < count($rest_lines) && $list_end_ix === false; $i++) { if (trim($rest_lines[$i]) == '') { $list_end_ix = $i - 1; } } if ($list_end_ix === false) { $list_end_ix = $i; } // add <ul></ul> around the list array_splice($rest_lines, $list_start_ix, 0, array('<ul>')); $list_start_ix++; $list_end_ix++; array_splice($rest_lines, $list_end_ix + 1, 0, array('</ul>')); // finally, wrap each ingredient in <li> for ($i = $list_start_ix; $i <= $list_end_ix; $i++) { $rest_lines[$i] = '<li>' . $rest_lines[$i] . '</li>'; } return $intro . implode("\n", $rest_lines); } else { trigger_error($row->getSource()['title'] . ": ingredients list not found under 'Ingredients:'", E_USER_WARNING); return $value; } } else { trigger_error($row->getSource()['title'] . ": has no ingredients", E_USER_WARNING); return $value; } }