/[drupal]/contributions/sandbox/alex_b/port/port.module
ViewVC logotype

Contents of /contributions/sandbox/alex_b/port/port.module

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


Revision 1.8 - (show annotations) (download) (as text)
Sat Aug 23 18:25:18 2008 UTC (15 months ago) by alexb
Branch: MAIN
CVS Tags: HEAD
Changes since 1.7: +82 -140 lines
File MIME type: text/x-php
Add partial export options.
1 <?php
2 // $Id: port.module,v 1.7 2008/08/09 14:38:12 alexb Exp $
3 /**
4 * Import/Export API for exporting and importing parts of a Drupal site.
5 * Usage - Implement hook_ports(), see port_ports() in core/port.inc for example.
6 */
7
8 define('PORT_STRUCTURE', 'PORT_STRUCTURE');
9 define('PORT_CONTENT', 'PORT_CONTENT');
10
11 /**
12 * Exports data.
13 *
14 * @todo: Contain version information and export data type
15 * information in export data. On import,
16 * display notices/warnings when versions diverge.
17 *
18 * @param array $export_functions
19 * Array of export functions to be executed.
20 * Format: array('port_id' => 'export_function)
21 * @param array $options
22 * Array of export options to be passed to export functions.
23 * Format: array('port_id' => array('opt' => 'ions'))
24 *
25 */
26 function port_export($export_functions, $export_options = array()) {
27 $all_e_functions = port_get_functions_map();
28 $all_i_functions = array_flip($all_e_functions);
29 // Export
30 foreach ($export_functions as $port_id => $export_function) {
31 // Validate import and export functions.
32 if (in_array($export_function, $all_e_functions)) {
33 $import_function = $all_i_functions[$export_function];
34 if ($import_function) {
35 $export[$import_function] = call_user_func($export_function, $export_options[$port_id]);
36 }
37 else {
38 drupal_set_message(t('No import function defined for !function.', array('!function' => $export_function)), 'error');
39 }
40 }
41 else {
42 drupal_set_message(t('Function !function is not a valid export function.', array('!function' => $export_function)), 'error');
43 }
44 }
45 return $export;
46 }
47
48 /**
49 * Imports data exported previously by port_export().
50 *
51 * @param Array $import_data
52 * As exported by port_export().
53 */
54 function port_import($import) {
55 port_include();
56 // Install modules first. @todo: Create a weight or dependency model.
57 $count = 0;
58 if ($import['system_import_modules']) {
59 call_user_func('system_import_modules', $import['system_import_modules']);
60 unset($import['system_import_modules']);
61 $count++;
62 }
63 // Now import rest.
64 $valid_functions = port_get_import_functions();
65 foreach ($import as $function => $import_data) {
66 if (in_array($function, $valid_functions)) {
67 call_user_func($function, $import_data);
68 $count++;
69 }
70 else {
71 drupal_set_message(t('Function !function is not a valid import function. (Required module enabled?)', array('!function' => $function)), 'error');
72 }
73 }
74 drupal_set_message(format_plural($count, 'Imported @count element.', 'Imported @count elements.'));
75 }
76
77 /**
78 * Converts the result of port_export() to executable PHP functions.
79 * @param $export array
80 * Array as exported by port_export().
81 * @return
82 * Callable PHP function definitions.
83 */
84 function port_convert_to_functions($export) {
85 $date = format_date(time(), 'custom', 'Y-m-d H:m:s');
86 $output = "/**\n";
87 $output .= " * Import function definitions. \n";
88 $output .= " * Exported $date\n";
89 $output .= " * From ". url(NULL, NULL, NULL, TRUE) ."\n";
90 $output .= " */\n";
91 $output .= "\n";
92 foreach ($export as $import_function => $data) {
93 $output .= "function install_$import_function() {\n";
94 $output .= ' $data = '. str_replace("\n", "\n ", var_export($data, TRUE)) .";\n";
95 $output .= ' '. $import_function .'($data);'."\n";
96 $output .= "}\n\n\n";
97 }
98 return $output;
99 }
100
101 /**
102 * Retrieve export functions.
103 *
104 * @param array $port_ids
105 * Array of port ids in the format
106 * array('port_id' => 'port_id').
107 * If not specified, all export options are returned.
108 *
109 * @return array
110 * Array of export functions in the format
111 * array('port_id' => 'function_name');
112 */
113 function port_get_export_functions($port_ids = NULL) {
114 $ports = port_get_ports();
115 $functions = array();
116 foreach ($ports as $port_id => $port) {
117 if ($port_ids && !$port_ids[$port_id]) {
118 continue;
119 }
120 $functions[$port_id] = $port['export callback'];
121 }
122 return $functions;
123 }
124
125 /**
126 * Retrieve all export functions.
127 *
128 * @return array
129 * Array of port names in the format
130 * array('port_id' => 'Human readable port name');
131 */
132 function port_get_port_names() {
133 $ports = port_get_ports();
134 $functions = array();
135 foreach ($ports as $port_id => $port) {
136 $functions[$port_id] = $port['name'];
137 }
138 return $functions;
139 }
140
141 /**
142 * Get options for given port ids.
143 *
144 * @param array $port_ids
145 * Array of port ids in the format
146 * array('port_id' => 'port_id').
147 * If not specified, all export options are returned.
148 * @return array
149 * Array of export options
150 */
151 function port_get_export_options($port_ids = NULL) {
152 $ports = port_get_ports();
153 $options = array();
154 foreach ($ports as $port_id => $port) {
155 if ($port_ids && !$port_ids[$port_id]) {
156 continue;
157 }
158 if (isset($port['export options'])) {
159 $options[$port_id] = call_user_func($port['export options']);
160 }
161 }
162 return $options;
163 }
164
165 /**
166 * Retrieve all import functions.
167 *
168 * @return Array of import functions
169 * array('port_id' => 'import_function_name');
170 */
171 function port_get_import_functions() {
172 $ports = port_get_ports();
173 $functions = array();
174 foreach ($ports as $port_id => $port) {
175 $functions[$port_id] = $port['import callback'];
176 }
177 return $functions;
178 }
179
180 /**
181 * Retrieve all port functions.
182 *
183 * @return Array of import/export functions
184 * array('import_function_name' => 'export_function_name');
185 */
186 function port_get_functions_map() {
187 $ports = port_get_ports();
188 $functions = array();
189 foreach ($ports as $port) {
190 $functions[$port['import callback']] = $port['export callback'];
191 }
192 return $functions;
193 }
194
195 /**
196 * Retrieve all available ports.
197 */
198 function port_get_ports() {
199 port_include();
200 $ports = array();
201 foreach (module_implements('ports') as $module) {
202 $ports = array_merge($ports, call_user_func($module .'_ports'));
203 }
204 return $ports;
205 }
206
207 /**
208 * Include port implementations.
209 */
210 function port_include() {
211 static $loaded = FALSE;
212 if (!$loaded) {
213 require_once(drupal_get_path('module', 'port') . '/crud.inc');
214 // Load all port implementations from /core and /contrib
215 foreach (array('core', 'contrib') as $sub_dir) {
216 $path = drupal_get_path('module', 'port') . '/'. $sub_dir;
217 $files = drupal_system_listing('.*\.inc$', $path, 'name', 0);
218 foreach($files as $file) {
219 require_once("./$file->filename");
220 }
221 }
222 }
223 $loaded = TRUE;
224 }

  ViewVC Help
Powered by ViewVC 1.1.2