From 590d619c9b9ef24bb0a0a4ef7b4a29367b9d8e32 Mon Sep 17 00:00:00 2001 From: Ishan Jayamanne Date: Mon, 26 Nov 2018 01:28:17 +0000 Subject: [PATCH] Upgrade to SS4 --- .gitignore | 2 + _config/extensions.yml | 8 +- _config/routes.yml | 6 +- code/DisqusCMSActionExtension.php | 30 --- code/DisqusComment.php | 33 --- code/DisqusController.php | 22 -- code/DisqusCount.php | 36 --- code/DisqusExtension.php | 159 ------------ code/DisqusSiteConfig.php | 40 --- code/DisqusSync.php | 83 ------- code/DisqusTask.php | 38 --- composer.json | 10 +- src/DisqusCMSActionExtension.php | 37 +++ src/DisqusComment.php | 42 ++++ src/DisqusController.php | 30 +++ src/DisqusCount.php | 44 ++++ src/DisqusExtension.php | 228 ++++++++++++++++++ src/DisqusSiteConfig.php | 53 ++++ src/DisqusSync.php | 95 ++++++++ src/DisqusTask.php | 42 ++++ {code => src}/thirdparty/.DS_Store | Bin {code => src}/thirdparty/disqus-php/LICENSE | 0 .../thirdparty/disqus-php/README.rst | 0 .../disqus-php/disqusapi/disqusapi.php | 0 .../disqus-php/disqusapi/interfaces.json | 0 .../thirdparty/disqus-php/disqusapi/json.php | 0 .../disqus-php/disqusapi/tests/disqusapi.php | 0 .../disqus-php/disqusapi/tests/json.php | 0 .../thirdparty/disqus-php/disqusapi/url.php | 0 templates/Includes/DisqusComments.ss | 28 +-- 30 files changed, 599 insertions(+), 467 deletions(-) create mode 100644 .gitignore delete mode 100644 code/DisqusCMSActionExtension.php delete mode 100644 code/DisqusComment.php delete mode 100644 code/DisqusController.php delete mode 100644 code/DisqusCount.php delete mode 100644 code/DisqusExtension.php delete mode 100644 code/DisqusSiteConfig.php delete mode 100644 code/DisqusSync.php delete mode 100644 code/DisqusTask.php create mode 100644 src/DisqusCMSActionExtension.php create mode 100644 src/DisqusComment.php create mode 100644 src/DisqusController.php create mode 100644 src/DisqusCount.php create mode 100644 src/DisqusExtension.php create mode 100644 src/DisqusSiteConfig.php create mode 100644 src/DisqusSync.php create mode 100644 src/DisqusTask.php rename {code => src}/thirdparty/.DS_Store (100%) rename {code => src}/thirdparty/disqus-php/LICENSE (100%) rename {code => src}/thirdparty/disqus-php/README.rst (100%) rename {code => src}/thirdparty/disqus-php/disqusapi/disqusapi.php (100%) rename {code => src}/thirdparty/disqus-php/disqusapi/interfaces.json (100%) rename {code => src}/thirdparty/disqus-php/disqusapi/json.php (100%) rename {code => src}/thirdparty/disqus-php/disqusapi/tests/disqusapi.php (100%) rename {code => src}/thirdparty/disqus-php/disqusapi/tests/json.php (100%) rename {code => src}/thirdparty/disqus-php/disqusapi/url.php (100%) diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2df7cd6 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.DS_store +.idea diff --git a/_config/extensions.yml b/_config/extensions.yml index 5df5350..1136921 100644 --- a/_config/extensions.yml +++ b/_config/extensions.yml @@ -1,9 +1,9 @@ --- Name: disqus-extensions --- -CMSMain: +SilverStripe\CMS\Controllers\CMSMain: extensions: - - DisqusCMSActionExtension -SiteConfig: + - Silverstripesk\Disqus\DisqusCMSActionExtension +SilverStripe\SiteConfig\SiteConfig: extensions: - - DisqusSiteConfig \ No newline at end of file + - Silverstripesk\Disqus\DisqusSiteConfig diff --git a/_config/routes.yml b/_config/routes.yml index b619f04..638e345 100644 --- a/_config/routes.yml +++ b/_config/routes.yml @@ -2,8 +2,6 @@ Name: disqussync After: framework/routes#coreroutes --- -Director: +SilverStripe\Control\Director: rules: - 'disqussync//$Action/$ID/$Name': 'Disqus_Controller' - - \ No newline at end of file + 'disqussync//$Action/$ID/$Name': 'Silverstripesk\Disqus\DisqusController' diff --git a/code/DisqusCMSActionExtension.php b/code/DisqusCMSActionExtension.php deleted file mode 100644 index f6f6f7d..0000000 --- a/code/DisqusCMSActionExtension.php +++ /dev/null @@ -1,30 +0,0 @@ - - * @notice SilverStripe.sk is not affiliated with the company SilverStripe Ltd. - * @date April 2011 - */ - -class DisqusCMSActionExtension extends LeftAndMainExtension { - - private static $allowed_actions = array( - 'syncAllCommentsAction' - ); - - function syncCommentsAction() { - - $id = (int)$_REQUEST['ID']; - $page = Page::get()->byID($id); - - DisqusSync::sync($page->disqusIdentifier()); - - $this->owner->response->addHeader('X-Status', sprintf('Synced successfuly')); - return; - } -} - -// EOF diff --git a/code/DisqusComment.php b/code/DisqusComment.php deleted file mode 100644 index b4f788c..0000000 --- a/code/DisqusComment.php +++ /dev/null @@ -1,33 +0,0 @@ - - * @notice SilverStripe.sk is not affiliated with the company SilverStripe Ltd. - * @date April 2011 - */ - -class DisqusComment extends DataObject { - private static $db = array( - "isSynced" => "Boolean", - "threadIdentifier" => "Varchar(32)", - "forum" => "Varchar", - "disqusId" => "Int", - "parent" => "Int", - "thread" => "Int", - "isApproved" => "Boolean", - "isDeleted" => "Boolean", - "isFlagged" => "Boolean", - "isHighlighted" => "Boolean", - "isSpam" => "Boolean", - "author_name" => "Varchar", - "createdAt" => "Datetime", - //"ipAddress" => "Varchar(32)", - API no longer returns IP address via publick secret key - "message" => "HTMLText" - ); - -} - -// EOF diff --git a/code/DisqusController.php b/code/DisqusController.php deleted file mode 100644 index e22e2ba..0000000 --- a/code/DisqusController.php +++ /dev/null @@ -1,22 +0,0 @@ - - * @notice SilverStripe.sk is not affiliated with the company SilverStripe Ltd. - * @date April 2011 - */ - -class Disqus_Controller extends Controller { - - private static $allowed_actions = array('sync_by_ident'); - - function sync_by_ident() { - $returnmessage = (Director::is_cli() || Director::isDev()) ? 1 : 0; - return DisqusSync::sync($this->request->param('ID'),$returnmessage); - } -} - -// EOF diff --git a/code/DisqusCount.php b/code/DisqusCount.php deleted file mode 100644 index fbb7e20..0000000 --- a/code/DisqusCount.php +++ /dev/null @@ -1,36 +0,0 @@ - - * @notice SilverStripe.sk is not affiliated with the company SilverStripe Ltd. - * @date April 2011 - */ - -class DisqusCount { - - protected static $addCountJS = true; - - public static function addCountJS($shortname,$extraVars = NULL) { - if (self::$addCountJS) { - $script = " - var disqus_shortname = '".$shortname."'; // required: replace example with your forum shortname - ".$extraVars." - - (function () { - var s = document.createElement('script'); s.async = true; - s.type = 'text/javascript'; - s.src = 'https://' + disqus_shortname + '.disqus.com/count.js'; - (document.getElementsByTagName('HEAD')[0] || document.getElementsByTagName('BODY')[0]).appendChild(s); - }()); - "; - Requirements::customScript($script); - self::$addCountJS = false; - } - } - -} - -// EOF diff --git a/code/DisqusExtension.php b/code/DisqusExtension.php deleted file mode 100644 index c4474d6..0000000 --- a/code/DisqusExtension.php +++ /dev/null @@ -1,159 +0,0 @@ - - * @notice SilverStripe.sk is not affiliated with the company SilverStripe Ltd. - * @date April 2011 - */ - -class DisqusExtension extends DataExtension { - - private static $db = array( - 'customDisqusIdentifier' => 'Varchar(32)' - ); - - public function updateCMSFields(FieldList $fields) { - $fields->removeByName("Comments"); - } - - public function updateSettingsFields(FieldList $fields) { - $fields->addFieldToTab('Root.Settings', TextField::create("customDisqusIdentifier", _t("Disqus.CUSTOMDISQUSIDENTIFIER", "Custom Disqus identifier"))->setDescription("Current identifier: " . $this->owner->disqusIdentifier())); - } - - function updateCMSActions(FieldList $actions) { - // added button for syncing comments with Disqus server manualy... - if ($this->owner->disqusEnabled()) { - $Action = new FormAction( - "syncCommentsAction", - _t("Disqus.SYNCCOMMENTSBUTTON", "Sync Disqus Comments") - ); - $actions->push($Action); - } - } - - function disqusEnabled() { - // comments module installed? Ask Comments module if enabled - if ($this->owner->hasExtension('CommentsExtension')) { - return ($this->owner->getCommentsEnabled()) ? true : false; - } - // no comments module - place disqus if template asks for - return true; - } - - function disqusIdentifier() { - $config = SiteConfig::current_site_config(); - return ($this->owner->customDisqusIdentifier) ? $this->owner->customDisqusIdentifier : $config->disqus_prefix."_".$this->owner->ID; - } - - function disqusLocaleJsVar() { - $l = (isset($this->owner->Locale)) ? $this->owner->Locale : i18n::get_locale(); - $loc = explode("_",$l); - return ($loc[1]) - ? 'var disqus_config = function () { this.language = "'.$loc[0].'"; };' - : NULL; - } - - function disqusDeveloperJsVar() { - return (Director::isLive()) ? NULL : "var disqus_developer = 1;"; - } - - function DisqusPageComments() { - // if the owner DataObject is Versioned, don't display DISQUS until the post is published - // to avoid identifier / URL conflicts. - if( $this->owner->hasExtension('Versioned') && Versioned::current_stage() == 'Stage') return '

'._t("Disqus.NOTLIVEALERT","Disqus comments are temporary OFF in Stage mode. Logout or turn in Live mode!").'

'; - - $config = SiteConfig::current_site_config(); - $ti = $this->disqusIdentifier(); - if ($config->disqus_shortname && $this->owner->disqusEnabled()) { - $script = ' - var disqus_shortname = \''.$config->disqus_shortname.'\'; - '.$this->owner->disqusDeveloperJsVar().' - var disqus_identifier = \''.$ti.'\'; - var disqus_url = \''.$this->owner->absoluteLink().'\'; - '.$this->owner->disqusLocaleJsVar().' - - (function() { - var dsq = document.createElement(\'script\'); dsq.type = \'text/javascript\'; dsq.async = true; - dsq.src = \'https://\' + disqus_shortname + \'.disqus.com/embed.js\'; - (document.getElementsByTagName(\'head\')[0] || document.getElementsByTagName(\'body\')[0]).appendChild(dsq); - })(); - '; - Requirements::customScript($script); - - $templateData = array( - 'SyncDisqus' => true - ); - - - // Hide Local Comments -> we will use Disqus service - $hideLocal = " - function hideLocalComments() { - document.getElementById('disqus_local').style.display = 'none'; - } - window.onload = hideLocalComments; - "; - Requirements::customScript($hideLocal); - - // Get comments - //$results = DataObject::get('DisqusComment',"isSynced = 1 AND isApproved = 1 AND threadIdentifier = '$ti'"); - $results = DisqusComment::get()->filter(array( - 'isSynced'=>'1', - 'isApproved'=>'1', - 'threadIdentifier'=>$ti - )); - - // Prepare data for template - $templateData['LocalComments'] = $results; - - // Sync comments - $now = time(); - $synced = strtotime($this->owner->LastEdited); - if (($now - $synced) > $config->disqus_synctime) { - if ($config->disqus_syncinbg) { - // background process - // from here: https://stackoverflow.com/questions/1993036/run-function-in-background - // TODO: Windows check is not fully correct - // Debug - // echo "trying to sync in BG"; - $cmd = "php " . Director::baseFolder() . DIRECTORY_SEPARATOR . "framework" . DIRECTORY_SEPARATOR . "cli-script.php /disqussync/sync_by_ident/" . $ti . "/"; - // Debug - //echo $cmd; - if (substr(php_uname(), 0, 7) == "Windows") { - pclose(popen("start /B ". $cmd, "r")); - } else { - exec($cmd . " > /dev/null &"); - } - } else { - $returnmessage = (Director::isDev()) ? 1 : 0; - DisqusSync::sync($ti, $returnmessage); - } - // updates LastEdited data - $this->owner->write(); // saves the record - } else { - // Debug - // echo "not needed to sync"; - } - - - return $this->owner - ->customise($templateData) - ->renderWith(array('DisqusComments')); - - } - } - - function disqusCountLink() { - $config = SiteConfig::current_site_config(); - - if ($config->disqus_shortname) { - DisqusCount::addCountJS($config->disqus_shortname, $this->owner->disqusLocaleJsVar().$this->owner->disqusDeveloperJsVar()); - } - - return ''._t("Disqus.COMMENTS","Comments").''; - } -} - -//EOF diff --git a/code/DisqusSiteConfig.php b/code/DisqusSiteConfig.php deleted file mode 100644 index b1c3e9c..0000000 --- a/code/DisqusSiteConfig.php +++ /dev/null @@ -1,40 +0,0 @@ - - * @notice SilverStripe.sk is not affiliated with the company SilverStripe Ltd. - * @date April 2011 - */ - -class DisqusSiteConfig extends DataExtension{ - // add database fields - private static $db = array( - 'disqus_shortname' => 'Varchar', - 'disqus_secretkey' => 'Varchar(64)', - 'disqus_prefix' => 'Varchar', - 'disqus_synctime' => 'Int', - 'disqus_syncinbg' => 'Boolean' - ); - - // Create CMS fields - public function updateCMSFields(FieldList $fields) { - $fields->addFieldToTab("Root.Disqus",new TextField("disqus_shortname", "Disqus shortname")); - $fields->addFieldToTab("Root.Disqus",new TextField("disqus_secretkey", "Disqus secret key")); - $fields->addFieldToTab("Root.Disqus",new TextField("disqus_prefix", "Disqus prefix")); - $fields->addFieldToTab("Root.Disqus",new TextField("disqus_synctime", "Disqus sync time (seconds)")); - $fields->addFieldToTab("Root.Disqus",new CheckboxField("disqus_syncinbg", "Disqus - sync as background process?")); - } - - /** - * Adds a button the Site Config page of the CMS to sync all disqus comments. - */ - public function updateCMSActions(FieldList $actions) { - //@todo work out why this throws an error. - //$actions->push( new InlineFormAction('syncAllCommentsAction', _t('Disqus.syncAllCommentsActionButton', 'Sync all Disqus comments') ) ); - } - -} -// EOF diff --git a/code/DisqusSync.php b/code/DisqusSync.php deleted file mode 100644 index a0d79a6..0000000 --- a/code/DisqusSync.php +++ /dev/null @@ -1,83 +0,0 @@ - - * @notice SilverStripe.sk is not affiliated with the company SilverStripe Ltd. - * @date April 2011 - */ - -class DisqusSync { - - static function sync($threadID, $returnmessage = false) { - $message = "no comments on disqus server"; - $comments = false; - - $config = SiteConfig::current_site_config(); - $config->disqus_secretkey; - - $disqus = new DisqusAPI($config->disqus_secretkey); - - try { - $comments = $disqus->threads->listPosts(array("forum"=>$config->disqus_shortname,"thread"=>"ident:".$threadID)); - - } catch (Exception $e) { - //user_error ( 'Caught exception (probably cant get thread by ID, does it exists?): ' . $e->getMessage()); - } - - - if ($comments) { - - $message = "There are some comments on disqus server"; - - // Debug - if ($returnmessage) { - //print_r($comments); - } - - DB::query("UPDATE DisqusComment SET `isSynced` = 0 WHERE `threadIdentifier` = '$threadID'"); - - foreach ($comments as $comment) { - - //if ($c = DataObject::get_one('DisqusComment',"disqusId = '$comment->id'")) { - if ( $c = DisqusComment::get()->filter('disqusId',$comment->id)->First() ){ - // Comment is already here, fine ;) - $message .= " | updating comment id ".$comment->id; - } else { - // Comment is new, create it - $c = new DisqusComment(); - $message .= " | adding comment id ".$comment->id; - } - - $c->isSynced = 1; - $c->threadIdentifier = $threadID; - $c->disqusId = $comment->id; - $c->author_name = $comment->author->name; - $c->forum = $comment->forum; - $c->parent = $comment->parent; - $c->thread = $comment->thread; - $c->isApproved = $comment->isApproved; - $c->isDeleted = $comment->isDeleted; - $c->isHighlighted = $comment->isHighlighted; - $c->isSpam = $comment->isSpam; - $c->createdAt = $comment->createdAt; - //$c->ipAddress = $comment->ipAddress; - $c->message = $comment->message; - - // finaly, save it to DB - $c->write(); - - } - - } - if ($returnmessage) { - - return $message; - - } - } -} - -//EOF diff --git a/code/DisqusTask.php b/code/DisqusTask.php deleted file mode 100644 index e174633..0000000 --- a/code/DisqusTask.php +++ /dev/null @@ -1,38 +0,0 @@ - - * @notice SilverStripe.sk is not affiliated with the company SilverStripe Ltd. - * @date April 2011 - */ -/* -class DisqusTask extends HourlyTask { - function process() { - //$pages = DataObject::get("Page","provideComments = 1 AND status = 'Published'"); - $pages = Page::get()->filter(array( - 'ProvideComments'=>'1' - //@todo remove this as param no longer returned on ss3.0 sitetree 'isPublished'=>'1' - )); - - if ($pages) { - echo ""; - } - - } -} -*/ -// EOF \ No newline at end of file diff --git a/composer.json b/composer.json index f6a27e9..882f636 100644 --- a/composer.json +++ b/composer.json @@ -1,15 +1,17 @@ { "name": "silverstripesk/silverstripe-disqus", "description": "Disqus module for SilverStripe CMS http://silverstripe.sk/clanky/disqus/", - "type": "silverstripe-module", + "type": "silverstripe-vendormodule", "keywords": ["silverstripe", "comments", "disqus"], "require": { "php": ">=5.3.2", - "silverstripe/framework": "~3.1", - "silverstripe/cms": "~3.1" + "silverstripe/framework": "^4.0", + "silverstripe/cms": "^4.0" }, "extra":{ "installer-name": "disqus" - } + }, + "minimum-stability": "dev", + "prefer-stable": true } diff --git a/src/DisqusCMSActionExtension.php b/src/DisqusCMSActionExtension.php new file mode 100644 index 0000000..035fdff --- /dev/null +++ b/src/DisqusCMSActionExtension.php @@ -0,0 +1,37 @@ + + * @notice SilverStripe.sk is not affiliated with the company SilverStripe Ltd. + * @date April 2011 + */ +class DisqusCMSActionExtension extends LeftAndMainExtension +{ + + /** + * @var array + */ + private static $allowed_actions = [ + 'syncAllCommentsAction', + ]; + + + public function syncCommentsAction() + { + $id = (int)$_REQUEST['ID']; + $page = Page::get()->byID($id); + + DisqusSync::sync($page->disqusIdentifier()); + + $this->owner->response->addHeader('X-Status', sprintf('Synced successfuly')); + return; + } +} diff --git a/src/DisqusComment.php b/src/DisqusComment.php new file mode 100644 index 0000000..b2973c4 --- /dev/null +++ b/src/DisqusComment.php @@ -0,0 +1,42 @@ + + * @notice SilverStripe.sk is not affiliated with the company SilverStripe Ltd. + * @date April 2011 + */ +class DisqusComment extends DataObject +{ + /** + * @var string + */ + private static $table_name = 'DisqusComment'; + + /** + * @var array + */ + private static $db = [ + "isSynced" => "Boolean", + "threadIdentifier" => "Varchar(32)", + "forum" => "Varchar", + "disqusId" => "Int", + "parent" => "Int", + "thread" => "Int", + "isApproved" => "Boolean", + "isDeleted" => "Boolean", + "isFlagged" => "Boolean", + "isHighlighted" => "Boolean", + "isSpam" => "Boolean", + "author_name" => "Varchar", + "createdAt" => "Datetime", + //"ipAddress" => "Varchar(32)", - API no longer returns IP address via publick secret key + "message" => "HTMLText", + ]; +} diff --git a/src/DisqusController.php b/src/DisqusController.php new file mode 100644 index 0000000..e3c5641 --- /dev/null +++ b/src/DisqusController.php @@ -0,0 +1,30 @@ + + * @notice SilverStripe.sk is not affiliated with the company SilverStripe Ltd. + * @date April 2011 + */ +class DisqusController extends Controller +{ + + /** + * @var array + */ + private static $allowed_actions = ['sync_by_ident']; + + public function sync_by_ident() + { + $returnmessage = (Director::is_cli() || Director::isDev()) ? 1 : 0; + + return DisqusSync::sync($this->request->param('ID'), $returnmessage); + } +} diff --git a/src/DisqusCount.php b/src/DisqusCount.php new file mode 100644 index 0000000..6701949 --- /dev/null +++ b/src/DisqusCount.php @@ -0,0 +1,44 @@ + + * @notice SilverStripe.sk is not affiliated with the company SilverStripe Ltd. + * @date April 2011 + */ +class DisqusCount +{ + /** + * @var bool + */ + protected static $addCountJS = true; + + /** + * @param $shortname + * @param null $extraVars + */ + public static function addCountJS($shortname, $extraVars = null) + { + if (self::$addCountJS) { + $script = " + var disqus_shortname = '" . $shortname . "'; // required: replace example with your forum shortname + " . $extraVars . " + + (function () { + var s = document.createElement('script'); s.async = true; + s.type = 'text/javascript'; + s.src = 'https://' + disqus_shortname + '.disqus.com/count.js'; + (document.getElementsByTagName('HEAD')[0] || document.getElementsByTagName('BODY')[0]).appendChild(s); + }()); + "; + Requirements::customScript($script); + self::$addCountJS = false; + } + } +} diff --git a/src/DisqusExtension.php b/src/DisqusExtension.php new file mode 100644 index 0000000..b1ee781 --- /dev/null +++ b/src/DisqusExtension.php @@ -0,0 +1,228 @@ + + * @notice SilverStripe.sk is not affiliated with the company SilverStripe Ltd. + * @date April 2011 + */ +class DisqusExtension extends DataExtension +{ + /** + * @var array + */ + private static $db = [ + 'customDisqusIdentifier' => 'Varchar(32)', + ]; + + /** + * @param FieldList $fields + */ + public function updateCMSFields(FieldList $fields) + { + $fields->removeByName("Comments"); + } + + /** + * @param FieldList $fields + */ + public function updateSettingsFields(FieldList $fields) + { + $fields->addFieldToTab( + 'Root.Settings', + TextField::create( + "customDisqusIdentifier", + _t(__CLASS__ . ".CUSTOMDISQUSIDENTIFIER", "Custom Disqus identifier") + ) + ->setDescription("Current identifier: " . $this->owner->disqusIdentifier()) + ); + } + + /** + * @param FieldList $actions + */ + public function updateCMSActions(FieldList $actions) + { + // added button for syncing comments with Disqus server manualy... + if ($this->owner->disqusEnabled()) { + $Action = new FormAction( + "syncCommentsAction", + _t(__CLASS__ . ".SYNCCOMMENTSBUTTON", "Sync Disqus Comments") + ); + $actions->push($Action); + } + } + + /** + * @return bool + */ + public function disqusEnabled() + { + // comments module installed? Ask Comments module if enabled + if ($this->owner->hasExtension('CommentsExtension')) { + return ($this->owner->getCommentsEnabled()) ? true : false; + } + // no comments module - place disqus if template asks for + return true; + } + + /** + * @return null|string + */ + public function disqusLocaleJsVar() + { + $l = (isset($this->owner->Locale)) ? $this->owner->Locale : i18n::get_locale(); + $loc = explode("_", $l); + return ($loc[1]) + ? 'var disqus_config = function () { this.language = "' . $loc[0] . '"; };' + : null; + } + + /** + * @return null|string + */ + public function disqusDeveloperJsVar() + { + return (Director::isLive()) ? null : "var disqus_developer = 1;"; + } + + /** + * @return DBHTMLText|string + * @throws ValidationException + */ + public function DisqusPageComments() + { + // if the owner DataObject is Versioned, don't display DISQUS until the post is published + // to avoid identifier / URL conflicts. + if ($this->owner->hasExtension('Versioned') && Versioned::current_stage() == 'Stage') { + return '

' . + _t(__CLASS__ . ".NOTLIVEALERT", "Disqus comments are + temporary OFF in Stage mode. Logout or turn in Live mode!") . '

'; + } + + $config = SiteConfig::current_site_config(); + $ti = $this->disqusIdentifier(); + if ($config->disqus_shortname && $this->owner->disqusEnabled()) { + $script = ' + var disqus_shortname = \'' . $config->disqus_shortname . '\'; + ' . $this->owner->disqusDeveloperJsVar() . ' + var disqus_identifier = \'' . $ti . '\'; + var disqus_url = \'' . $this->owner->absoluteLink() . '\'; + ' . $this->owner->disqusLocaleJsVar() . ' + + (function() { + var dsq = document.createElement(\'script\'); dsq.type = \'text/javascript\'; dsq.async = true; + dsq.src = \'https://\' + disqus_shortname + \'.disqus.com/embed.js\'; + (document.getElementsByTagName(\'head\')[0] || document.getElementsByTagName(\'body\')[0]).appendChild(dsq); + })(); + '; + Requirements::customScript($script); + + $templateData = [ + 'SyncDisqus' => true, + ]; + + + // Hide Local Comments -> we will use Disqus service + $hideLocal = " + function hideLocalComments() { + document.getElementById('disqus_local').style.display = 'none'; + } + window.onload = hideLocalComments; + "; + Requirements::customScript($hideLocal); + + // Get comments + //$results = DataObject::get('DisqusComment',"isSynced = 1 AND isApproved = 1 AND threadIdentifier = '$ti'"); + $results = DisqusComment::get()->filter([ + 'isSynced' => '1', + 'isApproved' => '1', + 'threadIdentifier' => $ti, + ]); + + // Prepare data for template + $templateData['LocalComments'] = $results; + + // Sync comments + $now = time(); + $synced = strtotime($this->owner->LastEdited); + if (($now - $synced) > $config->disqus_synctime) { + if ($config->disqus_syncinbg) { + // background process + // from here: https://stackoverflow.com/questions/1993036/run-function-in-background + // TODO: Windows check is not fully correct + // Debug + // echo "trying to sync in BG"; + $cmd = "php " . Director::baseFolder() . DIRECTORY_SEPARATOR . + "framework" . DIRECTORY_SEPARATOR . "cli-script.php /disqussync/sync_by_ident/" . $ti . "/"; + + // Debug + //echo $cmd; + if (substr(php_uname(), 0, 7) == "Windows") { + pclose(popen("start /B " . $cmd, "r")); + } else { + exec($cmd . " > /dev/null &"); + } + } else { + $returnmessage = (Director::isDev()) ? 1 : 0; + DisqusSync::sync($ti, $returnmessage); + } + // updates LastEdited data + $this->owner->write(); // saves the record + } else { + // Debug + // echo "not needed to sync"; + } + + return $this->owner + ->customise($templateData) + ->renderWith(['DisqusComments']); + } + } + + /** + * @return mixed|string + */ + public function disqusIdentifier() + { + $config = SiteConfig::current_site_config(); + return ($this->owner->customDisqusIdentifier) ? + $this->owner->customDisqusIdentifier : + $config->disqus_prefix . "_" . $this->owner->ID; + } + + /** + * @return string + */ + public function disqusCountLink() + { + $config = SiteConfig::current_site_config(); + + if ($config->disqus_shortname) { + DisqusCount::addCountJS( + $config->disqus_shortname, + $this->owner->disqusLocaleJsVar() . $this->owner->disqusDeveloperJsVar() + ); + } + + return '' + . _t(__CLASS__ . ".COMMENTS", "Comments") . ''; + } +} diff --git a/src/DisqusSiteConfig.php b/src/DisqusSiteConfig.php new file mode 100644 index 0000000..f9e38da --- /dev/null +++ b/src/DisqusSiteConfig.php @@ -0,0 +1,53 @@ + + * @notice SilverStripe.sk is not affiliated with the company SilverStripe Ltd. + * @date April 2011 + */ +class DisqusSiteConfig extends DataExtension +{ + // add database fields + private static $db = [ + 'disqus_shortname' => 'Varchar', + 'disqus_secretkey' => 'Varchar(64)', + 'disqus_prefix' => 'Varchar', + 'disqus_synctime' => 'Int', + 'disqus_syncinbg' => 'Boolean', + ]; + + // Create CMS fields + public function updateCMSFields(FieldList $fields) + { + $fields->addFieldsToTab( + 'Root.Disqus', + [ + TextField::create('disqus_shortname', 'Disqus shortname'), + TextField::create('disqus_secretkey', 'Disqus secret key'), + TextField::create('disqus_prefix', 'Disqus prefix'), + TextField::create('disqus_synctime', 'Disqus sync time (seconds)'), + CheckboxField::create('disqus_syncinbg', 'Disqus - sync as background process?') + ] + ); + } + + /** + * Adds a button the Site Config page of the CMS to sync all disqus comments. + * @param FieldList $actions + */ + public function updateCMSActions(FieldList $actions) + { + //@todo work out why this throws an error. + //$actions->push( new InlineFormAction('syncAllCommentsAction', _t('Disqus.syncAllCommentsActionButton', 'Sync all Disqus comments') ) ); + } +} diff --git a/src/DisqusSync.php b/src/DisqusSync.php new file mode 100644 index 0000000..92b0e7e --- /dev/null +++ b/src/DisqusSync.php @@ -0,0 +1,95 @@ + + * @notice SilverStripe.sk is not affiliated with the company SilverStripe Ltd. + * @date April 2011 + */ +class DisqusSync +{ + + /** + * @param $threadID + * @param bool $returnmessage + * + * @return string + * @throws ValidationException + */ + public static function sync($threadID, $returnmessage = false) + { + $message = "no comments on disqus server"; + $comments = false; + + $config = SiteConfig::current_site_config(); + $config->disqus_secretkey; + + $disqus = new DisqusAPI($config->disqus_secretkey); + + try { + $comments = $disqus->threads->listPosts([ + "forum" => $config->disqus_shortname, + "thread" => "ident:" . $threadID + ]); + + } catch (\Exception $e) { + //user_error ( 'Caught exception (probably cant get thread by ID, does it exists?): ' . $e->getMessage()); + } + + + if ($comments) { + $message = "There are some comments on disqus server"; + + // Debug + if ($returnmessage) { + //print_r($comments); + } + + DB::query("UPDATE DisqusComment SET `isSynced` = 0 WHERE `threadIdentifier` = '$threadID'"); + + foreach ($comments as $comment) { + //if ($c = DataObject::get_one('DisqusComment',"disqusId = '$comment->id'")) { + if ($c = DisqusComment::get()->filter('disqusId', $comment->id)->First()) { + // Comment is already here, fine ;) + $message .= " | updating comment id " . $comment->id; + } else { + // Comment is new, create it + $c = new DisqusComment(); + $message .= " | adding comment id " . $comment->id; + } + + $c->isSynced = 1; + $c->threadIdentifier = $threadID; + $c->disqusId = $comment->id; + $c->author_name = $comment->author->name; + $c->forum = $comment->forum; + $c->parent = $comment->parent; + $c->thread = $comment->thread; + $c->isApproved = $comment->isApproved; + $c->isDeleted = $comment->isDeleted; + $c->isHighlighted = $comment->isHighlighted; + $c->isSpam = $comment->isSpam; + $c->createdAt = $comment->createdAt; + //$c->ipAddress = $comment->ipAddress; + $c->message = $comment->message; + + // finaly, save it to DB + $c->write(); + + } + } + + if ($returnmessage) { + return $message; + } + } +} diff --git a/src/DisqusTask.php b/src/DisqusTask.php new file mode 100644 index 0000000..0568eb8 --- /dev/null +++ b/src/DisqusTask.php @@ -0,0 +1,42 @@ + + * @notice SilverStripe.sk is not affiliated with the company SilverStripe Ltd. + * @date April 2011 + */ +//class DisqusTask extends HourlyTask +//{ +// public function process() +// { +// //$pages = DataObject::get("Page","provideComments = 1 AND status = 'Published'"); +// $pages = Page::get()->filter([ +// 'ProvideComments' => '1' +// //@todo remove this as param no longer returned on ss3.0 sitetree 'isPublished'=>'1' +// ]); +// +// if ($pages) { +// echo ""; +// } +// } +//} + +// EOF diff --git a/code/thirdparty/.DS_Store b/src/thirdparty/.DS_Store similarity index 100% rename from code/thirdparty/.DS_Store rename to src/thirdparty/.DS_Store diff --git a/code/thirdparty/disqus-php/LICENSE b/src/thirdparty/disqus-php/LICENSE similarity index 100% rename from code/thirdparty/disqus-php/LICENSE rename to src/thirdparty/disqus-php/LICENSE diff --git a/code/thirdparty/disqus-php/README.rst b/src/thirdparty/disqus-php/README.rst similarity index 100% rename from code/thirdparty/disqus-php/README.rst rename to src/thirdparty/disqus-php/README.rst diff --git a/code/thirdparty/disqus-php/disqusapi/disqusapi.php b/src/thirdparty/disqus-php/disqusapi/disqusapi.php similarity index 100% rename from code/thirdparty/disqus-php/disqusapi/disqusapi.php rename to src/thirdparty/disqus-php/disqusapi/disqusapi.php diff --git a/code/thirdparty/disqus-php/disqusapi/interfaces.json b/src/thirdparty/disqus-php/disqusapi/interfaces.json similarity index 100% rename from code/thirdparty/disqus-php/disqusapi/interfaces.json rename to src/thirdparty/disqus-php/disqusapi/interfaces.json diff --git a/code/thirdparty/disqus-php/disqusapi/json.php b/src/thirdparty/disqus-php/disqusapi/json.php similarity index 100% rename from code/thirdparty/disqus-php/disqusapi/json.php rename to src/thirdparty/disqus-php/disqusapi/json.php diff --git a/code/thirdparty/disqus-php/disqusapi/tests/disqusapi.php b/src/thirdparty/disqus-php/disqusapi/tests/disqusapi.php similarity index 100% rename from code/thirdparty/disqus-php/disqusapi/tests/disqusapi.php rename to src/thirdparty/disqus-php/disqusapi/tests/disqusapi.php diff --git a/code/thirdparty/disqus-php/disqusapi/tests/json.php b/src/thirdparty/disqus-php/disqusapi/tests/json.php similarity index 100% rename from code/thirdparty/disqus-php/disqusapi/tests/json.php rename to src/thirdparty/disqus-php/disqusapi/tests/json.php diff --git a/code/thirdparty/disqus-php/disqusapi/url.php b/src/thirdparty/disqus-php/disqusapi/url.php similarity index 100% rename from code/thirdparty/disqus-php/disqusapi/url.php rename to src/thirdparty/disqus-php/disqusapi/url.php diff --git a/templates/Includes/DisqusComments.ss b/templates/Includes/DisqusComments.ss index a387389..c3ea505 100644 --- a/templates/Includes/DisqusComments.ss +++ b/templates/Includes/DisqusComments.ss @@ -1,17 +1,17 @@ -<% if SyncDisqus %> -
-

<% _t("Disqus.COMMENTSHEADING-JSOFF","Comments") %>

- <% if LocalComments %> - <% loop LocalComments %> -
-

{$author_name}:

-
$message
-
- <% end_loop %> - <% else %> -

<% _t("Disqus.NOCOMMENTS-JSOFF","No comments on this article.") %>

- <% end_if %> -
+<% if $SyncDisqus %> +
+

<% _t("Disqus.COMMENTSHEADING-JSOFF","Comments") %>

+ <% if $LocalComments %> + <% loop $LocalComments %> +
+

{$author_name}:

+
$message
+
+ <% end_loop %> + <% else %> +

<% _t("Disqus.NOCOMMENTS-JSOFF","No comments on this article.") %>

+ <% end_if %> +
<% end_if %>