User:Nx/Extensions/slider/slider.php

From RationalWiki
Jump to navigation Jump to search
<?php
#include ("/home/rationa1/public_html/bar.php");

// Extension credits that show up on Special:Version
$wgExtensionCredits['parserhook'][] = array(
        'name' => 'Generic slider',
        'author' => '[http://rationalwiki.com/wiki/User:Nx Nx]',
        'url' => 'http://rationalwiki.com/',
        'description' => 'Creates a one oft slider per parameter tag for general voting. Requires the wigo extension'
);

//Avoid unstubbing $wgParser on setHook() too early on modern (1.12+) MW versions, as per r35980
if ( defined( 'MW_SUPPORTS_PARSERFIRSTCALLINIT' ) ) {
  $wgHooks['ParserFirstCallInit'][] = 'sliderinit';
} else { // Otherwise do things the old fashioned way
  $wgExtensionFunctions[] = 'sliderinit';
}

$wgHooks['BeforePageDisplay'][] = 'slideraddjscss';


$wgSliderIP = dirname( __FILE__ );
$wgExtensionMessagesFiles['slider'] = "$wgSliderIP/slider.i18n.php";

function sliderinit() {
  global $wgParser;
  wfLoadExtensionMessages('slider');
  $wgParser->setHook('slider','sliderrender');
  return true;    
}

function slideraddjscss(&$out, &$sk) 
{
  global $wgJsMimeType, $wgScriptPath;
  $out->addScript("<script type=\"{$wgJsMimeType}\" src=\"{$wgScriptPath}/extensions/slider/js/range.js\"></script>");
  $out->addScript("<script type=\"{$wgJsMimeType}\" src=\"{$wgScriptPath}/extensions/slider/js/timer.js\"></script>");
  $out->addScript("<script type=\"{$wgJsMimeType}\" src=\"{$wgScriptPath}/extensions/slider/js/slider.js\"></script>");
  $out->addStyle("{$wgScriptPath}/extensions/slider/css/rational/rational.css");
  return true;
}

function sliderrender($input, $args, $parser)
{

  $voteid = $args['poll'];
  if (!$voteid)
  {
    static $err = null;
    if (is_null($err)) {
      wfLoadExtensionMessages('wigo3');
      $err = wfMsg('wigoerror');
    }
    $output = $parser->recursiveTagParse($input);
    return "<p><span style='color:red;'>{$err}</span> {$output}</p>";
  }
  //get minimum and maximum values if supplied
  if (array_key_exists('min',$args) && (intval($args['min'],10) !== 0 || $args['min'] == '0')) {
    $minvalue = $args['min'];
  } else {
    $minvalue = 0;
  }
  if (array_key_exists('max',$args) && (intval($args['max'],10) !== 0 || $args['max'] == '0')) {
    $maxvalue = $args['max'];
  } else {
    $maxvalue = 100;
  }

  #avoid hacking wigo votes
  $voteid = "slider" . $voteid;
  $dbr = wfGetDB(DB_SLAVE);
  $res = $dbr->select('wigovote','sum(vote), count(vote)',array('id' => $voteid, "vote >= {$minvalue}", "vote <= {$maxvalue}"),'sliderrender',array('GROUP BY' => 'id'));
  $votes = 0;
  $countvotes = 0;
  if ($row = $res->fetchRow())
  {
    $votes = $row['sum(vote)'];
    $countvotes = $row['count(vote)'];
  }
  $res->free();

  #get my vote
  global $wgUser, $wgWigo3ConfigStoreIPs;
  $voter = $wgWigo3ConfigStoreIPs ? getenv ("REMOTE_ADDR") : $wgUser->getName();
  $res = $dbr->select('wigovote','vote',array('id' => $voteid, 'voter_name' => $voter),'wigo3render');
  $myvote = null;
  if ($row = $res->fetchRow())
  {
    $myvote = $row['vote'];
  }
  $res->free();


  if ($countvotes != 0) {
    $voteaverage = round($votes/$countvotes,2);
  } else {
    $voteaverage = "no votes";
  }

  $output = $parser->recursiveTagParse($input);

  //Store in database - not needed for now, might be if/when bestof feature is integrated
  /*$dbw = wfGetDB(DB_MASTER);
  $dbw->replace('wigotext','vote_id',array('vote_id' => $voteid, 'text' => $output),__METHOD__);*/

  //parse magic only, to allow plural
  wfLoadExtensionMessages('wigo3');
  $totalvotes = wfMsgExt('wigovotestotal',array('parsemag'),array($countvotes));

  wfLoadExtensionMessages('slider');
  if (array_key_exists('closed',$args) && strcasecmp($args['closed'],"yes") === 0) {
    return "<table class=\"slidervote\" cellspacing=\"2\" cellpadding=\"2\" border=\"0\">" .
              "<tr>" .
                "<td>" .
                  "<!--$voteid-->$output" .
                "</td>" .
                "<td style=\"min-width:25px; text-align:center;\">" .
                  "<span id=\"{$voteid}\" title=\"{$totalvotes}\">{$voteaverage}</span>" .
                "</td>" .
              "</tr>" .
            "</table>";
  } else {
    return "<table class=\"slidervote\" cellspacing=\"2\" cellpadding=\"2\" border=\"0\">" .
              "<tr>" .
                "<td>" .
                  "<!--$voteid-->$output" .
                "</td>" .
                "<td style=\"min-width:25px; text-align:center;\">" .
                  "<div class=\"slider\" id=\"slider-{$voteid}\"></div>" .
                "</td>" .
                "<td>" .
                  "<input class=\"slider-input\" id=\"slider-input-{$voteid}\" name=\"slider-input-{$voteid}\" size=\"1\" maxlength=\"3\"" . ($myvote !== null ? "value=\"{$myvote}\"" : "") . "/> " .
                  "<span id=\"{$voteid}\" title=\"{$totalvotes}\">{$voteaverage}</span> " .
                  "<a " . "href=\"javascript:wigovotesend('{$voteid}',document.getElementById('slider-input-{$voteid}').value,$minvalue,$maxvalue)\" title=\"" . wfMsg("slider-votetitle") . "\">" . wfMsg("slider-votebutton") . "</a>" .
                "</td>" .
              "</tr>" .
            "</table>" . 
            "<script type=\"text/javascript\">" .
              "var s = new Slider(document.getElementById(\"slider-{$voteid}\")," .
                                 "document.getElementById(\"slider-input-{$voteid}\"));" .
              ($myvote !== null ? "s.setValue({$myvote});" : "") .
              ($minvalue !== null ? "s.setMinimum({$minvalue});" : "") .
              ($maxvalue !== null ? "s.setMaximum({$maxvalue});" : "") .
            "</script>";
  }
}