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

// <translate nowrap><!--T:43--> Force Preview and Edit-Summary - Start</translate>
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) { // <translate nowrap><!--T:44--> re-enable</translate>
				$wpSave.prop("disabled", false)
					.val(saveVal)
					.parent().removeClass("oo-ui-widget-disabled").addClass(classNames);
			}).parent().addClass(classNames);
		}
	});
// <translate nowrap><!--T:45--> Force Preview and Edit-Summary - End</translate>

For MediaWiki 1.17 or newer

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

// -------------------------------------------------------------------------------
//  <translate nowrap><!--T:7--> Force Preview  JavaScript code - Start</translate>
//
//  <translate nowrap><!--T:8--> To allow any group to bypass being forced to preview,</translate>
//  <translate nowrap><!--T:9--> enter the group name in the permittedGroups array.</translate>
//  <translate nowrap><!--T:10--> E.g.</translate>
//    var permittedGroups = [];                       // <translate nowrap><!--T:11--> force everyone</translate>
//    var permittedGroups = [ "user"];                // <translate nowrap><!--T:12--> permit logged-in users</translate>
//    var permittedGroups = [ "sysop", "bureaucrat"]; // <translate nowrap><!--T:13--> permit sysop, bureaucrat</translate>
// -------------------------------------------------------------------------------
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 );
// -----------------------------------------------------
//  <translate nowrap><!--T:16--> Force Preview  JavaScript code - End</translate>
// -----------------------------------------------------

For MediaWiki 1.14 or newer

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.

// -------------------------------------------------------------------------------
//  <translate nowrap><!--T:21--> Force Preview  JavaScript code - Start</translate>
//
//  <translate nowrap><!--T:22--> To allow any group to bypass being forced to preview,</translate>
//  <translate nowrap><!--T:23--> enter the group name in the permittedGroups array.</translate>
//  <translate nowrap><!--T:24--> E.g.</translate>
//    var permittedGroups = [];                       // <translate nowrap><!--T:25--> force everyone</translate>
//    var permittedGroups = [ "user"];                // <translate nowrap><!--T:26--> permit logged-in users</translate>
//    var permittedGroups = [ "sysop", "bureaucrat"]; // <translate nowrap><!--T:27--> permit sysop, bureaucrat</translate>
// -------------------------------------------------------------------------------
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);
// -----------------------------------------------------
//  <translate nowrap><!--T:30--> Force Preview  JavaScript code - End</translate>
// -----------------------------------------------------

Older Versions

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

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

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

/* <translate nowrap><!--T:37--> Force preview for anons</translate> */
/* <translate nowrap><!--T:38--> by Marc Mongenet, 2006, fr.wikipedia</translate> */

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

/* <translate nowrap><!--T:39--> End of forcePreview</translate> */

See also

  • Manual:FAQ
  • Extension:ForcePreview - A MediaWiki extension that performs the same function, but cannot be circumvented by disabling javascript
This article is issued from Mediawiki. The text is licensed under Creative Commons - Attribution - Sharealike. Additional terms may apply for the media files.