<?php 
error_reporting(0);
ini_set('display_errors',0);
defined('BASEPATH') OR exit('No direct script access allowed');
//header("Content-Type: text/html; charset=UTF-8");

#include_once(APPPATH.'core/web/cm_exceptions.php');
class cron_ct extends CI_Controller{

	var $tb_name = 'news_from_christiantoday';  // db table name
	var $news_tb_name = 'news';
	var $limit = 10; // max # of crawled article to migrate data into news table.


	function get_file_name($fname) {
		if(trim($fname)!='') {
			$tmp = explode('/',$fname);
			$fname = trim(array_pop($tmp));
			return $fname;
		}
	}

	/*
	 * ct article crawler from rss link
	 */
	function index() {
		$rss_link = 'http://www.christiantoday.co.kr/rss/articles/topnews/all.rss';
		$rss = file_get_contents($rss_link);

		if($rss==false) {
			echo date("Y-m-d H:i:s")." can not crawl rss feed.\n";
			exit;
		}

		$this->load->database();

 		$dom = new DOMDocument;
		$dom->preserveWhiteSpace = false;
		$dom->loadXML($rss);
		$i = 0;
		$cnt = 0;
		$fail_cnt = 0;
		$ret = [];
		while(is_object($row = $dom->getElementsByTagName('item')->item($i))) {
			foreach($row->childNodes as $nodename) {
				$retdate = date("Y-m-d H:i:s");
				if($nodename->nodeName=='title') {
					$ret[$i]['title'] = $nodename->nodeValue;
				} else if($nodename->nodeName=='description') {
					$ret[$i]['description'] = $nodename->nodeValue;
				} else if($nodename->nodeName=='link') {
					$ret[$i]['link'] = $nodename->nodeValue;
				} else if($nodename->nodeName=='media:content') {
					$ret[$i]['image'] = $nodename->getAttribute('url');
				} else if($nodename->nodeName=='pubDate') {
					$ret[$i]['pubdate'] = $nodename->nodeValue;
					$regdate = strtotime($ret[$i]['pubdate']);
					if($regdate>0) {
						$regdate = date("Y-m-d H:i:s",$regdate);
					}
				}
			}

			// upload main image
			$dest_file_name = '';
			if($ret[$i]['image']!='') {
	            $upload_path = '/home/crossmap/files/news/'.date("Y")."/".date("m");
	            if (!is_dir($upload_path)) {
	                mkdir($upload_path, 0777, TRUE);
	            }
	            $fname = date("Ymd-His-").$this->get_file_name($ret[$i]['image']);
	            $dest_file_name = $upload_path.'/'.$fname;
	            if(file_put_contents($dest_file_name, file_get_contents($ret[$i]['image']))==false) {
	            	echo "\nfile not restored\n";
	            	$dest_file_name = '';
	            }
	        }

	        $dest_file_name = ($dest_file_name!='') ? str_replace('/home/crossmap','',$dest_file_name) : '';

			$sql = "INSERT IGNORE INTO ".$this->tb_name." (".
				   "title, content, main_img, ".
				   "origin_url, regdate) VALUES (".
				   "'".addslashes($ret[$i]['title'])."', '".addslashes($ret[$i]['description'])."', '".$dest_file_name."', ".
				   "'".$ret[$i]['link']."', '$regdate')";
			$return = $this->db->query($sql);
			if($return==true) {
				$cnt++;
			} else {
				$fail_cnt++;
			}
			$i++;
		}
		echo date("Y-m-d H:i:s").' / success : '.$cnt.' / fail cnt : '.$fail_cnt."\n";
	}

	function migrate_ct_article_to_news() {
		$this->load->database();
		$ten_days_before = date("Y-m-d",(time()-(60*60*24*10))).' 00:00:00';

		$sql = "SELECT idx, title, content, main_img, origin_url, regdate FROM ".$this->tb_name." ".
			   "WHERE registered_tf=0 AND crawldate>='$ten_days_before' ".
			   "ORDER BY idx ASC LIMIT ".$this->limit;
		$query = $this->db->query($sql);
		$cnt = 0;
		$fail_cnt = 0;
		foreach($query->result_array() as $row) {
			if($row['main_img']!='') {
				$row['content'] = nl2br(addslashes('<p style="line-height: 1.8;"><span style="font-family: 나눔고딕코딩, NanumGothicCoding, sans-serif; font-size: 9pt;"><img src="'.$row['main_img'].'" /></span></p><br style="clear:both;">'.$row['content']));
			}
			$sql = "INSERT INTO ".$this->news_tb_name." (".
				   "category_code, news_type, title, ".
				   "sub_title, main_img, press_idx, ".
				   "press_name, content, org_url, ".
				   "is_del, is_display) VALUES (".
				   "'12', 1, '".$row['title']."', ".
				   "'".$row['title']."', '".$row['main_img']."', 29, ".
				   "'크리스천투데이', '".$row['content']."', '".$row['origin_url']."', ".
				   "'N', 'N')";
			//echo $sql."\n";
			$ret = $this->db->query($sql);
			if($ret==true) {
				$idx = $row['idx'];
				$sql = "UPDATE ".$this->tb_name." SET ".
					   "registered_tf=1 WHERE idx='$idx'";
				$this->db->query($sql);
				$cnt++;
			}
			else {
				$fail_cnt++;
			//echo $sql."\n";
			exit;
			}
		}
		echo date("Y-m-d H:i:s").' / succeess : '.$cnt.' / fail cnt : '.$fail_cnt."\n";
	}
}
?>