Jump to content

User:Proteins/summarizeAfD.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 AfD !votes.  Tip o' the hat to Graham87.
//
// To use this script, add "importScript('User:Proteins/summarizeAfD.js');" to your monobook.js subpage 
// under your user page, as you can see at User:Proteins/monobook.js

function summarizeAfD() {
	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_keep_votes = 0;
	var num_merge_votes = 0;
	var num_delete_votes = 0;
	var num_redirect_votes = 0;
	var num_refactor_votes = 0;
	var num_transwiki_votes = 0;
	var num_ambiguous_votes = 0;
	var num_error_messages = 0;

	var note_index = 0;
	var comment_index = 0;
	var keep_vote_index = 0;
	var merge_vote_index = 0;
	var delete_vote_index = 0;
	var redirect_vote_index = 0;
	var refactor_vote_index = 0;
	var transwiki_vote_index = 0;
	var ambiguous_vote_index = 0;
	var error_message_index = 0;

	var note_summary = "";
	var comment_summary = "";
	var keep_vote_summary = "";
	var merge_vote_summary = "";
	var delete_vote_summary = "";
	var redirect_vote_summary = "";
	var refactor_vote_summary = "";
	var transwiki_vote_summary = "";
	var ambiguous_vote_summary = "";
	var error_message_summary = "";

	var notes = new Array();
	var comments = new Array();
	var keep_votes = new Array();
	var merge_votes = new Array();
	var delete_votes = new Array();
	var redirect_votes = new Array();
	var refactor_votes = new Array();
	var transwiki_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;

// 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;


// Initializations

	num_notes = 0;
	num_comments = 0;
	num_keep_votes = 0;
	num_merge_votes = 0;
	num_delete_votes = 0;
	num_redirect_votes = 0;
	num_refactor_votes = 0;
	num_transwiki_votes = 0;
	num_ambiguous_votes = 0;

	notes[0] = "NONE";
	comments[0] = "NONE";
	keep_votes[0] = "NONE";
	merge_votes[0] = "NONE";
	delete_votes[0] = "NONE";
	redirect_votes[0] = "NONE";
	refactor_votes[0] = "NONE";
	transwiki_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(/delet/ig)) {
			num_delete_votes++; 
			reported_string = num_delete_votes + ". " + boldface_text + " — " + user_name + "\n";
//			window.alert(reported_string);
			delete_votes[num_delete_votes-1] = reported_string;
		} else if (boldface_text.match(/remov/ig)) { 
			num_delete_votes++;
			reported_string = num_delete_votes + ". " + boldface_text + " — " + user_name + "\n";
			delete_votes[num_delete_votes-1] = reported_string;
		} else if (boldface_text.match(/keep/ig)) { 
			num_keep_votes++;
			reported_string = num_keep_votes + ". " + boldface_text + " — " + user_name + "\n";
			keep_votes[num_keep_votes-1] = reported_string;
		} else if (boldface_text.match(/merg/ig)) { 
			num_merge_votes++; 
			reported_string = num_merge_votes + ". " + boldface_text + " — " + user_name + "\n";
			merge_votes[num_merge_votes-1] = reported_string;
		} else if (boldface_text.match(/redirect/ig)) { 
			num_redirect_votes++; 
			reported_string = num_redirect_votes + ". " + boldface_text + " — " + user_name + "\n";
			redirect_votes[num_redirect_votes-1] = reported_string;
		} else if (boldface_text.match(/transwiki/ig)) { 
			num_transwiki_votes++; 
			reported_string = num_transwiki_votes + ". " + boldface_text + " — " + user_name + "\n";
			transwiki_votes[num_transwiki_votes-1] = reported_string;
		} else if (boldface_text.match(/refactor/ig)) { 
			num_refactor_votes++; 
			reported_string = num_refactor_votes + ". " + boldface_text + " — " + user_name + "\n";
			refactor_votes[num_refactor_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(/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_keep_votes + num_delete_votes + num_merge_votes + num_redirect_votes + num_transwiki_votes + num_refactor_votes + num_ambiguous_votes;

// Print acknowledgment
	alert_string = "";
//	alert_string ="This article has " + num_unindented_list_items + " unindented list items of " + num_list_items + " possible.\n\n"

	if (num_votes == 1) { 
		alert_string += "One !vote was counted:\n";
	} else { 
		alert_string += num_votes + " !votes were counted:\n";
	}
	if (num_delete_votes == 1) { 
		alert_string += "\t one Delete !vote\n"; 
		delete_vote_summary = "There was one Delete !vote:\n\n"; 
	} else if (num_delete_votes) { 
		alert_string += "\t " + num_delete_votes + " Delete !votes\n";
		delete_vote_summary = "There were " + num_delete_votes + " Delete !votes:\n\n"; 
	}
	if (num_keep_votes == 1) { 
		alert_string += "\t one Keep !vote\n"; 
		keep_vote_summary = "There was one Keep !vote:\n\n"; 
	} else if (num_keep_votes) { 
		alert_string += "\t " + num_keep_votes + " Keep !votes\n";
		keep_vote_summary = "There were " + num_keep_votes + " Keep !votes:\n\n"; 
	}
	if (num_merge_votes == 1) { 
		alert_string += "\t one Merge !vote\n"; 
		merge_vote_summary = "There was one Merge !vote:\n\n"; 
	} else if (num_merge_votes) { 
		alert_string += "\t " + num_merge_votes + " Merge !votes\n"; 
		merge_vote_summary = "There were " + num_merge_votes + " Merge !votes:\n\n"; 
	}
	if (num_redirect_votes == 1) { 
		alert_string += "\t one Redirect !vote\n"; 
		redirect_vote_summary = "There was one Redirect !vote:\n\n"; 
	} else if (num_redirect_votes) { 
		alert_string += "\t " + num_redirect_votes + " Redirect !votes\n";
		redirect_vote_summary = "There were " + num_redirect_votes + " Redirect !votes:\n\n"; 
	}
	if (num_transwiki_votes == 1) { 
		alert_string += "\t one Transwiki !vote\n"; 
		transwiki_vote_summary = "There was one Transwiki !vote:\n\n"; 
	} else if (num_transwiki_votes) { 
		alert_string += "\t " + num_transwiki_votes + " Transwiki !votes\n"; 
		transwiki_vote_summary = "There were " + num_transwiki_votes + " Transwiki !votes:\n\n"; 
	}
	if (num_refactor_votes == 1) { 
		alert_string += "\t one Refactor !vote\n"; 
		refactor_vote_summary = "There was one Refactor !vote:\n\n"; 
	} else if (num_refactor_votes) { 
		alert_string += "\t " + num_refactor_votes + " Refactor !votes\n";
		refactor_vote_summary = "There were " + num_refactor_votes + " Refactor !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) { 
		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) == 1) { 
		alert_string += "\nIn addition, there was:\n";
	} else if (num_notes + num_comments) { 
		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) { 
		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) { 
		alert_string += "\t " + num_comments + " Comments\n"; 
		comment_summary = "There were " + num_comments + " comments:\n\n"; 
	}
	window.alert(alert_string);

// Report more details on each type of !vote
	if (num_delete_votes > 0) {
		alert_string = delete_vote_summary;
		for (delete_vote_index=0; delete_vote_index<num_delete_votes; delete_vote_index++) {
			if ((delete_vote_index%40 == 0) && (delete_vote_index > 1)) {
				alert_string += "\nContinued on next screen..."; 
				window.alert(alert_string);
				alert_string = "Continued from previous screen...\n\n";
			}
			alert_string += delete_votes[delete_vote_index];
		}
		window.alert(alert_string);
	}
	if (num_keep_votes > 0) { 
		alert_string = keep_vote_summary;
		for (keep_vote_index=0; keep_vote_index<num_keep_votes; keep_vote_index++) {
			if ((keep_vote_index%40 == 0) && (keep_vote_index > 1)) {
				alert_string += "\nContinued on next screen..."; 
				window.alert(alert_string);
				alert_string = "Continued from previous screen...\n\n";
			}
			alert_string += keep_votes[keep_vote_index];
		}
		window.alert(alert_string);
	}
	if (num_merge_votes > 0) { 
		alert_string = merge_vote_summary;
		for (merge_vote_index=0; merge_vote_index<num_merge_votes; merge_vote_index++) {
			if ((merge_vote_index%40 == 0) && (merge_vote_index > 1)) {
				alert_string += "\nContinued on next screen..."; 
				window.alert(alert_string);
				alert_string = "Continued from previous screen...\n\n";
			}
			alert_string += merge_votes[merge_vote_index];
		}
		window.alert(alert_string);
	}
	if (num_redirect_votes > 0) { 
		alert_string = redirect_vote_summary;
		for (redirect_vote_index=0; redirect_vote_index<num_redirect_votes; redirect_vote_index++) {
			if ((redirect_vote_index%40 == 0) && (redirect_vote_index > 1)) {
				alert_string += "\nContinued on next screen..."; 
				window.alert(alert_string);
				alert_string = "Continued from previous screen...\n\n";
			}
			alert_string += redirect_votes[redirect_vote_index];
		}
		window.alert(alert_string);
	}
	if (num_transwiki_votes > 0) { 
		alert_string = transwiki_vote_summary;
		for (transwiki_vote_index=0; transwiki_vote_index<num_transwiki_votes; transwiki_vote_index++) {
			if ((transwiki_vote_index%40 == 0) && (transwiki_vote_index > 1)) {
				alert_string += "\nContinued on next screen..."; 
				window.alert(alert_string);
				alert_string = "Continued from previous screen...\n\n";
			}
			alert_string += transwiki_votes[transwiki_vote_index];
		}
		window.alert(alert_string);
	}
	if (num_refactor_votes > 0) { 
		alert_string = refactor_vote_summary;
		for (refactor_vote_index=0; refactor_vote_index<num_refactor_votes; refactor_vote_index++) {
			if ((refactor_vote_index%40 == 0) && (refactor_vote_index > 1)) {
				alert_string += "\nContinued on next screen..."; 
				window.alert(alert_string);
				alert_string = "Continued from previous screen...\n\n";
			}
			alert_string += refactor_votes[refactor_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_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 summarizeAfD() 

addOnloadHook(function () {
            mw.util.addPortletLink('p-cactions', 'javascript:summarizeAfD()', 'AfD', 'ca-afd', 'Summarize AfD votes', 'a', '');
});
 
//</pre>