From 721ea54c7e93c3633a119a20e0b1f427453b42b9 Mon Sep 17 00:00:00 2001 From: rahul21 Date: Mon, 1 Apr 2013 03:25:52 +0530 Subject: [PATCH] Vector: Move 'editwarning' feature from Vector extension * Created a new Resource Loader module * Created an entry for the RL module in the Resources.php * Added messages to languages/MessagesEn.php and languages/MessagesQqq.php * Called the new RL module from the EditPage.php * Added required entries in the messages.inc, DefaultSettings.php Corresponding change in the Vector extension: I164f17255bc927 Bug: 46514 Change-Id: I7bdbd09f4083ccb316156307400ccfacdfec79e1 --- RELEASE-NOTES-1.22 | 5 ++ includes/DefaultSettings.php | 1 + includes/EditPage.php | 5 ++ includes/Preferences.php | 7 +++ languages/messages/MessagesEn.php | 3 + languages/messages/MessagesQqq.php | 2 + maintenance/language/messages.inc | 2 + resources/Resources.php | 6 ++ .../mediawiki.action.edit.editWarning.js | 56 +++++++++++++++++++ 9 files changed, 87 insertions(+) create mode 100644 resources/mediawiki.action/mediawiki.action.edit.editWarning.js diff --git a/RELEASE-NOTES-1.22 b/RELEASE-NOTES-1.22 index 06947e1e9b..c24cd7d201 100644 --- a/RELEASE-NOTES-1.22 +++ b/RELEASE-NOTES-1.22 @@ -16,6 +16,11 @@ production. * (bug 44525) mediawiki.jqueryMsg can now parse (whitelisted) HTML elements and attributes. * (bug 33454) Language::sprintfDate now has a timezone parameter, and supports the "eIOPTZ" formatting characters. +* EditWarning: A warning is shown when an editor leaves the edit form without + saving (enabled by default, users can opt-out via the 'useeditwarning' + preference). This feature was moved from the Vector extension, and is now part + of core for all skins. Take care when upgrading that you don't use an older + version of the Vector extension as this feature may conflict. === Bug fixes in 1.22 === diff --git a/includes/DefaultSettings.php b/includes/DefaultSettings.php index 53df457274..7b6e9a7d5c 100644 --- a/includes/DefaultSettings.php +++ b/includes/DefaultSettings.php @@ -3693,6 +3693,7 @@ $wgDefaultUserOptions = array( 'watchlisthidepatrolled' => 0, 'watchmoves' => 0, 'wllimit' => 250, + 'useeditwarning' => 1, ); /** An array of preferences to not show for the user */ diff --git a/includes/EditPage.php b/includes/EditPage.php index 96a57b4e59..86a7a77b3c 100644 --- a/includes/EditPage.php +++ b/includes/EditPage.php @@ -1890,6 +1890,11 @@ class EditPage { if ( $wgUser->getOption( 'uselivepreview', false ) ) { $wgOut->addModules( 'mediawiki.action.edit.preview' ); } + + if ( $wgUser->getOption( 'useeditwarning', false ) ) { + $wgOut->addModules( 'mediawiki.action.edit.editWarning' ); + } + // Bug #19334: textarea jumps when editing articles in IE8 $wgOut->addStyle( 'common/IE80Fixes.css', 'screen', 'IE 8' ); diff --git a/includes/Preferences.php b/includes/Preferences.php index 8edf60a84c..8766ad389e 100644 --- a/includes/Preferences.php +++ b/includes/Preferences.php @@ -818,6 +818,13 @@ class Preferences { 'section' => 'editing/advancedediting', 'label-message' => 'tog-uselivepreview', ); + + $defaultPreferences['useeditwarning'] = array( + 'type' => 'toggle', + 'section' => 'editing/advancedediting', + 'label-message' => 'tog-useeditwarning', + ); + } /** diff --git a/languages/messages/MessagesEn.php b/languages/messages/MessagesEn.php index 500488e46a..76dba58e70 100644 --- a/languages/messages/MessagesEn.php +++ b/languages/messages/MessagesEn.php @@ -682,6 +682,7 @@ XHTML id names. 'tog-showhiddencats' => 'Show hidden categories', 'tog-noconvertlink' => 'Disable link title conversion', # only translate this message to other languages if you have to change it 'tog-norollbackdiff' => 'Omit diff after performing a rollback', +'tog-useeditwarning' => 'Warn me when I leave an edit page with unsaved changes', 'underline-always' => 'Always', 'underline-never' => 'Never', @@ -1492,6 +1493,8 @@ It already exists.', 'content-failed-to-parse' => 'Failed to parse $2 content for $1 model: $3', 'invalid-content-data' => 'Invalid content data', 'content-not-allowed-here' => '"$1" content is not allowed on page [[$2]]', +'editwarning-warning' => 'Leaving this page may cause you to lose any changes you have made. +If you are logged in, you can disable this warning in the "Editing" section of your preferences.', # Content models 'content-model-wikitext' => 'wikitext', diff --git a/languages/messages/MessagesQqq.php b/languages/messages/MessagesQqq.php index 0abf42eee2..e003569d45 100644 --- a/languages/messages/MessagesQqq.php +++ b/languages/messages/MessagesQqq.php @@ -223,6 +223,7 @@ This option means "underline links as in your user skin or your browser", there {{Gender}} {{Identical|Browser default}}', +'tog-useeditwarning' => 'Caption of the editwarning preference, displayed on the right side of the checkbox', # Font style option in Special:Preferences 'editfont-style' => 'Used in [[Special:Preferences]], tab Editing. {{Gender}}', @@ -1603,6 +1604,7 @@ See also: 'content-not-allowed-here' => 'Error message indicating that the desired content model is not supported in given localtion. * $1 - the human readable name of the content model: {{msg-mw|Content-model-wikitext}}, {{msg-mw|Content-model-javascript}}, {{msg-mw|Content-model-css}} or {{msg-mw|Content-model-text}} * $2 - the title of the page in question', +'editwarning-warning' => 'Warning message that is displayed when a user moves to an other page without saving his changes', # Content models 'content-model-wikitext' => 'Name for the wikitext content model, used when decribing what type of content a page contains. diff --git a/maintenance/language/messages.inc b/maintenance/language/messages.inc index 64ffc09edd..1a0ff8abeb 100644 --- a/maintenance/language/messages.inc +++ b/maintenance/language/messages.inc @@ -72,6 +72,7 @@ $wgMessageStructure = array( 'tog-showhiddencats', 'tog-noconvertlink', 'tog-norollbackdiff', + 'tog-useeditwarning', ), 'underline' => array( 'underline-always', @@ -695,6 +696,7 @@ $wgMessageStructure = array( 'content-failed-to-parse', 'invalid-content-data', 'content-not-allowed-here', + 'editwarning-warning', ), 'contentmodels' => array( 'content-model-wikitext', diff --git a/resources/Resources.php b/resources/Resources.php index 0db3e200bf..5332b26035 100644 --- a/resources/Resources.php +++ b/resources/Resources.php @@ -737,6 +737,12 @@ return array( 'mediawiki.action.view.rightClickEdit' => array( 'scripts' => 'resources/mediawiki.action/mediawiki.action.view.rightClickEdit.js', ), + 'mediawiki.action.edit.editWarning' => array( + 'scripts' => 'resources/mediawiki.action/mediawiki.action.edit.editWarning.js', + 'messages' => array( + 'editwarning-warning', + ), + ), // Alias for backwards compatibility 'mediawiki.action.watch.ajax' => array( 'dependencies' => 'mediawiki.page.watch.ajax' diff --git a/resources/mediawiki.action/mediawiki.action.edit.editWarning.js b/resources/mediawiki.action/mediawiki.action.edit.editWarning.js new file mode 100644 index 0000000000..cfe9762762 --- /dev/null +++ b/resources/mediawiki.action/mediawiki.action.edit.editWarning.js @@ -0,0 +1,56 @@ +/* + * Javascript for module editWarning + */ +( function ( mw, $ ) { + $( document ).ready( function () { + // Check if EditWarning is enabled and if we need it + if ( $( '#wpTextbox1' ).length === 0 ) { + return true; + } + // Get the original values of some form elements + $( '#wpTextbox1, #wpSummary' ).each( function () { + $( this ).data( 'origtext', $( this ).val() ); + }); + var savedWindowOnBeforeUnload; + $( window ) + .on( 'beforeunload.editwarning', function () { + var retval; + + // Check if the current values of some form elements are the same as + // the original values + if ( + mw.config.get( 'wgAction' ) === 'submit' || + $( '#wpTextbox1' ).data( 'origtext' ) !== $( '#wpTextbox1' ).val() || + $( '#wpSummary' ).data( 'origtext' ) !== $( '#wpSummary' ).val() + ) { + // Return our message + retval = mw.msg( 'editwarning-warning' ); + } + + // Unset the onbeforeunload handler so we don't break page caching in Firefox + savedWindowOnBeforeUnload = window.onbeforeunload; + window.onbeforeunload = null; + if ( retval !== undefined ) { + // ...but if the user chooses not to leave the page, we need to rebind it + setTimeout( function () { + window.onbeforeunload = savedWindowOnBeforeUnload; + }, 1 ); + return retval; + } + } ) + .on( 'pageshow.editwarning', function () { + // Re-add onbeforeunload handler + if ( !window.onbeforeunload ) { + window.onbeforeunload = savedWindowOnBeforeUnload; + } + } ); + + // Add form submission handler + $( '#editform' ).submit( function () { + // Unbind our handlers + $( window ).off( '.editwarning' ); + } ); + } ); + +}( mediaWiki, jQuery ) ); + -- 2.20.1