jenkins-bot has submitted this change and it was merged.

Change subject: Add a preview/confirmation menu; major special page internal 
restructuring
......................................................................


Add a preview/confirmation menu; major special page internal restructuring

Restructuring:
* Use one Status object, stored as $this->status.
* $this->state attempts to say what phase we're in.
* Moved spamlist sanity checks (existence, redirection) into their own function.
* Requests go through one expandable callback function.
* Basic data verification is its own function, and is done before anything else.
* Killed all non-configuration globals.
* Added own submit buttons rather than using HTMLForm's.

Preview menu:
* Rather than immediately submitting, require the user to preview the message.
* On the preview menu, the user can modify the form values before submitting.
* The user has the option to re-preview before submitting.
* The preview is wrapped in a <fieldset> to distingiush it from the form.

Change-Id: If0a5abf5803d1ec91542e5adb0fa1a9888634acb
---
M MassMessage.i18n.php
M SpecialMassMessage.php
2 files changed, 176 insertions(+), 51 deletions(-)

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



diff --git a/MassMessage.i18n.php b/MassMessage.i18n.php
index dc7cf58..cb9c69b 100644
--- a/MassMessage.i18n.php
+++ b/MassMessage.i18n.php
@@ -20,8 +20,11 @@
        'massmessage-form-subject' => 'Subject of the message (Also used as the 
edit summary):',
        'massmessage-form-message' => 'Body of the message:',
        'massmessage-form-global' => 'This is a global message.',
+       'massmessage-form-preview' => 'Preview',
        'massmessage-form-submit' => 'Send',
+       'massmessage-fieldset-preview' => 'Preview',
        'massmessage-submitted' => 'Your message has been queued.',
+       'massmessage-just-preview' => 'This is just a preview. Press 
"{{int:massmessage-form-submit}}" to send the message.',
        'massmessage-account-blocked' => 'The account used to deliver messages 
has been blocked.',
        'massmessage-spamlist-doesnotexist' => 'The specified page-list page 
does not exist.',
        'massmessage-empty-subject' => 'The subject line is empty.',
@@ -47,9 +50,13 @@
        'massmessage-form-subject' => 'Label for an inputbox on the special 
page.',
        'massmessage-form-message' => 'Used as label for a textarea on the 
special page.',
        'massmessage-form-global' => 'Label for a checkbox on the special 
page.',
+       'massmessage-form-preview' => 'Label for the preview button on the 
special page.
+{{Identical|Preview}}',
        'massmessage-form-submit' => 'Label for the submit button on the 
special page.
 {{Identical|Send}}',
+       'massmessage-fieldset-preview' => 'Label for the fieldset box around 
the page preview.',
        'massmessage-submitted' => 'Confirmation message the user sees after 
the form is submitted successfully and the request is queued in the job queue.',
+       'massmessage-just-preview' => 'Warning to user that what they are 
seeing is just a preview, and they should hit the send button to actually 
submit it.',
        'massmessage-account-blocked' => 'Error message the user sees if the 
bot account has been blocked.',
        'massmessage-spamlist-doesnotexist' => 'Error message the user sees if 
an invalid spamlist is provided.
 
diff --git a/SpecialMassMessage.php b/SpecialMassMessage.php
index 61c73de..c0c21ce 100644
--- a/SpecialMassMessage.php
+++ b/SpecialMassMessage.php
@@ -18,28 +18,42 @@
  
        function execute( $par ) {
                $request = $this->getRequest();
+               $context = $this->getContext();
                $output = $this->getOutput();
+
                $output->addModules( 'ext.MassMessage.special' );
                $this->setHeaders();
                $this->outputHeader();
                $this->checkPermissions();
-               $context = $this->getContext();
+
+               // Some variables...
+               $this->status = new Status();
+
+               // Figure out what state we're in.
+               if ( $request->getText( 'wpsubmit-button' ) == $context->msg( 
'massmessage-form-submit' )->text() ) {
+                       $this->state = 'submit';
+               } elseif ( $request->getText( 'wppreview-button' ) == 
$context->msg( 'massmessage-form-preview' )->text() ) {
+                       $this->state = 'preview';
+               } else{
+                       $this->state = 'form';
+               }
+
                $form = new HtmlForm( $this->createForm(), $context );
                $form->setId( 'massmessage-form' );
                $form->setDisplayFormat( 'div' );
-               $form->setHeaderText( wfMessage( 'massmessage-form-header' 
)->text()  );
-               $form->setSubmitText( $context->msg( 'massmessage-form-submit' 
)->text() );
-               $form->setSubmitId( 'massmessage-submit' );
-               $form->setSubmitCallback( array( $this, 'submit' ) );
+               if ( $this->state == 'form' ) {
+                       $form->addPreText( $context->msg( 
'massmessage-form-header' )->parse() );
+               }
+               $form->setWrapperLegendMsg( 'massmessage' );
+               $form->suppressDefaultSubmit(); // We use our own buttons.
+               $form->setSubmitCallback( array( $this, 'callback' ) );
+               $form->setMethod( 'post' );
                
                $form->prepareForm();
                $result = $form->tryAuthorizedSubmit();
                if ( $result === true || ( $result instanceof Status && 
$result->isGood() ) ) {
-                       $this->getOutput()->addWikiMsg( 'massmessage-submitted' 
);
-               } elseif ( $result instanceof Status ) {
-                       $errors = $result->getErrorsArray();
-                       foreach ( $errors as $msg ) {
-                               $this->getOutput()->addWikiMsg( $msg );
+                       if ( $this->state == 'submit' ) { // If it's preview, 
everything is shown already.
+                               $this->getOutput()->addWikiMsg( 
'massmessage-submitted' );
                        }
                } else {
                        $form->displayForm( $result );
@@ -47,8 +61,8 @@
        }
 
        function createForm() {
-               global $wgUser;
                $request = $this->getRequest();
+               $context = $this->getContext();
                $m = array();
                // Who to send to
                $m['spamlist'] = array(
@@ -74,12 +88,26 @@
                        'default' => $request->getText( 'message' )
                );
 
-               if ( $wgUser->isAllowed( 'massmessage-global' ) ) {
+               if ( $this->getUser()->isAllowed( 'massmessage-global' ) ) {
                        $m['global'] = array(
                                'id' => 'form-global',
                                'type' => 'check',
                                'label-message' => 'massmessage-form-global',
                                'default' => $request->getText( 'global' ) == 
'yes' ? true : false
+                       );
+               }
+
+               $m['preview-button'] = array(
+                       'id' => 'form-preview-button',
+                       'type' => 'submit',
+                       'default' => $context->msg( 'massmessage-form-preview' 
)->text()
+               );
+
+               if ( $this->state == 'preview' ) {
+                       $m['submit-button'] = array(
+                               'id' => 'form-submit-button',
+                               'type' => 'submit',
+                               'default' => $context->msg( 
'massmessage-form-submit' )->text()
                        );
                }
 
@@ -126,16 +154,141 @@
         * @param $subject string
         */
        function logToWiki( $spamlist, $subject ) {
-               global $wgUser;
                // $title->getLatestRevID()
        
                $logEntry = new ManualLogEntry( 'massmessage', 'send' );
-               $logEntry->setPerformer( $wgUser );
+               $logEntry->setPerformer( $this->getUser() );
                $logEntry->setTarget( $spamlist );
                $logEntry->setComment( $subject ); 
 
                $logid = $logEntry->insert();
                $logEntry->publish( $logid );
+
+       }
+
+       /*
+        * Callback function
+        * Does some basic verification of data
+        * Decides whether to show the preview screen
+        * or the submitted message
+        *
+        * @param $data Array
+        * @return Status
+        */
+       function callback( $data ) {
+
+               $this->verifyData( $data );
+
+               // Die on errors.
+               if ( !$this->status->isOK() ) {
+                       $this->state = 'form';
+                       return $this->status;
+               }
+
+               if ( $this->state == 'submit' ) {
+                       return $this->submit( $data );
+               } else { // $this->state can only be 'preview' here
+                       return $this->preview( $data );
+               }
+       }
+       /*
+        * Parse and normalize the spamlist
+        *
+        * @param $title string
+        * @return Title|string string will be a error message key
+        */
+       function getLocalSpamlist( $title ) {
+               $spamlist = Title::newFromText( $title );
+               if ( $spamlist === null || !$spamlist->exists() ) {
+                       return 'massmessage-spamlist-doesnotexist' ;
+               } else {
+                       // Page exists, follow a redirect if possible
+                       $target = MassMessage::followRedirect( $spamlist );
+                       if ( $target === null || !$target->exists() ) {
+                               return 'massmessage-spamlist-doesnotexist' ; // 
Interwiki redirect or non-existent page.
+                       } else {
+                               $spamlist = $target;
+                       }
+               }
+               return $spamlist;
+
+       }
+
+       /*
+        * Sanity check the data, throwing any errors if necessary
+        *
+        * @param $data Array
+        * @return Status
+        */
+       function verifyData( $data ) {
+
+               $global = isset( $data['global'] ) && $data['global']; // If 
the message delivery is global
+
+               $spamlist = $this->getLocalSpamlist( $data['spamlist'] );
+               if ( !( $spamlist instanceof Title ) ) {
+                       $this->status->fatal( $spamlist );
+               }
+
+               // Check that our account hasn't been blocked.
+               $user = MassMessage::getMessengerUser();
+               if ( !$global && $user->isBlocked() ) {
+                       // If our delivery is global, it doesnt matter if our 
local account is blocked
+                       $this->status->fatal( 'massmessage-account-blocked' );
+               }
+
+               if ( trim( $data['subject'] ) === '' ) {
+                       $this->status->fatal( 'massmessage-empty-subject' );
+               }
+
+               if ( trim( $data['message'] ) === '' ) {
+                       $this->status->fatal( 'massmessage-empty-message' );
+               }
+
+               return $this->status;
+       }
+
+
+       /*
+        * A preview/confirmation screen
+        *
+        * @param $data Array
+        * @return Status
+        */
+       function preview( $data ) {
+
+               $spamlist = $this->getLocalSpamlist( $data['spamlist'] );
+               $targets = $this->getLocalTargets( $spamlist );
+               // $firstTarget = array_values( $targets )[0]; // Why doesn't 
this work??
+               $firstTarget = Title::newFromText( 'User talk:Example' );
+               $article = Article::newFromTitle( $firstTarget, 
$this->getContext() );
+
+               // Hacked up from EditPage.php
+               // Is this really the best way to do this???
+
+               $subject = $data['subject'];
+               $message = $data['message'];
+
+               // Convert into a content object
+               $content = ContentHandler::makeContent( $message, $firstTarget 
);
+
+               // Parser stuff. Taken from EditPage::getPreviewText()
+
+               $parserOptions = $article->makeParserOptions( 
$article->getContext() );
+               $parserOptions->setEditSection( false );
+               $parserOptions->setIsPreview( true );
+               $parserOptions->setIsSectionPreview( false );
+               $content = $content->addSectionHeader( $subject );
+
+               // Hooks not being run: EditPageGetPreviewContent, 
EditPageGetPreviewText
+
+               $content = $content->preSaveTransform( $firstTarget, 
$this->getUser(), $parserOptions );
+               $parserOutput = $content->getParserOutput( $firstTarget, null, 
$parserOptions );
+               $previewHTML = $parserOutput->getText();
+               $this->getOutput()->addWikiMsg( 'massmessage-just-preview' );
+               $fieldsetMessage = $this->getContext()->msg( 
'massmessage-fieldset-preview' )->text();
+               $wrapFieldset = Xml::fieldset( $fieldsetMessage, $previewHTML );
+               $this->getOutput()->addHTML( $wrapFieldset );
+               return false;
 
        }
 
@@ -146,43 +299,8 @@
         * @return Status
         */
        function submit( $data ) {
-               // Check that the spamlist exists.
-               $spamlist = Title::newFromText( $data['spamlist'] );
-               $global = isset( $data['global'] ) && $data['global']; // If 
the message delivery is global
-               $status = new Status();
-               $errors = array();
-               if ( $spamlist === null || !$spamlist->exists() ) {
-                       $status->fatal( 'massmessage-spamlist-doesnotexist' );
-               } else {
-                       // Page exists, follow a redirect if possible
-                       $target = MassMessage::followRedirect( $spamlist );
-                       if ( $target === null || !$target->exists() ) {
-                               $status->fatal( 
'massmessage-spamlist-doesnotexist' ); // Interwiki redirect or non-existent 
page.
-                       } else {
-                               $spamlist = $target;
-                       }
-               }
 
-
-               // Check that our account hasn't been blocked.
-               $user = MassMessage::getMessengerUser();
-               if ( !$global && $user->isBlocked() ) {
-                       // If our delivery is global, it doesnt matter if our 
local account is blocked
-                       $status->fatal( 'massmessage-account-blocked' );
-               }
-
-               if ( trim( $data['subject'] ) === '' ) {
-                       $status->fatal( 'massmessage-empty-subject' );
-               }
-
-               if ( trim( $data['message'] ) === '' ) {
-                       $status->fatal( 'massmessage-empty-message' );
-               }
-
-               // If we have any errors, abort.
-               if ( !$status->isOK() ) {
-                       return $status;
-               }
+               $spamlist = $this->getLocalSpamlist( $data['spamlist'] );
 
                // Log it.
                $this->logToWiki( $spamlist, $data['subject'] );
@@ -193,6 +311,6 @@
                        $job = new MassMessageJob( $page, $data );
                        JobQueueGroup::singleton()->push( $job );
                }
-               return $status;
+               return $this->status;
        }
 }

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

Gerrit-MessageType: merged
Gerrit-Change-Id: If0a5abf5803d1ec91542e5adb0fa1a9888634acb
Gerrit-PatchSet: 10
Gerrit-Project: mediawiki/extensions/MassMessage
Gerrit-Branch: master
Gerrit-Owner: Legoktm <[email protected]>
Gerrit-Reviewer: Aude <[email protected]>
Gerrit-Reviewer: Legoktm <[email protected]>
Gerrit-Reviewer: MZMcBride <[email protected]>
Gerrit-Reviewer: Yuvipanda <[email protected]>
Gerrit-Reviewer: jenkins-bot

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to