| Commit | Line | Data |
|---|---|---|
| 9f65d611 | 1 | <?php |
| 9f65d611 MR |
2 | |
| 3 | /** | |
| 4 | * @file | |
| 5 | * Documentation for Migrate module's hooks. | |
| 6 | */ | |
| 7 | ||
| 8 | /** | |
| e497e72a MR |
9 | * Provide information on this module's implementation of the Migrate API |
| 10 | */ | |
| 11 | function hook_migrate_api() { | |
| 12 | $api = array( | |
| 13 | // Required - if it does not match the API version of the migrate | |
| 14 | // module, your migrate hooks will not be invoked | |
| 15 | 'api' => 1, | |
| 16 | // Optional path containing migration code - relative to the module directory, | |
| 17 | // and defaults to the module directory. <mymodule>.migrate.inc will be | |
| 18 | // included from this directory | |
| 19 | 'path' => 'modules', | |
| 20 | // Optional - if this module provides migration implementations on behalf of | |
| 21 | // other modules, list them here and <module>.migrate.inc will be included | |
| 22 | // from the path above | |
| 23 | 'integration modules' => array( | |
| 24 | 'comment' => array( | |
| 25 | // Optionally describe the support you're providing, and explicitly set the status | |
| 26 | 'description' => 'integrates with comment module', | |
| 27 | 'status' => TRUE, | |
| 28 | ), | |
| 29 | 'node', | |
| 30 | 'user', | |
| 31 | ), | |
| 32 | ); | |
| 33 | return $api; | |
| 34 | } | |
| 35 | ||
| 36 | ||
| 37 | /** | |
| 9f65d611 MR |
38 | * Do one-time initialization, prior to when any migrate hook is called. |
| 39 | */ | |
| 40 | function hook_migrate_init() { | |
| e497e72a | 41 | // For example, load some global data to be used by all hooks |
| 9f65d611 MR |
42 | } |
| 43 | ||
| 44 | /** | |
| 45 | * Define possible destination "content types" which can accept incoming data. | |
| 46 | * | |
| 47 | * @return array | |
| 48 | * An array mapped from the internal name of the type (used to build hook | |
| 49 | * names, in particular) to the user-visible type name. | |
| 50 | */ | |
| 51 | function hook_migrate_types() { | |
| 52 | $types = array('user' => t('User'), 'role' => t('Role')); | |
| 53 | return $types; | |
| 54 | } | |
| 55 | ||
| 56 | /** | |
| 57 | * Expose list of possible table fields which can accept incoming data. | |
| 58 | * | |
| 59 | * @return array | |
| 60 | * An array mapped from the internal field name (within Drupal) to the | |
| 61 | * user-visible name. | |
| 62 | */ | |
| 63 | function hook_migrate_fields_$contenttype() { | |
| 64 | $fields = array( | |
| e497e72a | 65 | 'name' => t('Role: Name'), |
| 9f65d611 MR |
66 | ); |
| 67 | return $fields; | |
| 68 | } | |
| 69 | ||
| 70 | /** | |
| 71 | * Perform any tasks necessary when reverting a migration job. | |
| 72 | * | |
| 73 | * Establish deletion routine for object of type $contenttype and everything | |
| 74 | * that depends on that object. | |
| 75 | * | |
| 76 | * @param $id | |
| 77 | * Unique identifier of the destination object. | |
| 78 | */ | |
| 79 | function hook_migrate_delete_$contenttype($id) { | |
| 80 | db_query('DELETE FROM {users_roles} WHERE rid = %d', $id); | |
| 81 | db_query('DELETE FROM {permission} WHERE rid = %d', $id); | |
| 82 | db_query('DELETE FROM {role} WHERE rid = %d', $id); | |
| 83 | } | |
| 84 | ||
| 85 | /** | |
| 86 | * Perform steps required to import $contenttype content. | |
| 87 | * | |
| 88 | * A common practice is to invoke 'prepare' and 'complete' hooks at appropriate | |
| 89 | * times, so that other modules may act upon the current object's migration. | |
| 90 | * | |
| 91 | * @param $tblinfo | |
| 92 | * Meta-information about the content set. | |
| 93 | * @param $row | |
| 94 | * Source data for one object. | |
| 95 | * @return | |
| 96 | * Array of messages. Use migrate_message() to generate a message. | |
| 97 | */ | |
| 98 | function hook_migrate_import_$contenttype($tblinfo, $row) { | |
| 99 | $errors = array(); | |
| 100 | $new_role = array(); | |
| 101 | foreach ($tblinfo->fields as $destfield => $values) { | |
| 102 | if ($values['srcfield'] && isset($row->$values['srcfield'])) { | |
| 103 | $newvalue = $row->$values['srcfield']; | |
| 104 | } | |
| 105 | else { | |
| 106 | $newvalue = $values['default_value']; | |
| 107 | } | |
| 108 | $new_role[$destfield] = $newvalue; | |
| 109 | } | |
| 110 | ||
| 111 | // Prepare the role for import. | |
| 112 | $errors = migrate_destination_invoke_all('prepare_role', $new_role, $tblinfo, $row); | |
| 113 | ||
| 114 | $role_name = $new_role['name']; | |
| 115 | ||
| 116 | if ($role_name) { | |
| 117 | db_query("INSERT INTO {role} (name) VALUES ('%s')", $role_name); | |
| 118 | $sql = "SELECT rid FROM {role} WHERE name='%s'"; | |
| 119 | $rid = db_result(db_query($sql, $role_name)); | |
| 120 | $new_role['rid'] = $rid; | |
| 121 | // Call completion hooks, for any additional role-related processing | |
| 122 | // (such as assigning permissions). | |
| 123 | timer_start('role completion hooks'); | |
| 124 | $errors += migrate_destination_invoke_all('complete_role', $new_role, $tblinfo, $row); | |
| 125 | timer_stop('role completion hooks'); | |
| 126 | ||
| 127 | $sourcekey = $tblinfo->sourcekey; | |
| 128 | migrate_add_mapping($tblinfo->mcsid, $row->$sourcekey, $rid); | |
| 129 | } | |
| 130 | return $errors; | |
| 131 | } | |
| 132 | ||
| 133 | /** | |
| 134 | * Translate URIs from an old site to the new one. | |
| 135 | * | |
| 136 | * Requires adding RewriteRules to .htaccess. For example, if the URLs | |
| 137 | * for news articles had the form | |
| 138 | * http://example.com/issues/news/[OldID].html, use this rule: | |
| 139 | * | |
| 140 | * RewriteRule ^issues/news/([0-9]+).html$ /migrate/xlat/node/$1 [L] | |
| 141 | * | |
| 142 | * @param $newid | |
| 143 | * The ID of the $contenttype object in the new site. | |
| 144 | * @return string | |
| 145 | * The Drupal system path to the $contenttype object. | |
| 146 | */ | |
| 147 | function hook_migrate_xlat_$contenttype($newid) { | |
| 148 | return "node/$newid"; | |
| 149 | } |