function fb_post_cron in Drupal for Facebook 7.4
File
- ./
fb_post.module, line 650
Code
function fb_post_cron() {
$debug = FALSE;
// debug
$throttle = variable_get('fb_post_cron_throttle', 1);
$token = variable_get(FB_VAR_ADMIN_ACCESS_TOKEN, NULL);
$do_comments = variable_get('fb_post_cross_post_comments', FALSE);
if ($throttle && $token && $do_comments) {
$result = db_query("SELECT * FROM {fb_post_graph} ORDER BY last_cron");
if ($debug) {
print "<pre>";
}
if ($debug) {
print_r($GLOBALS['user']);
}
// just checking who cron is run as.
foreach ($result as $item) {
try {
if ($debug) {
print_r($item);
}
if ($item->entity_type == 'node') {
$info = fb_graph($item->graph_id, $token);
if ($debug) {
print_r($info);
}
// @TODO - respect comment privacy???
if ($info['comments']['count']) {
$node = node_load($item->entity_id);
foreach ($info['comments']['data'] as $fb_comment) {
$result2 = db_query("SELECT * FROM {fb_post_graph} WHERE graph_id=:graph_id", array(
':graph_id' => $fb_comment['id'],
));
$item2 = $result2
->fetchObject();
if ($item2->entity_id) {
// The comment is already saved locally.
}
else {
// Add the comment to drupal.
if ($debug) {
print_r($fb_comment);
}
$author_info = fb_graph($fb_comment['from']['id'], $token);
if ($debug) {
print_r($author_info);
}
$fake_comment = new stdClass();
$fake_comment->nid = $node->nid;
$fake_comment->name = $fb_comment['from']['name'];
$fake_comment->homepage = $author_info['link'];
// @TODO - get created date from $fb_comment['created_time']
$fake_comment->language = LANGUAGE_NONE;
$fake_comment->comment_body[$fake_comment->language][0]['value'] = $fb_comment['message'];
$fake_state = array(
'values' => array(
'op' => t('Save'),
),
);
// Give us permission
$old_perm = user_access('post comments');
// Must call user_access to make sure its not empty.
$perm =& drupal_static('user_access');
if ($debug) {
print_r($perm);
}
$perm[$GLOBALS['user']->uid]['post comments'] = TRUE;
if ($debug) {
print_r(drupal_static('user_access'));
}
$result3 = drupal_form_submit("comment_node_{$node->type}_form", $fake_state, $fake_comment);
$perm[$GLOBALS['user']->uid]['post comments'] = $old_perm;
if ($debug) {
print_r($result3);
}
if ($debug) {
print_r($fake_state);
}
if ($cid = $fake_state['values']['cid']) {
// The comment was saved.
db_query("INSERT INTO {fb_post_graph} (entity_id, entity_type, graph_id, actor_id, last_cron) VALUES (:entity_id, :entity_type, :graph_id, :actor_id, :last_cron)", array(
':entity_id' => $cid,
':entity_type' => 'comment',
':graph_id' => $fb_comment['id'],
':actor_id' => $fb_comment['from']['id'],
':last_cron' => REQUEST_TIME,
));
}
}
}
}
}
// end if entity_type == node
// Mark item as visited during this cron job.
db_query("UPDATE {fb_post_graph} SET last_cron = :last_cron WHERE graph_id=:graph_id", array(
':last_cron' => REQUEST_TIME,
':graph_id' => $item->graph_id,
));
} catch (Exception $e) {
// Could be just an expired token.
$message = '%function failed to process %type %id. %reason';
$args = array(
'%function' => __FUNCTION__,
'%type' => $item->entity_type,
'%id' => $item->entity_id,
'%reason' => $e
->getMessage(),
);
watchdog('fb', $message, $args, WATCHDOG_WARNING);
}
}
// end foreach $result as $item.
if ($debug) {
print "</pre>";
}
}
}