jenkins-bot has submitted this change and it was merged. ( 
https://gerrit.wikimedia.org/r/378996 )

Change subject: Convert to extension registration and OOjs-ify
......................................................................


Convert to extension registration and OOjs-ify

This brings compatibility with modern mediawiki and fixes questionable
code choices in previous iterations of the extension. The live preview
js is now a proper resourceloader module, and we now use native OOjs UI
methods rather than string replacements and hacking around it.

Change-Id: If553b4c6248d0d8800afd678ab2c600a9dab7820
---
D ForcePreview.i18n.php
M ForcePreview.php
A extension.json
M i18n/en.json
M i18n/qqq.json
A modules/ext.ForcePreview.livePreview.js
6 files changed, 113 insertions(+), 144 deletions(-)

Approvals:
  Skizzerz: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/ForcePreview.i18n.php b/ForcePreview.i18n.php
deleted file mode 100644
index 7874d20..0000000
--- a/ForcePreview.i18n.php
+++ /dev/null
@@ -1,35 +0,0 @@
-<?php
-/**
- * This is a backwards-compatibility shim, generated by:
- * 
https://git.wikimedia.org/blob/mediawiki%2Fcore.git/HEAD/maintenance%2FgenerateJsonI18n.php
- *
- * Beginning with MediaWiki 1.23, translation strings are stored in json files,
- * and the EXTENSION.i18n.php file only exists to provide compatibility with
- * older releases of MediaWiki. For more information about this migration, see:
- * https://www.mediawiki.org/wiki/Requests_for_comment/Localisation_format
- *
- * This shim maintains compatibility back to MediaWiki 1.17.
- */
-$messages = array();
-if ( !function_exists( 'wfJsonI18nShim7df31cd24afdd76f' ) ) {
-       function wfJsonI18nShim7df31cd24afdd76f( $cache, $code, &$cachedData ) {
-               $codeSequence = array_merge( array( $code ), 
$cachedData['fallbackSequence'] );
-               foreach ( $codeSequence as $csCode ) {
-                       $fileName = dirname( __FILE__ ) . "/i18n/$csCode.json";
-                       if ( is_readable( $fileName ) ) {
-                               $data = FormatJson::decode( file_get_contents( 
$fileName ), true );
-                               foreach ( array_keys( $data ) as $key ) {
-                                       if ( $key === '' || $key[0] === '@' ) {
-                                               unset( $data[$key] );
-                                       }
-                               }
-                               $cachedData['messages'] = array_merge( $data, 
$cachedData['messages'] );
-                       }
-
-                       $cachedData['deps'][] = new FileDependency( $fileName );
-               }
-               return true;
-       }
-
-       $GLOBALS['wgHooks']['LocalisationCacheRecache'][] = 
'wfJsonI18nShim7df31cd24afdd76f';
-}
diff --git a/ForcePreview.php b/ForcePreview.php
index 3cc8c46..f66b72e 100644
--- a/ForcePreview.php
+++ b/ForcePreview.php
@@ -5,122 +5,46 @@
  * @file
  * @ingroup Extensions
  * @author Ryan Schmidt
- * @license http://en.wikipedia.org/wiki/Public_domain Public domain
+ * @license MIT
  * @link http://www.mediawiki.org/wiki/Extension:ForcePreview Documentation
  */
 
-if( !defined( 'MEDIAWIKI' ) ) {
-       echo( "This file is an extension to the MediaWiki software and is not a 
valid access point" );
-       die( 1 );
-}
+class ForcePreview {
+       public static function onEditPageBeforeEditButtons( &$editpage, 
&$buttons, &$tabindex ) {
+               $user = $editpage->getContext()->getUser();
+               $isInitialLoad = !$editpage->preview && empty( $editpage->save 
);
 
-// Extension credits that will show up on Special:Version
-$wgExtensionCredits['other'][] = array(
-       'path' => __FILE__,
-       'name' => 'Force Preview',
-       'version' => '1.3.0',
-       'author' => 'Ryan Schmidt',
-       'descriptionmsg' => 'forcepreview-desc',
-       'url' => 'https://www.mediawiki.org/wiki/Extension:ForcePreview',
-);
-
-// i18n file
-$dir = dirname( __FILE__ ) . '/';
-$wgMessagesDirs['ForcePreview'] = __DIR__ . '/i18n';
-$wgExtensionMessagesFiles['ForcePreview'] = $dir . 'ForcePreview.i18n.php';
-
-// New user right, allows users to bypass the requirement of previewing before
-// saving the page
-$wgAvailableRights[] = 'forcepreviewexempt';
-
-$wgHooks['EditPageBeforeEditButtons'][] = 'efForcePreview';
-$wgHooks['BeforePageDisplay'][] = 'efForcePreviewLivePreview';
-
-//for GroupPermissions manager extension sorting
-$wgGPManagerSort['edit'][] = 'forcepreviewexempt';
-
-function efForcePreview( &$editpage, &$buttons ) {
-       global $wgUser;
-       if(
-               !$wgUser->isAllowed( 'forcepreviewexempt' ) &&
-               !$editpage->preview && empty( $editpage->save )
-       )
-       {
-               $buttons['save'] = str_replace(
-                       '/>', 'disabled="disabled" />', $buttons['save']
-               );
-               $buttons['save'] = preg_replace(
-                       '/value="' . wfMessage( 'savearticle' )->text() . '"/i',
-                       'value="' . wfMessage( 'forcepreview' )->text() . '"',
-                       $buttons['save']
-               );
-               if( $buttons['live'] !== '' ) {
-                       $buttons['preview'] = preg_replace(
-                               '/style="(.*?);?"/',
-                               'style="$1; font-weight: bold;"',
-                               $buttons['preview']
-                       ); # in case something else made it visible
-                       $buttons['live']  = str_replace(
-                               '/>',
-                               'style="font-weight: bold" />',
-                               $buttons['live']
-                       );
-               } else {
-                       $buttons['preview'] = str_replace(
-                               '/>',
-                               'style="font-weight: bold" />',
-                               $buttons['preview']
-                       );
+               if ( !$user->isAllowed( 'forcepreviewexempt' ) && 
$isInitialLoad ) {
+                       $buttons['save']->setDisabled( true );
+                       $buttons['save']->setLabel( wfMessage( 'forcepreview', 
$buttons['save']->getLabel() )->text() );
+                       $buttons['save']->setFlags( [ 'primary' => false ] );
+                       $buttons['preview']->setFlags( [ 'primary' => true, 
'constructive' => true ] );
                }
-       }
-       return true;
-}
 
-function efForcePreviewLivePreview( &$out, $sk = null ) {
-       global $wgUser, $wgRequest, $wgLivePreview;
-       if( !$wgLivePreview || !$out->getTitle()->userCan( 'edit' ) ) {
                return true;
        }
-       if(
-               $wgUser->isAllowed( 'forcepreviewexempt' ) ||
-               !$wgUser->getBoolOption( 'uselivepreview' )
-       )
-       {
+
+       public static function onBeforePageDisplay( &$out, &$skin ) {
+               $user = $out->getUser();
+               $request = $out->getRequest();
+               $title = $out->getTitle();
+               
+               if (
+                       !$title->userCan( 'edit' )
+                       || $user->isAllowed( 'forcepreviewexempt' )
+                       || !$user->getBoolOption( 'uselivepreview' )
+                       || !in_array( $request->getVal( 'action' ), [ 'edit', 
'submit' ] )
+               ) {
+                       return true;
+               }
+
+               $out->addModules( 'ext.ForcePreview.livePreview' );
                return true;
        }
-       if(
-               !$wgRequest->getVal( 'action' ) == 'edit' ||
-               !$wgRequest->getVal( 'action' ) == 'submit'
-       )
-       {
+
+       public static function onResourceLoaderGetConfigVars( &$vars ) {
+               $config = 
MediaWiki\MediaWikiServices::getInstance()->getMainConfig();
+               $vars['wgEditSubmitButtonLabelPublish'] = $config->get( 
'EditSubmitButtonLabelPublish' );
                return true;
        }
-       $out->addHTML(
-               "<script type=\"text/javascript\">
-               var liveButton = document.getElementById( 'wpLivePreview' );
-               var msg = \"" . wfMessage( 'savearticle' )->escaped() . "\";
-               function enableSave() {
-                       if( !liveButton ) {
-                               return;
-                       }
-                       liveButton.style.fontWeight = 'normal';
-                       var previewButton = document.getElementById( 
'wpPreview' );
-                       if( previewButton ) {
-                               previewButton.style.fontWeight = 'normal';
-                       }
-                       var saveButton = document.getElementById( 'wpSave' );
-                       if( !saveButton ) {
-                               return;
-                       }
-                       saveButton.disabled = false;
-                       saveButton.value = msg;
-               }
-               if( window.addEventListener ) {
-                       liveButton.addEventListener( 'click', enableSave, false 
);
-               } elseif( window.attachEvent ) {
-                       liveButton.attachEvent( 'onclick', enableSave );
-               }
-               </script>"
-       );
-       return true;
 }
diff --git a/extension.json b/extension.json
new file mode 100644
index 0000000..743d567
--- /dev/null
+++ b/extension.json
@@ -0,0 +1,55 @@
+{
+       "name": "ForcePreview",
+       "version": "2.0.0",
+       "author": [
+               "Ryan Schmidt"
+       ],
+       "url": "https://www.mediawiki.org/wiki/Extension:ForcePreview";,
+       "descriptionmsg": "forcepreview-desc",
+       "license-name": "MIT",
+       "type": "other",
+       "requires": {
+               "MediaWiki": ">= 1.27.0"
+       },
+       "MessagesDirs": {
+               "ForcePreview": [
+                       "i18n"
+               ]
+       },
+       "Hooks": {
+               "EditPageBeforeEditButtons": [
+                       "ForcePreview::onEditPageBeforeEditButtons"
+               ],
+               "BeforePageDisplay": [
+                       "ForcePreview::onBeforePageDisplay"
+               ],
+               "ResourceLoaderGetConfigVars": [
+                       "ForcePreview::onResourceLoaderGetConfigVars"
+               ]
+       },
+       "AutoloadClasses": {
+               "ForcePreview": "ForcePreview.php"
+       },
+       "AvailableRights": [
+               "forcepreviewexempt"
+       ],
+       "ResourceModules": {
+               "ext.ForcePreview.livePreview": {
+                       "scripts": "ext.ForcePreview.livePreview.js",
+                       "messages": [
+                               "publishpage",
+                               "publishchanges",
+                               "savearticle",
+                               "savechanges"
+                       ],
+                       "dependencies": [
+                               "oojs-ui-core"
+                       ]
+               }
+       },
+       "ResourceFileModulePaths": {
+               "localBasePath": "modules",
+               "remoteExtPath": "ForcePreview/modules"
+       },
+       "manifest_version": 1
+}
diff --git a/i18n/en.json b/i18n/en.json
index c5d6276..d1e6321 100644
--- a/i18n/en.json
+++ b/i18n/en.json
@@ -4,7 +4,7 @@
                        "Ryan Schmidt"
                ]
        },
-       "forcepreview": "Save page (use preview first)",
+       "forcepreview": "$1 (use preview first)",
        "forcepreview-desc": "Force preview for unprivileged users",
        "right-forcepreviewexempt": "May save without using Show preview"
-}
\ No newline at end of file
+}
diff --git a/i18n/qqq.json b/i18n/qqq.json
index 8300464..280bf04 100644
--- a/i18n/qqq.json
+++ b/i18n/qqq.json
@@ -8,7 +8,7 @@
                        "Umherirrender"
                ]
        },
-       "forcepreview": "Used as Save Page button text.\nThe button is disabled 
by default.\n\n\"Use preview first\" is an imperative sentence.",
+       "forcepreview": "Used as Save Page button text.\nThe button is disabled 
by default.\n\n\"Use preview first\" is an imperative sentence. $1 is the 
original text of the save button.",
        "forcepreview-desc": "{{desc|name=Force 
Preview|url=https://www.mediawiki.org/wiki/Extension:ForcePreview}}";,
        "right-forcepreviewexempt": "{{doc-right|forcepreviewexempt}}"
 }
diff --git a/modules/ext.ForcePreview.livePreview.js 
b/modules/ext.ForcePreview.livePreview.js
new file mode 100644
index 0000000..f47596e
--- /dev/null
+++ b/modules/ext.ForcePreview.livePreview.js
@@ -0,0 +1,25 @@
+$( function () {
+       "use strict";
+
+       var preview = OO.ui.infuse( "wpPreviewWidget" ),
+               save = OO.ui.infuse( "wpSaveWidget" ),
+               newPage = mw.config.get( "wgArticleId" ) === 0,
+               message;
+
+       // This logic was lifted from EditPage::getSaveButtonLabel,
+       // which is sadly a private function
+       if ( mw.config.get( "wgEditSubmitButtonLabelPublish" ) ) {
+               message = newPage ? "publishpage" : "publishchanges";
+       } else {
+               message = newPage ? "savearticle" : "savechanges";
+       }
+
+       preview.on( "click", function enableSave() {
+               save.setFlags( { primary: true, constructive: true } )
+                       .setDisabled( false )
+                       .setLabel( OO.ui.msg( message ) );
+
+               preview.setFlags( { primary: false, constructive: false } )
+                       .off( "click", enableSave );
+       } );
+} );

-- 
To view, visit https://gerrit.wikimedia.org/r/378996
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: If553b4c6248d0d8800afd678ab2c600a9dab7820
Gerrit-PatchSet: 5
Gerrit-Project: mediawiki/extensions/ForcePreview
Gerrit-Branch: master
Gerrit-Owner: Skizzerz <skizz...@skizzerz.net>
Gerrit-Reviewer: Siebrand <siebr...@kitano.nl>
Gerrit-Reviewer: Skizzerz <skizz...@skizzerz.net>
Gerrit-Reviewer: jenkins-bot <>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to