Skip to content

Instantly share code, notes, and snippets.

@sanhuang
Created June 21, 2018 01:07
Show Gist options
  • Save sanhuang/71ea2d7f3face2d80ddb8b40d0ecf81a to your computer and use it in GitHub Desktop.
Save sanhuang/71ea2d7f3face2d80ddb8b40d0ecf81a to your computer and use it in GitHub Desktop.
Discuz自動加標籤任務計劃

Discuz自動加標籤任務計劃

此功能在wetalk與herostory都有使用,Discuz計畫任務路徑在*/source/include/cron/*建立後的檔案可以從後台計畫任務設定排程執行 除基本需求外,包含判斷處理時間、發警示信件等附加需求做法。

<?php
/**
* 檔案路徑:/source/include/cron/cron_thread_autoadd_tags_daily.php
* 需求:
* 1. 建立標籤詞庫(人工或來自DZ系統發文時附加做法)
* 2. 取出一定數量論壇文章,比對過的文章會配置 pre_forum_thread 新欄位 cronaddtag 次數值加1(後續持續循環找出最小數值持續比對附加)
* 3. 根據特定規則(標題與內容搜尋)更新文章與標籤關聯,每篇最多5個標籤。
* 1. 文章標題符合標籤
* 2. 文章內文抓到符合出現3個以上同一標籤
* */
if(!defined('IN_DISCUZ')) {
exit('Access Denied');
}
date_default_timezone_set("Asia/Taipei");
/**
* 記錄檔儲存參數
* 1. 目錄夾
* 2. 檔名規則
* */
$log_path = DISCUZ_ROOT.'/data/plugindata/AUTOADDTAGS/'.date('Ymd');
if( !is_dir($log_path) ){
mkdir($log_path, 0777, true);
chmod($log_path, 0777);
}
$today = date("Ymd");
$log_filename = "tagRecord-".date('H-i').".txt";
/*設定參數*/
$lineString = [];
$limitNum = 350;
$startTime = microtime(true);
$endTime = "";
$lineString["limitNum"] = $limitNum;
$lineString["rowCount"] = 0;
// 處理時間超時(sec)
$toolong_time = 80;
$toMails = array("");
// 取出前100個 cronaddtag
$sql = "SELECT FT.tid, FT.subject, FT.cronaddtag, FP.message
FROM ".DB::table('forum_thread')." FT
LEFT JOIN ".DB::table('forum_post')." FP ON FT.tid=FP.tid AND FP.first=1
ORDER BY cronaddtag ASC
Limit ".$limitNum;
$rows=DB::fetch_all($sql);
// 取出目前現有標籤數
$tags=C::t('common_tag')->fetch_all_by_status(0);
foreach ($tags as $tag) {
$namearr[] = $tag['tagname'];
}
$strtag = implode("|",$namearr);
$class_tag = new tag();
foreach($rows as $i => $row){
$singleStartTime = microtime(true);
$matchtag=array();
if( preg_match_all("/$strtag/u", $row['subject'], $m1)){
foreach($m1[0] as $k => $tag){
if( $k < 5 ){
$matchtag[]=$tag;
}
}
}
// 處理內容過濾htmltag
$content=strip_tags($row['message']);
if( preg_match_all("/$strtag/u", $content, $m2)){
$content_tags= array_flip( array_count_values($m2[0]) );
krsort($content_tags);
foreach($content_tags as $times => $tag){
// 若以設定5個標籤則停止。
if( count($matchtag)==5 ){
break;
// 附加多的標籤與判斷比對到3次以上
}elseif( !in_array($tag, $matchtag) && $times >= 3){
$matchtag[]=$tag;
}
}
}
$lineString["msg"][$i]["subject"] = $row['subject'];
$lineString["msg"][$i]["newTag"] = implode(", ",$matchtag);
$sqlt = "SELECT t.tagname
FROM ".DB::table('common_tagitem')." AS ti, ".DB::table('common_tag')." AS t
WHERE ti.itemid = ".$row['tid']." AND ti.tagid = t.tagid
ORDER BY ti.tagid asc";
$rowt = DB::fetch_all($sqlt);
$oldTag = [];
if( $rowt ) {
foreach($rowt as $t){
$oldTag[] = $t["tagname"];
}
}else {
$oldTag = "";
}
$lineString["msg"][$i]["oldTag"] = implode(", ",$oldTag);
// if( count($matchtag) == 0 ){ continue; }
//比對新舊tag是否相同
$diffArr=array_intersect($matchtag, $oldTag);
if(count($diffArr) == 0) {
$lineString["diff"]++;
}else{
$lineString["same"]++;
}
$addtagstr = implode(',',$matchtag);
/**
* 如果原本已經有加入或設定過?
* 1. 完全排除自動加功能
* 2. 只先針對舊文設定一段時間之後都改手動加
* */
// update tags delete all set item first then add again with new set.
C::t('common_tagitem')->delete(0, $row['tid'], 'tid');
$tagarray = $class_tag->add_tag($addtagstr, $row['tid'], 'tid', 1);
// update cronaddtag field
$cronaddtag = ( empty($row['cronaddtag']) ) ? 0 : intval($row['cronaddtag']);
DB::update('forum_thread', ['cronaddtag'=>$cronaddtag+1], "tid={$row['tid']}");
$lineString["msg"][$i]["sub_totalTime"] = number_format((microtime(true) - $singleStartTime), 3, '.', '');
unset($row);
unset($matchtag);
unset($totalTime);
$lineString["rowCount"]++;
}
$lineString["totalTime"] = number_format((microtime(true) - $startTime), 3, '.', '');
$lineString["rowCount"] = $rowCount;
$lines_string .= "\r\n共花 " .number_format((microtime(true) - $startTime), 3, '.', '')." 秒\r\n";
$fp = fopen($log_path.DIRECTORY_SEPARATOR.$log_filename,'w');
fwrite($fp, "花費總時長: ".$lineString["totalTime"]." 秒\r\n");
fwrite($fp, "總數:".$lineString["limitNum"]." 筆 異動:".$lineString["diff"]." 筆 未異動:".$lineString["same"]." 筆 \r\n");
foreach ($lineString["msg"] as $key => $value) {
fwrite($fp, "\r\n==========================================\r\n");
fwrite($fp, $value["subject"]."\r\n");
fwrite($fp, "newTag: ".$value["newTag"]."\r\n");
fwrite($fp, "oldTag: ".$value["oldTag"]."\r\n");
fwrite($fp, "處理時間: ".$value["sub_totalTime"]."\r\n");
}
fclose($fp);
if($lineString["limitNum"] != ($lineString["diff"] + $lineString["same"]) || $lineString["totalTime"] > 9) {
include libfile('function/mail');
$dateInterval = "WeTalk論壇_autotag_alarm_".date("Y/m/d H:i", time());
$sendMsgs = array();
if($lineString["limitNum"] != ($lineString["diff"] + $lineString["same"])){
$sendMsgs[] = "<div><font color='#09C'>處理筆數錯誤</font></div>";
$sendMsgs[] = "<div>總數:".$lineString["limitNum"]." 筆 異動:".$lineString["diff"]." 筆 未異動:".$lineString["same"]." 筆 </div><br><br>";
}
if($lineString["totalTime"] > $toolong_time){
$sendMsgs[] = "<div><font color='#09C'>處理時間過長</font></div>";
$sendMsgs[] = "<div>花費總時長: ".$lineString["totalTime"]." 秒 </div><br><br>";
}
if( count($sendMsgs) > 0 ){
foreach ($toMails as $key => $toMail) {
if(!sendmail( $toMail, $dateInterval, implode("<BR/>",$sendMsgs)) ){
$errMsg[] = "send mail to ({$toMail}) error accured!";
}
}
}
if( count($errMsg) > 0 ){
echo implode("<BR/>", $errMsg);
echo $sendMsgs;
}else{
echo '<p style="display:none;">done!</p>';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment