Extension:HyperComments

From mediawiki.org
MediaWiki extensions manual
HyperComments
Release status: unmaintained
Implementation User interface
Description Adds a HyperComments box at the bottom of every article page
Author(s) (Dmitry Ogarkovtalk)
Latest version 1.0 (2014-10-27)
MediaWiki 1.18+
Database changes No
License GNU General Public License 2.0
Download No link
Example Какой велосипед купить - Rulus.ru
$wgHyperCommentsId

The HyperComments extension adds a HyperComments box at the bottom of every article page, except for the main page. It is based on Extension:FacebookComments, with a few minor fixes.

Installation[edit]

Copy and paste the code below to the files: $IP/extensions/HyperComments/HyperComments.php and $IP/extensions/HyperComments/HyperComments.class.php.

Add the following to your LocalSettings.php:

$wgHyperCommentsId = '?????'; // Your HyperComments widget ID
require_once("$IP/extensions/HyperComments/HyperComments.php");

To position the comments box (div#comments), add some margins to your MediaWiki:Common.css, for example:

#comments {
	margin: 2em 0;
}

Code[edit]

HyperComments.php
<?php
/*
 * MediaWiki extension to add HyperComments on every article page
 * Installation instructions can be found on
 * http://www.mediawiki.org/wiki/Extension:HyperComments
 *
 * @ingroup Extensions
 * @author Dmitry Ogarkov
 * @license GNU Public License
 */

if( !defined( 'MEDIAWIKI' ) ) exit;
 
$dir = dirname(__FILE__).'/';
$wgAutoloadClasses['HyperComments'] = $dir.'HyperComments.class.php';
$wgHooks['SkinAfterBottomScripts'][] = 'HyperComments::renderHyperComments';
$wgExtensionCredits['parserhook'][] = array(
'path'        => __FILE__,
'name'        => 'HyperComments',
'version'     => '1.0',
'author'      => 'Dmitry Ogarkov',
'url'         => 'https://www.mediawiki.org/wiki/Extension:HyperComments',
'description' => 'Adds a HyperComments box at the bottom of every article page'
);
HyperComments.class.php
<?php
/*
 * Class file for the HyperComments extension
 *
 * @ingroup Extensions
 * @author Dmitry Ogarkov
 * @license GNU Public License
 */

if( !defined( 'MEDIAWIKI' ) ) exit;

class HyperComments {
	static function getURL() {
		if ( isset($_SERVER['HTTPS']) && $_SERVER['HTTPS']=='on' ) { $protocol =  'https'; $port = '443'; }
		else { $protocol = 'http'; $port = '80'; }
		$port = $_SERVER['SERVER_PORT']==$port ? '' : ':' . $_SERVER['SERVER_PORT'];
		return $protocol.'://'.$_SERVER['HTTP_HOST'].$port;
	}
	static function renderHyperComments( $skin, &$text ) {
		global $mediaWiki, $wgOut, $wgArticlePath, $wgScriptPath;
		global $wgHyperCommentsId;
		$title = $skin->getTitle();
		if( isset($_REQUEST['redirect']) ) return true;
		if( isset($mediaWiki) ) {
			if( $mediaWiki->getAction() != 'view' ) return true;
		}
		if( !$title->exists() ) return true;
		if( $title->getNamespace() != 0) return true;
		if( $title->isMainPage() ) return true;
		if( !isset($wgHyperCommentsId) ) $wgHyperCommentsId = '';
		$comments = '<div id="comments">';

		// HyperComments code below
		$comments .= '<div id="hypercomments_widget"></div>';
		$comments .= '<script type="text/javascript">';
		$comments .= '_hcwp = window._hcwp || [];';
		$comments .= '_hcwp.push({widget:"Stream", widget_id:'.$wgHyperCommentsId.'});';
		$comments .= '(function() {';
		$comments .= 'if("HC_LOAD_INIT" in window)return;';
		$comments .= 'HC_LOAD_INIT = true;';
		$comments .= 'var lang = "ru";';
		$comments .= 'var hcc = document.createElement("script"); hcc.type = "text/javascript"; hcc.async = true;';
		$comments .= 'hcc.src = ("https:" == document.location.protocol ? "https" : "http")+"://w.hypercomments.com/widget/hc/'.$wgHyperCommentsId.'/"+lang+"/widget.js";';
		$comments .= 'var s = document.getElementsByTagName("script")[0];';
		$comments .= 's.parentNode.insertBefore(hcc, s.nextSibling);';
		$comments .= '})();';
		$comments .= '</script>';

		$comments .= '</div>';
		$wgOut->addHTML($comments);
		return true;
	}
}

Configuration[edit]

$wgHyperCommentsId
Your HyperComments widget ID.

See also[edit]