/modules/news/cron/index.php

https://github.com/Sunvas/Eleanor-CMS · PHP · 92 lines · 77 code · 6 blank · 9 comment · 9 complexity · e76a4e05a54eff10dcc2055fbf4867b9 MD5 · raw file

  1. <?php
  2. /*
  3. Copyright © Eleanor CMS
  4. URL: http://eleanor-cms.ru, http://eleanor-cms.su, http://eleanor-cms.com, http://eleanor-cms.net, http://eleanor.su
  5. E-mail: support@eleanor-cms.ru, support@eleanor.su
  6. Developing: Alexander Sunvas*
  7. Interface: Rumin Sergey
  8. =====
  9. *Pseudonym
  10. */
  11. if(!defined('CMS'))die;
  12. global$Eleanor;
  13. $conf=include$Eleanor->module['path'].'config.php';
  14. if(Eleanor::$Cache->Get($conf['n'].'-runned')===false)
  15. {
  16. Eleanor::$Cache->Put($conf['n'].'-runned',true,100);
  17. $n=150;
  18. $ids=array();
  19. $R=Eleanor::$Db->Query('SELECT `id`,`date`,`pinned`,`tags` FROM `'.$conf['t'].'` WHERE `date`<=\''.date('Y-m-d H:i:s').'\' AND `status`=2 LIMIT '.$n);
  20. while($a=$R->fetch_assoc())
  21. {
  22. $n--;
  23. $upd=array(
  24. 'status'=>1,
  25. );
  26. if((int)$a['pinned'])
  27. $upd+=array(
  28. 'date'=>$a['pinned'],
  29. 'pinned'=>$a['date'],
  30. );
  31. $ids[]=$a['id'];
  32. Eleanor::$Db->Update($conf['t'],$upd,'`id`='.$a['id'].' LIMIT 1');
  33. if($a['tags'])
  34. {
  35. $a['tags']=explode(',,',trim($a['tags'],','));
  36. Eleanor::$Db->Insert($conf['rt'],array('id'=>array_fill(0,count($a['tags']),$a['id']),'tag'=>$a['tags']));
  37. Eleanor::$Db->Update($conf['tt'],array('!cnt'=>'`cnt`+1'),'`id`'.Eleanor::$Db->In($a['tags']));
  38. }
  39. }
  40. if($ids)
  41. Eleanor::$Db->Update($conf['tl'],array('lstatus'=>1),'`id`'.Eleanor::$Db->In($ids));
  42. if($n>0)
  43. {
  44. $ids=array();
  45. $R=Eleanor::$Db->Query('SELECT `id`,`date`,`pinned` FROM `'.$conf['t'].'` WHERE `enddate`BETWEEN \'0000-00-00 00:00:01\' AND \''.date('Y-m-d H:i:s').'\' AND `status`>0 LIMIT '.$n);
  46. while($a=$R->fetch_assoc())
  47. {
  48. $n--;
  49. $ids[]=$a['id'];
  50. $upd=array(
  51. 'status'=>0,
  52. );
  53. if((int)$a['pinned'])
  54. $upd+=array(
  55. 'date'=>$a['pinned'],
  56. 'pinned'=>$a['date'],
  57. );
  58. Eleanor::$Db->Update($conf['t'],$upd,'`id`='.$a['id'].' LIMIT 1');
  59. }
  60. if($ids)
  61. {
  62. $in=Eleanor::$Db->In($ids);
  63. Eleanor::$Db->Update($conf['tl'],array('lstatus'=>0),'`id`'.$in);
  64. $R=Eleanor::$Db->Query('SELECT `tag`,COUNT(`id`) `cnt` FROM `'.$conf['rt'].'` WHERE `id`'.$in.' GROUP BY `tag`');
  65. while($a=$R->fetch_assoc())
  66. Eleanor::$Db->Update($conf['tt'],array('!cnt'=>'GREATEST(0,`cnt`-'.$a['cnt'].')'),'`id`='.$a['tag'].' AND `cnt`>0');
  67. Eleanor::$Db->Delete($conf['rt'],'`id`'.$in);
  68. }
  69. }
  70. if($n>0)
  71. {
  72. $R=Eleanor::$Db->Query('SELECT `id`,`pinned` FROM `'.$conf['t'].'` WHERE `status`=1 AND `date`<=\''.date('Y-m-d H:i:s').'\' AND `pinned`!=\'0000-00-00 00:00:00\' LIMIT '.$n);
  73. while($a=$R->fetch_assoc())
  74. Eleanor::$Db->Update($conf['t'],array('date'=>$a['pinned'],'pinned'=>'0000-00-00 00:00:00'),'`id`='.$a['id'].' LIMIT 1');
  75. $R=Eleanor::$Db->Query('SELECT UNIX_TIMESTAMP(`date`) FROM `'.$conf['t'].'` WHERE `status`=1 AND `date`>\''.date('Y-m-d H:i:s').'\' AND `pinned`!=\'0000-00-00 00:00:00\' ORDER BY `date` ASC LIMIT 1');
  76. list($near)=$R->fetch_row();
  77. $near=(int)$near;
  78. $totomor=strtotime('+1 DAY',mktime(0,0,0));
  79. $near=$near>0 ? min($near,$totomor) : $totomor;
  80. $R=Eleanor::$Db->Query('SELECT UNIX_TIMESTAMP(`date`) FROM `'.$conf['t'].'` WHERE `status`=2 AND `date`>\''.date('Y-m-d H:i:s').'\' ORDER BY `date` ASC LIMIT 1');
  81. list($near2)=$R->fetch_row();
  82. Eleanor::$Cache->Put($conf['n'].'_nextrun',true,($near2>0 ? min($near,$near2) : $near2)-time());
  83. }
  84. Eleanor::$Cache->Obsolete($conf['n'].'-runned');
  85. }
  86. Start();