-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathfeedapi2feeds.module
114 lines (108 loc) · 3.25 KB
/
feedapi2feeds.module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
<?php
// $Id$
/**
* @file
* Provides a page callback to migrate from FeedAPI to Feeds, using the Batch API
*/
/**
* Implementation of hook_menu().
*/
function feedapi2feeds_menu() {
$items = array();
$items['admin/build/feeds/migrate'] = array(
'title' => t('Migrate from FeedAPI'),
'page callback' => 'drupal_get_form',
'page arguments' => array('feedapi2feeds_migrate_form'),
'access arguments' => array('administer feeds'),
'type' => MENU_LOCAL_TASK,
'weight' => 1,
);
return $items;
}
/**
* Migration form, choose between types.
*/
function feedapi2feeds_migrate_form() {
drupal_set_title(t('Migrate from FeedAPI to Feeds'));
if (variable_get('site_offline', FALSE) == FALSE) {
return array('msg' => array('#value' => t('Create backup and put the site into !mode first.', array('!mode' => l(t('maintenance mode'), 'admin/settings/site-maintenance')))));
}
else {
module_load_include('php', 'feedapi2feeds', 'feedapi2feeds');
$migration = new FeedAPI2Feeds();
$to_migrate = $migration->getTypesToMigrate();
$form['types'] = array(
'#required' => TRUE,
'#type' => 'checkboxes',
'#options' => drupal_map_assoc($to_migrate),
'#title' => t('Types to migrate'),
'#default_value' => drupal_map_assoc($to_migrate),
);
$form['migrate'] = array(
'#type' => 'submit',
'#value' => t('Start migration'),
);
return $form;
}
}
/**
* Submit handler for feedapi2feeds_migrate_form().
*/
function feedapi2feeds_migrate_form_submit($form, $form_state) {
foreach ($form_state['values']['types'] as $k => $v) {
if ($k != $v) {
unset($form_state['values']['types'][$k]);
}
}
feedapi2feeds_batch_set(t('Migration from FeedAPI'), 'migrate', array_keys($form_state['values']['types']));
}
/**
* Batch callback.
*
* @param $method
* Method to execute: migrate.
* @param $types
* Array of content-type names
* @param $context
* Batch context.
*/
function feedapi2feeds_batch($method, $types, &$context) {
if (!isset($context['sandbox']['progress'])) {
$context['sandbox']['progress'] = 0;
$context['sandbox']['max'] = count($types);
}
if ($method == 'migrate') {
module_load_include('php', 'feedapi2feeds', 'feedapi2feeds');
$migration = new FeedAPI2Feeds();
$current_type = $types[$context['sandbox']['progress']++];
$migration->migrateType($current_type);
$msgs = $migration->getMessages();
foreach ($msgs as $msg) {
drupal_set_message($msg);
}
$context['message'] = t('Now processing %type', array('%type' => $current_type));
$context['finished'] = $context['sandbox']['progress'] / $context['sandbox']['max'];
}
}
/**
* Batch helper.
*
* @param $title
* Title to show to user when executing batch.
* @param $method
* Method to execute on importer; one of 'import', 'clear' or 'expire'.
* @param $importer_id
* Identifier of a FeedsImporter object.
* @param $feed_nid
* If importer is attached to content type, feed node id identifying the
* source to be imported.
*/
function feedapi2feeds_batch_set($title, $method, $types) {
$batch = array(
'title' => $title,
'operations' => array(
array('feedapi2feeds_batch', array($method, $types)),
),
);
batch_set($batch);
}