将 UCenter 事件导入 Discuz! X1.5 家园

最初做站便是从 Discuz! 开始,还记得当年用的版本是6.0,论坛关闭以后便没再继续使用。两年多过去了,现在的版本已经发展到了Discuz! X1.5。可以说 Discuz! X1.5 将 UCHome 和 Discuz! 论坛很好的整合在了一起。

UCenter 对于站长来说是个很好的平台和工具,利用 UCenter 可以将很多程序的会员系统整合在一起,不仅仅是会员系统,还包括用户事件(feed)、短消息等。

一直使用 UCenter 整合使用 hdwiki、phpcms 等程序,最近开始测试 Discuz! X1.5。一段时间后发现 UCenter 后台中的用户事件积累了很长的列表,没有通知到 Discuz! X 的家园。到dz论坛去找答案,发现很多人有我同样的问题,不过都没有很好的办法解决,无奈之下,只好自己动手。

从论坛中得知 UCHome 可以显示 UCenter 中其他应用的事件,研究了相关代码后发现,UCHome 通过程序的计划任务定时获取 UCenter 中的事件并导入到 UCHome 数据库,某个目录下 cron/getfeed.php 的脚本,而 Discuz! X 的计划任务中则没有这一项。

那么我们只需要改造一下这个脚本,让它在 Discuz! X 下工作就可以了,还是不废话了,直接贴出源码:

<?php
if(!defined(‘IN_DISCUZ’)) {
exit(‘Access Denied’);
}

//从uc获取feed
include_once(DISCUZ_ROOT.’./config/config_UCenter.php’);
include_once(DISCUZ_ROOT.’./uc_client/client.php’);

//去掉slassh
function sstripslashes($string) {
if(is_array($string)) {
foreach($string as $key => $val) {
$string[$key] = sstripslashes($val);
}
} else {
$string = stripslashes($string);
}
return $string;
}

if($results = uc_feed_get(10)) {//每次取10个

$cols = array(‘uid’,’username’,’appid’,’icon’,’dateline’,’hash_template’,’hash_data’,’title_template’,’title_data’,’body_template’,’body_data’,’body_general’,’image_1′,’image_1_link’,’image_2′,’image_2_link’,’image_3′,’image_3_link’,’image_4′,’image_4_link’,’target_ids’);

$inserts = array();
foreach ($results as $value) {
if(empty($value[‘uid’]) || empty($value[‘username’])) continue;

$vs = array();
foreach ($cols as $key) {
if(is_array($value[$key])) {
//数组处理
$value[$key] = addslashes(serialize(sstripslashes($value[$key])));
} else {
$value[$key] = addslashes(sstripslashes($value[$key]));
}
$vs[] = ‘\”.$value[$key].’\”;
}
$inserts[] = ‘(‘.implode(‘,’, $vs).’)’;
}

//入库
if($inserts) {
DB::query(“INSERT INTO “.DB::table(‘home_feed’).” (`”.implode(‘`,`’, $cols).”`) VALUES “.implode(‘,’, $inserts));
}
}
?>

保存上面的代码另存为 getfeed.php ,上传至 /source/include/cron/ ,再到后台去添加一个自定义的计划任务,可以自由设定多久执行一次。

下载地址:https://www.wilf.cn/zb_users/upload/2011/2/getfeed.rar

最后说说dz的计划任务,这不是linux的cron,而是一个伪cron,wordpress中也有类似的伪cron,没有进一步仔细研究这个计划任务是怎样实现的。

10 thoughts to “将 UCenter 事件导入 Discuz! X1.5 家园”

  1. 不能用,有错误,一运行就报错:

    UCenter info: MySQL Query Error
    SQL:SELECT * FROM [Table]notelist WHERE closed=’0′ AND app1<‘1′ AND app1>’-5′ LIMIT 1
    Error:Unknown column ‘app1’ in ‘where clause’
    Errno:1054

回复 西楚中国风 取消回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注