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 "' . + _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 "<% _t("Disqus.NOCOMMENTS-JSOFF","No comments on this article.") %>
- <% end_if %> -<% _t("Disqus.NOCOMMENTS-JSOFF","No comments on this article.") %>
+ <% end_if %> +
{$author_name}:
-