-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathget_deepflow_content.module
More file actions
109 lines (89 loc) · 4.12 KB
/
get_deepflow_content.module
File metadata and controls
109 lines (89 loc) · 4.12 KB
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
<?php
/**
* Implements hook_menu().
*/
function get_deepflow_content_menu() {
$items = array();
// 配置页面
$items['admin/config/deepflow/server'] = array(
'title' => 'Deepflow配置',
'description' => '配置Deepflow内容获取模块',
'page callback' => 'drupal_get_form',
'page arguments' => array('get_deepflow_content_settings_form'),
'access arguments' => array('administer get deepflow content'),
'type' => MENU_LOCAL_TASK,
);
// 导入页面
$items['admin/content/deepflow/import'] = array(
'title' => '导入Deepflow内容',
'description' => '从Deepflow导入内容',
'page callback' => 'drupal_get_form',
'page arguments' => array('get_deepflow_content_import_form'),
'access arguments' => array('administer get deepflow content'),
'type' => MENU_LOCAL_TASK,
);
return $items;
}
/**
* 加载权限定义
*/
module_load_include('inc', 'get_deepflow_content', 'get_deepflow_content.permissions');
/**
* 加载管理表单
*/
module_load_include('inc', 'get_deepflow_content', 'get_deepflow_content.admin');
function get_deepflow_content_cron(){
$config = variable_get('get_deepflow_content_settings', array());
$server = isset($config['server']) ? $config['server'] : '';
$key = isset($config['key']) ? $config['key'] : '';
$token = isset($config['token']) ? $config['token'] : '';
$type = isset($config['type']) ? $config['type'] : '';
$image_field = isset($config['image_field']) ? $config['image_field'] : '';
$body_field = isset($config['body_field']) ? $config['body_field'] : '';
$brief_field = isset($config['brief_field']) ? $config['brief_field'] : '';
$keywords_field = isset($config['keywords_field']) ? $config['keywords_field'] : '';
if (empty($server) || empty($key) || empty($token) || empty($type)) {
watchdog('get_deepflow_content', '配置不完整,无法执行cron任务', array(), WATCHDOG_ERROR);
return;
}
$signature_client = hash('sha512', $key . $token);
// 对URL参数进行编码
$url = $server . '/personal-admin/website/publisher/content?token=' . urlencode($token) . '&signature=' . urlencode($signature_client);
// 使用drupal_http_request()替代file_get_contents(),更好的错误处理
$response = drupal_http_request($url);
// 检查请求是否成功
if ($response->code != 200) {
watchdog('get_deepflow_content', '获取内容失败,HTTP状态码: @code', array('@code' => $response->code), WATCHDOG_ERROR);
return;
}
$content_arr = json_decode($response->data);
// 检查JSON解析是否成功
if (json_last_error() !== JSON_ERROR_NONE) {
watchdog('get_deepflow_content', '解析JSON失败: @error', array('@error' => json_last_error_msg()), WATCHDOG_ERROR);
return;
}
// 检查返回数据结构是否正确
if (!isset($content_arr->node_count) || $content_arr->node_count <= 0) {
watchdog('get_deepflow_content', '没有新内容需要导入', array(), WATCHDOG_INFO);
return;
}
// 检查data属性是否存在且是数组
if (!isset($content_arr->data) || !is_array($content_arr->data)) {
watchdog('get_deepflow_content', '返回数据格式不正确,缺少data数组', array(), WATCHDOG_ERROR);
return;
}
foreach ($content_arr->data as $record) {
// 检查记录是否包含必要字段
if (!isset($record->title, $record->body)) {
watchdog('get_deepflow_content', '内容记录缺少必要字段', array(), WATCHDOG_WARNING);
continue;
}
$title = $record->title;
$body = isset($record->body) ? $record->body : '';
$brief = isset($record->brief) ? $record->brief : '';
$keywords = isset($record->keywords) ? $record->keywords : '';
$image_path = isset($record->image) ? $record->image : '';
get_deepflow_content_save_node($title, $body, $brief, $keywords, $image_path, $type, $body_field, $image_field, $brief_field, $keywords_field);
}
watchdog('get_deepflow_content', '成功导入 @count 条内容', array('@count' => $content_arr->node_count), WATCHDOG_INFO);
}