| 19 |
* |
* |
| 20 |
* @return array |
* @return array |
| 21 |
*/ |
*/ |
| 22 |
function advogato_import_menu($may_cache) { |
function advogato_import_menu() { |
| 23 |
$items = array(); |
$items = array(); |
| 24 |
|
|
| 25 |
if (!$may_cache) { |
$items['admin/settings/advogato_import'] = array( |
| 26 |
$items[] = array( |
'title' => 'Advogato import', |
| 27 |
'path' => 'admin/settings/advogato_import', |
'description' => 'Set default import locations, settings, and limits.', |
| 28 |
'title' => t('Advogato import'), |
'page callback' => 'drupal_get_form', |
| 29 |
'description' => t('Set default import locations, settings, and limits.'), |
'page arguments' => array('advogato_import_admin_settings'), |
| 30 |
'callback' => 'drupal_get_form', |
'access callback' => 'user_access', |
| 31 |
'callback arguments' => array('advogato_import_admin_settings'), |
'access arguments' => array('administer site configuration'), |
| 32 |
'access' => user_access('administer site configuration'), |
'type' => MENU_NORMAL_ITEM |
| 33 |
'type' => MENU_NORMAL_ITEM, |
); |
| 34 |
); |
$items['user/%user/advogato_import'] = array( |
| 35 |
} |
'title' => 'Advogato import', |
| 36 |
if (arg(0) == 'user' && is_numeric(arg(1))) { |
'page callback' => 'drupal_get_form', |
| 37 |
$items[] = array( |
'page arguments' => array('advogato_import_diary_user', 'user'), |
| 38 |
'path' => 'user/'. arg(1) .'/advogato_import', |
'type' => MENU_LOCAL_TASK, |
| 39 |
'title' => t('Advogato import'), |
'access callback' => 'variable_get', |
| 40 |
'callback' => 'drupal_get_form', |
'access arguments' => array('advogato_import_configured', FALSE), |
| 41 |
'callback arguments' => array('advogato_import_diary_user'), |
'weight' => 2 |
| 42 |
'type' => MENU_LOCAL_TASK, |
); |
|
'access' => variable_get('advogato_import_configured', false), |
|
|
'weight' => 2 |
|
|
); |
|
|
} |
|
| 43 |
|
|
| 44 |
return $items; |
return $items; |
| 45 |
} |
} |
| 126 |
/** |
/** |
| 127 |
* Accepts username, and diary entry numbers to the import queue |
* Accepts username, and diary entry numbers to the import queue |
| 128 |
*/ |
*/ |
| 129 |
function advogato_import_diary_user() { |
function advogato_import_diary_user($user) { |
|
global $user; |
|
| 130 |
$edit = array( |
$edit = array( |
| 131 |
'username' => '', |
'username' => '', |
| 132 |
'startentry' => '', |
'startentry' => '', |
| 181 |
return $form; |
return $form; |
| 182 |
} |
} |
| 183 |
|
|
| 184 |
function advogato_import_diary_user_validate($form, &$form_values) { |
function advogato_import_diary_user_validate($form, &$form_status) { |
| 185 |
if (!$form_values['username']) { |
if (!$form_status['values']['username']) { |
| 186 |
form_set_error('username', t('You must enter a username.')); |
form_set_error('username', t('You must enter a username.')); |
| 187 |
} |
} |
| 188 |
if (!isset($form_values['startentry']) || !is_numeric($form_values['startentry'])) { |
if (!isset($form_status['values']['startentry']) || !is_numeric($form_status['values']['startentry'])) { |
| 189 |
form_set_error('startentry', t('You must enter a starting entry.')); |
form_set_error('startentry', t('You must enter a starting entry.')); |
| 190 |
} |
} |
| 191 |
if (!isset($form_values['endentry']) || !is_numeric($form_values['endentry'])) { |
if (!isset($form_status['values']['endentry']) || !is_numeric($form_status['values']['endentry'])) { |
| 192 |
form_set_error('endentry', t('You must enter an ending entry.')); |
form_set_error('endentry', t('You must enter an ending entry.')); |
| 193 |
} |
} |
| 194 |
} |
} |
| 195 |
|
|
| 196 |
function advogato_import_diary_user_submit($form, &$form_values) { |
function advogato_import_diary_user_submit($form, &$form_status) { |
| 197 |
global $user; |
global $user; |
| 198 |
db_query("INSERT INTO {advogato_import} (uid, username, startentry, endentry) VALUES (%d, '%s', '%s', '%s')", $user->uid, $form_values['username'], $form_values['startentry'], $form_values['endentry']); |
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.')); |
drupal_set_message(t('Your diary import request has been queued.')); |
| 201 |
|
|
| 210 |
function advogato_import_cron() { |
function advogato_import_cron() { |
| 211 |
if (variable_get('advogato_import_configured', 0)) { |
if (variable_get('advogato_import_configured', 0)) { |
| 212 |
|
|
| 213 |
$result = db_query('SELECT qid, uid, username, startentry, endentry, ABS(endentry - startentry) AS difference FROM {advogato_import}'); |
$result = db_result(db_query('SELECT COUNT(qid) FROM {advogato_import}')); |
| 214 |
if (db_num_rows($result) > 0) { |
if ($result > 0) { |
| 215 |
$loop_limit = $cron_limit = (int)variable_get('advogato_import_cron_limit', 0); |
$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'); |
$xmlrpc_addr = variable_get('advogato_import_xmlrpc', 'http://www.advogato.org/XMLRPC'); |
| 217 |
$node->status = variable_get('advogato_import_status', 0); // 1 = published |
$node->status = variable_get('advogato_import_status', 0); // 1 = published |
| 223 |
$node->format = 3; // Drupal's default for Full HTML; Advogato already did filtering |
$node->format = 3; // Drupal's default for Full HTML; Advogato already did filtering |
| 224 |
$node->files = array(); |
$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)) { |
while ($queue = db_fetch_object($result)) { |
| 228 |
$account = user_load(array('uid' => $queue->uid)); |
$account = user_load(array('uid' => $queue->uid)); |
| 229 |
$node->name = $account->name; |
$node->name = $account->name; |
| 232 |
$queue->startentry = (int)$queue->startentry; // xmlrpc is picky about getting strings where it wants integers |
$queue->startentry = (int)$queue->startentry; // xmlrpc is picky about getting strings where it wants integers |
| 233 |
|
|
| 234 |
$abs = (int)$queue->difference; |
$abs = (int)$queue->difference; |
| 235 |
$asc = $queue->startentry < $queue->endentry ? true : false; |
$asc = $queue->startentry < $queue->endentry ? TRUE : FALSE; |
| 236 |
do { |
do { |
| 237 |
// fetch entry |
// fetch entry |
| 238 |
$entry = xmlrpc($xmlrpc_addr, 'diary.get', $queue->username, $queue->startentry); |
$entry = xmlrpc($xmlrpc_addr, 'diary.get', $queue->username, $queue->startentry); |
| 245 |
$node->teaser = $node->body = str_replace("\n", ' ', $entry); // Drupal does nl2br by default |
$node->teaser = $node->body = str_replace("\n", ' ', $entry); // Drupal does nl2br by default |
| 246 |
|
|
| 247 |
// write to DB |
// write to DB |
| 248 |
unset($node->nid); // if nid isn't reset, node_save will just overwrite the previous loop's save to the old nid |
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 |
// node_submit() would probably be more "proper", but it calls |
| 251 |
// drupal_set_message() on each execution, which can be messy |
// drupal_set_message() on each execution, which can be messy |
| 252 |
// on users' next page load with lots of imports. |
// on users' next page load with lots of imports. |
| 253 |
var_dump(node_save($node)); |
node_save($node); |
| 254 |
|
|
| 255 |
// node_submit() normally calls this |
// node_submit() normally calls this |
| 256 |
watchdog('content', t('%type: added %title.', |
watchdog('content', '%type: added %title.', |
| 257 |
array('%type' => theme('placeholder', t($node->type)), '%title' => theme('placeholder', $node->title))), |
array('%type' => $node->type, '%title' => $node->title), |
| 258 |
WATCHDOG_NOTICE, l(t('view'), "node/$node->nid")); |
WATCHDOG_INFO, l('view', "node/$node->nid")); |
| 259 |
|
|
| 260 |
// update queue |
// update queue |
| 261 |
$asc === true ? $queue->startentry++ : $queue->startentry--; |
$asc === TRUE ? $queue->startentry++ : $queue->startentry--; |
| 262 |
db_query('UPDATE {advogato_import} SET startentry = %d WHERE qid = %d', $queue->startentry, $queue->qid); |
db_query('UPDATE {advogato_import} SET startentry = %d WHERE qid = %d', $queue->startentry, $queue->qid); |
| 263 |
$abs--; |
$abs--; |
| 264 |
$loop_limit--; |
$loop_limit--; |
| 265 |
} while (($cron_limit === 0 xor $loop_limit > 0) && $abs >= 0); |
} 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)) { |
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); |
db_query('DELETE FROM {advogato_import} WHERE qid = %d', $queue->qid); |
| 269 |
} |
} |
| 270 |
} |
} |