Skip to content

Commit aa75cb5

Browse files
committed
Keep track of articles to post relationships
1 parent a377a0b commit aa75cb5

File tree

4 files changed

+172
-39
lines changed

4 files changed

+172
-39
lines changed

jobs/GetFeedUpdates.php

Lines changed: 57 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -11,29 +11,30 @@
1111

1212
use sij\humhub\modules\rss\components\MarkdownHelper;
1313
use 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
*/
2020
class 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

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
use yii\db\Migration;
4+
5+
class m220202_180401_initial extends Migration
6+
{
7+
8+
public function up()
9+
{
10+
$this->createTable('rss_posts', [
11+
'id' => 'pk',
12+
'rss_link' => 'varchar(1024) NOT NULL',
13+
'post_id' => 'int(11) NOT NULL',
14+
], '');
15+
16+
// creates index for column `rss_link`
17+
// hopefully mysql is smart enaugh to handle varchar(1024) decently
18+
$this->createIndex(
19+
'idx-rss_posts-rss_link',
20+
'rss_posts',
21+
'rss_link',
22+
);
23+
}
24+
25+
public function down()
26+
{
27+
echo "m220202_180401_initial does not support migration down.\n";
28+
return false;
29+
}
30+
31+
}

migrations/uninstall.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
use yii\db\Migration;
4+
5+
class uninstall extends Migration
6+
{
7+
public function up()
8+
{
9+
$this->dropTable('rss_posts');
10+
}
11+
12+
public function down()
13+
{
14+
echo "uninstall does not support migration down.\n";
15+
return false;
16+
}
17+
18+
}

models/RssPosts.php

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
3+
namespace sij\humhub\modules\rss\models;
4+
5+
use Yii;
6+
use humhub\components\ActiveRecord;
7+
#use humhub\modules\user\models\User;
8+
#use humhub\modules\tasks\models\Task;
9+
10+
/**
11+
* This is the model class for table "rss_posts".
12+
*
13+
* The followings are the available columns in table 'rss_posts':
14+
* @property integer $id
15+
* @property string $rss_link
16+
* @property integer $post_id
17+
*/
18+
class RssPosts extends ActiveRecord
19+
{
20+
21+
/**
22+
* @return string the associated database table name
23+
*/
24+
public static function tableName()
25+
{
26+
return 'rss_posts';
27+
}
28+
29+
/**
30+
* @return array validation rules for model attributes.
31+
*/
32+
public function rules()
33+
{
34+
return [
35+
[ ['rss_link', 'post_id'], 'required' ],
36+
[ ['rss_link'], 'string' ],
37+
[ ['post_id'], 'integer' ],
38+
];
39+
}
40+
41+
/*
42+
public function getTask()
43+
{
44+
return $this->hasOne(Task::className(), ['id' => 'task_id']);
45+
}
46+
*/
47+
48+
/*
49+
public function afterSave($insert, $changedAttributes)
50+
{
51+
if ($insert) {
52+
if(version_compare(Yii::$app->version, '1.2', '<')) {
53+
$notification = new \humhub\modules\tasks\notifications\Assigned();
54+
$notification->source = $this->task;
55+
$notification->originator = Yii::$app->user->getIdentity();
56+
$notification->send($this->user);
57+
} else {
58+
\humhub\modules\tasks\notifications\Assigned::instance()->from(Yii::$app->user->getIdentity())->about($this->task)->send($this->user);
59+
}
60+
}
61+
62+
return parent::afterSave($insert, $changedAttributes);
63+
}
64+
*/
65+
66+
}

0 commit comments

Comments
 (0)