Extension:HyperComments

From MediaWiki.org
Jump to: navigation, search


MediaWiki extensions manualManual:Extensions
Crystal Clear action run.png
HyperComments

Release status:Extension status beta

ImplementationTemplate:Extension#type User interface
DescriptionTemplate:Extension#description Adds a HyperComments box at the bottom of every article page
Author(s)Template:Extension#username (Dmitry Ogarkovtalk)
Latest versionTemplate:Extension#version 1.0 (2014-10-27)
MediaWikiTemplate:Extension#mediawiki 1.18+
LicenseTemplate:Extension#license GNU General Public License
Download No link
ExampleTemplate:Extension#example Какой велосипед купить - Rulus.ru
ParametersTemplate:Extension#parameters

$wgHyperCommentsId

Hooks usedTemplate:Extension#hook
SkinAfterBottomScriptsManual:Hooks/SkinAfterBottomScripts

Translate the HyperComments extension if it is available at translatewiki.net

Check usage and version matrix.

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[edit]

<?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[edit]

<?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 parameters[edit]

$wgHyperCommentsId
Your HyperComments widget ID.

See also[edit]

Extension:FacebookComments