User:Proteins/quickdiffs.js

This is an old revision of this page, as edited by Proteins (talk | contribs) at 13:08, 3 November 2008 (add error messages). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
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>
//Summarizes the differences between two page versions in pop-up windows
// Perhaps calculate fractional change in document 

function quickDiffs() {
	var alert_string = "";

	var body_content;
	var article_tables;
	var num_diff_tables = 0;

	var diff_table;
	var diff_table_row;

	var diff_old_version_info; //className = "diff-otitle"
	var diff_new_version_info; //className = "diff-ntitle"

	var mw_diff_otitle1 = "";
	var mw_diff_otitle2 = "";
	var mw_diff_otitle3 = "";
	var mw_diff_otitle4 = "";

	var mw_diff_ntitle1 = "";
	var mw_diff_ntitle2 = "";
	var mw_diff_ntitle3 = "";
	var mw_diff_ntitle4 = "";

	var num_line_numbers = 0;
	var temp_diff_old_line_numbers = 0;
	var temp_diff_new_line_numbers = 0;
	var diff_old_line_numbers = new Array();
	var diff_new_line_numbers = new Array();

	var num_additions = 0;
	var num_deletions = 0;
	var num_replacements = 0;

	var num_changed_lines = 0;
	var diff_added_lines = new Array();
	var diff_deleted_lines = new Array();
	var num_changes_in_line = new Array();

	var diff_changes = new Array();
	var diff_change_line_numbers = new Array();

// Check that there is exactly one table on this page with the className "diff"
	body_content = document.getElementById('bodyContent);
	if (!body_content) { 
		alert_string = "ERROR: This article has no bodyContent element.";
		window.alert(alert_string);
		return;
	}

	article_tables = body_content.getElementsByTagName("TABLE");
	if (!article_tables) { 
		alert_string = "ERROR: This article has no TABLE elements.";
		window.alert(alert_string);
		return;
	}
	num_article_tables = article_tables.length;
	if (!num_article_tables) { 
		alert_string = "ERROR: This article has no TABLE elements.";
		window.alert(alert_string);
		return;
	}

	num_diff_tables = 0;
	for (article_table_index=0; article_table_index<num_article_tables; article_table_index++) { 


	} // closes loop searching for the diff table
	if (num_diff_tables == 0) { 
		alert_string = "ERROR: This article has no TABLE elements belonging to the \"diff\" class.";
		window.alert(alert_string);
		return;
	} else if (num_diff_tables != 1) { 
		alert_string = "ERROR: This article has " + num_diff_tables + " TABLE elements belonging to the \"diff\" class.";
		alert_string += "An article should have at most one \"diff\" table.\n";
		window.alert(alert_string);
		return;
	}

// Check that there is exactly one table body
	diff_table_bodies = diff_table.getElementsByTagName("TBODY");
	if (!diff_table_bodies) { 
		alert_string = "ERROR: This article has no TBODY element within its \"diff\" table.";
		window.alert(alert_string);
		return;
	}
	num_diff_table_bodies = diff_table_bodies.length;
	if (!num_diff_table_bodies) { 
		alert_string = "ERROR: This article has no TBODY element within its \"diff\" table.";
		window.alert(alert_string);
		return;
	} else if (num_diff_table_bodies != 1) { 
		alert_string = "ERROR: This article has " + num_diff_table_bodies + " TBODY elements within its \"diff\" table.";
		alert_string += "A \"diff\" table should have at most one TBODY element.\n";
		window.alert(alert_string);
		return;
	}
	diff_table_body = diff_table_bodies[0];

// Loop over the table rows in the table body 
	diff_table_rows = diff_table_body.getElementsByTagName("TR");
	if (!diff_table_rows) { 
		alert_string = "ERROR: This article has no TR elements within its \"diff\" table body.";
		window.alert(alert_string);
		return;
	}
	num_diff_table_rows = diff_table_rows.length;
	if (!num_diff_table_rows) { 
		alert_string = "ERROR: This article has no TR elements within its \"diff\" table body.";
		window.alert(alert_string);
		return;
	} else if (num_diff_table_rows < 3) { 
		alert_string = "ERROR: This article has " + num_diff_table_rows + " rows (TR elements) within its \"diff\" table body.";
		alert_string += "A \"diff\" table body should have at least three rows.\n";
		window.alert(alert_string);
		return;
	}

	for (table_row_index=0; table_row_index<num_diff_table_rows; table_row_index++) { 

//MAIN LOGIC OF THE FUNCTION GOES HERE

	} // closes loop over the table rows

// Report the results
	alert_string = "This diff has " + num_additions + " additions, " + num_deletions + " deletions, and " + num_replacements + "replacements.\n\n";
	window.alert(alert_string);
} // closes function quickDiffs()
 
addOnloadHook(function () {
addPortletLink('p-cactions', 'javascript:quickDiffs()', 'diff', 'ca-diff', 'Summarizes the differences between two page versions', 'b', '');
});
 
//</pre>