Extension:Flattr/MediaWiki-1.7.1

From MediaWiki.org
Jump to: navigation, search

[edit] Description

MediaWiki 1.7.1 did not work with the main code, so here is a version that works with it. No guarantees as to how it might work on other versions, this has only been explicitly tested with 1.7.1.


[edit] Code

<?php
if( !defined( 'MEDIAWIKI' ) ) {
        echo( "This is an extension to the MediaWiki package and cannot be run standalone.\n" );
        die( -1 );
}
 
// Extension credits that will show up on Special:Version    
$wgExtensionCredits['parserhook'][] = array(
    'path'         => __FILE__,
        'name'         => 'Flattr',
        'version'      => '0.5',
        'author'       => 'Christian Riesen', 
        'url'          => 'http://www.mediawiki.org/wiki/Extension:Flattr',
        'description'  => 'Allows safe placements of flattr buttons on your wiki (For MW 1.7.1)'
);
 
$wgExtensionFunctions[] = 'wfFlattrSetup';
 
function wfFlattrSetup() {
        global $wgParser;
        $wgParser->setHook( 'flattr', 'wfFlattrRender' );
    return true;
}
 
function wfFlattrRender( $input, $argsraw, $parser) {
        if (count($argsraw) <= 0) {
                return "<span title=\"No arguments present, needs at least one\"
                        style=\"color: red;\">flattr error 2</span>";
        }
 
        global $wgFlattrUserid, $wgFlattrForceUserid;
 
        // Check sanity on $wgFlattrForceUserid
        if (!isset($wgFlattrForceUserid) || $wgFlattrForceUserid != true || $wgFlattrForceUserid != false) {
                $wgFlattrForceUserid = false;
        }
 
        $args = array();
        foreach ($argsraw as $name => $value) {
                $value = $parser->replaceVariables($value);
                $args[$name] = htmlspecialchars($value);
        }
 
        // two version, auto discover and url set
        if (isset($args['url']) && !isset($args['uid'])) {
                // Doing it the direct way, needs to be added to the page by hand already!
                // if a UID is present then this is probably an autosubmit, but we NEED the url either way!
                return "<script type=\"text/javascript\">var flattr_url = '" 
                        . $args['url'] 
                        . "';</script><script src=\"http://api.flattr.com/button/load.js\" type=\"text/javascript\"></script>";
        } else {
                // Auto discovery way, need all required params
                $categories = array('text','images','video','audio','software','rest');
                if (!isset($args['title']) || 
                        !isset($args['description']) || !isset($args['category']) || 
                        !in_array($args['category'], $categories)) {
                        return "<span title=\"Not all required attributes or category not correct\" 
                                style=\"color: red;\">flattr error 1</span>";
                }
 
                $out = "";
                $out .= "<script type=\"text/javascript\">" . PHP_EOL;
                if (isset($wgFlattrUserid) && $wgFlattrUserid > 0) {
                        // check if you have to overwrite
                        if (true === $wgFlattrForceUserid || !isset($args['uid'])) {
                                $out .= "var flattr_uid = '" . $wgFlattrUserid . "';" . PHP_EOL;
                        } else {
                                $out .= "var flattr_uid = '" . $args['uid'] . "';" . PHP_EOL;
                        }
                } else if (isset($args['uid'])) {
                        $out .= "var flattr_uid = '" . $args['uid'] . "';" . PHP_EOL;
                } else {
                        return "<span title=\"User id not present!\" 
                                style=\"color: red;\">flattr error 3</span>";
                }
 
                $out .= "var flattr_tle = '" . $args['title'] . "';" . PHP_EOL;
                $out .= "var flattr_dsc = '" . $args['description'] . "';" . PHP_EOL;
                $out .= "var flattr_cat = '" . $args['category'] . "';" . PHP_EOL;
 
                // Tags in comma separated list!
                if (isset($args['tags'])) {
                        $out .= "var flattr_tag = '" . $args['tags'] . "';" . PHP_EOL;
                }
 
                // URL as canonical, in case the same content is on multiple pages
                if (isset($args['url'])) {
                        $out .= "var flattr_tag = '" . $args['url'] . "';" . PHP_EOL;
                }
 
                $out .= "</script>" . PHP_EOL;
                $out .= "<script src=\"http://api.flattr.com/button/load.js\" type=\"text/javascript\"></script>" . PHP_EOL;
                return $out;
        }
}
Personal tools
Namespaces

Variants
Actions
Navigation
Support
Download
Development
Communication
Print/export
Toolbox