Dotclear

source: plugins/periodical/_public.php @ 2329

Revision 2329, 5.3 KB checked in by JcDenis, 13 years ago (diff)

periodical 0.3

  • Switched to DC 2.2
  • Fixed simultaneous updates (uses flock)
  • Added twitter option when posts are published
Line 
1<?php
2# -- BEGIN LICENSE BLOCK ----------------------------------
3# This file is part of periodical, a plugin for Dotclear 2.
4#
5# Copyright (c) 2009-2010 JC Denis and contributors
6# jcdenis@gdwd.com
7#
8# Licensed under the GPL version 2.0 license.
9# A copy of this license is available in LICENSE file or at
10# http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
11# -- END LICENSE BLOCK ------------------------------------
12
13if (!defined('DC_RC_PATH')){return;}
14if (!in_array($core->url->type,array('default','feed'))) {return;}
15
16$core->addBehavior('publicBeforeDocument',array('publicBehaviorPeriodical','publishPeriodicalEntries'));
17
18class publicBehaviorPeriodical
19{
20     public static function publishPeriodicalEntries($core)
21     {
22          try
23          {
24               $per = new periodical($core);
25               $s = $core->blog->settings->periodical;
26               
27               $per->lockUpdate();
28               
29               # Get periods
30               $periods =  $core->auth->sudo(array($per,'getPeriods'));
31               
32               # No period
33               if ($periods->isEmpty())
34               {
35                    $per->unlockUpdate();
36                    return;
37               }
38               
39               $twitter_msg = periodicalLibDcTwitter::getMessage('periodical');
40               $now = dt::toUTC(time());
41               $posts_order = $s->periodical_pub_order;
42               if (!preg_match('/^(post_dt|post_creadt|post_id) (asc|desc)$/',$posts_order))
43               {
44                    $posts_order = 'post_dt asc';
45               }
46               $cur_period = $core->con->openCursor($core->prefix.'periodical');
47               
48               while($periods->fetch())
49               {
50                    # Check if period is ongoing
51                    $cur_tz = strtotime($periods->periodical_curdt);
52                    $end_tz = strtotime($periods->periodical_enddt);
53                    $now_tz = $now + dt::getTimeOffset($periods->periodical_tz,$now);
54                    if ($now_tz > $cur_tz && $now_tz < $end_tz)
55                    {
56                         $last_nb = 0;
57                         $last_tz = $cur_tz;
58                         
59                         $max_nb = $periods->periodical_pub_nb;
60                         $max_tz = $end_tz < $now_tz ? $end_tz : $now_tz;
61                         
62                         # Calculate nb of posts to get
63                         $loop_tz = $cur_tz;
64                         $limit = 0;
65                         try
66                         {
67                              while(1)
68                              {
69                                   if ($loop_tz > $max_tz)
70                                   {
71                                        break;
72                                   }
73                                   $loop_tz = $per->getNextTime($loop_tz,$periods->periodical_pub_int);
74                                   $limit += 1;
75                              }
76                         }
77                         catch (Exception $e) { }
78                         
79                         # If period need update
80                         if ($limit > 0)
81                         {
82                              # Get posts to publish related to this period
83                              $posts_params = array();
84                              $posts_params['periodical_id'] = $periods->periodical_id;
85                              $posts_params['post_status'] = '-2';
86                              $posts_params['order'] = $posts_order;
87                              $posts_params['limit'] = $limit * $max_nb;
88                              $posts_params['no_content'] = true;
89                              $posts =  $core->auth->sudo(array($per,'getPosts'),$posts_params);
90                             
91                              if (!$posts->isEmpty())
92                              {
93                                   $cur_post = $core->con->openCursor($core->prefix.'post');
94                                   
95                                   while($posts->fetch())
96                                   {
97                                        # Publish post with right date
98                                        $cur_post->clean();
99                                        $cur_post->post_status = 1;
100                                       
101                                        # Update post date with right date
102                                        if ($s->periodical_upddate)
103                                        {
104                                             $cur_post->post_dt = date('Y-m-d H:i:s',$last_tz);
105                                             $cur_post->post_tz = $periods->periodical_tz;
106                                        }
107                                        else
108                                        {
109                                             $cur_post->post_dt = $posts->post_dt;
110                                        }
111                                       
112                                        # Also update post url with right date
113                                        if ($s->periodical_updurl)
114                                        {
115                                             $cur_post->post_url = $core->blog->getPostURL('',$cur_post->post_dt,$posts->post_title,$posts->post_id);
116                                        }
117                                       
118                                        $cur_post->update(
119                                             'WHERE post_id = '.$posts->post_id.' '.
120                                             "AND blog_id = '".$core->con->escape($core->blog->id)."' "
121                                        );
122                                       
123                                        # Delete post relation to this period
124                                        $per->delPost($posts->post_id);
125                                       
126                                        $last_nb++;
127                                       
128                                        # Increment upddt if nb of publishing is to the max
129                                        if ($last_nb == $max_nb)
130                                        {
131                                             $last_tz = $per->getNextTime($last_tz,$periods->periodical_pub_int);
132                                             $last_nb = 0;
133                                        }
134                                        # Auto tweet
135                                        if ($twwiter_msg)
136                                        {
137                                             $shortposturl = periodicalLibDcTwitterSender::shorten($posts->getURL());
138                                             $shortposturl = $shortposturl ? $shortposturl : $posts->getURL();
139                                             $shortsiteurl = periodicalLibDcTwitterSender::shorten($core->blog->url);
140                                             $shortsiteurl = $shortsiteurl ? $shortsiteurl : $core->blog->url;
141                                             
142                                             $twitter_msg = str_replace(
143                                                  array('%posttitle%','%posturl%','%shortposturl%','%postauthor%','%sitetitle%','%siteurl%','%shortsiteurl%'),
144                                                  array($posts->post_title,$posts->getURL(),$shortposturl,$posts->getUserCN(),$core->blog->name,$core->blog->url,$shortsiteurl),
145                                                  $twitter_msg
146                                             );
147                                             if (!empty($twitter_msg))
148                                             {
149                                                  periodicalLibDcTwitter::sendMessage('periodical',$twitter_msg);
150                                             }
151                                        }
152                                   }
153                                   $core->blog->triggerBlog();
154                              }
155                         }
156                         
157                         # Update last published date of this period even if there's no post to publish
158                         $cur_period->clean();
159                         $cur_period->periodical_curdt = date('Y-m-d H:i:s',$loop_tz);
160                         $cur_period->update(
161                              'WHERE periodical_id = '.$periods->periodical_id.' '.
162                              "AND blog_id = '".$core->con->escape($core->blog->id)."' "
163                         );
164                    }
165               }
166               $per->unlockUpdate();
167          }
168          catch (Exception $e)
169          {
170               $per->unlockUpdate();
171               return;
172          }
173     }
174}
175?>
Note: See TracBrowser for help on using the repository browser.

Sites map