Extension:FacebookComments

From MediaWiki.org
Jump to: navigation, search
MediaWiki extensions manual
Crystal Clear action run.png
FacebookComments

Release status: beta

Implementation Tag
Description Facebook comments on every page.
Author(s) Jmkim dot com
Latest version 1.1.2 (2012-08-26)
MediaWiki 1.18.0
License GNU General Public License
Download FacebookComments.tgz
Hooks used
SkinAfterBottomScripts

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

Check usage and version matrix; code metrics

Download instructions[edit | edit source]

You can download the php file from FacebookComments.tgz or copy and paste the code below to the file: $IP/extensions/FacebookComments/FacebookComments.php. Note: $IP stands for the root directory of your MediaWiki installation, the same directory that holds LocalSettings.php.

Installation[edit | edit source]

To install this extension, add the following to LocalSettings.php:

require_once("$IP/extensions/FacebookComments/FacebookComments.php");
$wgFacebookAppId = '123456789012345'; // (optional)

Code[edit | edit source]

FacebookComments.php[edit | edit source]

<?php
/*
 * MediaWiki extension to add Facebook Comments
 * Installation instructions can be found on
 * http://www.mediawiki.org/wiki/Extension:FacebookComments
 *
 * @ingroup Extensions
 * @author Jmkim dot com
 * @license GNU Public License
 */
 
// Exit if called outside of MediaWiki
if( !defined( 'MEDIAWIKI' ) ) exit;
 
// SETTINGS
$wgFacebookCommentsNumPosts = 5;
$wgFacebookCommentsWidth = 470;
$wgFacebookCommentsColorscheme = 'light';
 
$dir = dirname(__FILE__).'/';
$wgAutoloadClasses['FacebookComments'] = $dir.'FacebookComments.class.php';
$wgHooks['SkinAfterBottomScripts'][] = 'FacebookComments::renderFacebookComments';
$wgExtensionCredits['parserhook'][] = array(
'path'        => __FILE__,
'name'        => 'FacebookComments',
'version'     => '1.1.2',
'author'      => 'Jmkim dot com',
'description' => 'Facebook Comments Extension',
'url'         => 'http://www.mediawiki.org/wiki/Extension:FacebookComments'
);

FacebookComments.class.php[edit | edit source]

<?php
if( !defined( 'MEDIAWIKI' ) ) exit;
/**
 * Class file for the FacebookComments extension
 *
 * @ingroup Extensions
 * @author Jmkim dot com
 * @license GNU Public License
 */
class FacebookComments {
	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 renderFacebookComments( $skin, &$text ) {
		global $mediaWiki, $wgOut, $wgArticlePath, $wgScriptPath;
		global $wgFacebookCommonScriptWritten, $wgFacebookAppId;
		global $wgFacebookCommentsNumPosts, $wgFacebookCommentsWidth, $wgFacebookCommentsColorscheme;
		$title = $skin->getTitle();
		if( isset($_REQUEST['redirect']) ) return true;
		if( $mediaWiki->getAction() != 'view' ) return true;
		if( !$title->exists() ) return true;
		if( $title->getNamespace() != 0) return true;
		$fbcomments = '';
		if( !isset($wgFacebookAppId) ) $wgFacebookAppId = '';
		if( !isset($wgFacebookCommonScriptWritten) || !$wgFacebookCommonScriptWritten ) {
			$wgFacebookCommonScriptWritten = true;
			$fbcomments .= '<div id="fb-root"></div><script src="http://connect.facebook.net/en_US/all.js#xfbml=1&appId='.$wgFacebookAppId.'"></script>';
		}
		if( isset($wgArticlePath) ) $href = str_replace('$1','',FacebookComments::getURL().$wgArticlePath).$title;
		else $href = FacebookComments::getURL().$wgScriptPath.'/index.php/'.$title;
		$fbcomments .= '<br><div class="fb-comments" data-href="'.$href.'"';
		$fbcomments .= ' data-num-posts="'.$wgFacebookCommentsNumPosts.'"';
		$fbcomments .= ' data-width="'.$wgFacebookCommentsWidth.'"';
		$fbcomments .= ' data-colorscheme="'.$wgFacebookCommentsColorscheme.'"';
		$fbcomments .= '></div>';
		$wgOut->addHTML($fbcomments);
		return true;
	}
}

See also[edit | edit source]