Personal tools




Extension:Friends.php

From OrganicDesign Wiki

Jump to: navigation, search
<?php
# Extension:Friends
# - Licenced under LGPL (http://www.gnu.org/copyleft/lesser.html)
# - Author: [http://www.organicdesign.co.nz/User:Nad Nad], [http://www.organicdesign.co.nz/User:Rob Rob]
 
if (!defined('MEDIAWIKI')) die('Not an entry point.');
 
define('FRIENDS_VERSION','0.0.1');
 
$egFriendsMagic             = "friends";
$egFriendsTag               = "friends";
$wgExtensionFunctions[]        = 'efSetupFriends';
$wgHooks['LanguageGetMagic'][] = 'efFriendsLanguageGetMagic';
 
$wgExtensionCredits['parserhook'][] = array(
	'name'        => 'Friends',
	'author'      => '[http://www.organicdesign.co.nz/User:Nad Nad], [http://www.organicdesign.co.nz/User:Rob Rob]',
	'description' => 'Outputs a Facebook-like panel with photos of those who are associated with this page.',
	'url'         => 'http://www.organicdesign.co.nz/Extension:Friends',
	'version'     => FRIENDS_VERSION
	);
 
class Friends {
 
	# Properties
	var $prop1 = 'default value';
	var $prop2 = 'default value';
 
	# Constructor
	function __construct() {
		global $wgHooks,$wgParser,$egFriendsMagic,$egFriendsTag;
 
		# Add the parser-function
		$wgParser->setFunctionHook($egFriendsMagic,array($this,'magicFriends'));
 
		# Add the tagHook
		$wgParser->setHook($egFriendsTag,array($this,'tagFriends'));
 
		}
 
	# Expand the friends-magic
	function magicFriends(&$parser) {
		global $egFriendsMagic;
		# Populate $argv with both named and numeric parameters
		$argv = array();
		foreach (func_get_args() as $arg) if (!is_object($arg)) {
			if (preg_match('/^(.+?)\\s*=\\s*(.+)$/',$arg,$match)) $argv[$match[1]] = $match[2]; else $argv[] = $arg;
			}
 
		# Build text of expanded result
		$args = '';
		foreach ($argv as $k => $v) $args .= "*'''$k:''' ''$v''\n";
	        $args =  htmlspecialchars($args);
		$text = "=== Magic $egFriendsMagic(): ===\n$args";
 
		global $wgTitle, $wgScriptPath;
 
		$id = $wgTitle->getArticleID();
 
		$dbr  = &wfGetDB(DB_SLAVE);
		$u = $dbr->tableName('user');
		$r = $dbr->tableName('revision');
 
		$sql = "select distinct user_name from $u,$r where $u.user_id = $r.rev_user and $r.rev_page = $id;";
		$res  = $dbr->query($sql);
 
		$out .=  "$sql\n\n<div class='friendsContainer'>";
		while ($row = $dbr->fetchRow($res)) {
			$title = Title::newFromText($row['user_name']."jpg", NS_IMAGE);
 
			$image = Image::newFromTitle($title);
			if( $image && $image->exists() ) $i = $image->getURL(); else $i = "";
 
			$html = $wgParser->parse($wikitext,$title,new ParserOptions(),true,true)->getText();
 
		        # this next line should probably use NS_IMAGE to be safe and use some heristic to 
		        # allow other file types eg png, gif
		        $title    = Title::newFromText("Image:".$row['user_name'].".jpg");
		        $article  = new Article($title);
		        /*if($title->exists()) 
		                $out .= "<img src='$wgScriptPath/thumb.php?f=".$row['user_name'].".jpg&width=48'>";
		        else 
		                $out .= "<img src='$wgScriptPath/thumb.php?f=Anon.png&width=48'>";*/
		        $out .= "[[:Template:Friends]]";
		}
		$out .= "</div>";
 
		# Return result with available parser flags
		return array(
			$out,
			'found'   => false,
			'noparse' => true,
			'isHTML'  => false
			);
 
		}
 
	# Convert the <friends> tags to HTML
	function tagFriends($text,$argv,&$parser) {
        global $wgTitle, $wgScriptPath;
 
        $id = $wgTitle->getArticleID();
 
        $dbr  = &wfGetDB(DB_SLAVE);
        $u = $dbr->tableName('user');
        $r = $dbr->tableName('revision');
 
        $sql = "select distinct user_name from $u,$r where $u.user_id = $r.rev_user and $r.rev_page = $id;";
        $res  = $dbr->query($sql);
 
        $out .=  "$sql\n\n<div class='friendsContainer'>";
        while ($row = $dbr->fetchRow($res)) {
                # this next line should probably use NS_IMAGE to be safe and use some heristic to 
                # allow other file types eg png, gif
                $title    = Title::newFromText("Image:".$row['user_name'].".jpg");
                $article  = new Article($title);
                $out .= "<div class='friendsCell'>";
                if($title->exists()) 
                        $out .= "<img src='$wgScriptPath/thumb.php?f=Image:".$row['user_name'].".jpg&width=48'>";
                else 
                        $out .= "<img src='$wgScriptPath/thumb.php?f=Image:Unknown.jpg'>";
                $out .= "</div>";
        }
        $out .= "</div>";
        return $out;
		}
 
	# Needed in some versions to prevent Special:Version from breaking
	function __toString() { return 'Friends'; }
 	}
 
# Called from $wgExtensionFunctions array when initialising extensions
function efSetupFriends() {
	global $egFriends;
	$egFriends = new Friends();
	}
 
# Needed in MediaWiki >1.8.0 for magic word hooks to work properly
function efFriendsLanguageGetMagic(&$magicWords,$langCode = 0) {
	global $egFriendsMagic;
	$magicWords[$egFriendsMagic] = array(0,$egFriendsMagic);
	return true;
	}

The GNU Project Debian Linux Ubuntu Linux Wikipedia Affiliate Button MediaWiki