Jump to content

Manual:Force preview

From mediawiki.org

Force preview is JavaScript that prevents specified individuals or groups from saving a wiki page before they preview it at least once.

In order to use this code, insert it into your wiki's MediaWiki:Common.js page.

Note that this method is not foolproof. If a user disables JavaScript in their browser, they will not have preview forced on them. If you want to ensure that this policy is enforced, then you should look at Extension:ForcePreview .

NOTE: some of these scripts are outdated and use addOnloadHook that is deprecated.

For MediaWiki 1.23 or newer

[edit]
// Force Preview and Edit-Summary - Start
if (mw.config.get("wgAction") === "edit")
	$.when(mw.loader.using("user.options"), $.ready).then(function () {
		var $wpSave = $("#wpSave"),
			$wpPreview = $("#wpPreview"),
			saveVal = $wpSave.val(),
			classNames = "oo-ui-widget-enabled oo-ui-flaggedElement-progressive oo-ui-flaggedElement-primary";
		if (!mw.user.options.get("forceeditsummary") || mw.user.options.get("previewonfirst"))
			mw.loader.using("mediawiki.api", function () {
				new mw.Api().saveOptions({forceeditsummary: 1, previewonfirst: 0});
			});
		if (!$("#wikiPreview,#wikiDiff").is(":visible") && $wpSave.length && $wpPreview.length) {
			$wpSave.prop("disabled", true)
				.val("Save page (use preview first)")
				.parent().removeClass(classNames).addClass("oo-ui-widget-disabled");
			$wpPreview.one("click", function (e) { // re-enable
				$wpSave.prop("disabled", false)
					.val(saveVal)
					.parent().removeClass("oo-ui-widget-disabled").addClass(classNames);
			}).parent().addClass(classNames);
		}
	});
// Force Preview and Edit-Summary - End

For MediaWiki 1.17 or newer

[edit]

Not right working with LivePreview (v.1.17) and OOUI (v.1.23).

// -------------------------------------------------------------------------------
//  Force Preview  JavaScript code - Start
//
//  To allow any group to bypass being forced to preview,
//  enter the group name in the permittedGroups array.
//  E.g.
//    var permittedGroups = [];                       // force everyone
//    var permittedGroups = [ "user"];                // permit logged-in users
//    var permittedGroups = [ "sysop", "bureaucrat"]; // permit sysop, bureaucrat
// -------------------------------------------------------------------------------
var permittedGroups = [];

function forcePreview() {
	if ( mw.config.get( "wgAction" ) !== "edit" ) return;
	if ( mw.config.get( "wgUserGroups" ).filter(function(group) {
		return permittedGroups.indexOf(group) > -1;
	}).length ) return;
	var saveButton = document.getElementById( "wpSave" );
	if ( !saveButton ) return;
	saveButton.disabled = true;
	saveButton.value = "Save page (use preview first)";
	saveButton.style.fontWeight = "normal";
	document.getElementById("wpPreview").style.fontWeight = "bold";
}

jQuery(document).ready( forcePreview );
// -----------------------------------------------------
//  Force Preview  JavaScript code - End
// -----------------------------------------------------

For MediaWiki 1.14 or newer

[edit]

The following will permit you to control who has preview mode forced on them. It permits you to set one or more user groups to bypass forced preview and save directly. In order to permit a user group, add the group name to the permittedGroups array as shown in the comments with the JavaScript code.

// -------------------------------------------------------------------------------
//  Force Preview  JavaScript code - Start
//
//  To allow any group to bypass being forced to preview,
//  enter the group name in the permittedGroups array.
//  E.g.
//    var permittedGroups = [];                       // force everyone
//    var permittedGroups = [ "user"];                // permit logged-in users
//    var permittedGroups = [ "sysop", "bureaucrat"]; // permit sysop, bureaucrat
// -------------------------------------------------------------------------------
var permittedGroups = [];

function forcePreview()
{
  if( wgAction !== "edit") return;
  if( wgUserGroups === null) {
    wgUserGroups = [];
  }
  if( wgUserGroups.filter(function(group) {
    return permittedGroups.indexOf(group) > -1;
  }).length ) {
    return;
  }
  var saveButton = document.getElementById("wpSave");
  if( !saveButton )
    return;
  saveButton.disabled = true;
  saveButton.value = "Save page (use preview first)";
  saveButton.style.fontWeight = "normal";
  document.getElementById("wpPreview").style.fontWeight = "bold";
}

addOnloadHook(forcePreview);
// -----------------------------------------------------
//  Force Preview  JavaScript code - End
// -----------------------------------------------------

Older Versions

[edit]

The below scripts are based on Marc Mongenet's script, from fr.wikipedia.org.

Force preview by wgUserGroups, requires a MW version >= 1.10

[edit]
function forcePreview() {
  if (wgUserGroups === "user" || wgAction !== "edit") return;
  saveButton = document.getElementById("wpSave");
  if (!saveButton) return;
  saveButton.disabled = true;
  saveButton.value = "Save page (use preview first)";
  saveButton.style.fontWeight = "normal";
  document.getElementById("wpPreview").style.fontWeight = "bold";
}
addOnloadHook(forcePreview);

If you wish to provide exceptions to certain usergroups. Just change === (EQUAL TO) signs to !== (NOT EQUAL TO)

  if (wgUserGroups !== "user" || wgAction !== "edit") return;

Common choices range from "user", "bureaucrat", "sysop" and more depending on your configuration.

Marc Mongenet's force preview, by wgUserName, requires a MW version > 1.6

[edit]
/* Force preview for anons */
/* by Marc Mongenet, 2006, fr.wikipedia */

function forcePreview() {
  if (wgUserName !== null || wgAction !== "edit") return;
  saveButton = document.getElementById("wpSave");
  if (!saveButton) return;
  saveButton.disabled = true;
  saveButton.value = "Save page (use preview first)";
  saveButton.style.fontWeight = "normal";
  document.getElementById("wpPreview").style.fontWeight = "bold";
}
addOnloadHook(forcePreview);

/* End of forcePreview */

See also

[edit]