Jump to content

User:Proteins/summarizeFAC.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.
//<pre>
// This script counts and lists FAC !votes.  Modeled after the summarizeAfD.js script written for Graham87.
//
// To use this script, add "importScript('User:Proteins/summarizeFAC.js');" to your monobook.js subpage 
// under your user page, as you can see at User:Proteins/monobook.js


//******************
// The Main Function
//******************

function summarizeFAC() {
	var alert_string = "";
	var reported_string = "";
	var diagnostic_string = "";
 
	var indented;
	var top_node;
	var parent_node;
 
	var list_items;
	var temp_list_item;
	var num_list_items = 0;
	var list_item_index = 0;
	var num_unindented_list_items = 0;
 
	var boldface_elements;
	var boldface_text = "";
	var first_boldface_element;
	var num_boldface_elements = 0;
	var boldface_element_index = 0;
 
	var num_votes = 0;
	var num_notes = 0;
	var num_comments = 0;
	var num_questions = 0;
	var num_oppose_votes = 0;
	var num_support_votes = 0;
	var num_neutral_votes = 0;
	var num_ambiguous_votes = 0;
	var num_error_messages = 0;
 
	var note_index = 0;
	var comment_index = 0;
	var question_index = 0;
	var oppose_vote_index = 0;
	var support_vote_index = 0;
	var neutral_vote_index = 0;
	var ambiguous_vote_index = 0;
	var error_message_index = 0;
 
	var note_summary = "";
	var comment_summary = "";
	var question_summary = "";
	var oppose_vote_summary = "";
	var support_vote_summary = "";
	var neutral_vote_summary = "";
	var ambiguous_vote_summary = "";
	var error_message_summary = "";
 
	var notes = new Array();
	var comments = new Array();
	var questions = new Array();
	var oppose_votes = new Array();
	var support_votes = new Array();
	var neutral_votes = new Array();
	var ambiguous_votes = new Array();
	var error_messages = new Array();
 
	var user_name = "";
	var alias_name = "";
	var user_talk_name = "";
	var alias_talk_name = "";
 
	var list_item_links;
	var temp_list_item_link;
	var num_list_item_links = 0;
	var list_item_link_index = 0;
	var list_item_link_text = "";
 
	var possible_user_name = true;
	var num_enclosing_list_tags = 0;


//****************
// Initializations
//****************

// Get the top node
	top_node = document.getElementById('bodyContent');
	if (!top_node) { 
		window.alert("No bodyContent node in this document.");
		return;
	}
 
// Get the list items
	list_items = top_node.getElementsByTagName("LI");
	if (!list_items) { 
		window.alert("This article has no list items.");
		return;
	}
	num_list_items = list_items.length;
 
 
// Variable initializations
 
	num_notes = 0;
	num_comments = 0;
	num_questions = 0;
	num_oppose_votes = 0;
	num_support_votes = 0;
	num_neutral_votes = 0;
	num_ambiguous_votes = 0;
 
	notes[0] = "NONE";
	comments[0] = "NONE";
	questions[0] = "NONE";
	oppose_votes[0] = "NONE";
	support_votes[0] = "NONE";
	neutral_votes[0] = "NONE";
	ambiguous_votes[0] = "NONE";


//************************* 
// Loop over the list items
//*************************
	num_unindented_list_items = 0;
	for (list_item_index=1; list_item_index<=num_list_items; list_item_index++) { 
		temp_list_item = list_items[list_item_index-1];
		if (!temp_list_item) {
			window.alert("List item " + list_item_index + " doesn't exist."); 
			continue; 
		} // shouldn't happen, but...
 
		// find the enclosing ordered or unordered list
		found_parent_list = false;
		parent_node = temp_list_item.parentNode;
		while ((parent_node) && (parent_node != top_node)) {
			if (parent_node.nodeType != 1) {
				parent_node = parent_node.parentNode;
				continue; 
			} // examine only Element nodes
 
			if ((parent_node.nodeName == "UL") || (parent_node.nodeName == "OL")) { 
				found_parent_list = true; 
				break;
			} // closes check for a parental list element
			parent_node = parent_node.parentNode;
		} // closes loop climbing up the document tree
		if (found_parent_list == false) {
			diagnostic_string = "List item " + list_item_index + " doesn't have an enclosing list:\n\n";
			diagnostic_string += temp_list_item.innerHTML.replace(/(<([^>]+)>)/ig,"") + "\n";
			window.alert(diagnostic_string); 
			continue; 			
		}
 
		// check that the list item is not indented
		indented = false;
		parent_node = parent_node.parentNode;
		while ((parent_node) && (parent_node != top_node)) {
			if (parent_node.nodeType != 1) {
				parent_node = parent_node.parentNode;
				continue; 
			} // examine only Element nodes
 
			if ((parent_node.nodeName == "DD") || (parent_node.nodeName == "DL")  || (parent_node.nodeName == "UL") || (parent_node.nodeName == "OL") || (parent_node.nodeName == "LI")) { 
				indented = true; 
				break;
			} // closes check for a parental list element
			parent_node = parent_node.parentNode;
		} // closes loop climbing up the document tree
		if (indented == true) { 
//			window.alert("List item " + list_item_index + " is indented."); 
			continue; 
		}
		num_unindented_list_items++;
 
		// get the first bold-faced item within this list item
		boldface_elements = temp_list_item.getElementsByTagName("B");
		if (!boldface_elements) {
			diagnostic_string = "List item " + list_item_index + " has no boldface elements:\n\n";
			diagnostic_string += temp_list_item.innerHTML.replace(/(<([^>]+)>)/ig,"") + "\n";
			window.alert(diagnostic_string); 
			continue; 
		}
		num_boldface_elements = boldface_elements.length;
		if (num_boldface_elements == 0) { 
//			window.alert("List item " + list_item_index + " has no first boldface element.\n");
			continue;
		} else if (num_boldface_elements == 1) {
//			window.alert("List item " + list_item_index + " has one boldface element.\n");
		} else { 
//			window.alert("List item " + list_item_index + " has " + num_boldface_elements + " boldface elements.\n");
		}
 
		first_boldface_element = null;
		for (boldface_element_index = 0; boldface_element_index<num_boldface_elements; boldface_element_index++) { 
			first_boldface_element = boldface_elements[boldface_element_index];
 
			parent_node = parent_node.parentNode;
			while ((parent_node) && (parent_node != temp_list_item)) {
				if (parent_node.nodeName == "S") { 
					first_boldface_element = null;
					break;
				}
				parent_node = parent_node.parentNode;
			}
			if (first_boldface_element) { break; }
		} // closes loop identifying the first valid boldface element of this list item
		if (!first_boldface_element) {
			window.alert("List item " + list_item_index + " has no first boldface element.\n"); 
			continue; 
		}
 
		// Find the user name 
		list_item_links = temp_list_item.getElementsByTagName("A");
		if (!list_item_links) {
			diagnostic_string = "List item " + list_item_index + " has no links:\n\n";
			diagnostic_string += temp_list_item.innerHTML.replace(/(<([^>]+)>)/ig,"") + "\n";
			window.alert(diagnostic_string); 
			continue; 
		}
		num_list_item_links = list_item_links.length;
		if (num_list_item_links == 0) {
			diagnostic_string = "List item " + list_item_index + " has no links:\n\n";
			diagnostic_string += temp_list_item.innerHTML.replace(/(<([^>]+)>)/ig,"") + "\n";
			window.alert(diagnostic_string);  
			continue;
		}
 
		user_name = "";
		user_talk_name = "";
		for (list_item_link_index=num_list_item_links; list_item_link_index>0; list_item_link_index--) { 
			temp_list_item_link = list_item_links[list_item_link_index-1]; 
			if (!temp_list_item_link) { 
				window.alert("The " + list_item_link_index + " link in the " + list_item_index + "list item doesn't exist.\n");
				continue;
			}
 
			possible_user_name = true;
			num_enclosing_list_tags = 0;
 
			parent_node = temp_list_item_link.parentNode; 
			while ((parent_node) && (parent_node != top_node)) {
				if (parent_node.nodeType != 1) {
					parent_node = parent_node.parentNode;
					continue; 
				} // examine only Element nodes
 
				if (parent_node.nodeName == "S") { 
					possible_user_name = false;
					break;
				}
				if (parent_node.nodeName == "UL") { 
					possible_user_name = false;
					break;
				}
				if (parent_node.nodeName == "OL") { 
					possible_user_name = false;
					break;
				}
				if (parent_node.nodeName == "DL") { 
					possible_user_name = false;
					break;
				}
				if (parent_node.nodeName == "DD") { 
					possible_user_name = false;
					break;
				}
				if (parent_node.nodeName == "LI") { 
					num_enclosing_list_tags++; 
					if (num_enclosing_list_tags > 1) { 
						possible_user_name = false;
						break;
					}
				} // closes check for a parental list element
				if (parent_node == temp_list_item) { 
					break;
				}
				parent_node = parent_node.parentNode;
			} // closes loop climbing up the document tree
			if (possible_user_name == false) { continue; }
			if (num_enclosing_list_tags != 1) { continue; }
			if (parent_node != temp_list_item) { continue; }
 
			list_item_link_text = temp_list_item_link.title;
			list_item_link_text = list_item_link_text.replace(/(<([^>]+)>)/ig,""); // remove HTML tags
			if ((list_item_link_text.match(/^User:/)) && (!list_item_link_text.match(/\//))) { 
				user_name = list_item_link_text.replace(/^User:/, "");
				user_name = user_name.replace(/\s+\(page does not exist\)$/, "");
				user_name = user_name.replace(/&nbsp;/ig, " ");  // convert non-breaking spaces to spaces
				user_name = user_name.replace(/&#160;/ig, " ");  // convert non-breaking spaces to spaces
				user_name = user_name.replace(/\s+/ig, " "); // convert all whitespace to a single space
				alias_name = temp_list_item_link.innerHTML;
				alias_name = alias_name.replace(/(<([^>]+)>)/ig,""); // remove HTML tags
				alias_name = alias_name.replace(/&nbsp;/ig, " ");  // convert non-breaking spaces to spaces
				alias_name = alias_name.replace(/&#160;/ig, " ");  // convert non-breaking spaces to spaces
				alias_name = alias_name.replace(/\s+/ig, " "); // convert all whitespace to a single space
				if (alias_name.replace(/\s/g,"").toLowerCase() != user_name.replace(/\s/g,"").toLowerCase()) { 
					user_name += " (" + alias_name + ")";
				}
				break;
			}
			if ((list_item_link_text.match(/^User\stalk:/)) && (!list_item_link_text.match(/\//))) { 
				user_talk_name = list_item_link_text.replace(/^User\stalk:/, "");
				user_talk_name = user_talk_name.replace(/\s+\(page does not exist\)$/, "");
				user_talk_name = user_talk_name.replace(/&nbsp;/ig, " ");  // non-breaking spaces to spaces
				user_talk_name = user_talk_name.replace(/&#160;/ig, " ");  // non-breaking spaces to spaces
				user_talk_name = user_talk_name.replace(/\s+/ig, " "); // all whitespace to a single space
				alias_talk_name = temp_list_item_link.innerHTML;
				alias_talk_name = alias_talk_name.replace(/(<([^>]+)>)/ig,""); // remove HTML tags
				alias_talk_name = alias_talk_name.replace(/&nbsp;/ig, " ");  // non-breaking spaces to spaces
				alias_talk_name = alias_talk_name.replace(/&#160;/ig, " ");  // non-breaking spaces to spaces
				alias_talk_name = alias_talk_name.replace(/\s+/ig, " "); // all whitespace to a single space
				if (alias_talk_name.replace(/\s/g,"").toLowerCase() != user_talk_name.replace(/\s/g,"").toLowerCase()) { 
					user_talk_name += " (" + alias_talk_name + ")";
				}
			}
		} // closes loop over the list-item links 
		if (!user_name) {
			if (!user_talk_name) {
				diagnostic_string = "List item " + list_item_index + " has no user name.\n\n";
				diagnostic_string += temp_list_item.innerHTML.replace(/(<([^>]+)>)/ig,"") + "\n";
				window.alert(diagnostic_string); 
				continue;
			} else { 
				user_name = user_talk_name;
			}
		}
 
		num_boldface_elements = boldface_elements.length;
		if (num_boldface_elements == 0) { 
//			window.alert("List item " + list_item_index + " has no first boldface element.\n");
			continue;
		} else if (num_boldface_elements == 1) {
//			window.alert("List item " + list_item_index + " has one boldface element.\n");
		} else { 
//			window.alert("List item " + list_item_index + " has " + num_boldface_elements + " boldface elements.\n");
		}
 
 
		// Analyze the votes
		boldface_text = first_boldface_element.innerHTML;
		if (!boldface_text) {
			 window.alert("List item " + list_item_index + " has no boldface text.\n");
		} 
		boldface_text = boldface_text.replace(/(<([^>]+)>)/ig,""); // remove HTML tags
//		window.alert("List item " + list_item_index + " has boldface text: " + boldface_text + "\n"); 
 
 
		if (boldface_text.match(/support/ig)) {
			num_support_votes++; 
			reported_string = num_support_votes + ". " + boldface_text + " — " + user_name + "\n";
//			window.alert(reported_string);
			support_votes[num_support_votes-1] = reported_string;
		} else if (boldface_text.match(/neutral/ig)) { 
			num_neutral_votes++;
			reported_string = num_neutral_votes + ". " + boldface_text + " — " + user_name + "\n";
			neutral_votes[num_neutral_votes-1] = reported_string;
		} else if (boldface_text.match(/oppos/ig)) { 
			num_oppose_votes++;
			reported_string = num_oppose_votes + ". " + boldface_text + " — " + user_name + "\n";
			oppose_votes[num_oppose_votes-1] = reported_string;
		} else if (boldface_text.match(/comment/ig)) { 
			num_comments++; 
			reported_string = num_comments + ". " + boldface_text + " — " + user_name + "\n";
			comments[num_comments-1] = reported_string;
		} else if (boldface_text.match(/question/ig)) { 
			num_questions++; 
			reported_string = num_questions + ". " + boldface_text + " — " + user_name + "\n";
			questions[num_questions-1] = reported_string;
		} else if (boldface_text.match(/note/ig)) { 
			num_notes++; 
			reported_string = num_notes + ". " + boldface_text + " — " + user_name + "\n";
			notes[num_notes-1] = reported_string;
		} else {
			num_ambiguous_votes++;
			reported_string = num_ambiguous_votes + ". " + boldface_text + " — " + user_name + "\n";
			ambiguous_votes[num_ambiguous_votes-1] = reported_string;
		} 
	} // closes loop over list items
 
	num_votes = num_support_votes + num_oppose_votes + num_neutral_votes + num_ambiguous_votes;

//	window.alert("There were " + num_votes + " !votes.\n\n");


//**************
// Print summary
//**************
	alert_string = "";
 
	if (num_votes == 1) { 
		alert_string += "One !vote was counted:\n";
	} else { 
		alert_string += num_votes + " !votes were counted:\n";
	}
	if (num_oppose_votes == 1) { 
		alert_string += "\t one Oppose !vote\n"; 
		oppose_vote_summary = "There was one Oppose !vote:\n\n"; 
	} else if (num_oppose_votes>1) { 
		alert_string += "\t " + num_oppose_votes + " Oppose !votes\n";
		oppose_vote_summary = "There were " + num_oppose_votes + " Oppose !votes:\n\n"; 
	}
	if (num_support_votes == 1) { 
		alert_string += "\t one Support !vote\n"; 
		support_vote_summary = "There was one Support !vote:\n\n"; 
	} else if (num_support_votes>1) { 
		alert_string += "\t " + num_support_votes + " Support !votes\n";
		support_vote_summary = "There were " + num_support_votes + " Support !votes:\n\n"; 
	}
	if (num_neutral_votes == 1) { 
		alert_string += "\t one Neutral !vote\n"; 
		neutral_vote_summary = "There was one Neutral !vote:\n\n"; 
	} else if (num_neutral_votes>1) { 
		alert_string += "\t " + num_neutral_votes + " Neutral !votes\n"; 
		neutral_vote_summary = "There were " + num_neutral_votes + " Neutral !votes:\n\n"; 
	}
	if (num_ambiguous_votes == 1) { 
		alert_string += "\t one ambiguous !vote\n"; 
		ambiguous_vote_summary = "There was one ambiguous !vote:\n\n"; 
	} else if (num_ambiguous_votes>1) { 
		alert_string += "\t " + num_ambiguous_votes + " ambiguous !votes\n";
		ambiguous_vote_summary = "There were " + num_ambiguous_votes + " ambiguous !votes:\n\n"; 
	}
	if ((num_notes + num_comments + num_questions) == 1) { 
		alert_string += "\nIn addition, there was:\n";
	} else if ((num_notes + num_comments + num_questions)>1) { 
		alert_string += "\nIn addition, there were:\n";
	}
	if (num_notes == 1) { 
		alert_string += "\t one Note\n";
		note_summary = "There was one Note:\n\n"; 
	} else if (num_notes>1) { 
		alert_string += "\t " + num_notes + " Notes\n";
		note_summary = "There were " + num_notes + " Notes:\n\n"; 
	}
	if (num_comments == 1) {  
		alert_string += "\t one Comment\n";
		comment_summary = "There was one Comment:\n\n";  
	} else if (num_comments>1) { 
		alert_string += "\t " + num_comments + " Comments\n"; 
		comment_summary = "There were " + num_comments + " comments:\n\n"; 
	}
	if (num_questions == 1) {  
		alert_string += "\t one Question\n";
		question_summary = "There was one Question:\n\n";  
	} else if (num_questions>1) { 
		alert_string += "\t " + num_questions + " Questions\n"; 
		question_summary = "There were " + num_questions + " Questions:\n\n"; 
	}
	window.alert(alert_string);


//*************************************
// Report details on each type of !vote
//*************************************
	if (num_oppose_votes > 0) {
		alert_string = oppose_vote_summary;
		for (oppose_vote_index=0; oppose_vote_index<num_oppose_votes; oppose_vote_index++) {
			if ((oppose_vote_index%40 == 0) && (oppose_vote_index > 1)) {
				alert_string += "\nContinued on next screen..."; 
				window.alert(alert_string);
				alert_string = "Continued from previous screen...\n\n";
			}
			alert_string += oppose_votes[oppose_vote_index];
		}
		window.alert(alert_string);
	}
	if (num_support_votes > 0) { 
		alert_string = support_vote_summary;
		for (support_vote_index=0; support_vote_index<num_support_votes; support_vote_index++) {
			if ((support_vote_index%40 == 0) && (support_vote_index > 1)) {
				alert_string += "\nContinued on next screen..."; 
				window.alert(alert_string);
				alert_string = "Continued from previous screen...\n\n";
			}
			alert_string += support_votes[support_vote_index];
		}
		window.alert(alert_string);
	}
	if (num_neutral_votes > 0) { 
		alert_string = neutral_vote_summary;
		for (neutral_vote_index=0; neutral_vote_index<num_neutral_votes; neutral_vote_index++) {
			if ((neutral_vote_index%40 == 0) && (neutral_vote_index > 1)) {
				alert_string += "\nContinued on next screen..."; 
				window.alert(alert_string);
				alert_string = "Continued from previous screen...\n\n";
			}
			alert_string += neutral_votes[neutral_vote_index];
		}
		window.alert(alert_string);
	}
	if (num_ambiguous_votes > 0) { 
		alert_string = ambiguous_vote_summary;
		for (ambiguous_vote_index=0; ambiguous_vote_index<num_ambiguous_votes; ambiguous_vote_index++) {
			if ((ambiguous_vote_index%40 == 0) && (ambiguous_vote_index > 1)) {
				alert_string += "\nContinued on next screen..."; 
				window.alert(alert_string);
				alert_string = "Continued from previous screen...\n\n";
			}
			alert_string += ambiguous_votes[ambiguous_vote_index];
		}
		window.alert(alert_string);
	}
	if (num_comments > 0) { 
		alert_string = comment_summary;
		for (comment_index=0; comment_index<num_comments; comment_index++) {
			if ((comment_index%40 == 0) && (comment_index > 1)) {
				alert_string += "\nContinued on next screen..."; 
				window.alert(alert_string);
				alert_string = "Continued from previous screen...\n\n";
			}
			alert_string += comments[comment_index];
		}
		window.alert(alert_string);
	}
	if (num_questions > 0) { 
		alert_string = question_summary;
		for (question_index=0; question_index<num_questions; question_index++) {
			if ((question_index%40 == 0) && (question_index > 1)) {
				alert_string += "\nContinued on next screen..."; 
				window.alert(alert_string);
				alert_string = "Continued from previous screen...\n\n";
			}
			alert_string += questions[question_index];
		}
		window.alert(alert_string);
	}
	if (num_notes > 0) { 
		alert_string = note_summary;
		for (note_index=0; note_index<num_notes; note_index++) {
			if ((note_index%40 == 0) && (note_index > 1)) {
				alert_string += "\nContinued on next screen..."; 
				window.alert(alert_string);
				alert_string = "Continued from previous screen...\n\n";
			}
			alert_string += notes[note_index];
		}
		window.alert(alert_string);
	}
 
} // closes function summarizeFAC() 
 
$(function () {
            mw.util.addPortletLink('p-cactions', 'javascript:summarizeFAC()', 'FAC', 'ca-fac', 'Summarize FAC votes', '!', '');
});
 
//</pre>