Open main menu

User:Diploid/ATS (Article Tagging System)/ATS.php

<?php
# Rob's Article Tagging System (ATS) for MediaWiki! Extension: WikiInterface.php - a work in progress
 /* Copyright (c) 2007 MediaWiki.org User: Diploid
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy 
 * of this software and associated documentation files (the "Software"), to deal 
 * in the Software without restriction, including without limitation the rights to 
 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 
 * the Software, and to permit persons to whom the Software is furnished to do 
 * so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all 
 * copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 
 * OTHER DEALINGS IN THE SOFTWARE. 
 * -----------------------------------------------------------------------
 */

if ( ! defined( 'MEDIAWIKI' ) )
	die( 1 );
	
require(dirname(__FILE__) .'/ATS_Body.php');
$ATSajax = new ATSajaxresponse();
$ATSajax->Setup();

/*$wgOut->addLink( 
	array( 
		'rel' => 'stylesheet', 
		'type' => 'text/css', 
		'href' => $wgScriptPath . '/extensions/ATS/ATS.css' 
	)
);*/

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

//$wgHooks['BeforePageDisplay'][] = 'ATSloadeventimage';


# Define a setup function
$wgExtensionFunctions[] = 'wfATSParserFunction_Setup';
# Add a hook to initialise the magic word
$wgHooks['LanguageGetMagic'][]       = 'wfATSParserFunction_Magic';

function wfATSParserFunction_Setup() {
        global $wgParser;
        # Set a function hook associating the "tagcloud" magic word with our function
        $wgParser->setFunctionHook( 'tagcloud', 'ATSwikitext' );
        # A tagcloudhtmlonly function is inserted in the wikitext by the tagcloud function
        $wgParser->setFunctionHook( 'tagcloudhtmlonly', 'ATShtml' );
		
        # Used in categories that have tagged pages to get a list of the top tagged pages for that category
        $wgParser->setFunctionHook( 'tagranking', 'ATScategoryTagRanking' );

}
 
function wfATSParserFunction_Magic( &$magicWords, $langCode ) {
        # Add the magic word
        # The first array element is case sensitive, in this case it is not case sensitive
        # All remaining elements are synonyms for our parser function
        $magicWords['tagcloud'] = array( 0, 'tagcloud' );
        $magicWords['tagcloudhtmlonly'] = array( 0, 'tagcloudhtmlonly' );
        $magicWords['tagranking'] = array( 0, 'tagranking' );
        # unless we return true, other parser functions extensions won't get loaded.
        return true;
}

function IsMetaKeywordsInstalled()
{
	global $wgExtensionCredits;
	foreach ($wgExtensionCredits['parserhook'] as $index=>$extension)
	{
		try {
			if ($extension['name'] == 'MetaKeywordsTag') return true;
		}
		catch (Exception $e) {
		}
	}
	return false;
}

function ATSwikitext(&$parser){
	global $wgTitle, $ATSajax;
	if (IsMetaKeywordsInstalled() == true)
	{
	$metatags = "<keywords content=\"".$ATSajax->GetTags_CommaSeperatedText($wgTitle->getPrefixedText())."\" />";
	}
	
	$tagdata = $ATSajax->GetTags($wgTitle->getPrefixedText());
	$size = count($tagdata);
	$categories = "";
	if ($tagdata!=false && $size != 0)
	{
		for ($i=0; $i<$size; $i++)
		{
			$categories .= "[[Category:".$tagdata[$i]["Tag"]."]]";
		}
	}
	
	$output = ""
	."{{#tagcloudhtmlonly:}}"
	.$metatags
	.$categories
	.""
	;
	return $output;
}

function ATShtml( &$parser) {
		global $Path;
        # The parser function itself
        # The input parameters are wikitext with templates expanded
        # The output should be wikitext too
        
        #returns the div which is then manipulated by javascript to contain the
        #tag cloud upon the page being loaded
        $output =""
        #Rounded Corners - Top
        ."<div id=\"ATSborder\">"
		."<b class=\"rtop\"><b class=\"r1\"></b><b class=\"r2\"></b><b class=\"r3\"></b><b class=\"r4\"></b></b>"
        #Content
        ."<div  id=\"ATStagcloud\">"
        ."</div>"
        #Various Loading Overlays for grey out and loading message
        ."<div  id=\"ATStagcloudOverlayStreamliner\">"    
        ."<div id=\"ATStagcloudOverlay\" class=\"ATShidden\">"
        ."<img src=\"".$Path."/extensions/ATS/ajax-loader.gif\" onLoad=\"AsyncLoadATS('');\">"
        ." Loading..."
        ."</div>"
        ."</div>"
        #Rounded Corners - Bottom
        ."<b class=\"rbottom\"><b class=\"r4\"></b><b class=\"r3\"></b><b class=\"r2\"></b><b class=\"r1\"></b></b>"
		."</div>"
        ;
        $parser->disableCache();
        return array($output, 'noparse' => true, 'isHTML' => true);
}

function ATScategoryTagRanking ( &$parser)
{
		global $Path, $wgTitle, $ATSajax;
        # The parser function itself
        # The input parameters are wikitext with templates expanded
        # The output should be wikitext too
        
        #Getting the list of pages tagged as belonging to this category and the number of tags they each have
        $tag = str_replace("Category:","",$wgTitle->getPrefixedText());
    	$tagdata = $ATSajax->GetTaggedPages($tag);
		$size = count($tagdata);
		$pagelist = "";
		if ($tagdata!=false && $size != 0)
		{
			for ($i=0; $i<$size; $i++)
			{
				$tagurl = $Path."/index.php?title=".$tagdata[$i]["Page"];
				$pagelist .=
				"<li><a href=\"".$tagurl."\">".$tagdata[$i]["Page"]."</a> (".$tagdata[$i]["tagcount"].")</li>";
			}
		}
        
        #returns the div which is then manipulated by javascript to contain the
        #tag cloud upon the page being loaded
        $output =""
        ."<div  id=\"ATStagranking\">"
        #Rounded Corners - Top
		."<b class=\"rtop\"><b class=\"r1\"></b><b class=\"r2\"></b><b class=\"r3\"></b><b class=\"r4\"></b></b>"
        #Content
        ."<div  id=\"ATStagranking_content\">"
        ."<h2>Most Tagged</h2>"
        ."<ul>"
        .$pagelist
        ."</ul>"
        ."</div>"
        #Rounded Corners - Bottom
        ."<b class=\"rbottom\"><b class=\"r4\"></b><b class=\"r3\"></b><b class=\"r2\"></b><b class=\"r1\"></b></b>"
        ."</div>"
        ;
        $parser->disableCache();
        return array($output, 'noparse' => true, 'isHTML' => true);

}

function MakeHashKey()
{
	global $wgTitle, $wgUser, $ATSajax;
	if ($wgUser->isLoggedIn())
	{
		$sessiondata['ATSuser'] = $wgUser->getName();
	}
	else
	{
		return "";
	};
	$sessiondata['ATSpage'] = $wgTitle->getPrefixedText();
	$key = (rand()%99999999999);
	$sessiondata['ATShash'] = sha1($key);
	$ATSajax->PostSessionData($sessiondata, $sessiondata['ATSuser']);
	return $key;
}


function ATSaddscript() {
	global $wgOut, $wgUser, $Path, $wgTitle;
	//$wgOut->addScript("<script type=\"text/javascript\" />");
//<img src=\"".$Path."/extensions/ATS/loading_circle.gif\" style=\"position:absolute; left: -500px; top: 0px; width: 1px;\" onLoad=\"AsyncLoadATS('', '');\">
	$wgOut->addScript("<link rel=\"stylesheet\" type=\"text/css\" href=\"".$Path."/extensions/ATS/ATS.css\" />");
	$wgOut->addScript("
<script type=\"text/javascript\">
function AsyncLoadATS(tag)
{
  var page = \"".$wgTitle->getPrefixedText()."\";
  var user = \"".$wgUser->getName()."\";
  var id = \"".MakeHashKey()."\";

  var ATSdiv = document.getElementById('ATStagcloud');
  var ATSloadingDiv = document.getElementById('ATStagcloudOverlay');
  ATSdiv.setAttribute('class',\"ATSloading\");
  ATSloadingDiv.setAttribute('class',\"ATSvisible\");

  args = '&ATSuser=' + user;
  args = args + '&ATSpagename=' + page;
  args = args + '&ATSid=' + id;
  if (tag!='') args = args + '&ATSaddtag=' + tag;
  
	
  var xmlHttp;
  try
    {
    // Firefox, Opera 8.0+, Safari
    xmlHttp=new XMLHttpRequest();
    }
  catch (e)
    {
    // Internet Explorer
    try
      {
      xmlHttp=new ActiveXObject(\"Msxml2.XMLHTTP\");
      }
    catch (e)
      {
      try
        {
        xmlHttp=new ActiveXObject(\"Microsoft.XMLHTTP\");
        }
      catch (e)
        {
        alert(\"Your browser does not support AJAX!\");
        return false;
        }
      }
    }
    xmlHttp.onreadystatechange=function()
      {
      if(xmlHttp.readyState==4)
        {
	ATSdiv.innerHTML = xmlHttp.responseText;
    ATSdiv.setAttribute('class',\"ATSloaded\");
    ATSloadingDiv.setAttribute('class',\"ATShidden\");
        }
      }

    xmlHttp.open(\"GET\",\"".$Path."/extensions/ATS/ATS_HTML_Update.php?\"+args,true);
    xmlHttp.send(null);


}


</script>
\n"
	);
}