Skizzerz has uploaded a new change for review. ( 
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
A modules/ext.ForcePreview.livePreview.js
4 files changed, 90 insertions(+), 143 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/ForcePreview 
refs/changes/96/378996/1

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..8d22100 100644
--- a/ForcePreview.php
+++ b/ForcePreview.php
@@ -5,122 +5,42 @@
  * @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']->setValue( wfMessage( 'forcepreview' 
)->text() );
+                       $buttons['save']->setFlags( [ 'primary' => false ] );
+                       $buttons['preview']->setFlags( [ 'primary' => 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();
+               $live = $out->getConfig()->get( 'LivePreview' );
+               $title = $out->getTitle();
+               
+               if (
+                       !$live
+                       || !$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'
-       )
-       {
-               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..4f91dd2
--- /dev/null
+++ b/extension.json
@@ -0,0 +1,47 @@
+{
+       "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"
+               ]
+       },
+       "AutoloadClasses": {
+               "ForcePreview": "ForcePreview.php"
+       },
+       "AvailableRights": [
+               "forcepreviewexempt"
+       ],
+       "ResourceModules": {
+               "ext.ForcePreview.livePreview": {
+                       "scripts": [
+                               "modules/ext.ForcePreview.livePreview.js"
+                       ]
+               },
+               "messages": [
+                       "savearticle"
+               ],
+               "dependencies": [
+                       "oojs-ui-core"
+               ]
+       },
+       "manifest_version": 1
+}
diff --git a/modules/ext.ForcePreview.livePreview.js 
b/modules/ext.ForcePreview.livePreview.js
new file mode 100644
index 0000000..2d20048
--- /dev/null
+++ b/modules/ext.ForcePreview.livePreview.js
@@ -0,0 +1,15 @@
+$( function () {
+       "use strict";
+
+       var preview = OO.ui.infuse( "wpPreviewWidget" ),
+               save = OO.ui.infuse( "wpSaveWidget" );
+
+       preview.on( "click", function enableSave() {
+               save.setFlags( { primary: true } )
+                       .setDisabled( false )
+                       .setValue( OO.ui.msg( "savearticle" ) );
+
+               preview.setFlags( { primary: false } )
+                       .off( "click", enableSave );
+       } );
+} );

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: If553b4c6248d0d8800afd678ab2c600a9dab7820
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/ForcePreview
Gerrit-Branch: master
Gerrit-Owner: Skizzerz <skizz...@skizzerz.net>

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

Reply via email to