/[drupal]/contributions/modules/artman2/artman2.admin.inc
ViewVC logotype

Contents of /contributions/modules/artman2/artman2.admin.inc

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


Revision 1.1 - (show annotations) (download) (as text)
Fri Sep 5 19:50:47 2008 UTC (14 months, 2 weeks ago) by bangpound
Branch: MAIN
CVS Tags: HEAD
Branch point for: DRUPAL-6--1
File MIME type: text/x-php
initial commit of the Article Manager 2 migration tool
1 <?php
2 // $Id$
3
4 /**
5 * @file
6 * Admin page callbacks for the artman2 module.
7 */
8
9 /**
10 * Menu callback; displays the artman2 module settings page.
11 *
12 * @ingroup forms
13 * @see system_settings_form()
14 */
15 function artman2_admin_settings() {
16 include_once './includes/install.inc';
17
18 $db_url = artman2_db_url_parse(variable_get('artman2_db_url', ''));
19 $db_path = $db_url['path'];
20 $db_type = $db_url['scheme'];
21 $db_host = ($db_url['host']) ? $db_url['host'] : 'localhost';
22 $db_user = $db_url['user'];
23 $db_pass = $db_url['pass'];
24 $db_port = $db_url['port'];
25
26 $db_types = drupal_detect_database_types();
27
28 // If both 'mysql' and 'mysqli' are available, we disable 'mysql':
29 if (isset($db_types['mysqli'])) {
30 unset($db_types['mysql']);
31 }
32 $form['artman2_db_url'] = array(
33 '#type' => 'hidden',
34 '#value' => variable_get('artman2_db_url', ''),
35 );
36
37 $form['artman2_db'] = array(
38 '#type' => 'fieldset',
39 '#title' => t('Article Manager 2 Database'),
40 '#collapsible' => ($db_path == '') ? FALSE : TRUE,
41 '#collapsed' => ($db_path == '') ? FALSE : TRUE,
42 );
43
44 if (count($db_types) > 1) {
45 $form['artman2_db']['db_type'] = array(
46 '#type' => 'radios',
47 '#title' => t('Database type'),
48 '#required' => TRUE,
49 '#options' => $db_types,
50 '#default_value' => ($db_type ? $db_type : current($db_types)),
51 );
52 }
53 else {
54 if (count($db_types) == 1) {
55 $db_types = array_values($db_types);
56 $form['artman2_db']['db_type'] = array(
57 '#type' => 'hidden',
58 '#value' => $db_types[0],
59 );
60 }
61 }
62
63 // Database name
64 $form['artman2_db']['db_path'] = array(
65 '#type' => 'textfield',
66 '#title' => t('Database name'),
67 '#default_value' => $db_path,
68 '#size' => 45,
69 '#maxlength' => 45,
70 '#required' => TRUE,
71 );
72
73 // Database username
74 $form['artman2_db']['db_user'] = array(
75 '#type' => 'textfield',
76 '#title' => t('Database username'),
77 '#default_value' => $db_user,
78 '#size' => 45,
79 '#maxlength' => 45,
80 '#required' => TRUE,
81 );
82
83 // Database username
84 $form['artman2_db']['db_pass'] = array(
85 '#type' => 'textfield',
86 '#title' => t('Database password'),
87 '#default_value' => $db_pass,
88 '#size' => 45,
89 '#maxlength' => 45,
90 );
91
92 // Database host
93 $form['artman2_db']['db_host'] = array(
94 '#type' => 'textfield',
95 '#title' => t('Database host'),
96 '#default_value' => $db_host,
97 '#size' => 45,
98 '#maxlength' => 45,
99 '#required' => TRUE,
100 '#description' => t('If your database is located on a different server, change this.'),
101 );
102
103 // Database port
104 $form['artman2_db']['db_port'] = array(
105 '#type' => 'textfield',
106 '#title' => t('Database port'),
107 '#default_value' => $db_port,
108 '#size' => 45,
109 '#maxlength' => 45,
110 '#description' => t('If your database server is listening to a non-standard port, enter its number.'),
111 );
112
113 // Table prefix
114 $form['artman2_db']['artman2_db_prefix'] = array(
115 '#type' => 'textfield',
116 '#title' => t('Table prefix'),
117 '#default_value' => variable_get('artman2_db_prefix', 'db_'),
118 '#size' => 45,
119 '#maxlength' => 45,
120 );
121
122 if ($db_path != '') {
123 // Collect some stats
124 $processed = db_result(db_query('SELECT COUNT(*) FROM {artman2} WHERE last_updated_time > 0'));
125 _artman2_db_connect();
126 db_set_active('artman2');
127 $total = db_result(db_query('SELECT COUNT(*) FROM {article}'));
128 db_set_active();
129 $remaining = $total - $processed;
130
131 $count = format_plural($remaining, 'There is 1 article left to migrate.', 'There are @count articles left to migrate.');
132 $percentage = ((int)min(100, 100 * ($total - $remaining) / max(1, $total))) .'%';
133
134 $form['artman2_migrate'] = array(
135 '#type' => 'checkbox',
136 '#title' => 'Migrate during cron maintenance tasks',
137 '#default_value' => variable_get('artman2_migrate', FALSE),
138 '#description' => t('%percentage of the articles have been migrated.', array('%percentage' => $percentage)) .' '. $count,
139 );
140 // Migration throttle:
141 $form['artman2_cron_limit'] = array(
142 '#type' => 'select',
143 '#title' => t('Number of articles to migrate per cron run'),
144 '#default_value' => variable_get('artman2_cron_limit', 10),
145 '#options' => drupal_map_assoc(array(10, 20, 50, 100, 200, 500)),
146 '#description' => t('The maximum number of articles migrated in each pass of a <a href="@cron">cron maintenance task</a>. If necessary, reduce the number of items to prevent timeouts and memory errors while migrating.', array('@cron' => url('admin/reports/status')))
147 );
148 }
149
150 $items = array();
151 $types = node_get_types();
152 foreach ($types as $type) {
153 $items[$type->type] = $type->name;
154 }
155
156 $form['artman2_default_node_type'] = array(
157 '#type' => 'select',
158 '#title' => t('Default node type for imported articles'),
159 '#default_value' => variable_get('artman2_default_node_type', 'story'),
160 '#options' => $items,
161 );
162
163 $items = array();
164 $formats = filter_formats();
165 foreach ($formats as $format) {
166 $items[$format->format] = $format->name;
167 }
168 $form['artman2_default_format'] = array(
169 '#type' => 'select',
170 '#title' => t('Default format for imported articles'),
171 '#default_value' => variable_get('artman2_default_format', variable_get('filter_default_format', 1)),
172 '#options' => $items,
173 );
174
175 $form['artman2_upload_path'] = array(
176 '#type' => 'textfield',
177 '#title' => t('Article Manager 2 upload path/URL'),
178 '#default_value' => variable_get('artman2_upload_path', ''),
179 '#description' => t('The URL or path (relative to Drupal root) where images and uploaded files are located.'),
180 );
181 return system_settings_form($form);
182 }
183
184 function artman2_db_url_parse($db_url) {
185 $encoded_url = parse_url($db_url);
186 foreach ($encoded_url as $key => $value) {
187 if ($key == 'path') {
188 $url[$key] = ltrim(urldecode($value), '/');
189 }
190 else {
191 $url[$key] = isset($value) ? urldecode($value) : '';
192 }
193 }
194 return $url;
195 }
196
197 /**
198 * Form API validate for install_settings form.
199 */
200
201 function artman2_admin_settings_validate($form, &$form_state) {
202 $db_type = $form_state['values']['db_type'];
203 $db_host = $form_state['values']['db_host'];
204 $db_user = $form_state['values']['db_user'];
205 $db_pass = $form_state['values']['db_pass'];
206 $db_port = $form_state['values']['db_port'];
207 $db_path = $form_state['values']['db_path'];
208
209 // Verify the table prefix
210 if (!empty($form_state['values']['artman2_db_prefix']) && is_string($form_state['values']['artman2_db_prefix']) && !preg_match('/^[A-Za-z0-9_.]+$/', $form_state['values']['artman2_db_prefix'])) {
211 form_set_error('db_prefix', t('The database table prefix you have entered, %db_prefix, is invalid. The table prefix can only contain alphanumeric characters, periods, or underscores.', array('%db_prefix' => $form_state['values']['artman2_db_prefix'])), 'error');
212 }
213
214 if (!empty($db_port) && !is_numeric($db_port)) {
215 form_set_error('db_port', t('Database port must be a number.'));
216 }
217 $databases = drupal_detect_database_types();
218 if (!in_array($db_type, $databases)) {
219 form_set_error('db_type', t("In your %settings_file file you have configured @drupal to use a %db_type server, however your PHP installation currently does not support this database type.", array('%settings_file' => $settings_file, '@drupal' => drupal_install_profile_name(), '%db_type' => $db_type)));
220 }
221 else {
222 // Verify
223 $db_url = $db_type .'://'. urlencode($db_user) . ($db_pass ? ':'. urlencode($db_pass) : '') .'@'. ($db_host ? urlencode($db_host) : 'localhost') . ($db_port ? ":$db_port" : '') .'/'. urlencode($db_path);
224 if (isset($form)) {
225 form_set_value($form['artman2_db_url'], $db_url, $form_state);
226 }
227 }
228 }
229
230 /**
231 * Form API submit for install_settings form.
232 */
233
234 function artman2_admin_settings_submit($form, &$form_state) {
235 unset($form_state['values']['db_type']);
236 unset($form_state['values']['db_host']);
237 unset($form_state['values']['db_user']);
238 unset($form_state['values']['db_pass']);
239 unset($form_state['values']['db_port']);
240 unset($form_state['values']['db_path']);
241 }

  ViewVC Help
Powered by ViewVC 1.1.2