periodical/inc/class.periodical.php

432 lines
13 KiB
PHP
Raw Normal View History

2021-08-23 11:24:19 +00:00
<?php
2021-09-02 12:56:02 +00:00
/**
* @brief periodical, a plugin for Dotclear 2
*
2021-09-02 12:56:02 +00:00
* @package Dotclear
* @subpackage Plugin
*
2021-09-02 12:56:02 +00:00
* @author Jean-Christian Denis and contributors
*
2021-09-02 12:56:02 +00:00
* @copyright Jean-Christian Denis
* @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html
*/
if (!defined('DC_RC_PATH')) {
2021-08-23 22:52:29 +00:00
return;
}
2021-08-23 11:24:19 +00:00
class periodical
{
2021-08-23 22:52:29 +00:00
public $con;
protected $table;
protected $blog;
private $lock = null;
2022-11-14 19:55:22 +00:00
public function __construct()
2021-08-23 22:52:29 +00:00
{
2022-11-14 19:55:22 +00:00
$this->con = dcCore::app()->con;
2021-08-23 22:52:29 +00:00
2022-11-14 19:55:22 +00:00
$this->table = dcCore::app()->con->escape(dcCore::app()->prefix . 'periodical');
$this->blog = dcCore::app()->con->escape(dcCore::app()->blog->id);
2021-08-23 22:52:29 +00:00
}
public function openCursor()
{
return $this->con->openCursor($this->table);
}
# Get periods
public function getPeriods($params = [], $count_only = false)
{
if ($count_only) {
$q = 'SELECT count(T.periodical_id) ';
} else {
$q = 'SELECT T.periodical_id, T.periodical_type, ';
if (!empty($params['columns']) && is_array($params['columns'])) {
$q .= implode(', ', $params['columns']) . ', ';
}
$q .= 'T.periodical_title, T.periodical_tz, ' .
2021-08-23 22:52:29 +00:00
'T.periodical_curdt, T.periodical_enddt, ' .
'T.periodical_pub_int, T.periodical_pub_nb ';
}
$q .= 'FROM ' . $this->table . ' T ';
if (!empty($params['from'])) {
$q .= $params['from'] . ' ';
}
$q .= "WHERE T.blog_id = '" . $this->blog . "' ";
if (isset($params['periodical_type'])) {
if (is_array($params['periodical_type']) && !empty($params['periodical_type'])) {
$q .= 'AND T.periodical_type ' . $this->con->in($params['periodical_type']);
} elseif ($params['periodical_type'] != '') {
$q .= "AND T.periodical_type = '" . $this->con->escape($params['periodical_type']) . "' ";
}
} else {
$q .= "AND T.periodical_type = 'post' ";
}
if (!empty($params['periodical_id'])) {
if (is_array($params['periodical_id'])) {
array_walk($params['periodical_id'], create_function('&$v,$k', 'if($v!==null){$v=(integer)$v;}'));
} else {
$params['periodical_id'] = [(int) $params['periodical_id']];
2021-08-23 22:52:29 +00:00
}
$q .= 'AND T.periodical_id ' . $this->con->in($params['periodical_id']);
}
if (!empty($params['periodical_title'])) {
$q .= "AND T.periodical_title = '" . $this->con->escape($params['periodical_title']) . "' ";
}
if (!empty($params['sql'])) {
$q .= $params['sql'] . ' ';
2021-08-23 22:52:29 +00:00
}
if (!$count_only) {
if (!empty($params['order'])) {
$q .= 'ORDER BY ' . $this->con->escape($params['order']) . ' ';
2021-08-23 22:52:29 +00:00
} else {
$q .= 'ORDER BY T.periodical_id ASC ';
}
}
if (!$count_only && !empty($params['limit'])) {
$q .= $this->con->limit($params['limit']);
}
$rs = $this->con->select($q);
2022-11-14 19:55:22 +00:00
//$rs->core = dcCore::app();
//$rs->periodical = $this;
2021-08-23 22:52:29 +00:00
return $rs;
}
public function addPeriod($cur)
{
$this->con->writeLock($this->table);
try {
$id = $this->con->select(
'SELECT MAX(periodical_id) FROM ' . $this->table
)->f(0) + 1;
$cur->periodical_id = $id;
$cur->blog_id = $this->blog;
2021-08-23 22:52:29 +00:00
$cur->periodical_type = 'post';
$cur->periodical_tz = dcCore::app()->auth->getInfo('user_tz');
2021-08-23 22:52:29 +00:00
$cur->insert();
$this->con->unlock();
} catch (Exception $e) {
$this->con->unlock();
2021-08-23 22:52:29 +00:00
throw $e;
}
2021-08-23 22:52:29 +00:00
return $cur->periodical_id;
}
public function updPeriod($period_id, $cur)
2021-08-23 22:52:29 +00:00
{
$period_id = (int) $period_id;
2021-08-23 22:52:29 +00:00
if ($cur->periodical_tz == ''
2021-08-23 22:52:29 +00:00
&& ($cur->periodical_curdt != '' || $cur->periodical_enddt != '')) {
2022-11-14 19:55:22 +00:00
$cur->periodical_tz = dcCore::app()->auth->getInfo('user_tz');
2021-08-23 22:52:29 +00:00
}
$cur->update(
"WHERE blog_id = '" . $this->blog . "' " .
'AND periodical_id = ' . $period_id . ' '
2021-08-23 22:52:29 +00:00
);
}
# Delete a period
public function delPeriod($period_id)
{
$period_id = (int) $period_id;
2021-08-23 22:52:29 +00:00
$params = [];
2021-08-23 22:52:29 +00:00
$params['periodical_id'] = $period_id;
$params['post_status'] = '';
$rs = $this->getPosts($params);
2021-08-23 22:52:29 +00:00
if (!$rs->isEmpty()) {
throw new Exception('Periodical is not empty');
}
$this->con->execute(
'DELETE FROM ' . $this->table . ' ' .
"WHERE blog_id = '" . $this->blog . "' " .
'AND periodical_id = ' . $period_id . ' '
2021-08-23 22:52:29 +00:00
);
}
# Remove all posts related to a period
public function delPeriodPosts($period_id)
{
$params = [];
$params['post_status'] = '';
$params['periodical_id'] = (int) $period_id;
2021-08-23 22:52:29 +00:00
$rs = $this->getPosts($params);
if ($rs->isEmpty()) {
return;
}
$ids = [];
while ($rs->fetch()) {
2021-08-23 22:52:29 +00:00
$ids[] = $rs->post_id;
}
2021-08-23 23:32:27 +00:00
if (empty($ids)) {
2021-08-23 22:52:29 +00:00
return;
}
$this->con->execute(
2022-11-14 19:55:22 +00:00
'DELETE FROM ' . dcCore::app()->prefix . 'meta ' .
2021-08-23 22:52:29 +00:00
"WHERE meta_type = 'periodical' " .
'AND post_id ' . $this->con->in($ids)
2021-08-23 22:52:29 +00:00
);
}
# Get posts related to periods
public function getPosts($params = [], $count_only = false)
{
if (!isset($params['columns'])) {
$params['columns'] = [];
}
if (!isset($params['from'])) {
$params['from'] = '';
}
if (!isset($params['join'])) {
$params['join'] = '';
}
2021-08-23 22:52:29 +00:00
if (!isset($params['sql'])) {
$params['sql'] = '';
}
$params['columns'][] = 'T.periodical_id';
$params['columns'][] = 'T.periodical_title';
$params['columns'][] = 'T.periodical_type';
$params['columns'][] = 'T.periodical_tz';
$params['columns'][] = 'T.periodical_curdt';
$params['columns'][] = 'T.periodical_enddt';
$params['columns'][] = 'T.periodical_pub_int';
$params['columns'][] = 'T.periodical_pub_nb';
2022-11-14 19:55:22 +00:00
$params['join'] .= 'LEFT JOIN ' . dcCore::app()->prefix . 'meta R ON P.post_id = R.post_id ';
$params['join'] .= 'LEFT JOIN ' . $this->table . ' T ON CAST(T.periodical_id as char) = CAST(R.meta_id as char) ';
2021-08-23 22:52:29 +00:00
$params['sql'] .= "AND R.meta_type = 'periodical' ";
$params['sql'] .= "AND T.periodical_type = 'post' ";
if (!empty($params['periodical_id'])) {
if (is_array($params['periodical_id'])) {
array_walk($params['periodical_id'], function ($v) { if ($v !== null) { $v = (int) $v; } });
2021-08-23 22:52:29 +00:00
} else {
$params['periodical_id'] = [(int) $params['periodical_id']];
2021-08-23 22:52:29 +00:00
}
$params['sql'] .= 'AND T.periodical_id ' . $this->con->in($params['periodical_id']);
unset($params['periodical_id']);
}
2022-11-20 20:38:20 +00:00
if (dcCore::app()->auth->check(dcAuth::PERMISSION_ADMIN, dcCore::app()->blog->id)) {
2021-08-23 22:52:29 +00:00
if (isset($params['post_status'])) {
if ($params['post_status'] != '') {
$params['sql'] .= 'AND P.post_status = ' . (int) $params['post_status'] . ' ';
2021-08-23 22:52:29 +00:00
}
unset($params['post_status']);
}
} else {
$params['sql'] .= 'AND P.post_status = -2 ';
}
$rs = dcCore::app()->blog->getPosts($params, $count_only);
2021-08-23 22:52:29 +00:00
$rs->periodical = $this;
return $rs;
}
# Add post to periodical
public function addPost($period_id, $post_id)
{
$period_id = (int) $period_id;
$post_id = (int) $post_id;
2021-08-23 22:52:29 +00:00
# Check if exists
$rs = $this->getPosts(['post_id' => $post_id, 'periodical_id' => $period_id]);
2021-08-23 22:52:29 +00:00
if (!$rs->isEmpty()) {
return;
}
$cur = $this->con->openCursor(dcCore::app()->prefix . 'meta');
2022-11-14 19:55:22 +00:00
$this->con->writeLock(dcCore::app()->prefix . 'meta');
2021-09-02 12:56:02 +00:00
2021-08-23 22:52:29 +00:00
try {
$cur->post_id = $post_id;
$cur->meta_id = $period_id;
2021-08-23 22:52:29 +00:00
$cur->meta_type = 'periodical';
$cur->insert();
$this->con->unlock();
} catch (Exception $e) {
$this->con->unlock();
2021-08-23 22:52:29 +00:00
throw $e;
}
}
# Delete post from periodical
public function delPost($post_id)
{
$post_id = (int) $post_id;
2021-08-23 22:52:29 +00:00
$this->con->execute(
2022-11-14 19:55:22 +00:00
'DELETE FROM ' . dcCore::app()->prefix . 'meta ' .
2021-08-23 22:52:29 +00:00
"WHERE meta_type = 'periodical' " .
"AND post_id = '" . $post_id . "' "
);
2021-08-23 22:52:29 +00:00
return true;
}
# Remove all posts without pending status from periodical
public function cleanPosts($period_id = null)
{
$params = [];
2021-08-23 22:52:29 +00:00
$params['post_status'] = '';
$params['sql'] = 'AND post_status != -2 ';
2021-08-23 22:52:29 +00:00
if ($period_id !== null) {
$params['periodical_id'] = (int) $period_id;
2021-08-23 22:52:29 +00:00
}
$rs = $this->getPosts($params);
if ($rs->isEmpty()) {
return;
}
$ids = [];
while ($rs->fetch()) {
2021-08-23 22:52:29 +00:00
$ids[] = $rs->post_id;
}
if (empty($ids)) {
return;
}
2021-09-02 12:56:02 +00:00
2021-08-23 22:52:29 +00:00
$this->con->execute(
2022-11-14 19:55:22 +00:00
'DELETE FROM ' . dcCore::app()->prefix . 'meta ' .
2021-08-23 22:52:29 +00:00
"WHERE meta_type = 'periodical' " .
'AND post_id ' . $this->con->in($ids)
2021-08-23 22:52:29 +00:00
);
}
public static function getTimesCombo()
{
2021-08-23 23:32:27 +00:00
return [
__('Hourly') => 'hour',
2021-08-23 22:52:29 +00:00
__('twice a day') => 'halfday',
__('Daily') => 'day',
__('Weekly') => 'week',
__('Monthly') => 'month',
2021-08-23 22:52:29 +00:00
];
}
2021-09-02 12:56:02 +00:00
2021-08-23 22:52:29 +00:00
public static function getNextTime($ts, $period)
{
$ts = (int) $ts;
$e = explode(',', date('H,i,s,n,j,Y', $ts));
switch($period) {
2021-08-23 22:52:29 +00:00
case 'hour':
$new_ts = mktime($e[0] + 1, $e[1], $e[2], $e[3], $e[4], $e[5]);
break;
2021-08-23 22:52:29 +00:00
case 'halfday':
$new_ts = mktime($e[0] + 12, $e[1], $e[2], $e[3], $e[4], $e[5]);
break;
2021-08-23 22:52:29 +00:00
case 'day':
$new_ts = mktime($e[0], $e[1], $e[2], $e[3], $e[4] + 1, $e[5]);
break;
2021-08-23 22:52:29 +00:00
case 'week':
$new_ts = mktime($e[0], $e[1], $e[2], $e[3], $e[4] + 7, $e[5]);
break;
2021-08-23 22:52:29 +00:00
case 'month':
$new_ts = mktime($e[0], $e[1], $e[2], $e[3] + 1, $e[4], $e[5]);
break;
2021-08-23 22:52:29 +00:00
default:
$new_ts = 0;
throw new Exception(__('Unknow frequence'));
break;
2021-08-23 22:52:29 +00:00
}
2021-08-23 22:52:29 +00:00
return $new_ts;
}
# Lock a file to see if an update is ongoing
public function lockUpdate()
{
try {
# Need flock function
if (!function_exists('flock')) {
throw new Exception("Can't call php function named flock");
2021-08-23 22:52:29 +00:00
}
# Cache writable ?
if (!is_writable(DC_TPL_CACHE)) {
throw new Exception("Can't write in cache fodler");
}
# Set file path
$f_md5 = md5($this->blog);
2021-08-23 22:52:29 +00:00
$cached_file = sprintf(
'%s/%s/%s/%s/%s.txt',
DC_TPL_CACHE,
'periodical',
substr($f_md5, 0, 2),
substr($f_md5, 2, 2),
$f_md5
);
# Real path
$cached_file = path::real($cached_file, false);
# Make dir
if (!is_dir(dirname($cached_file))) {
files::makeDir(dirname($cached_file), true);
2021-08-23 22:52:29 +00:00
}
# Make file
if (!file_exists($cached_file)) {
!$fp = @fopen($cached_file, 'w');
if ($fp === false) {
throw new Exception("Can't create file");
2021-08-23 22:52:29 +00:00
}
fwrite($fp, '1', strlen('1'));
fclose($fp);
}
# Open file
if (!($fp = @fopen($cached_file, 'r+'))) {
throw new Exception("Can't open file");
2021-08-23 22:52:29 +00:00
}
# Lock file
if (!flock($fp, LOCK_EX)) {
throw new Exception("Can't lock file");
2021-08-23 22:52:29 +00:00
}
$this->lock = $fp;
2021-08-23 22:52:29 +00:00
return true;
} catch (Exception $e) {
throw $e;
}
2021-08-23 22:52:29 +00:00
return false;
}
public function unlockUpdate()
{
@fclose($this->lock);
$this->lock = null;
}
}