User:Mdale/wikitrust.js

From Wikipedia, the free encyclopedia
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
/* to install put in User:UserName/monobook.js 
 * document.write('<script type="text/javascript" src="http://en.wikipedia.org/w/index.php?title=User:Mdale/wikitrust.js&action=raw&ctype=text/javascript">');
*/
//weird loading order issue necessitates wgNotice: : 
var wgNotice = "";
var wgNoticeLocal = "";
var wgNoticeLang = "en";
var wgNoticeProject = "wikipedia";

var trustServer = 'http://enwiki-trust.cse.ucsc.edu/index.php';
var wikitrust_api_loc = 'http://enwiki-trust.cse.ucsc.edu/api.php';
//var wikitrust_api_loc = 'http://gaston.cse.ucsc.edu:81/api.php';

var TdoLoad = true;

var sample_returned = {
	"query": {
		"normalized": [
			{
				"from": "Computer_graphics",
				"to": "Computer graphics"
			}
		],
		"pages": {
			"6518": {
				"pageid": 6518,
				"ns": 0,
				"title": "Computer graphics",
				"revisions": {
					"106858492": {
						"revid": 106858492,
						"pageid": 6518,
						"oldid": 106858492,
						"*": "\n{{#trust:3|begin}}Computer graphics {{#trust:4|begin}}is a sub-field of {{#trust:5|begin}}[[computer science]]{{#trust:5|begin}} and is {{#trust:6|begin}}concerned with digitally synthesizing {{#trust:7|begin}}and manipulating visual content.  Although the term often refers to three-dimensional computer graphics, it also encompasses two-dimensional graphics and image processing.  Graphics is often differentiated from the field of {{#trust:7|begin}}[[visualization (graphic)|visualization]]{{#trust:7|begin}}, although the two have many similarities.  Entertainment (in the form of animated movies and video games) is perhaps the most well-known application of graphics.\n\n{{#trust:7|begin}}Some major subproblems in computer graphics include:\n\n# {{#trust:7|begin}}describing the shape of an object (modeling)\n# {{#trust:7|begin}}describing the motion of an {{#trust:6|begin}}object (animation)\n# {{#trust:6|begin}}creating {{#trust:5|begin}}an image {{#trust:4|begin}}of an {{#trust:3|begin}}object (rendering)\n{{#trust:3|begin}}<br \/>{{#trust:3|begin}}\n{{#trust:3|begin}}[[Image:utah_teapot.png|thumb|300px|The [[Utah teapot]]]]{{#trust:4|begin}}\n\n=={{#trust:4|begin}} Branches of {{#trust:5|begin}}Computer Graphics ==\n\n==={{#trust:6|begin}} Modeling ===\n\n{{#trust:6|begin}}Modeling describes {{#trust:7|begin}}the shape of an object.  The two most common sources of 3D models are those created by an artist using some kind of 3D modeling tool, and those {{#trust:7|begin}}[[3d scanning|scanned]]{{#trust:7|begin}} into a computer from real-world objects.  Models can also be produced {{#trust:7|begin}}[[procedural modeling|procedurally]]{{#trust:7|begin}} or via {{#trust:7|begin}}[[physical simulation]]{{#trust:7|begin}}.\n\n{{#trust:7|begin}}Because the appearance of an object depends largely on the exterior of the object, {{#trust:7|begin}}[[boundary representation|boundary representations]]{{#trust:7|begin}} are most common in computer graphics.  Two dimensional {{#trust:7|begin}}[[surface|surfaces]]{{#trust:7|begin}} are a good analogy for the objects used in graphics, though quite often these objects are non-{{#trust:7|begin}}[[manifold]]{{#trust:7|begin}}.  Since surfaces are not finite, a {{#trust:6|begin}}discrete digital approximation is {{#trust:5|begin}}required: {{#trust:5|begin}}[[polygon mesh|polygonal meshes]]{{#trust:5|begin}} (and to a lesser extent {{#trust:5|begin}}[[subdivision surfaces]]{{#trust:5|begin}}) {{#trust:6|begin}}are by far the {{#trust:7|begin}}most common representation, although {{#trust:7|begin}}[[point-based rendering|point-based]]{{#trust:7|begin}} representations have been gaining some popularity in recent years.  {{#trust:7|begin}}[[level set|Level sets]]{{#trust:6|begin}} are a useful representation {{#trust:5|begin}}for deforming surfaces which undergo many topological changes such {{#trust:6|begin}}as {{#trust:6|begin}}[[fluids]]{{#trust:6|begin}}.\n\n===={{#trust:6|begin}} Subfields ====\n\n* {{#trust:7|begin}}{{#trust:7|begin}}[[Subdivision surfaces]]{{#trust:7|begin}}\n\n* {{#trust:7|begin}}{{#trust:7|begin}}[[Digital geometry processing]]{{#trust:7|begin}} - {{#trust:7|begin}}[[3d scanning|surface reconstruction]]{{#trust:7|begin}}, {{#trust:7|begin}}[[mesh simplification]]{{#trust:7|begin}}, {{#trust:7|begin}}[[mesh repair]]{{#trust:7|begin}}, {{#trust:7|begin}}[[mesh parameterization|parameterization]]{{#trust:7|begin}}, {{#trust:7|begin}}[[remeshing]]{{#trust:7|begin}}, {{#trust:7|begin}}[[mesh generation]]{{#trust:7|begin}}, {{#trust:7|begin}}[[mesh compression]]{{#trust:7|begin}}, and {{#trust:7|begin}}[[mesh editing]]{{#trust:7|begin}} all fall under this heading.\n\n* {{#trust:7|begin}}{{#trust:7|begin}}[[Discrete differential geometry]]{{#trust:7|begin}} - DDG is a recent topic which defines geometric quantities for the discrete surfaces used in computer graphics.\n\n* {{#trust:7|begin}}{{#trust:7|begin}}[[Point-based graphics]]{{#trust:7|begin}} - a recent field which focuses on points as the fundamental representation of surfaces.\n\n==={{#trust:7|begin}} Shading ===\n\n{{#trust:7|begin}}Texturing, or more generally, shading is the process of describing surface appearance.  This description can be as {{#trust:6|begin}}simple as the specification of a color in some {{#trust:6|begin}}[[colorspace]]{{#trust:6|begin}} or as elaborate as a {{#trust:6|begin}}[[shader]]{{#trust:6|begin}} program which describes numerous appearance {{#trust:5|begin}}attributes across the surface.  Simple shading is achieved with {{#trust:5|begin}}[[texture mapping]]{{#trust:5|begin}}, which uses a {{#trust:5|begin}}[[raster image]]{{#trust:5|begin}} to specify the diffuse color of a surface, giving it more apparent detail.  A {{#trust:6|begin}}more generic description of {{#trust:7|begin}}surface appearance is given by the {{#trust:7|begin}}[[bidirectional scattering distribution function]]{{#trust:7|begin}}, which describes the relationship between incoming and outgoing illumination at a given point.\n\n==={{#trust:7|begin}} Animation ===\n\n{{#trust:7|begin}}Animation refers to the temporal description of an object, i.e., how it moves and deforms over time.  There are numerous ways to describe these motion, many of which are used in conjunction with each-other.  Popular methods include {{#trust:7|begin}}[[keyframing]]{{#trust:7|begin}}, {{#trust:7|begin}}[[inverse kinematics]]{{#trust:7|begin}}, and {{#trust:7|begin}}[[motion capture]]{{#trust:7|begin}}.  As with modeling, {{#trust:7|begin}}[[physical simulation]]{{#trust:7|begin}} is another way of specifying motion.\n\n==={{#trust:7|begin}} Rendering ===\n\n{{#trust:7|begin}}Rendering converts a model into an image either by simulating {{#trust:7|begin}}[[light transport theory|light transport]]{{#trust:7|begin}} to get physically-based photorealistic images, or by applying some kind of style as in {{#trust:7|begin}}[[non-photorealistic rendering]]{{#trust:7|begin}}.  See {{#trust:7|begin}}[[Rendering]]{{#trust:7|begin}} for more information.\n\n===={{#trust:7|begin}} Subfields ====\n\n* {{#trust:7|begin}}{{#trust:7|begin}}[[physically-based rendering]]{{#trust:7|begin}} - concerned with generating images according to the laws of {{#trust:7|begin}}[[geometric optics]]{{#trust:7|begin}}\n* {{#trust:7|begin}}{{#trust:7|begin}}[[real time rendering]]{{#trust:7|begin}} - focuses on rendering for interactive applications, typically using specialized hardware like {{#trust:7|begin}}[[graphics processing unit|GPUs]]{{#trust:7|begin}}\n* {{#trust:7|begin}}{{#trust:7|begin}}[[non-photorealistic rendering]]{{#trust:7|begin}}\n* {{#trust:7|begin}}{{#trust:7|begin}}[[relighting]]{{#trust:7|begin}} - recent area concerned with quickly re-rendering scenes\n\n=={{#trust:7|begin}} History ==\n\n{{#trust:7|begin}}[[William Fetter]]{{#trust:7|begin}} was credited with coining the term {{#trust:8|begin}}Computer Graphics in 1960, to {{#trust:7|begin}}describe his work at {{#trust:7|begin}}[[Boeing]]{{#trust:7|begin}}.    One of the first displays of computer animation was {{#trust:7|begin}}[[Futureworld]]{{#trust:7|begin}} ({{#trust:7|begin}}[[1976]]{{#trust:8|begin}}), which included an {{#trust:8|begin}}[[animation]]{{#trust:8|begin}} of a human face and hand — produced {{#trust:7|begin}}by {{#trust:7|begin}}[[Edwin Catmull|Ed Catmull]]{{#trust:7|begin}} and {{#trust:7|begin}}[[Fred Parke]]{{#trust:7|begin}} at the {{#trust:7|begin}}[[University of Utah]]{{#trust:7|begin}}.\n\n{{#trust:7|begin}}There are several international conferences and journals where the most significant results in computer graphics are published. Among them are the {{#trust:7|begin}}[[SIGGRAPH]]{{#trust:7|begin}} and {{#trust:7|begin}}[[Eurographics]]{{#trust:7|begin}} conferences and the {{#trust:7|begin}}[[ACM]]{{#trust:7|begin}} Transactions on Graphics journal. The joint Eurographics and ACM SIGGRAPH symposium series features the major venues for the more specialized sub-fields: Symposium {{#trust:6|begin}}on Geometry Processing {{#trust:6|begin}}[http:\/\/www.geometryprocessing.org]{{#trust:6|begin}}, {{#trust:5|begin}}Symposium on Rendering, {{#trust:4|begin}}and Symposium on Computer Animation.\n\n{{#trust:5|begin}}An extensive history of computer {{#trust:4|begin}}graphics can be found {{#trust:3|begin}}at {{#trust:3|begin}}[http:\/\/accad.osu.edu\/~waynec\/history\/lessons.html]{{#trust:3|begin}}.\n\n=={{#trust:3|begin}} Applications ==\n\n*{{#trust:4|begin}}{{#trust:4|begin}}[[Movies|Special effects]]{{#trust:4|begin}}\n*{{#trust:4|begin}}{{#trust:4|begin}}[[Video games]]{{#trust:4|begin}}\n\n\n\n=={{#trust:4|begin}} Connected Studies ==\n\n* {{#trust:5|begin}}{{#trust:5|begin}}[[Computer vision]]{{#trust:5|begin}}\n* {{#trust:5|begin}}{{#trust:5|begin}}[[Image processing]]{{#trust:5|begin}}\n\n\n\n=={{#trust:5|begin}} See also ==\n\n{{#trust:6|begin}}Numerous sub-areas of computer graphics can {{#trust:7|begin}}be found {{#trust:6|begin}}in {{#trust:6|begin}}[[:Category:3D computer graphics]]{{#trust:6|begin}}.\n\n==={{#trust:6|begin}} Miscellaneous ===\n* {{#trust:6|begin}}{{#trust:6|begin}}[[Computer facial animation]]{{#trust:6|begin}}\n* {{#trust:6|begin}}{{#trust:6|begin}}[[Digital geometry]]{{#trust:7|begin}}\n* {{#trust:7|begin}}{{#trust:7|begin}}[[Digital image editing]]{{#trust:7|begin}}\n* {{#trust:7|begin}}{{#trust:7|begin}}[[Graphics processing unit]]{{#trust:7|begin}} (GPU)\n* {{#trust:7|begin}}{{#trust:7|begin}}[[Graphical output device]]{{#trust:7|begin}}s\n* {{#trust:6|begin}}{{#trust:6|begin}}[[Utah Teapot]]{{#trust:6|begin}}\n* {{#trust:6|begin}}{{#trust:6|begin}}[[Stanford Bunny]]{{#trust:6|begin}}\n* {{#trust:6|begin}}{{#trust:6|begin}}[[SIGGRAPH]]{{#trust:6|begin}}\n* {{#trust:6|begin}}{{#trust:6|begin}}[[Timeline of CGI in films]]{{#trust:6|begin}}\n* {{#trust:6|begin}}{{#trust:6|begin}}[[ASCII art]]{{#trust:6|begin}}\n\n=={{#trust:6|begin}} External {{#trust:5|begin}}Links ==\n{{#trust:6|begin}}{{Wiktionarypar|computer graphics}}{{#trust:6|begin}}\n*{{#trust:6|begin}}{{#trust:6|begin}}[http:\/\/accad.osu.edu\/~waynec\/history\/lessons.html A Critical History of Computer Graphics and Animation]{{#trust:6|begin}} \n*{{#trust:6|begin}}{{#trust:6|begin}}[http:\/\/hem.passagen.se\/des\/hocg\/hocg_1960.htm ''History of Computer Graphics'' series of articles]{{#trust:6|begin}}\n*{{#trust:6|begin}}{{#trust:6|begin}}[http:\/\/www.acid.org\/radio\/index.html#ARTS-EP05 The ARTS: Episode 5]{{#trust:7|begin}} An in depth interview with Legalize on {{#trust:8|begin}}the {{#trust:7|begin}}subject of the History of Computer {{#trust:6|begin}}Graphics. (Available in MP3 {{#trust:5|begin}}audio format)\n*{{#trust:5|begin}}{{#trust:5|begin}}[http:\/\/www.cgsociety.org\/ CGSociety]{{#trust:4|begin}} The Computer Graphics {{#trust:3|begin}}Society\n\n{{#trust:3|begin}}[[:Category:Computer graphics| ]]{{#trust:3|begin}}\n\n{{#trust:3|begin}}[[bs:Ra\u010dunarska grafika]]{{#trust:3|begin}}\n{{#trust:3|begin}}[[da:Computergrafik]]{{#trust:3|begin}}\n{{#trust:3|begin}}[[de:Computergrafik]]{{#trust:3|begin}}\n{{#trust:3|begin}}[[es:Gr\u00e1ficos por computadora]]{{#trust:3|begin}}\n{{#trust:3|begin}}[[eo:Komputila grafiko]]{{#trust:3|begin}}\n{{#trust:3|begin}}[[fr:Synth\u00e8se d'image]]{{#trust:3|begin}}\n{{#trust:3|begin}}[[hr:ra\u010dunalna grafika]]{{#trust:3|begin}}\n{{#trust:3|begin}}[[it:Computer grafica]]{{#trust:3|begin}}\n{{#trust:3|begin}}[[ko:\ucef4\ud4e8\ud130 \uadf8\ub798\ud53d\uc2a4]]{{#trust:3|begin}}\n{{#trust:3|begin}}[[nl:Computergraphics]]{{#trust:3|begin}}\n{{#trust:3|begin}}[[ja:\u30b3\u30f3\u30d4\u30e5\u30fc\u30bf\u30b0\u30e9\u30d5\u30a3\u30c3\u30af\u30b9]]{{#trust:3|begin}}\n{{#trust:3|begin}}[[ru:\u041a\u043e\u043c\u043f\u044c\u044e\u0442\u0435\u0440\u043d\u0430\u044f \u0433\u0440\u0430\u0444\u0438\u043a\u0430]]{{#trust:3|begin}}\n{{#trust:3|begin}}[[fi:Tietokonegrafiikka]]{{#trust:3|begin}}\n{{#trust:3|begin}}[[pl:grafika komputerowa]]{{#trust:3|begin}}\n{{#trust:3|begin}}[[sv:Datorgrafik]]{{#trust:3|begin}}\n{{#trust:3|begin}}[[zh:\u8ba1\u7b97\u673a\u56fe\u5f62\u5b66]]{{#trust:3|begin}}"
					}
				}
			}
		}
	}
}

//only activate on "view" pages: 
if(wgAction=="view"){
	addOnloadHook(do_disp_trust_link);	
}
//do color trust()
function do_color_trust(){   
bodyContent =  document.getElementById('bodyContent')
       bodyContent.innerHTML = 'loading<blink>...</blink>';
       if(typeof sajax_init_object == 'undefined'){
       		if(TdoLoad)loadExternalJs('/skins-1.5/common/ajax.js');
       		TdoLoad=false;
       		//js_log('still not set loading');
       		setTimeout('do_color_trust()', 20);
       }else{
       		js_log('do sjax');
       		TdoLoad=true;
	       	do_get_trustpage();
       }          
}
/*
 * do the api call: 
 */
function do_get_trustpage(){
	url = wikitrust_api_loc + '?format=json&action=query' +
			'&callback=wiki_trust_callback'+ 
			'&titles='+wgPageName+'&prop=revisions&rvprop=content';
	js_log('loading: ' + url);
	//loadExternalJs(url);
	wiki_trust_callback(sample_returned);
}
function wiki_trust_callback(result){
	js_log('wiki_trust_callback');
	//@@todo check if its an error or bad query
	for(i in result['query']['pages']){
		for(j in result['query']['pages'][i]['revisions']){	
			js_log('path: result[\'query\'][\'pages\']['+i+'][\'revisions\']['+j+'][\'*\']');		
			get_preivew_html(result['query']['pages'][i]['revisions'][j]['*']);
			break;
		}
	}
	//do_sajax_request( [0]['revisions'][0]);
}
//uses the wikitrust_page global for content: 
function get_preivew_html(wikiturst_text){	
	   wikiturst_text = swap_trust_template_for_span_color(wikiturst_text);
	   a = sajax_init_object();
	   a.open("POST", '/w/index.php?title='+wgPageName+'&action=submit&live', true);
	   var Boundary = '--------p1415';
	   a.setRequestHeader('Content-Type', 'multipart/form-data; boundary='+Boundary);
	   var PostData = '--' + Boundary 
			+ '\nContent-Disposition: form-data; name="wpTextbox1"\n\n'
			+ wikiturst_text;
	   if (a.overrideMimeType) a.overrideMimeType('text/html');
	   js_log('send post data: ' + PostData);
	   a.send(PostData);
	   a.onreadystatechange = function(){
			if (a.readyState != 4) return;
			var html = a.responseText;
			html = html.replace(/&gt;/g,'>').replace(/&lt;/g,'<').replace(/&quot;/g,'"').replace(/&amp;/g,'&').replace(/&apos;/g,"'");			
			//get rid of the preview note: first <h2> to close </dvi> 
			var sp = html.indexOf('<h2>');
			var ep = html.indexOf('</div>');			
			html = html.substr(0,sp) + html.substr(ep);
			bodyContent.innerHTML = html;										
	};
}
function swap_trust_template_for_span_color(text){
	/*text = text.replace('/{{#trust:1|begin}}/g', '<span style="background-color:#FFD085;">');
	text = text.replace('/{{#trust:2|begin}}/g', '<span style="background-color:#FFD085;">');
	text = text.replace('/{{#trust:3|begin}}/g', '<span style="background-color:#FFD085;">');
	text = text.replace('/{{#trust:4|begin}}/g', '<span style="background-color:#FFD085;">');
	text = text.replace('/{{#trust:5|begin}}/g', '<span style="background-color:#FFD085;">');*/
	
	text = text.replace(/\{\{#trust:([0-9])\|begin\}\}/g, replace_callback);
	return text;
}
function replace_callback(trust_string){
	num = trust_string.substring(9,trust_string.indexOf('|',9));
	var c='';
	switch(num){
		case '0':c='FF8888';break;
		case '1':c='FF9999';break;
		case '2':c='FF9898';break;
		case '3':c='FFAAAA';break;
		case '3':c='FFA8A8';break;
		case '4':c='FFBBBB';break;
		case '5':c='FFB8B8';break;
		case '6':c='FFCCCC';break;
		case '7':c='FFC8C8';break;
		case '8':c='FFEEEE';break;
		case '9':c='FFE8E8';break;
		case '10':c='FFFFFF';break;
	}
	return '<span style="background-color:#'+c+';">';
}
// replace callback support for safari.
(function(){
  var default_replace = String.prototype.replace;
  String.prototype.replace = function(search,replace){
        // replace is not function
        if(typeof replace != "function"){
                return default_replace.apply(this,arguments)
        }
        var str = "" + this;
        var callback = replace;
        // search string is not RegExp
        if(!(search instanceof RegExp)){
                var idx = str.indexOf(search);
                return (
                        idx == -1 ? str :
                        default_replace.apply(str,[search,callback(search, idx, str)])
                )
        }
        var reg = search;
        var result = [];
        var lastidx = reg.lastIndex;
        var re;
        while((re = reg.exec(str)) != null){
                var idx  = re.index;
                var args = re.concat(idx, str);
                result.push(
                        str.slice(lastidx,idx),
                        callback.apply(null,args).toString()
                );
                if(!reg.global){
                        lastidx += RegExp.lastMatch.length;
                        break
                }else{
                        lastidx = reg.lastIndex;
                }
        }
        result.push(str.slice(lastidx));
        return result.join("")
  }
})();

function do_disp_trust_link(){		
	newLi = document.createElement('li');
	newLi.id = 'ca-trust';
var titlePos = document.URL.indexOf('/wiki/');
    if( titlePos !=-1 ){
       var page_title = document.URL.substr(titlePos+6); 
}
	newLi.innerHTML = '<a href="javascript:do_color_trust();" title="View Trust analysis">View Trust</a>';
	//newLi.innerHTML = '<a href="'+trustServer+page_title+'" onclick="do_color_trust();" title="View Trust analysis">View Trust</a>';

	lastLi = document.getElementById('ca-watch');
	if(lastLi)
		lastLi.parentNode.appendChild(newLi);
}
/* 
 * this won't work in some versions of Safari
 * you have to do your document.writes before the page is done rendering..
 * ....so @@todo for safari: 
 * have the view_trust issue a page refresh with flag in get request:
 * and check window.location.search paramaters to do the remote request 
 * with a document.write insted of DOM injection. 
 */
function loadExternalJs(url){
   //js_log('load js: ' + url);
   var e = document.createElement("script");
   e.src = url;
   e.type="text/javascript";
   document.getElementsByTagName("head")[0].appendChild(e);  
}
function js_log(string){
   if( window.console ){
        console.log(string); 
   }else{
   	 //disable alerts
     //alert(string);          
   }
}