/[drupal]/contributions/modules/imc_alba/dadamigrate/dadamigrate.module
ViewVC logotype

Contents of /contributions/modules/imc_alba/dadamigrate/dadamigrate.module

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


Revision 1.19 - (show annotations) (download) (as text)
Wed Aug 27 17:49:20 2008 UTC (14 months, 4 weeks ago) by tomm
Branch: MAIN
CVS Tags: DRUPAL-6--1-2, HEAD
Changes since 1.18: +1 -2 lines
File MIME type: text/x-php
make sure admin control panel doesn't end up in rss feeds and search results..
1 <?php
2 // $Id: dadamigrate.module,v 1.18 2008/05/17 23:11:08 tomm Exp $
3
4 define('DADA_UID_OFFSET', 1000);
5
6 // From dadaimc. Used to sortof hash media files into directories 1..13.
7 // subdir: return a "magic" directory number
8 // parameters: filename (string)
9 // returns: integer
10 function dadamigrate_subdir($str) {
11 // gets a number between 1 and 13 using the first character
12 $num = 0;
13 for ($x=0;$x<strlen($str);$x++) {
14 $num += ord($str[$x]);
15 }
16 return ($num % 13) + 1;
17 }
18
19 function __dm_urlpath($path) { return '/'.file_directory_path().'/usermedia/'.$path; }
20 function __dm_uploadpath($path) { return file_directory_path().'/usermedia/'.$path; }
21
22 function dadamigrate_filepath($mime_class, $filename) {
23 return $mime_class.'/'.dadamigrate_subdir($filename).'/'.$filename;
24 }
25
26 function dadamigrate_filepath_thumbnail($mime_class, $filename) {
27 return $mime_class.'/'.dadamigrate_subdir($filename).'/thumb/'.$filename;
28 }
29
30 function dadamigrate_filepath_large($mime_class, $filename) {
31 return $mime_class.'/'.dadamigrate_subdir($filename).'/large/'.$filename;
32 }
33
34 /**
35 * Implementation of hook_menu.
36 */
37 function dadamigrate_menu() {
38 $items = array();
39 $items['admin/settings/indymedia/dadamigrate'] = array(
40 'title' => t('Indymedia Dada to Drupal migrator.'),
41 'description' => t('Converts your old dada site database to drupal.'),
42 'page callback' => 'drupal_get_form',
43 'page arguments' => array('dadamigrate_settings'),
44 'access arguments' => array('administer site configuration'),
45 );
46 $items['indymedia/dadamigrate/setup'] = array(
47 'page callback' => 'dadamigrate_setup',
48 'access arguments' => array('administer site configuration'),
49 'type' => MENU_CALLBACK
50 );
51 $items['newswire/display/%'] = array(
52 'page callback' => 'dadamigrate_redirect_newswire',
53 'access callback' => TRUE,
54 'type' => MENU_CALLBACK
55 );
56 $items['feature/display/%'] = array(
57 'page callback' => 'dadamigrate_redirect_feature',
58 'access callback' => TRUE,
59 'type' => MENU_CALLBACK
60 );
61 $items['mod/otherpress/display/%'] = array(
62 'page callback' => 'dadamigrate_redirect_otherpress',
63 'access callback' => TRUE,
64 'type' => MENU_CALLBACK
65 );
66 $items['media/all/display/%'] = array(
67 'page callback' => 'dadamigrate_redirect_media',
68 'access callback' => TRUE,
69 'type' => MENU_CALLBACK
70 );
71 $items['mod/comments/display/%'] = array(
72 'page callback' => 'dadamigrate_redirect_comments',
73 'access callback' => TRUE,
74 'type' => MENU_CALLBACK
75 );
76
77 return $items;
78 }
79
80 /*
81 * It is nice to keep the dada urls working.
82 */
83 function dadamigrate_redirect_comments() {
84 $q = db_fetch_array(db_query("SELECT nid FROM {dada_comments} WHERE objectid = %d", intval(arg(3))));
85 if ($q) drupal_goto('node/'.$q['nid']);
86 }
87 function dadamigrate_redirect_media() {
88 $q = db_fetch_array(db_query("SELECT nid FROM {dada_media} WHERE objectid = %d", intval(arg(3))));
89 if ($q) drupal_goto('node/'.$q['nid']);
90 }
91 function dadamigrate_redirect_otherpress() {
92 $q = db_fetch_array(db_query("SELECT nid FROM {dada_otherpress} WHERE objectid = %d", intval(arg(3))));
93 if ($q) drupal_goto('node/'.$q['nid']);
94 }
95 function dadamigrate_redirect_feature() {
96 $q = db_fetch_array(db_query("SELECT nid FROM {dada_features} WHERE objectid = %d", intval(arg(2))));
97 if ($q) drupal_goto('node/'.$q['nid']);
98 }
99 function dadamigrate_redirect_newswire() {
100 $q = db_fetch_array(db_query("SELECT nid FROM {dada_articles} WHERE objectid = %d", intval(arg(2))));
101 if ($q) drupal_goto('node/'.$q['nid']);
102 }
103
104 /**
105 * Configuration options for Indymedia cities list.
106 */
107 function dadamigrate_settings() {
108 $form = array();
109
110 if (is_dir(__dm_uploadpath('.'))) {
111 $form[] = array('#value' => '<p>Dada usermedia found at '.__dm_uploadpath('.').' You are ready to run the migration script!</p>');
112 } else {
113 drupal_set_message('Dada usermedia not found. You should link or copy
114 the usermedia directory from your dada setup to your drupal directory
115 (into '.file_directory_path().').
116 If you do not do this then images and videos will not
117 be imported correctly.', 'error');
118 }
119 if (!db_result(db_query("SELECT rid FROM {role} WHERE name = 'editor'")) ||
120 !db_result(db_query("SELECT rid FROM {role} WHERE name = 'admin'"))) {
121 drupal_set_message("Roles named 'editor' and 'admin' could not
122 be found. If you run the dada migration without creating these
123 roles, editors and admins from the old site will lose their
124 privileges.", 'error');
125 }
126
127 $form[] = array(
128 '#value' => '<p>The dada migrator will import feature,
129 articles, comments and media from your dadaimc database. It
130 will also set up redirects from dada newswire and feature urls
131 to drupal nodes, keeping most old urls working.</p><p>'.
132 l(t("Run the dada migration script"), "indymedia/dadamigrate/setup").'</p>',
133 );
134 return system_settings_form($form);
135 }
136
137 function dadamigrate_setup_form() {
138 $form = array();
139
140 $form['dbhost'] = array(
141 '#title' => t('Database host'),
142 '#type' => 'textfield',
143 '#default_value' => 'localhost',
144 '#required' => TRUE,
145 );
146 $form['dbuser'] = array(
147 '#title' => t('Database user'),
148 '#type' => 'textfield',
149 '#required' => TRUE,
150 );
151 $form['dbpasswd'] = array(
152 '#title' => t('Database password'),
153 '#type' => 'password',
154 );
155 $form['dbname'] = array(
156 '#title' => t('Database name'),
157 '#type' => 'textfield',
158 '#required' => TRUE,
159 );
160 $form['submit'] = array(
161 '#type' => 'submit',
162 '#value' => 'Run'
163 );
164 return $form;
165 }
166
167 function dadamigrate_setup_form_validate($form_id, $form_state) {
168 // form_set_error('dbname', t("DBname is silly"));
169 }
170
171 function _dada_db_connect(&$args, $verbose=false) {
172 $connection = @mysql_connect($args['dbhost'],
173 $args['dbuser'], $args['dbpasswd'], TRUE, 2);
174 if (!$connection) {
175 if ($verbose) drupal_set_message(t("Could not connect to database ".$args['dbname']));
176 } else {
177 if ($verbose) drupal_set_message(t("Connected OK"));
178 if (!mysql_select_db($args['dbname'])) {
179 if ($verbose) drupal_set_message('Unable to select database. Does it exist?');
180 return;
181 }
182 if ($verbose) drupal_set_message(t("Selected database ".$args['dbname']));
183 }
184 return $connection;
185 }
186
187 function _dada_to_drupal_uid($uid) {
188 if ($uid != 0) {
189 $uid += DADA_UID_OFFSET;
190 $q = db_fetch_array(db_query("SELECT uid FROM {users} WHERE uid = %d", $uid));
191 return $q['uid'];
192 } else {
193 return 0;
194 }
195 }
196
197 function _import_users(&$dbArgs, &$context) {
198 $connection = _dada_db_connect($dbArgs);
199 if (!empty($context['sandbox'])) {
200 $start = $context['sandbox']['current_objectid'];
201 } else {
202 $start = 0;
203 $a = mysql_fetch_array(mysql_query("SELECT COUNT(objectid) FROM users", $connection));
204 $context['sandbox']['max'] = $a[0];
205 drupal_set_message("Users to import: ".$context['sandbox']['max']);
206 }
207 $STEP = 200;
208 $result = mysql_query("SELECT objectid,username,password,email,created_datetime,lastaccess_timestamp,level,pw_method FROM users LIMIT $start,$STEP", $connection);
209 $context['sandbox']['current_objectid'] = $start+$STEP;
210 $i=0;
211
212 $editor_rid = db_result(db_query("SELECT rid FROM {role} WHERE name = 'editor'"));
213 $admin_rid = db_result(db_query("SELECT rid FROM {role} WHERE name = 'admin'"));
214
215 while ($user = mysql_fetch_array($result)) {
216 $i++;
217 // ignore dada users with pre-MD5 password hash. they are boned.
218 if (strtoupper($user['pw_method']) != 'MD5') continue;
219
220 // some old dada users don't have valid last access times
221 if ($user['lastaccess_timestamp'] == '0000-00-00 00:00:00') {
222 $last_access = strtotime($user['created_datetime']);
223 } else {
224 $last_access = strtotime($user['lastaccess_timestamp']);
225 }
226
227 $junk = array(
228 'uid' => $user['objectid']+DADA_UID_OFFSET,
229 'name' => $user['username'],
230 'pass' => $user['password'],
231 'created' => strtotime($user['created_datetime']),
232 'access' => $last_access,
233 'login' => $last_access,
234 'status' => 1,
235 );
236 if ($user['email']) $junk['mail'] = $user['email'];
237
238 user_save('', $junk);
239 // user_save likes to store the md5sum of the password
240 // provided, but actually we already have that so let's put it
241 // in the db
242 db_query("UPDATE {users} SET pass='%s' WHERE uid=%d",
243 $user['password'], $junk['uid']);
244 if (($user['level'] == 'Editor') && $editor_rid) {
245 db_query("INSERT INTO {users_roles} (uid, rid) VALUES (%d, %d)", $junk['uid'], $editor_rid);
246 }
247 if (($user['level'] == 'Admin') && $admin_rid) {
248 db_query("INSERT INTO {users_roles} (uid, rid) VALUES (%d, %d)", $junk['uid'], $admin_rid);
249 }
250 }
251 if ($i != 0) {
252 $max = $context['sandbox']['max'];
253 $context['finished'] = ($start+$i) / $context['sandbox']['max'];
254 $context['message'] = "Importing users";
255 }
256 }
257
258 function _dadamigrate_save_article(&$dbArgs, $article, $connection, $type) {
259 $uid = _dada_to_drupal_uid($article['authorid']);
260
261 // user doesn't exist...
262 if (!db_query("SELECT * FROM {users} WHERE uid = %d", $uid)) $uid = 0;
263 $title = str_replace("&quot;","\"", $article['heading']);
264
265 $created_datetime = strtotime($article['created_datetime']);
266 if (!$created_datetime) $created_datetime = strtotime($article['edit_datetime']);
267
268 $node = (object)array(
269 'type' => ($type == 'comment' ? 'nodecomment' : 'article'),
270 'title' => $title,
271 'status' => $article['displayable'],
272 'created' => $created_datetime,
273 'changed' => strtotime($article['modified_timestamp']),
274 'promote' => $article['feature'],
275 'comment' => 2, // allow comments (1 = no further comments)
276 'format' => 1, // unfiltered HTML
277 //'language' => 'en',
278 'uid' => $uid,
279 );
280 $node->teaser = $article['summary'];
281 $node->body = $article['summary'].($article['body'] ? "<!--break-->\n\n".$article['body'] : '');
282
283 if ($type == 'comment') {
284 $node->parent_nid = $article['parent_nid'];
285 }
286
287 if ($article['related_url1'] || $article['related_url2'])
288 $node->body .= '<p><strong>Related</strong></p>';
289 if ($article['related_url1'])
290 $node->body .= '<p><a href="'.$article['related_url1'].'">'.
291 $article['related_url1'].'</a></p>';
292 if ($article['related_url2'])
293 $node->body .= '<p><a href="'.$article['related_url2'].'">'.
294 $article['related_url2'].'</a></p>';
295
296 if (($article['heading'] == "") &&
297 ($article['summary'] == "") &&
298 ($article['body'] == "")) return;
299
300 node_save($node);
301
302 // anonymous attribution
303 if (($uid == 0) && ($article['author'])) {
304 db_query('INSERT INTO {nodextradata} (nid, author) VALUES (%d,"%s")', $node->nid, $article['author']);
305 }
306
307 if ($type != 'comment') {
308 // do terms
309 $terms = array();
310 $sql = sprintf('SELECT category_id FROM hash_category WHERE ref_class="%s" AND ref_id=%d',
311 $type,
312 $article['objectid']);
313 $q = mysql_query($sql, $connection);
314 while ($cat = mysql_fetch_array($q)) {
315 $terms[] = $dbArgs['cats'][$cat['category_id']];
316 }
317
318
319 if ($type == 'Otherpress') {
320 $section_tid = variable_get('dada_section_Otherpress', 0);
321 if ($section_tid) $terms[] = $section_tid;
322 } else {
323 $section_tid = variable_get('dada_section_'.$article['section'], 0);
324 if ($section_tid) $terms[] = $section_tid;
325 }
326
327 taxonomy_node_save($node, $terms);
328 }
329 return $node;
330 }
331
332 function _dadamigrate_register_node_media($node, &$minfo) {
333 foreach ($minfo as $m) {
334 $path = dadamigrate_filepath($m['mime_class'], $m['filename']);
335 if (!file_exists(__dm_uploadpath($path))) continue;
336 if (strncmp ('image/', $m['mime_type'], 6) === 0) {
337 // XXX what about large image size?...
338 $file = array(
339 'uid' => $node->uid,
340 'filename' => basename($path),
341 'filepath' => __dm_uploadpath($path),
342 'filemime' => $m['mime_type'],
343 'filesize' => filesize(__dm_uploadpath($path)),
344 'status' => FILE_STATUS_PERMANENT,
345 'timestamp' => $node->created,
346 );
347 drupal_write_record('files', $file);
348 $upload = array(
349 'fid' => $file['fid'],
350 'nid' => $node->nid,
351 'vid' => $node->vid,
352 'description' => '',
353 'list' => 0,
354 'weight' => 0,
355 );
356 drupal_write_record('upload', $upload);
357
358 $parent_fid = $file['fid'];
359 $thumb = dadamigrate_filepath_thumbnail($m['mime_class'], $m['filename']);
360 if (file_exists(__dm_uploadpath($thumb))) {
361 $file = array(
362 'uid' => $node->uid,
363 'filename' => '_thumbnail',
364 'filepath' => __dm_uploadpath($thumb),
365 'filemime' => $m['mime_type'],
366 'filesize' => filesize(__dm_uploadpath($thumb)),
367 'status' => FILE_STATUS_PERMANENT,
368 'timestamp' => $node->created,
369 );
370 drupal_write_record('files', $file);
371 $upload_deriv = array(
372 'fid' => $file['fid'],
373 'fid_parent' => $parent_fid,
374 'type' => 'img.thumb',
375 );
376 drupal_write_record('upload_derivative', $upload_deriv);
377 }
378 $big = dadamigrate_filepath_large($m['mime_class'], $m['filename']);
379 if (file_exists(__dm_uploadpath($big))) {
380 $file = array(
381 'uid' => $node->uid,
382 'filename' => '_large',
383 'filepath' => __dm_uploadpath($big),
384 'filemime' => $m['mime_type'],
385 'filesize' => filesize(__dm_uploadpath($big)),
386 'status' => FILE_STATUS_PERMANENT,
387 'timestamp' => $node->created,
388 );
389 drupal_write_record('files', $file);
390 $upload_deriv = array(
391 'fid' => $file['fid'],
392 'fid_parent' => $parent_fid,
393 'type' => 'img.large',
394 );
395 drupal_write_record('upload_derivative', $upload_deriv);
396 }
397 } else {
398 $file = array(
399 'uid' => $node->uid,
400 'filename' => basename($path),
401 'filepath' => __dm_uploadpath($path),
402 'filemime' => $m['mime_type'],
403 'filesize' => filesize(__dm_uploadpath($path)),
404 'status' => FILE_STATUS_PERMANENT,
405 'timestamp' => $node->created,
406 );
407 drupal_write_record('files', $file);
408 $upload = array(
409 'fid' => $file['fid'],
410 'nid' => $node->nid,
411 'vid' => $node->vid,
412 'description' => '',
413 'list' => 1,
414 'weight' => 0,
415 );
416 drupal_write_record('upload', $upload);
417 }
418 }
419 }
420
421 function dadamigrate_feature_add_media(&$feature, &$m) {
422 if (strncmp ('image/', $m['mime_type'], 6) === 0) {
423 //$fid = db_next_id('{files}_fid');
424 $filepath = dadamigrate_filepath_thumbnail($m['mime_class'], $m['filename']);
425 // TODO check files actually exist and do not insert if not
426 // TODO sometimes there is no big version of the image
427 $imgtag = "<img align=\"".($m['alignment'] ? $m['alignment'] : "left")."\" src=\"".__dm_urlpath($filepath)."\" />";
428 $feature['summary'] = dadamigrate_image_insert($feature['summary'], $feature['embedded_media'], $imgtag, $m['sequence_number']);
429 } else {
430 $filepath = dadamigrate_filepath($m['mime_class'], $m['filename']);
431 // don't know what to do with it yet...
432 $feature['summary'] .= "<p><a href=\"".__dm_urlpath($filepath)."\">".$m['filename']."</a></p>";
433 }
434 }
435
436 function _import_features(&$dbArgs, &$context) {
437 $connection = _dada_db_connect($dbArgs);
438 if (!empty($context['sandbox'])) {
439 $start = $context['sandbox']['current_objectid'];
440 } else {
441 db_query('DELETE FROM {dada_features}');
442 $start = 0;
443 $a = mysql_fetch_array(mysql_query("SELECT COUNT(objectid) FROM features", $connection));
444 $context['sandbox']['max'] = $a[0];
445 drupal_set_message("Features to import: ".$context['sandbox']['max']);
446 }
447 $STEP = 200;
448 $result = mysql_query("SELECT * FROM features LIMIT $start,$STEP", $connection);
449
450 $context['sandbox']['current_objectid'] = $start+$STEP;
451
452 if (!$result) {
453 drupal_set_message("Error in SELECT * FROM features...");
454 return;
455 }
456
457 $i=0;
458
459 while ($feature = mysql_fetch_array($result)) {
460 $i++;
461
462 $sql = sprintf("SELECT * FROM media WHERE parentid=%d AND parent_class='Feature' ORDER BY sequence_number %s",
463 $feature['objectid'],
464 ($feature['embedded_media'] == 'aftersummary' ? "DESC" : "ASC"));
465 $mq = mysql_query($sql, $connection);
466 $minfo = array();
467 if ($mq) while ($media = mysql_fetch_array($mq)) {
468 dadamigrate_feature_add_media(&$feature, &$media);
469 $minfo[] = array('mime_class' => $media['mime_class'],
470 'mime_type' => $media['mime_type'],
471 'filename' => $media['filename']);
472 }
473
474 // inline media convocation!
475 preg_match('/#media_(\d+);(left|right)#/', $feature['summary'], $results);
476 if ($results) {
477 $media_id = $results[1];
478 $align = $results[2];
479
480 $m = mysql_fetch_array(mysql_query("SELECT * FROM media WHERE objectid = ".$media_id, $connection));
481 if ($m) {
482 $thumb_filepath = dadamigrate_filepath_thumbnail($m['mime_class'], $m['filename']);
483 $feature['summary'] = str_replace("#media_".$media_id.";".$align."#", '<img align="'.$align.'" src="'.__dm_urlpath($thumb_filepath).'" />', $feature['summary']);
484 }
485 }
486
487 if ($feature['refid']) {
488 /* feature linked to an article */
489 if ($feature['ref_class'] != 'Article') continue;
490
491 $nid = db_result(db_query('SELECT nid FROM {dada_articles} WHERE objectid = %d', $feature['refid']));
492 if (!$nid) continue;
493
494 $created_datetime = strtotime($feature['created_datetime']);
495 if (!$created_datetime) $created_datetime = strtotime($feature['edit_datetime']);
496 // move the newswire summary into the body and set
497 // summmary to be that of the feature
498 $node = node_load($nid);
499 $node->teaser = $feature['summary'];
500 $node->created = $created_datetime;
501 $node->status = $feature['displayable'];
502 $node->body = "<!--break-->".str_replace("<!--break-->", "", $node->body);
503 node_save($node);
504 _dadamigrate_register_node_media($node, &$minfo);
505
506 db_query('INSERT INTO {dada_features} (objectid, nid) VALUES (%d,%d)',
507 $feature['objectid'], $node->nid);
508 } else {
509 /* feature not linked to article */
510 $feature['feature'] = 1;
511 $feature['body'] = "";
512 $node = _dadamigrate_save_article(&$dbArgs, $feature, $connection, "Feature");
513
514 if ($node) {
515 _dadamigrate_register_node_media($node, &$minfo);
516 db_query('INSERT INTO {dada_features} (objectid, nid) VALUES (%d,%d)',
517 $feature['objectid'], $node->nid);
518 }
519 }
520 }
521
522 if ($i != 0) {
523 $max = $context['sandbox']['max'];
524 $context['finished'] = ($start+$i) / $context['sandbox']['max'];
525 $context['message'] = "Importing features";
526 }
527 }
528
529 function dadamigrate_image_insert($body, $embedded_media_type, $imgtag, $img_seq_no) {
530 switch ($embedded_media_type) {
531 case 'aftersummary':
532 $body = $imgtag.$body;
533 break;
534 case 'oneaftersummary':
535 if ($img_seq_no == 1) {
536 $body = $imgtag.$body;
537 } else {
538 $body .= $imgtag;
539 }
540 break;
541 case 'placeholders':
542 $body = str_replace("#file_".$img_seq_no."#", $imgtag, $body);
543 break;
544 default:
545 $body .= $imgtag;
546 break;
547 }
548 return $body;
549 }
550
551 function dadamigrate_article_add_media(&$article, &$m) {
552 if (strncmp ('image/', $m['mime_type'], 6) === 0) {
553 // TODO check files actually exist and do not insert if not
554 // TODO sometimes there is no big version of the image
555 $filepath = dadamigrate_filepath($m['mime_class'], $m['filename']);
556 $large_filepath = dadamigrate_filepath_large($m['mime_class'], $m['filename']);
557 // ignore first character (/), so that it is relative path in unix fs
558 $imgtag = '<span class="imgbox">';
559 if (is_file(__dm_uploadpath($large_filepath))) {
560 $imgtag .= sprintf('<a href="%s"><img src="%s" /></a>', __dm_urlpath($large_filepath), __dm_urlpath($filepath));
561 } else {
562 $imgtag .= sprintf('<img src="%s" />', __dm_urlpath($filepath));
563 }
564 if ($m['caption'] != $article['heading']) {
565 $imgtag .= '<div class="imgcaption">'.t($m['caption']).'</div>';
566 }
567 $imgtag .= '</span>';
568 $article['body'] = dadamigrate_image_insert($article['body'], $article['embedded_media'], $imgtag, $m['sequence_number']);
569 } else {
570 $filepath = dadamigrate_filepath($m['mime_class'], $m['filename']);
571 // don't know what to do with it yet...
572 $article['body'] .= "<p><a href=\"".__dm_urlpath($filepath)."\">".$m['filename']."</a></p>";
573 }
574 }
575
576
577 function _import_articles(&$dbArgs, &$context) {
578 $connection = _dada_db_connect($dbArgs);
579 if (!empty($context['sandbox'])) {
580 $start = $context['sandbox']['current_objectid'];
581 } else {
582 db_query('DELETE FROM {dada_articles}');
583 $start = 0;
584 $a = mysql_fetch_array(mysql_query("SELECT COUNT(objectid) FROM articles", $connection));
585 $context['sandbox']['max'] = $a[0];
586 drupal_set_message("Articles to import: ".$context['sandbox']['max']);
587 }
588 $STEP = 200;
589 $result = mysql_query("SELECT * FROM articles LIMIT $start,$STEP", $connection);
590
591 $context['sandbox']['current_objectid'] = $start+$STEP;
592
593 if (!$result) {
594 drupal_set_message("Error in SELECT * FROM articles...");
595 return;
596 }
597
598 $i=0;
599
600 while ($article = mysql_fetch_array($result)) {
601 $i++;
602
603 // drop articles marked deleted by dada. probably desirable
604 if ($article['deleted']==1) continue;
605
606 $sql = sprintf("SELECT * FROM media WHERE parentid=%d AND parent_class='Article' ORDER BY sequence_number %s",
607 $article['objectid'],
608 ($article['embedded_media'] == 'aftersummary' ? "DESC" : "ASC"));
609 $mq = mysql_query($sql, $connection);
610 $minfo = array();
611 if ($mq) while ($media = mysql_fetch_array($mq)) {
612 dadamigrate_article_add_media(&$article, &$media);
613 $minfo[] = array('mime_class' => $media['mime_class'],
614 'mime_type' => $media['mime_type'],
615 'filename' => $media['filename']);
616 }
617
618 $node = _dadamigrate_save_article(&$dbArgs, $article, $connection, "Article");
619 _dadamigrate_register_node_media($node, &$minfo);
620
621 if ($node) db_query('INSERT INTO {dada_articles} (objectid, nid) VALUES (%d,%d)',
622 $article['objectid'], $node->nid);
623 }
624 if ($i != 0) {
625 $max = $context['sandbox']['max'];
626 $context['finished'] = ($start+$i) / $context['sandbox']['max'];
627 $context['message'] = "Importing articles";
628 }
629 }
630
631 function _import_otherpress(&$dbArgs, &$context) {
632 $connection = _dada_db_connect($dbArgs);
633 if (!empty($context['sandbox'])) {
634 $start = $context['sandbox']['current_objectid'];
635 } else {
636 $start = 0;
637 $a = mysql_fetch_array(mysql_query("SELECT COUNT(*) FROM otherpress", $connection));
638 $context['sandbox']['max'] = $a[0];
639 drupal_set_message("Otherpress to import: ".$context['sandbox']['max']);
640 }
641
642 $STEP = 200;
643 $i=0;
644 $context['sandbox']['current_objectid'] = $start+$STEP;
645 // import objections
646 $result = mysql_query("SELECT * FROM otherpress LIMIT $start,$STEP", $connection);
647 while ($article = mysql_fetch_array($result)) {
648 $i++;
649
650 // drop articles marked deleted by dada. probably desirable
651 if ($article['deleted']==1) continue;
652
653 $article['displayable'] = 1;
654 $article['feature'] = 0;
655 $article['body'] = "";
656 $article['related_url1'] = $article['link'];
657 $article['related_url2'] = "";
658 $node = _dadamigrate_save_article(&$dbArgs, $article, $connection, "Otherpress");
659
660 if ($node) db_query('INSERT INTO {dada_otherpress} (objectid, nid) VALUES (%d,%d)',
661 $article['objectid'], $node->nid);
662 }
663 if ($i != 0) {
664 $max = $context['sandbox']['max'];
665 $context['finished'] = ($start+$i) / $context['sandbox']['max'];
666 $context['message'] = "Importing otherpress";
667 }
668 }
669
670 function _import_objections(&$dbArgs, &$context) {
671 $connection = _dada_db_connect($dbArgs);
672 if (!empty($context['sandbox'])) {
673 $start = $context['sandbox']['current_objectid'];
674 } else {
675 $start = 0;
676 $a = mysql_fetch_array(mysql_query('SELECT COUNT(*) FROM objections WHERE hide=1 AND (type="Article" OR type="Comment" OR type="Media") ', $connection));
677 $context['sandbox']['max'] = $a[0];
678 drupal_set_message("Objections to import: ".$context['sandbox']['max']);
679 }
680
681 $STEP = 200;
682 $i=0;
683 $context['sandbox']['current_objectid'] = $start+$STEP;
684 // import objections
685 $result = mysql_query('SELECT * FROM objections WHERE hide=1 AND (type="Article" OR type="Comment" OR type="Media") ORDER BY modified_timestamp DESC LIMIT '.$start.','.$STEP, $connection);
686 while ($obj = mysql_fetch_array($result)) {
687 $i++;
688 switch ($obj['type']) {
689 case 'Comment': $_dbpostfix = 'comments'; break;
690 case 'Media': $_dbpostfix = 'media'; break;
691 default: $_dbpostfix = 'articles'; break;
692 }
693 $nid = db_result(db_query('SELECT nid FROM {dada_'.$_dbpostfix.'} WHERE objectid = %d', $obj['refid']));
694 if (!$nid) continue;
695 // there can be objections duplicates due to the way dada stored them...
696 if (db_result(db_query('SELECT COUNT(*) FROM {imc_node_moderation} WHERE nid = %d', $nid))) continue;
697
698 // featured articles just drop hidden info. it is usually users voting it down but the effect is silly
699 if (db_result(db_query('SELECT COUNT(*) FROM {node} WHERE nid = %d AND promote = 1', $nid))) continue;
700
701 $reason = ($obj['hide_code'] ? $obj['hide_code'].": ".$obj['hide_notes'] : $obj['hide_notes']);
702
703 db_query("INSERT INTO {imc_node_moderation} (nid, status, comment, uid, timestamp) VALUES (%d,%d,'%s',%d,%d)",
704 $nid, IMCEDITOR_NODE_STATUS_HIDDEN, $reason, 1, strtotime($obj['modified_timestamp']));
705 db_query("UPDATE {node} SET status=0 WHERE nid=%d", $nid);
706 }
707
708 if ($i != 0) {
709 $max = $context['sandbox']['max'];
710 $context['finished'] = ($start+$i) / $context['sandbox']['max'];
711 $context['message'] = "Importing objections";
712 }
713 }
714
715 function _import_comments(&$dbArgs, &$context) {
716 $connection = _dada_db_connect($dbArgs);
717 if (!empty($context['sandbox'])) {
718 $start = $context['sandbox']['current_objectid'];
719 } else {
720 $start = 0;
721 $a = mysql_fetch_array(mysql_query("SELECT COUNT(objectid) FROM comments", $connection));
722 $context['sandbox']['max'] = $a[0];
723 drupal_set_message("Comments to import: ".$context['sandbox']['max']);
724 }
725
726 $STEP = 200;
727 $i=0;
728 $context['sandbox']['current_objectid'] = $start+$STEP;
729 // convert comments
730 $result = mysql_query("SELECT * FROM comments LIMIT $start,$STEP", $connection);
731 while ($dcom = mysql_fetch_array($result)) {
732 $i++;
733 if ($dcom['deleted']) continue;
734
735 $q = db_query('SELECT nid FROM {dada_articles} WHERE objectid = %d', $dcom['parentid']);
736 if (!$q) continue;
737 else if ($q = db_fetch_array($q)) {
738 $parent_nid = reset($q);
739 }
740
741 $uid = _dada_to_drupal_uid($dcom['authorid']);
742
743 // user doesn't exist...
744 if (!db_query("SELECT COUNT(*) FROM {users} WHERE uid = %d", $uid)) $uid = 0;
745
746 $sql = sprintf("SELECT * FROM media WHERE parentid=%d AND parent_class='Comment' ORDER BY sequence_number %s",
747 $dcom['objectid'],
748 ($dcom['embedded_media'] == 'aftersummary' ? "DESC" : "ASC"));
749 $mq = mysql_query($sql, $connection);
750 $minfo = array();
751 if ($mq) while ($media = mysql_fetch_array($mq)) {
752 dadamigrate_article_add_media(&$dcom, &$media);
753 $minfo[] = array('mime_class' => $media['mime_class'],
754 'mime_type' => $media['mime_type'],
755 'filename' => $media['filename']);
756 }
757
758 if (function_exists('nodecomment_perm')) {
759 $dcom['feature'] = 0;
760 $dcom['parent_nid'] = $parent_nid;
761 // full view even on teaser.
762 $dcom['summary'] .= $dcom['body'];
763 $dcom['body'] = '';
764 $node = _dadamigrate_save_article(&$dbArgs, $dcom, $connection, "comment");
765 if ($node) {
766 _dadamigrate_register_node_media($node, &$minfo);
767 db_query('INSERT INTO {dada_comments} (objectid, nid) VALUES (%d,%d)',
768 $dcom['objectid'], $node->nid);
769 }
770 } else {
771 // normal comment module
772 $comment = array(
773 'cid' => 0,
774 'nid' => $parent_nid,
775 'subject' => $dcom['heading'],
776 'comment' => $dcom['summary']."\n".$dcom['body'],
777 //'timestamp' => strtotime($dcom['modified_timestamp']),
778 'format' => 1, // unfiltered HTML
779 'pid' => 0, // parent id.
780 'uid' => $uid,
781 'name' => $dcom['author'],
782 );
783
784 $cid = comment_save($comment);
785 // sadly the comment_save function sets timestamp to time()...
786 db_query("UPDATE {comments} SET timestamp=%d WHERE cid=%d",
787 strtotime($dcom['created_datetime']),
788 $cid);
789 }
790 }
791
792 if ($i != 0) {
793 $max = $context['sandbox']['max'];
794 $context['finished'] = ($start+$i) / $context['sandbox']['max'];
795 $context['message'] = "Importing comments";
796 }
797 }
798
799 function _import_media(&$dbArgs, &$context) {
800 $connection = _dada_db_connect($dbArgs);
801 if (!empty($context['sandbox'])) {
802 $start = $context['sandbox']['current_objectid'];
803 } else {
804 $start = 0;
805 $a = mysql_fetch_array(mysql_query("SELECT COUNT(*) FROM media WHERE parent_class=''", $connection));
806 $context['sandbox']['max'] = $a[0];
807 drupal_set_message("Media objects to import: ".$context['sandbox']['max']);
808 }
809
810 $STEP = 200;
811 $i=0;
812 $context['sandbox']['current_objectid'] = $start+$STEP;
813 // import media (note: you must copy the contents of your dada
814 // usermedia/ directory to your drup files/ directory.
815 // cp -r dada/usermedia/* drupal/files/
816 $result = mysql_query("SELECT * FROM media WHERE parent_class='' LIMIT $start,$STEP", $connection);
817 while ($m = mysql_fetch_array($result)) {
818 $i++;
819 // comment, article and feature media has already been
820 // imported. we are only interested in the unparented crap
821 if ($m['parent_class']) continue;
822 if ($m['deleted']) continue;
823 $article = array();
824 // media things have to title, so we have to make one...
825 if ($m['caption']) {
826 if (strlen($m['caption']) > 64) {
827 $article['heading'] = substr($m['caption'], 0, 64).'...';
828 } else if (strlen($m['caption']) > 0) {
829 $article['heading'] = $m['caption'];
830 }
831 $article['heading'] = $m['orig_filename'].": ".$article['heading'];
832 } else {
833 $article['heading'] = $m['orig_filename'];
834 }
835 $article['created_datetime'] = $m['created_datetime'];
836 $article['displayable'] = 1;
837 $article['feature'] = 0;
838 $article['summary'] = '';
839 $article['body'] = '';
840 $article['section'] = '';
841 $article['modified_timestamp'] = $m['modified_timestamp'];
842 $article['embedded_media'] = '';
843 $article['authorid'] = '';
844 $article['author'] = '';
845 $article['related_url1'] = $m['related_url1'];
846 $article['related_url2'] = '';
847 $article['objectid'] = $m['objectid'];
848
849 dadamigrate_article_add_media(&$article, &$m);
850
851 $node = _dadamigrate_save_article(&$dbArgs, $article, $connection, "");
852 if ($node) {
853 $minfo = array($m);
854 _dadamigrate_register_node_media($node, &$minfo);
855 db_query('INSERT INTO {dada_media} (objectid, nid) VALUES (%d,%d)',
856 $m['objectid'], $node->nid);
857 }
858 }
859 if ($i != 0) {
860 $max = $context['sandbox']['max'];
861 $context['finished'] = ($start+$i) / $context['sandbox']['max'];
862 $context['message'] = "Importing media objects";
863 }
864 }
865
866 function _dadamigrate_stuff_terms(&$terms, $vid) {
867 $tids = array();
868 foreach ($terms as $name => $desc) {
869 $term = array(
870 'name' => $name, 'vid' => $vid,
871 'description' => $desc,
872 );
873 taxonomy_save_term($term);
874 $tids[] = $term['tid'];
875 }
876 return $tids;
877 }
878
879 function _import_categories(&$dbArgs) {
880 $connection = _dada_db_connect($dbArgs);
881
882 // check sections exist. if not then make them
883 if (variable_get('dada_section_vid', 0) == 0) {
884 $vocab = array(
885 'nodes' => array('article' => 1),
886 'name' => 'Section',
887 'relations' => 1,
888 'hierarchy' => 1,
889 'multiple' => 0,
890 'required' => 1,
891 'tags' => 0,
892 );
893 taxonomy_save_vocabulary($vocab);
894
895 // for that split-newswire feature that people ask for..........
896 $terms = array('News' => '',
897 'Commentary' => '',
898 'Announcements' => '',
899 'Reviews' => '',
900 'Interviews' => '',
901 'Other Press' => 'Links to interesting articles by other media outlets.');
902 $tids = _indymedia_alba_stuff_terms(&$terms, $vocab['vid']);
903
904 variable_set('dada_section_vid', $vocab['vid']);
905 variable_set('dada_section_News', $tids[0]);
906 variable_set('dada_section_Commentary', $tids[1]);
907 variable_set('dada_section_Announcement', $tids[2]);
908 variable_set('dada_section_Review', $tids[3]);
909 variable_set('dada_section_Interview', $tids[4]);
910 variable_set('dada_section_Otherpress', $tids[5]);
911 }
912
913
914 $vid = db_result(db_query("SELECT vid FROM {vocabulary} WHERE name='Category'"));
915 if (!$vid) {
916 // doesn't exist. make it
917 $vocab = array(
918 'nodes' => array('article' => 1),
919 'name' => 'Category',
920 'relations' => 1,
921 'hierarchy' => 1,
922 'multiple' => 1,
923 'required' => 1,
924 'tags' => 0,
925 );
926 taxonomy_save_vocabulary($vocab);
927 $vid = $vocab['vid'];
928 }
929
930 $dbArgs['cats'] = array();
931
932 $i = 0;
933 $result = mysql_query("SELECT * FROM categories", $connection);
934 while ($c = mysql_fetch_array($result)) {
935 $i++;
936
937 // if it already exists...
938 $r = db_query("SELECT tid FROM {term_data} WHERE name='%s' AND vid=%d", $c['catname'], $vid);
939 if ($term = db_fetch_array($r)) {
940 $dbArgs['cats'][$c['objectid']] = $term['tid'];
941 continue;
942 }
943
944 // otherwise create term
945 $term = array(
946 'name' => $c['catname'],
947 'description' => $c['description'],
948 'vid' => $vid,
949 );
950 taxonomy_save_term($term);
951 $dbArgs['cats'][$c['objectid']] = $term['tid'];
952 }
953 drupal_set_message("Categories to import: ".$i);
954
955 // LOCAL or global interest crud
956 /*
957 $vid = db_result(db_query("SELECT vid FROM {vocabulary} WHERE name='Location'"));
958 if (!$vid) {
959 // doesn't exist. make it
960 $vocab = array(
961 'nodes' => array('article' => 1),
962 'name' => 'Location',
963 'relations' => 1,
964 'hierarchy' => 1,
965 'multiple' => 0,
966 'required' => 1,
967 'tags' => 0,
968 );
969 taxonomy_save_vocabulary($vocab);
970 $vid = $vocab['vid'];
971 }
972
973 $dbArgs['locs'] = array();
974 */
975 }
976
977 function _import_do_node_indexing(&$dbArgs, &$context) {
978 $connection = _dada_db_connect($dbArgs);
979 if (!empty($context['sandbox'])) {
980 $start = $context['sandbox']['current_objectid'];
981 } else {
982 $start = 0;
983 $num = db_result(db_query("SELECT COUNT(*) FROM {node}"));
984 $context['sandbox']['max'] = $num;
985 drupal_set_message("Media objects to import: ".$context['sandbox']['max']);
986 }
987
988 $STEP = 10;
989 $i=0;
990 $context['sandbox']['current_objectid'] = $start+$STEP;
991 // import media (note: you must copy the contents of your dada
992 // usermedia/ directory to your drup files/ directory.
993 // cp -r dada/usermedia/* drupal/files/
994 $result = db_query("SELECT nid FROM {node} LIMIT $start,$STEP");
995 while ($nid = db_result($result)) {
996 $i++;
997 // comment, article and feature media has already been
998 // imported. we are only interested in the unparented crap
999 $node = (object)array('nid' => $nid);
1000 _node_index_node($node);
1001 }
1002 if ($i != 0) {
1003 $max = $context['sandbox']['max'];
1004 $context['finished'] = ($start+$i) / $context['sandbox']['max'];
1005 $context['message'] = "Building article search index";
1006 }
1007 }
1008
1009 function _import_done() {
1010 drupal_set_message("Import done.");
1011 }
1012
1013 function dadamigrate_setup_form_submit($form_id, $form_state) {
1014 $connection = _dada_db_connect($form_state['values'], true);
1015
1016 _import_categories(&$form_state['values']);
1017
1018 $batch = array(
1019 'title' => t('Importing DadaIMC database'),
1020 'operations' => array(
1021 array('_import_users', array(&$form_state['values'])),
1022 array('_import_articles', array(&$form_state['values'])),
1023 array('_import_otherpress', array(&$form_state['values'])),
1024 array('_import_features', array(&$form_state['values'])),
1025 array('_import_comments', array(&$form_state['values'])),
1026 array('_import_media', array(&$form_state['values'])),
1027 array('_import_objections', array(&$form_state['values'])),
1028 // this seems to be a bit buggered (seems to build
1029 // index but searches don't work)
1030 // array('_import_do_node_indexing', array(&$form_state['values'])),
1031 ),
1032 'finished' => '_import_done',
1033 );
1034 batch_set($batch);
1035 }
1036
1037 function dadamigrate_setup() {
1038 return drupal_get_form('dadamigrate_setup_form');
1039 }

  ViewVC Help
Powered by ViewVC 1.1.2