1111
1212use sij \humhub \modules \rss \components \MarkdownHelper ;
1313use sij \humhub \modules \rss \components \RssElement ;
14- use sij \humhub \modules \rss \controllers \ RssController ;
14+ use sij \humhub \modules \rss \models \ RssPosts ;
1515
1616/**
1717 * Reads the RSS Feed URL for this space
1818 * and adds any new posts into the stream.
1919 */
2020class GetFeedUpdates extends ActiveJob
2121{
22+ /**
23+ * @var string mutex to acquire
24+ */
25+ const MUTEX_ID = 'rss-queue ' ;
2226
2327 public $ space ; # humhub\modules\space\models\Space
2428 public $ force ; # when true, ignore any cached values (used when config changed)
2529
2630 private $ logFileHandle ;
2731
2832 # configuration settings
29- private $ cfg_url ; # the URL of the RSS feed
30- private $ cfg_article ; # 'summary' or 'full'
31- private $ cfg_pictures ; # 'yes' or 'no'
32- private $ cfg_maxwidth ; # maximum width of pictures
33+ private $ cfg_url ; # the URL of the RSS feed
34+ private $ cfg_article ; # 'summary' or 'full'
35+ private $ cfg_pictures ; # 'yes' or 'no'
36+ private $ cfg_maxwidth ; # maximum width of pictures
3337 private $ cfg_maxheight ; # maximum height of pictures
34- private $ cfg_owner ; # user id of the owner of the posts
35- private $ cfg_dayshistory ; # maximum number of days history
36- private $ cfg_daysfuture ; # maximum number of days into the future
3738
3839 private $ created_by ; # int: user id number
3940 private $ rss_folder ; # string: full name of the folder to store the RSS feed
@@ -46,25 +47,18 @@ class GetFeedUpdates extends ActiveJob
4647 private $ feed_copyright ; # string: copyright information for this RSS feed
4748 private $ article ; # string: markdown document representing HTML feed item body
4849 private $ now ; # DateTime: the current time
49- private $ oldest ; # oldest date we are accepting
50- private $ newest ; # newest date we are accepting
5150 private $ items ; # array of sij\humhub\modules\rss\components\RssElement keyed by pubDate
5251
53- /**
54- * @var string mutex to acquire
55- */
56- const MUTEX_ID = 'rss-queue ' ;
57-
5852 private function log ($ message ) {
5953 if ( $ this ->logFileHandle ) {
6054 fwrite ($ this ->logFileHandle , $ message );
6155 fflush ($ this ->logFileHandle );
6256 }
6357 }
6458
65- /**
66- * Creates a new Post in the Space
67- */
59+ /**
60+ * Creates a new Post in the Space
61+ */
6862 private function postError ($ title , $ info )
6963 {
7064
@@ -89,26 +83,31 @@ private function postError($title, $info)
8983
9084 }
9185
92- /**
93- * Creates a new Post in the Space
94- */
95- private function postMessage ($ message , $ datePublished = false )
86+ /**
87+ * Creates a new Post in the Space
88+ */
89+ private function postMessage ($ message , $ link = false , $ datePublished = false )
9690 {
97-
9891 $ post = null ;
9992
93+ // find previous version of the post via db
94+ if ( $ link ) {
95+ $ url2id = RssPosts::findOne (['rss_link ' => $ link ]);
96+ if ( $ url2id !== null )
97+ $ post = Post::findOne ($ url2id ->post_id );
98+ }
99+
100100 // attempt to locate a previous version of the post
101- if ( $ datePublished ) {
101+ // guess this should go some day - themroc
102+ if ( $ post === null && $ datePublished ) {
102103 $ stamp = $ datePublished ->format ("Y-m-d H:i:s " );
103104 $ oldContent = Content::findAll ([
104105 'contentcontainer_id ' => $ this ->space ->contentcontainer_id ,
105- // 'created_by' => $this->created_by, // cant rely on this field if config changed
106+ 'created_by ' => $ this ->created_by ,
106107 'created_at ' => $ stamp ,
107108 ]);
108109 if ( count ($ oldContent ) == 1 ) {
109110 $ post = Post::findOne ($ oldContent [0 ]->object_id );
110- $ this ->log ("\n\n### update Post \n" );
111- Console::stdout ("RSS queue: updating post... " );
112111 }
113112 }
114113
@@ -117,6 +116,19 @@ private function postMessage($message, $datePublished = false)
117116 $ post = new Post ($ this ->space );
118117 $ this ->log ("\n\n### new Post \n" );
119118 Console::stdout ("RSS queue: creating new post... " );
119+ } else {
120+ if ( $ datePublished ) {
121+ if ($ stamp > $ post ->created_at ) {
122+ $ this ->log ("\n\n### update Post \n" );
123+ Console::stdout ("RSS queue: updating post... " );
124+ } else {
125+ return ; // not changed
126+ }
127+ } else {
128+ if ( $ post !== null ) {
129+ return ; // we assume it hasn't changed - better miss an update than rewrite the post every time.
130+ }
131+ }
120132 }
121133
122134 $ post ->created_by =
@@ -134,6 +146,13 @@ private function postMessage($message, $datePublished = false)
134146 $ post ->save ();
135147 $ this ->log (print_r ($ post , true ));
136148
149+ if (! $ url2id ) {
150+ $ url2id = new RssPosts ();
151+ $ url2id ->rss_link = $ link ;
152+ $ url2id ->post_id = $ post ->id ;
153+ $ url2id ->save ();
154+ }
155+
137156 // make it look like the space post was created at the same time as the RSS article
138157 // note $post->save() always sets the time stamps to "now"
139158 if ( $ datePublished ) {
@@ -189,10 +208,11 @@ private function parseNewsItem($item)
189208 $ datePublished = false ;
190209 }
191210
192- // if the published date is known, skip the item if it is out of range
211+ // check if published date is known and in the future
193212 if ( $ datePublished ) {
194- if ( $ datePublished < $ this ->oldest ) return ;
195- if ( $ datePublished > $ this ->newest ) return ;
213+ if ( $ datePublished > $ this ->now ) {
214+ return ; // don't publish it here if it's not time yet
215+ }
196216 }
197217
198218 // choose which version of the article to use
@@ -259,7 +279,7 @@ private function parseNewsItem($item)
259279 }
260280
261281 // post the message in the stream
262- $ this ->postMessage ($ message , $ datePublished );
282+ $ this ->postMessage ($ message , $ link , $ datePublished );
263283
264284 }
265285
@@ -453,35 +473,33 @@ private function downloadNewsFeed()
453473 */
454474 public function run ()
455475 {
456- if (! Yii::$ app ->mutex ->acquire (static ::MUTEX_ID )) {
476+ if (!Yii::$ app ->mutex ->acquire (static ::MUTEX_ID )) {
457477 Console::stdout ("RSS queue execution skipped - already running! \n" );
458478 return ;
459479 }
460480
481+ # Yii::warning('Running RSS queue');
482+ Yii::error ('Running RSS queue ' );
483+
461484####### $this->logFileHandle = fopen(dirname(__FILE__) . '/log.txt', 'w');
462485
463486 $ this ->cfg_url = $ this ->space ->getSetting ('url ' , 'rss ' );
464487 $ this ->cfg_article = $ this ->space ->getSetting ('article ' , 'rss ' , 'summary ' );
465488 $ this ->cfg_pictures = $ this ->space ->getSetting ('pictures ' , 'rss ' , 'yes ' );
466489 $ this ->cfg_maxwidth = (int )$ this ->space ->getSetting ('maxwidth ' , 'rss ' , '500 ' );
467490 $ this ->cfg_maxheight = (int )$ this ->space ->getSetting ('maxheight ' , 'rss ' , '500 ' );
468- $ this ->cfg_owner = $ this ->space ->getSetting ('owner ' , 'rss ' , '' );
469- $ this ->cfg_dayshistory = (int )$ this ->space ->getSetting ('dayshistory ' , 'rss ' , '31 ' );
470- $ this ->cfg_daysfuture = (int )$ this ->space ->getSetting ('daysfuture ' , 'rss ' , '1 ' );
471491
472492 MarkdownHelper::$ cfg_pictures = $ this ->cfg_pictures ;
473493 MarkdownHelper::$ cfg_maxwidth = $ this ->cfg_maxwidth ;
474494 MarkdownHelper::$ cfg_maxheight = $ this ->cfg_maxheight ;
475495
476- $ this ->created_by = RssController:: vetOwner ( $ this ->cfg_owner , $ this -> space )-> id ;
496+ $ this ->created_by = $ this ->space -> created_by ;
477497
478498 $ this ->rss_folder = Yii::getAlias ('@runtime/rss ' );
479499 $ this ->rss_file = $ this ->rss_folder . '/ ' . $ this ->space ->guid . '.xml ' ;
480500 $ this ->new_file = $ this ->rss_folder . '/ ' . $ this ->space ->guid . '.new ' ;
481501
482- $ this ->now = new \DateTime ('now ' );
483- $ this ->oldest = (new \DateTime ('now ' ))->sub (new \DateInterval ('P ' . $ this ->cfg_dayshistory . 'D ' ));
484- $ this ->newest = (new \DateTime ('now ' ))->add (new \DateInterval ('P ' . $ this ->cfg_daysfuture . 'D ' ));
502+ $ this ->now = new \DateTime ("now " );
485503
486504 $ this ->log ("\n\n### run at " . print_r ($ this ->now , true ));
487505
0 commit comments