From 95de011a2cc81201d936425bffa7149bd9369203 Mon Sep 17 00:00:00 2001 From: Mark Hamstra Date: Fri, 7 Jul 2017 11:58:11 +0100 Subject: [PATCH 1/3] Load the dashboard feeds (news/security) over AJAX to prevent remote delays from slowing down the manager --- core/lexicon/en/default.inc.php | 1 + .../system/dashboard/widget/feed.class.php | 82 +++++++++++++++++++ .../modext/widgets/modx.panel.welcome.js | 33 ++++++++ .../default/dashboard/widget.modx-news.php | 26 +----- .../dashboard/widget.modx-security.php | 26 +----- 5 files changed, 122 insertions(+), 46 deletions(-) create mode 100644 core/model/modx/processors/system/dashboard/widget/feed.class.php diff --git a/core/lexicon/en/default.inc.php b/core/lexicon/en/default.inc.php index caedb754d54..4aced5ccc4a 100644 --- a/core/lexicon/en/default.inc.php +++ b/core/lexicon/en/default.inc.php @@ -147,6 +147,7 @@ $_lang['error'] = 'Error'; $_lang['error_sending_email'] = 'Error sending email'; $_lang['error_sending_email_to'] = 'Error while sending mail to '; +$_lang['error_loading_feed'] = 'An error occurred loading the feed.'; $_lang['event_id'] = 'Event Id'; $_lang['existing_category'] = 'Existing Category'; $_lang['expand_all'] = 'Expand All'; diff --git a/core/model/modx/processors/system/dashboard/widget/feed.class.php b/core/model/modx/processors/system/dashboard/widget/feed.class.php new file mode 100644 index 00000000000..34ede7a2ec7 --- /dev/null +++ b/core/model/modx/processors/system/dashboard/widget/feed.class.php @@ -0,0 +1,82 @@ +html. + */ +class modDashboardWidgetFeedProcessor extends modProcessor +{ + /** + * @var modRSSParser + */ + protected $rss; + + public function process() + { + $feed = $this->getProperty('feed', 'news'); + if (!in_array($feed, array('news', 'security'), true)) { + return $this->failure('Invalid feed type'); + } + + sleep(5); + $enabled = $this->modx->getOption('feed_modx_' . $feed . '_enabled', null, true); + if (!$enabled) { + return $this->failure(); + } + + $url = $this->modx->getOption('feed_modx_' . $feed); + // Check if the domain is valid before attempting to load it + $feedHost = parse_url($url, PHP_URL_HOST); + if ($feedHost && function_exists('checkdnsrr') && !checkdnsrr($feedHost, 'A')) { + return $this->failure(); + } + + return $this->loadFeed($url); + } + + public function loadFeed($url) + { + $this->modx->loadClass('xmlrss.modRSSParser','',false,true); + $this->rss = new modRSSParser($this->modx); + + $o = array(); + $rss = $this->rss->parse($url); + if (is_object($rss)) { + foreach (array_keys($rss->items) as $key) { + $item= &$rss->items[$key]; + $item['pubdate'] = strftime('%c',$item['date_timestamp']); + $o[] = $this->getFileChunk('dashboard/rssitem.tpl',$item); + } + } + return $this->success('', array('html' => implode("\n",$o))); + } + + /** + * @param string $tpl + * @param array $placeholders + * @return string + */ + public function getFileChunk($tpl,array $placeholders = array()) { + $output = ''; + $file = $tpl; + if (!file_exists($file)) { + $file = $this->modx->getOption('manager_path').'templates/'.$this->modx->getOption('manager_theme',null,'default').'/'.$tpl; + } + if (!file_exists($file)) { + $file = $this->modx->getOption('manager_path').'templates/default/'.$tpl; + } + if (file_exists($file)) { + /** @var modChunk $chunk */ + $chunk = $this->modx->newObject('modChunk'); + $chunk->setCacheable(false); + $tplContent = file_get_contents($file); + $chunk->setContent($tplContent); + $output = $chunk->process($placeholders); + } + return $output; + } +} + +return 'modDashboardWidgetFeedProcessor'; diff --git a/manager/assets/modext/widgets/modx.panel.welcome.js b/manager/assets/modext/widgets/modx.panel.welcome.js index 3b014cfcc12..eb26cf87e72 100644 --- a/manager/assets/modext/widgets/modx.panel.welcome.js +++ b/manager/assets/modext/widgets/modx.panel.welcome.js @@ -33,7 +33,40 @@ Ext.extend(MODx.panel.Welcome, MODx.Panel,{ if (this.config.dashboard && this.config.dashboard.hide_trees) { Ext.getCmp('modx-layout').hideLeftbar(false); } + var newsContainer = Ext.get('modx-news-feed-container'); + if (newsContainer) { + this.loadFeed(newsContainer, 'news'); + } + var securityContainer = Ext.get('modx-security-feed-container'); + if (securityContainer) { + this.loadFeed(securityContainer, 'security'); + } MODx.fireEvent('ready'); + }, + + loadFeed: function(container, feed) { + MODx.Ajax.request({ + url: MODx.config.connector_url + '?action=system/dashboard/widget/feed&feed=' + feed + ,listeners: { + success: { + fn: function(response) { + if (response.success) { + container.update(response.object.html); + } + else if (response.message.length > 0) { + container.update('

' + response.message + '

'); + } + }, scope: this + } + ,failure: { + fn: function(response) { + var message = response.message.length > 0 ? response.message : _('error_loading_feed'); + container.update('

' + message + '

'); + }, scope: this + } + } + } + ); } }); Ext.reg('modx-panel-welcome', MODx.panel.Welcome); diff --git a/manager/controllers/default/dashboard/widget.modx-news.php b/manager/controllers/default/dashboard/widget.modx-news.php index 667e13e0914..d008dcf3931 100644 --- a/manager/controllers/default/dashboard/widget.modx-news.php +++ b/manager/controllers/default/dashboard/widget.modx-news.php @@ -8,36 +8,16 @@ * @subpackage dashboard */ class modDashboardWidgetNewsFeed extends modDashboardWidgetInterface { - /** - * @var modRSSParser $rss - */ - public $rss; - /** * @return string */ public function render() { - $url = $this->modx->getOption('feed_modx_news'); - $feedHost = parse_url($url, PHP_URL_HOST); - if ($feedHost && function_exists('checkdnsrr') && !checkdnsrr($feedHost, 'A')) { + $enabled = $this->modx->getOption('feed_modx_news_enabled',null,true); + if (!$enabled) { return ''; } - $this->modx->loadClass('xmlrss.modRSSParser','',false,true); - $this->rss = new modRSSParser($this->modx); - $o = array(); - $newsEnabled = $this->modx->getOption('feed_modx_news_enabled',null,true); - if (!empty($url) && !empty($newsEnabled)) { - $rss = $this->rss->parse($url); - if (is_object($rss)) { - foreach (array_keys($rss->items) as $key) { - $item= &$rss->items[$key]; - $item['pubdate'] = strftime('%c',$item['date_timestamp']); - $o[] = $this->getFileChunk('dashboard/rssitem.tpl',$item); - } - } - } - return implode("\n",$o); + return '
' . $this->modx->lexicon('loading') . '
'; } } return 'modDashboardWidgetNewsFeed'; diff --git a/manager/controllers/default/dashboard/widget.modx-security.php b/manager/controllers/default/dashboard/widget.modx-security.php index e685398c78a..1a2ff19402a 100644 --- a/manager/controllers/default/dashboard/widget.modx-security.php +++ b/manager/controllers/default/dashboard/widget.modx-security.php @@ -8,33 +8,13 @@ * @subpackage dashboard */ class modDashboardWidgetSecurityFeed extends modDashboardWidgetInterface { - /** - * @var modRSSParser $rss - */ - public $rss; - public function render() { - $url = $this->modx->getOption('feed_modx_security'); - $feedHost = parse_url($url, PHP_URL_HOST); - if ($feedHost && function_exists('checkdnsrr') && !checkdnsrr($feedHost, 'A')) { + $enabled = $this->modx->getOption('feed_modx_security_enabled',null,true); + if (!$enabled) { return ''; } - $this->modx->loadClass('xmlrss.modRSSParser','',false,true); - $this->rss = new modRSSParser($this->modx); - $o = array(); - $newsEnabled = $this->modx->getOption('feed_modx_security_enabled',null,true); - if (!empty($url) && !empty($newsEnabled)) { - $rss = $this->rss->parse($url); - if (is_object($rss)) { - foreach (array_keys($rss->items) as $key) { - $item= &$rss->items[$key]; - $item['pubdate'] = strftime('%c',$item['date_timestamp']); - $o[] = $this->getFileChunk('dashboard/rssitem.tpl',$item); - } - } - } - return implode("\n",$o); + return '
' . $this->modx->lexicon('loading') . '
'; } } return 'modDashboardWidgetSecurityFeed'; From 75c900845a12d99f64cb02d1fd614d4db97d815a Mon Sep 17 00:00:00 2001 From: Mark Hamstra Date: Fri, 7 Jul 2017 12:01:27 +0100 Subject: [PATCH 2/3] Remove sleep() call from testing the loading spinner --- .../model/modx/processors/system/dashboard/widget/feed.class.php | 1 - 1 file changed, 1 deletion(-) diff --git a/core/model/modx/processors/system/dashboard/widget/feed.class.php b/core/model/modx/processors/system/dashboard/widget/feed.class.php index 34ede7a2ec7..c4f53b8ad42 100644 --- a/core/model/modx/processors/system/dashboard/widget/feed.class.php +++ b/core/model/modx/processors/system/dashboard/widget/feed.class.php @@ -20,7 +20,6 @@ public function process() return $this->failure('Invalid feed type'); } - sleep(5); $enabled = $this->modx->getOption('feed_modx_' . $feed . '_enabled', null, true); if (!$enabled) { return $this->failure(); From 6e52883596fe711e803e56bd477ccb3a15dbd712 Mon Sep 17 00:00:00 2001 From: Mark Hamstra Date: Fri, 7 Jul 2017 12:41:45 +0100 Subject: [PATCH 3/3] Use $modx->getService instead of loadClass and direct instantiation of the RSS parser --- .../modx/processors/system/dashboard/widget/feed.class.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/core/model/modx/processors/system/dashboard/widget/feed.class.php b/core/model/modx/processors/system/dashboard/widget/feed.class.php index c4f53b8ad42..96589357a1f 100644 --- a/core/model/modx/processors/system/dashboard/widget/feed.class.php +++ b/core/model/modx/processors/system/dashboard/widget/feed.class.php @@ -37,8 +37,7 @@ public function process() public function loadFeed($url) { - $this->modx->loadClass('xmlrss.modRSSParser','',false,true); - $this->rss = new modRSSParser($this->modx); + $this->rss = $this->modx->getService('rss', 'xmlrss.modRSSParser'); $o = array(); $rss = $this->rss->parse($url);