/[drupal]/contributions/sandbox/alex_b/mtk/id_map/id_map.module
ViewVC logotype

Contents of /contributions/sandbox/alex_b/mtk/id_map/id_map.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.2 - (show annotations) (download) (as text)
Thu Aug 7 17:25:18 2008 UTC (15 months, 3 weeks ago) by alexb
Branch: MAIN
CVS Tags: HEAD
Changes since 1.1: +1 -0 lines
File MIME type: text/x-php
Add version string.
1 <?php
2 // $Id:$
3
4 /**
5 * Map old id to new id.
6 *
7 * @param $from_table string, table name of source id
8 * @param $from_id unsigned integer source id
9 * @param $table string, name of the specific table that this mapping will map to
10 * @param $key string, name of the specific column this mapping will map to
11 * @param $id unsigned integer new id $id if available
12 *
13 * @param $custom custom information to store with mapping
14 *
15 * MAP FORWARD:
16 * Pass in 0 for $id if you don't know the id you're mapping to.
17 * id_map will map automatically if a successive function defines a valid matching
18 * from_table/from_id/id triple.
19 *
20 * See _id_map_fill_in_matches().
21 *
22 * DETAILS:
23 * There can be multiple ids for a from_table/from_id tuple.
24 * There can multiple table/key combinations for the same from_table/from_id/id triple.
25 *
26 * E. g. a legacy content piece's id with a category that is mapped to a node with taxonomy will
27 * wind up with the mappings:
28 * from_table from_id table key id
29 * legacy_content 765 node nid 123
30 * legacy_content 765 term_node nid 123
31 *
32 * Examples:
33 *
34 * Map to node table:
35 * $legacy_object = stdObject(
36 * 'legacy_id' => 43,
37 * //...
38 * );
39 * node_save($legacy_object);
40 * id_map('legacy_objects', $legacy_object->legacy_id, 'node', 'nid', $legacy_object->nid);
41 */
42 function id_map($from_table, $from_id, $table, $key, $id = 0, $custom = array()) {
43 if ($id == 0) {
44 $id = $from_id;
45 $valid = FALSE;
46 }
47 else {
48 $valid = TRUE;
49 }
50 _id_map_add($from_table, $from_id, $table, $key, $id, $valid, $custom);
51 _id_map_fill_in_matches($from_table, $from_id, $table, $key, $id, $valid);
52 }
53
54 /**
55 * Get new id.
56 * @param $table string, table name of source id
57 * @param $id unsigned integer source id
58 * @param $source bool if true, use 'from_' columns.
59 * @return array array of ids $from_id => $to_id .
60 */
61 function id_map_get($table, $id = NULL, $source = TRUE) {
62 if ($id && $source) {
63 $query = "SELECT id, from_id FROM {id_map} WHERE from_table = '%s' AND from_id = %d AND valid = 1";
64 }
65 elseif ($id && !$source) {
66 $query = "SELECT id, from_id FROM {id_map} WHERE table = '%s' AND id = %d AND valid = 1";
67 }
68 elseif (!$id && $source) {
69 $query = "SELECT id, from_id FROM {id_map} WHERE from_table = '%s' AND valid = 1";
70 }
71 elseif (!$id && !$source) {
72 $query = "SELECT id, from_id FROM {id_map} WHERE table = '%s' AND valid = 1";
73 }
74 $result = db_query($query, $table, $id);
75 // @todo: mapping more than one result could lead to trouble here...
76 $ids = array();
77 while ($map = db_fetch_object($result)) {
78 $ids[$map->from_id] = $map->id;
79 }
80 return $ids;
81 }
82
83 /**
84 * Fill in the blanks.
85 * Updates tables that were mapped forward and updates map table.
86 * @todo: to be tested.
87 */
88 function _id_map_fill_in_matches($from_table, $from_id, $table, $key, $id, $valid) {
89 // We've got a new valid id, fill in blanks in previous entries.
90 // @todo: add consistency check.
91 if ($valid) {
92 // Pull all results that map from_table, from_id where id is 0
93 $result = db_query('SELECT * FROM {id_map} WHERE from_table = "%s" AND from_id = %d AND id = %d AND valid = 0', $from_table, $from_id, $from_id);
94 // For every mapping result, update table mapping points to and update mapping.
95 while ($map = db_fetch_object($result)) {
96 db_query('UPDATE {%s} SET %s = %d WHERE %s = %d', $map->table, $map->key, $map->from_id, $map->key, $id);
97 db_query('UPDATE {id_map} SET id = %d AND valid = 1 WHERE from_table = "%s" AND from_id = %d AND id = %d AND valid = 0', $id, $from_table, $from_id, $map->id);
98 }
99 }
100 // The given id is not valid, see whether we've got previous entries that match.
101 else {
102 $ids = id_map_get($from_table, $from_id);
103 foreach ($ids as $id) {
104 _id_map_fill_in_matches($from_table, $from_id, $table, $key, $id, TRUE);
105 }
106 }
107 }
108
109 /**
110 * Add a mapping to the map table.
111 */
112 function _id_map_add($from_table, $from_id, $table, $key, $id, $valid, $custom) {
113 db_query('DELETE FROM {id_map} WHERE from_table ="%s" AND from_id = %d AND `table` = "%s" AND `key` = "%s" AND id = %d', $from_table, $from_id, $table, $key, $id);
114 db_query('INSERT INTO {id_map}(from_table, from_id, `table`, `key`, id, valid, custom) VALUES("%s", %d, "%s", "%s", %d, %d, "%s")', $from_table, $from_id, $table, $key, $id, $valid, serialize($custom));
115 }

  ViewVC Help
Powered by ViewVC 1.1.2