<?php
namespace application\collection\controller;
use application\admin\controller\auth as auth;
use ticky\page;
use ticky\collection;
class collection_content extends auth {
public function index() {
$search = array();
$ret = $this->db->page('collection_node', $search, 'nodeid', $this->p);
$this->assign('page', $ret['page']);
$this->assign('items', $ret['items']);
$this->display('collection_node_list');
}
public function add() {
if (isset($_POST['dosubmit'])) {
if (!$_POST['urlpage'])
showmsg('网址配置不能为空!');
$res = db('collection_node')->insert($_POST);
if ($res) {
showmsg(L('operation_success'), '/collection/collection_content/index', 1);
} else {
showmsg(L('operation_failure'));
}
} else {
$data['pagesize_start'] = 1;
$data['pagesize_end'] = 10;
$data['par_num'] = 1;
$data['sourcetype'] = 2;
$data['sourcecharset'] = 'gbk';
$data['title_rule'] = '<title>[内容]</title>';
$data['content_html_rule'] = '<div([^>]*)>[|]</div>[|]';
$data['down_attachment'] = 1;
$data['coll_order'] = 1;
$this->assign('item', $data);
$this->display('collection_node_add');
}
}
public function edit() {
if (isset($_POST['dosubmit'])) {
$id = isset($_GET['nodeid']) ? intval($_GET['nodeid']) : 0;
if (db('collection_node')->update($_POST, array('nodeid' => $id))) {
showmsg(L('operation_success'), '/collection/collection_content/index', 1);
} else {
showmsg(L('operation_failure'));
}
} else {
$id = isset($_GET['nodeid']) ? intval($_GET['nodeid']) : 0;
$data = db('collection_node')->where(array('nodeid' => $id))->find();
$this->assign('item', $data);
$this->display('collection_node_add');
}
}
public function collection_test() {
$id = isset($_GET['nodeid']) ? intval($_GET['nodeid']) : 0;
if (!$id) {
showmsg(L('lose_parameters'));
}
$data = db('collection_node')->where(array('nodeid' => $id))->find();
if ($data['urlpage'] == '') {
showmsg('网址配置不能为空!', 'stop');
}
if ($data['sourcetype'] == 1) {
$url = str_replace('(*)', $data['pagesize_start'], $data['urlpage']);
} else {
$url = $data['urlpage'];
}
$url_start = $data['url_start'];
$url_end = $data['url_end'];
if ($url_start == '' || $url_end == '') {
showmsg('列表区域配置不能为空!', 'stop');
}
$content = collection::get_content($url);
$content = collection::get_sub_content($content, $url_start, $url_end);
if ($content) {
if ($data['sourcecharset'] == 'gbk') {
$content = array_iconv($content);
}
$content = collection::get_all_url($content, $data['url_contain'], $data['url_except']);
$articleurl = isset($content['url'][0]) ? $content['url'][0] : '';
if (!empty($articleurl)) {
$article = collection::get_content($articleurl);
$article = collection::get_filter_html($article, $this->get_config($data));
if ($data['sourcecharset'] == 'gbk')
$article = array_iconv($article);
}else {
$article = '列表规则错误!';
}
} else {
$article = '列表规则错误!';
}
$this->assign('data', $data);
$this->assign('content', $content);
$this->assign('article', $article);
$this->display('collection_test');
}
public function collection_list_url() {
$id = isset($_GET['nodeid']) ? intval($_GET['nodeid']) : 0;
if (!$id)
showmsg(L('lose_parameters'));
$data = db('collection_node')->where(array('nodeid' => $id))->find();
if (!$data || $data['urlpage'] == '')
showmsg('网址配置不能为空!', 'stop');
if ($data['sourcetype'] == 1) {
$url = array();
for ($i = $data['pagesize_start']; $i <= $data['pagesize_end']; $i = $i + $data['par_num']) {
$url[] = str_replace('(*)', $i, $data['urlpage']);
}
} else {
$url[0] = $data['urlpage'];
}
$url_start = $data['url_start'];
$url_end = $data['url_end'];
if ($url_start == '' || $url_end == '')
showmsg('列表区域配置不能为空!', 'stop');
$i = $j = 0;
foreach ($url as $v) {
$content = collection::get_content($v);
$content = collection::get_sub_content($content, $url_start, $url_end);
if (!$content)
continue;
if ($data['sourcecharset'] == 'gbk')
$content = array_iconv($content);
$content = collection::get_all_url($content, $data['url_contain'], $data['url_except']);
if (!empty($content['url']))
foreach ($content['url'] as $k => $v) {
$r = db('collection_content')->field('url')->where(array('url' => $v))->find();
if (!$r) {
db('collection_content')->insert(array('nodeid' => $data['nodeid'], 'status' => 0, 'url' => $v, 'title' => $content['title'][$k]));
$j++;
} else {
$i++;
}
}
}
showmsg('操作成功,共去除' . $i . '条重复数据,新增' . $j . '条数据!');
}
public function collection_article_content() {
$id = isset($_GET['id']) ? intval($_GET['id']) : 0;
if (!$id)
showmsg(L('lose_parameters'), 'stop');
$page = isset($_GET['page']) ? intval($_GET['page']) : 1;
$total = isset($_GET['total']) ? intval($_GET['total']) : 0;
if (!$total)
$total = db('collection_content')->field('id, url')->where(array('nodeid' => $id, 'status' => 0))->total();
$total_page = ceil($total / 2);
$list = db('collection_content')->field('id, url')->where(array('nodeid' => $id, 'status' => 0))->order('id DESC')->limit('2')->select();
if (empty($list))
showmsg('没有找到网址列表,请先进行网址采集!', 'stop');
$data = db('collection_node')->field('sourcecharset,down_attachment,watermark,title_rule,title_html_rule,time_rule,time_html_rule,content_rule,content_html_rule')->where(array('nodeid' => $id))->find();
$i = 0;
foreach ($list as $v) {
$article = collection::get_content($v['url']);
if ($data['sourcecharset'] == 'gbk')
$article = array_iconv($article);
$article = collection::get_filter_html($article, $this->get_config($data));
if ($data['down_attachment'])
$article['content'] = grab_image($article['content'], collection::$url);
$collection_content->update(array('status' => 1, 'data' => array2string($article)), array('id' => $v['id']));
$i++;
}
if ($total_page > $page) {
showmsg('采集正在进行中,采集进度:' . ($i + ($page - 1) * 2) . '/' . $total, U('collection_article_content', array('id' => $id, 'page' => $page + 1, 'total' => $total)), 0.1);
} else {
db('collection_node')->update(array('lastdate' => NOW_TIME), array('nodeid' => $id));
showmsg('采集完成!', '/collection/collection_content/collection_list', 2);
}
}
public function collection_list() {
$id = isset($_GET['id']) ? intval($_GET['id']) : 0;
$nodeid = isset($_GET['nodeid']) ? intval($_GET['nodeid']) : 0;
$where = '1 = 1';
if (isset($_GET['dosubmit'])) {
$type = isset($_GET['type']) ? intval($_GET['type']) : 0;
$status = isset($_GET['status']) ? intval($_GET['status']) : 99;
$keyword = isset($_GET['keyword']) ? safe_replace($_GET['keyword']) : '';
if ($type && $keyword) {
if ($type == '1')
$where .= ' AND nodeid = \'' . $keyword . '\'';
else
$where .= ' AND title LIKE \'%' . $keyword . '%\'';
}
if ($status != 99) {
$where .= ' AND status = ' . $status;
}
}
$total = db('collection_content')->where($where)->total();
$page = new page($total, 10);
$data = db('collection_content')->where($where)->order('id DESC')->limit($page->limit())->select();
$this->assign('data', $data);
$this->assign('page', $page);
$this->display('collection_list');
}
}