| 1 |
<?php
|
| 2 |
// $Id: advogato_import.module,v 1.7 2008/03/19 02:15:15 deekayen Exp $
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @file
|
| 6 |
* Imports diary entries to Drupal from Advogato.org XMLRPC
|
| 7 |
*
|
| 8 |
* @author David Kent Norman
|
| 9 |
* @link http://deekayen.net/
|
| 10 |
* @link http://www.advogato.org/xmlrpc.html
|
| 11 |
* @copyright Copyright 2006, David Kent Norman
|
| 12 |
* @todo make option to delete diary entries from Advogato after imported to Drupal
|
| 13 |
*/
|
| 14 |
|
| 15 |
/**
|
| 16 |
* Implementation of hook_menu().
|
| 17 |
*
|
| 18 |
* Displays a tab in users' account area.
|
| 19 |
*
|
| 20 |
* @return array
|
| 21 |
*/
|
| 22 |
function advogato_import_menu() {
|
| 23 |
$items = array();
|
| 24 |
|
| 25 |
$items['admin/settings/advogato_import'] = array(
|
| 26 |
'title' => 'Advogato import',
|
| 27 |
'description' => 'Set default import locations, settings, and limits.',
|
| 28 |
'page callback' => 'drupal_get_form',
|
| 29 |
'page arguments' => array('advogato_import_admin_settings'),
|
| 30 |
'access callback' => 'user_access',
|
| 31 |
'access arguments' => array('administer site configuration'),
|
| 32 |
'type' => MENU_NORMAL_ITEM
|
| 33 |
);
|
| 34 |
$items['user/%user/advogato_import'] = array(
|
| 35 |
'title' => 'Advogato import',
|
| 36 |
'page callback' => 'drupal_get_form',
|
| 37 |
'page arguments' => array('advogato_import_diary_user', 'user'),
|
| 38 |
'type' => MENU_LOCAL_TASK,
|
| 39 |
'access callback' => 'variable_get',
|
| 40 |
'access arguments' => array('advogato_import_configured', FALSE),
|
| 41 |
'weight' => 2
|
| 42 |
);
|
| 43 |
|
| 44 |
return $items;
|
| 45 |
}
|
| 46 |
|
| 47 |
/**
|
| 48 |
* Administration variables must be set before users can queue imports
|
| 49 |
*
|
| 50 |
* @return array
|
| 51 |
*/
|
| 52 |
function advogato_import_admin_settings() {
|
| 53 |
$form = array();
|
| 54 |
$form['advogato_import_cron_limit'] = array(
|
| 55 |
'#type' => 'select',
|
| 56 |
'#title' => t('Import limit'),
|
| 57 |
'#default_value' => variable_get('advogato_import_cron_limit', 15),
|
| 58 |
'#options' => drupal_map_assoc(array(0, 15, 30, 50, 100, 250, 500)),
|
| 59 |
'#description' => t('The maximum number of diary entries to import from Advogato in a single cron run. Set to 0 for unlimited imports. Set this lower if your cron is timing out or if PHP is running out of memory.')
|
| 60 |
);
|
| 61 |
$form['advogato_import_node_type'] = array(
|
| 62 |
'#type' => 'select',
|
| 63 |
'#title' => t('Default node type'),
|
| 64 |
'#default_value' => variable_get('advogato_import_node_type', 'blog'),
|
| 65 |
'#options' => drupal_map_assoc(array('blog', 'page', 'story')),
|
| 66 |
'#description' => t('This module has not checked to see if the options are enabled in the modules control panel. Please verify the node type you select is a node type your site supports.')
|
| 67 |
);
|
| 68 |
|
| 69 |
$form['default_options'] = array(
|
| 70 |
'#type' => 'fieldset',
|
| 71 |
'#title' => t('Default options')
|
| 72 |
);
|
| 73 |
$form['default_options']['advogato_import_status'] = array(
|
| 74 |
'#type' => 'checkbox',
|
| 75 |
'#title' => t('Published'),
|
| 76 |
'#default_value' => variable_get('advogato_import_status', 1)
|
| 77 |
);
|
| 78 |
$form['default_options']['advogato_import_moderation_queue'] = array(
|
| 79 |
'#type' => 'checkbox',
|
| 80 |
'#title' => t('In moderation queue'),
|
| 81 |
'#default_value' => variable_get('advogato_import_moderation_queue', 0)
|
| 82 |
);
|
| 83 |
$form['default_options']['advogato_import_promoted'] = array(
|
| 84 |
'#type' => 'checkbox',
|
| 85 |
'#title' => t('Promoted to front page'),
|
| 86 |
'#default_value' => variable_get('advogato_import_promoted', 0)
|
| 87 |
);
|
| 88 |
$form['default_options']['advogato_import_sticky'] = array(
|
| 89 |
'#type' => 'checkbox',
|
| 90 |
'#title' => t('Sticky at top of lists'),
|
| 91 |
'#default_value' => variable_get('advogato_import_sticky', 0)
|
| 92 |
);
|
| 93 |
|
| 94 |
$form['user_comments'] = array(
|
| 95 |
'#type' => 'fieldset',
|
| 96 |
'#title' => t('User comments')
|
| 97 |
);
|
| 98 |
$form['user_comments']['advogato_import_comment'] = array(
|
| 99 |
'#type' => 'radios',
|
| 100 |
'#default_value' => variable_get('advogato_import_comment', 0),
|
| 101 |
'#options' => array(0 => t('Disabled'), 1 => t('Read only'), 2 => t('Read/write'))
|
| 102 |
);
|
| 103 |
|
| 104 |
/* not really sure how/if this should be implemented - DKN
|
| 105 |
$form['advogato_import_format'] = array(
|
| 106 |
'#type' => 'radios',
|
| 107 |
'#default_value' => variable_get('advogato_import_format', 0),
|
| 108 |
'#options' => array(0 => t('Filtered HTML'), 1 => t('PHP code'), 2 => t('Full HTML'))
|
| 109 |
);
|
| 110 |
*/
|
| 111 |
$form['advogato_import_xmlrpc'] = array(
|
| 112 |
'#type' => 'textfield',
|
| 113 |
'#title' => t('Advogato XMLRPC interface'),
|
| 114 |
'#default_value' => variable_get('advogato_import_xmlrpc', 'http://www.advogato.org/XMLRPC'),
|
| 115 |
'#size' => 70,
|
| 116 |
'#maxlength' => 255,
|
| 117 |
'#description' => t('Location of Advogato\'s XMLRPC interface. This should not need changing, but if Advogato changes, this should make it so you don\'t have to edit the module source.')
|
| 118 |
);
|
| 119 |
$form['advogato_import_configured'] = array(
|
| 120 |
'#type' => 'hidden',
|
| 121 |
'#value' => 1
|
| 122 |
);
|
| 123 |
return system_settings_form($form);
|
| 124 |
}
|
| 125 |
|
| 126 |
/**
|
| 127 |
* Accepts username, and diary entry numbers to the import queue
|
| 128 |
*/
|
| 129 |
function advogato_import_diary_user($user) {
|
| 130 |
$edit = array(
|
| 131 |
'username' => '',
|
| 132 |
'startentry' => '',
|
| 133 |
'endentry' => ''
|
| 134 |
);
|
| 135 |
$result = db_query('SELECT username, startentry, endentry FROM {advogato_import} WHERE uid = %d', $user->uid);
|
| 136 |
while ($record = db_fetch_array($result)) {
|
| 137 |
if (!empty($record['username'])) {
|
| 138 |
$edit = $record;
|
| 139 |
}
|
| 140 |
}
|
| 141 |
unset($record, $result);
|
| 142 |
|
| 143 |
$form = array();
|
| 144 |
$form['advogato_import'] = array(
|
| 145 |
'#type' => 'fieldset',
|
| 146 |
'#title' => t('Import Advogato diary'),
|
| 147 |
'#description' => t('Queue import of diary entries from Advogato.org. Entries will be added in chunks during cron jobs. Advogato diary entries are numbered starting at 0 (zero).')
|
| 148 |
);
|
| 149 |
$form['advogato_import']['username'] = array(
|
| 150 |
'#type' => 'textfield',
|
| 151 |
'#title' => t('Advogato username'),
|
| 152 |
'#default_value' => isset($edit['username']) ? $edit['username'] : '',
|
| 153 |
'#size' => 20,
|
| 154 |
'#maxlength' => 50,
|
| 155 |
'#required' => TRUE,
|
| 156 |
'#weight' => 1
|
| 157 |
);
|
| 158 |
$form['advogato_import']['startentry'] = array(
|
| 159 |
'#type' => 'textfield',
|
| 160 |
'#title' => t('Start entry'),
|
| 161 |
'#default_value' => $edit['startentry'],
|
| 162 |
'#size' => 5,
|
| 163 |
'#maxlength' => 5,
|
| 164 |
'#required' => TRUE,
|
| 165 |
'#weight' => 2
|
| 166 |
);
|
| 167 |
$form['advogato_import']['endentry'] = array(
|
| 168 |
'#type' => 'textfield',
|
| 169 |
'#title' => t('End entry'),
|
| 170 |
'#default_value' => $edit['endentry'],
|
| 171 |
'#size' => 5,
|
| 172 |
'#maxlength' => 5,
|
| 173 |
'#required' => TRUE,
|
| 174 |
'#weight' => 3
|
| 175 |
);
|
| 176 |
$form['advogato_import']['queue_import'] = array(
|
| 177 |
'#type' => 'submit',
|
| 178 |
'#value' => t('Queue Import'),
|
| 179 |
'#weight' => 20
|
| 180 |
);
|
| 181 |
return $form;
|
| 182 |
}
|
| 183 |
|
| 184 |
function advogato_import_diary_user_validate($form, &$form_status) {
|
| 185 |
if (!$form_status['values']['username']) {
|
| 186 |
form_set_error('username', t('You must enter a username.'));
|
| 187 |
}
|
| 188 |
if (!isset($form_status['values']['startentry']) || !is_numeric($form_status['values']['startentry'])) {
|
| 189 |
form_set_error('startentry', t('You must enter a starting entry.'));
|
| 190 |
}
|
| 191 |
if (!isset($form_status['values']['endentry']) || !is_numeric($form_status['values']['endentry'])) {
|
| 192 |
form_set_error('endentry', t('You must enter an ending entry.'));
|
| 193 |
}
|
| 194 |
}
|
| 195 |
|
| 196 |
function advogato_import_diary_user_submit($form, &$form_status) {
|
| 197 |
global $user;
|
| 198 |
db_query("INSERT INTO {advogato_import} (uid, username, startentry, endentry) VALUES (%d, '%s', '%s', '%s')", $user->uid, $form_status['values']['username'], $form_status['values']['startentry'], $form_status['values']['endentry']);
|
| 199 |
|
| 200 |
drupal_set_message(t('Your diary import request has been queued.'));
|
| 201 |
|
| 202 |
drupal_goto("user/$user->uid");
|
| 203 |
}
|
| 204 |
|
| 205 |
/**
|
| 206 |
* Implementation of hook_cron().
|
| 207 |
*
|
| 208 |
* This function could benefit from a transactional DB
|
| 209 |
*/
|
| 210 |
function advogato_import_cron() {
|
| 211 |
if (variable_get('advogato_import_configured', 0)) {
|
| 212 |
|
| 213 |
$result = db_result(db_query('SELECT COUNT(qid) FROM {advogato_import}'));
|
| 214 |
if ($result > 0) {
|
| 215 |
$loop_limit = $cron_limit = (int)variable_get('advogato_import_cron_limit', 0);
|
| 216 |
$xmlrpc_addr = variable_get('advogato_import_xmlrpc', 'http://www.advogato.org/XMLRPC');
|
| 217 |
$node->status = variable_get('advogato_import_status', 0); // 1 = published
|
| 218 |
$node->type = variable_get('advogato_import_node_type', 'blog');
|
| 219 |
$node->moderate = variable_get('advogato_import_moderation_queue', 0);
|
| 220 |
$node->promote = variable_get('advogato_import_promoted', 0);
|
| 221 |
$node->sticky = variable_get('advogato_import_sticky', 0);
|
| 222 |
$node->comment = variable_get('advogato_import_comment', 0); // 0 = disabled
|
| 223 |
$node->format = 3; // Drupal's default for Full HTML; Advogato already did filtering
|
| 224 |
$node->files = array();
|
| 225 |
|
| 226 |
$result = db_query('SELECT qid, uid, username, startentry, endentry, ABS(endentry - startentry) AS difference FROM {advogato_import}');
|
| 227 |
while ($queue = db_fetch_object($result)) {
|
| 228 |
$account = user_load(array('uid' => $queue->uid));
|
| 229 |
$node->name = $account->name;
|
| 230 |
$node->uid = $queue->uid;
|
| 231 |
|
| 232 |
$queue->startentry = (int)$queue->startentry; // xmlrpc is picky about getting strings where it wants integers
|
| 233 |
|
| 234 |
$abs = (int)$queue->difference;
|
| 235 |
$asc = $queue->startentry < $queue->endentry ? TRUE : FALSE;
|
| 236 |
do {
|
| 237 |
// fetch entry
|
| 238 |
$entry = xmlrpc($xmlrpc_addr, 'diary.get', $queue->username, $queue->startentry);
|
| 239 |
$date = xmlrpc($xmlrpc_addr, 'diary.getDates', $queue->username, $queue->startentry);
|
| 240 |
|
| 241 |
// assign entry vars
|
| 242 |
$node->created = mktime($date[0]->hour, $date[0]->minute, $date[0]->second, $date[0]->month, $date[0]->day, $date[0]->year);
|
| 243 |
$node->changed = mktime($date[1]->hour, $date[1]->minute, $date[1]->second, $date[1]->month, $date[1]->day, $date[1]->year);
|
| 244 |
$node->title = strftime("%d %b %Y", $node->created);
|
| 245 |
$node->teaser = $node->body = str_replace("\n", ' ', $entry); // Drupal does nl2br by default
|
| 246 |
|
| 247 |
// write to DB
|
| 248 |
unset($node->nid, $node->vid); // if nid isn't reset, node_save will just overwrite the previous loop's save to the old nid
|
| 249 |
|
| 250 |
// node_submit() would probably be more "proper", but it calls
|
| 251 |
// drupal_set_message() on each execution, which can be messy
|
| 252 |
// on users' next page load with lots of imports.
|
| 253 |
node_save($node);
|
| 254 |
|
| 255 |
// node_submit() normally calls this
|
| 256 |
watchdog('content', '%type: added %title.',
|
| 257 |
array('%type' => $node->type, '%title' => $node->title),
|
| 258 |
WATCHDOG_INFO, l('view', "node/$node->nid"));
|
| 259 |
|
| 260 |
// update queue
|
| 261 |
$asc === TRUE ? $queue->startentry++ : $queue->startentry--;
|
| 262 |
db_query('UPDATE {advogato_import} SET startentry = %d WHERE qid = %d', $queue->startentry, $queue->qid);
|
| 263 |
$abs--;
|
| 264 |
$loop_limit--;
|
| 265 |
} while (($cron_limit === 0 xor $loop_limit > 0) && $abs >= 0);
|
| 266 |
|
| 267 |
if (($asc === TRUE && $queue->startentry > $queue->endentry) || ($asc === FALSE && $queue->startentry < $queue->endentry)) {
|
| 268 |
db_query('DELETE FROM {advogato_import} WHERE qid = %d', $queue->qid);
|
| 269 |
}
|
| 270 |
}
|
| 271 |
}
|
| 272 |
}
|
| 273 |
|
| 274 |
db_query('DELETE FROM {advogato_import} WHERE ABS(endentry - startentry) = 0');
|
| 275 |
}
|