EBernhardson (WMF) has submitted this change and it was merged.

Change subject: Move post actions into their own class.
......................................................................


Move post actions into their own class.

Change-Id: I56936c41e0dbee39a93d7dcecfc5ad04ecd8f801
---
M Flow.php
M includes/Templating.php
A includes/View/PostActionMenu.php
M templates/post.html.php
4 files changed, 139 insertions(+), 93 deletions(-)

Approvals:
  EBernhardson (WMF): Looks good to me, approved
  jenkins-bot: Verified



diff --git a/Flow.php b/Flow.php
index 4a750a4..a464a84 100755
--- a/Flow.php
+++ b/Flow.php
@@ -74,6 +74,9 @@
 $wgAutoloadClasses['Flow\Model\Workflow'] = $dir . 
'includes/Model/Workflow.php';
 $wgAutoloadClasses['Flow\Model\UUID'] = "$dir/includes/Model/UUID.php";
 
+// Helpers for templating
+$wgAutoloadClasses['Flow\View\PostActionMenu'] = 
"$dir/includes/View/PostActionMenu.php";
+
 // Classes that deal with database interaction between database and the models
 $wgAutoloadClasses['Flow\Repository\TreeRepository'] = $dir . 
'includes/Repository/TreeRepository.php';
 $wgAutoloadClasses['Flow\Repository\MultiGetList'] = $dir . 
'includes/Repository/MultiGetList.php';
diff --git a/includes/Templating.php b/includes/Templating.php
index 87943f8..f647189 100644
--- a/includes/Templating.php
+++ b/includes/Templating.php
@@ -16,6 +16,7 @@
 use SpecialPage;
 use Title;
 use User;
+use Flow\View\PostActionMenu;
 
 class Templating {
        protected $namespaces;
@@ -92,6 +93,9 @@
                        array(
                                'block' => $block,
                                'post' => $post,
+                               // An ideal world may pull this from the 
container, but for now this is fine.  This templating
+                               // class has too many responsibilities to keep 
receiving all required objects in the constructor.
+                               'postActionMenu' => new PostActionMenu( 
$this->urlGenerator ),
                        ),
                        $return
                );
diff --git a/includes/View/PostActionMenu.php b/includes/View/PostActionMenu.php
new file mode 100644
index 0000000..e7a5dbf
--- /dev/null
+++ b/includes/View/PostActionMenu.php
@@ -0,0 +1,129 @@
+<?php
+
+namespace Flow\View;
+
+use Flow\Block\Block;
+use Flow\Model\PostRevision;
+use Flow\UrlGenerator;
+use Html;
+
+class PostActionMenu {
+       // Received via constructor
+       protected $urlGenerator;
+
+       // Received for each call of self::get
+       protected $block;
+       protected $editToken;
+       protected $post;
+       protected $user;
+
+       public function __construct( UrlGenerator $urlGenerator ) {
+               $this->urlGenerator = $urlGenerator;
+       }
+
+       public function get( $user, Block $block, PostRevision $post, 
$editToken ) {
+               // For this type of shared-state to remain consistent self::get 
*must* be the only public methodctio
+               $this->user = $user;
+               $this->block = $block;
+               $this->post = $post;
+               $this->editToken = $editToken;
+
+               $actions = array();
+
+               switch( $post->getModerationState() ) {
+               case $post::MODERATED_NONE:
+                       if ( $user->isAllowed( 'flow-hide' ) ) {
+                               $actions['hide'] = $this->postAction( 
'hide-post', array( 'postId' => $post->getPostId()->getHex() ), 
'mw-ui-destructive' );
+                       }
+                       if ( $user->isAllowed( 'flow-delete' ) ) {
+                               $actions['delete'] = $this->postAction( 
'delete-post', array( 'postId' => $post->getPostId()->getHex() ), 
'mw-ui-destructive' );
+                       }
+                       if ( $user->isAllowed( 'flow-censor' ) ) {
+                               $actions['censor'] = $this->postAction( 
'censor-post', array( 'postId' => $post->getPostId()->getHex() ), 
'mw-ui-destructive' );
+                       }
+                       $actions['history'] = $this->getAction( 'post-history' 
);
+                       $actions['edit-post'] = $this->getAction( 'edit-post' );
+                       break;
+
+               case $post::MODERATED_HIDDEN:
+                       if ( $user->isAllowed( 'flow-hide' ) ) {
+                               $actions['restore'] = $this->postAction( 
'restore-post', array( 'postId' => $post->getPostId()->getHex() ), 
'mw-ui-constructive' );
+                       }
+                       if ( $user->isAllowed( 'flow-delete' ) ) {
+                               $actions['delete'] = $this->postAction( 
'delete-post', array( 'postId' => $post->getPostId()->getHex() ), 
'mw-ui-destructive' );
+                       }
+                       if ( $user->isAllowed( 'flow-censor' ) ) {
+                               $actions['censor'] = $this->postAction( 
'censor-post', array( 'postId' => $post->getPostId()->getHex() ), 
'mw-ui-destructive' );
+                       }
+                       $actions['history'] = $this->getAction( 'post-history' 
);
+                       break;
+
+               case $post::MODERATED_DELETED:
+                       if ( $user->isAllowedAny( 'flow-delete', 'flow-censor' 
) ) {
+                               $actions['restore'] = $this->postAction( 
'restore-post', array( 'postId' => $post->getPostId()->getHex() ), 
'mw-ui-constructive' );
+                       }
+                       $actions['history'] = $this->getAction( 'post-history' 
);
+                       break;
+
+               case $post::MODERATED_CENSORED:
+                       if ( !$user->isAllowed( 'flow-censor' ) ) {
+                               // no children, no nothing
+                               return;
+                       }
+                       $actions['restore'] = $this->postAction( 
'restore-post', array( 'postId' => $post->getPostId()->getHex() ), 
'mw-ui-constructive' );
+                       $actions['history'] = $this->getAction( 'post-history' 
);
+                       break;
+               }
+
+               // Default always-available actions
+               $actions['permalink'] = $this->getAction( 'view' );
+
+               return $actions;
+       }
+
+       protected function postAction( $action, array $data = array(), $class ) 
{
+               // actions that change things must be post requests
+               $output = array(
+                       Html::openElement( 'form', array(
+                               'method' => 'POST',
+                               'action' => $this->urlGenerator->generateUrl( 
$this->block->getWorkflowId(), $action )
+                       ) ),
+                       Html::element( 'input', array( 'type' => 'hidden', 
'name' => 'wpEditToken', 'value' => $this->editToken ) )
+               );
+
+               foreach ( $data as $name => $value ) {
+                       $output[] = Html::element( 'input', array(
+                               'type' => 'hidden',
+                               'name' => $this->block->getName() . "[$name]",
+                               'value' => $value,
+                       ) );
+               }
+               // Messages: flow-post-action-censor-post, 
flow-post-action-delete-post,
+               // flow-post-action-hide-post, flow-post-action-restore-post
+               $output[] = Html::element( 'input', array(
+                       'type' => 'submit',
+                       'class' => "mw-ui-button $class",
+                       'value' => wfMessage( "flow-post-action-$action" 
)->plain(),
+               ) ) . '</form>';
+
+               return implode( '', $output );
+       }
+
+       protected function getAction( $action ) {
+               $url = $this->urlGenerator->generateUrl(
+                       $this->block->getWorkflowId(),
+                       $action,
+                       array(
+                               $this->block->getName() . '[postId]' => 
$this->post->getPostId()->getHex(),
+                       )
+               );
+               // Messages: flow-post-action-view, 
flow-post-action-post-history, flow-post-action-edit-post
+               return Html::element(
+                       'a',
+                       array(
+                               'href' => $url,
+                       ),
+                       wfMessage( "flow-post-action-$action" )->plain()
+               );
+       }
+}
diff --git a/templates/post.html.php b/templates/post.html.php
index 93b1608..9cfd422 100644
--- a/templates/post.html.php
+++ b/templates/post.html.php
@@ -2,50 +2,6 @@
 
 $self = $this;
 
-$postAction = function( $action, array $data = array(), $class = '' ) use( 
$self, $block, $editToken ) {
-       // actions that change things must be post requests
-       $output = '';
-       $output .= Html::openElement( 'form', array(
-               'method' => 'POST',
-               'action' => $self->generateUrl( $block->getWorkflowId(), 
$action )
-       ) );
-       $output .= Html::element( 'input', array( 'type' => 'hidden', 'name' => 
'wpEditToken', 'value' => $editToken) );
-       foreach ( $data as $name => $value ) {
-               $output .= Html::element( 'input', array(
-                       'type' => 'hidden',
-                       'name' => $block->getName() . "[$name]",
-                       'value' => $value,
-               ) );
-       }
-       // Messages: flow-post-action-censor-post, flow-post-action-delete-post,
-       // flow-post-action-hide-post, flow-post-action-restore-post
-       $output .= Html::element( 'input', array(
-               'type' => 'submit',
-               'class' => 'mw-ui-button '.$class,
-               'value' => wfMessage( "flow-post-action-$action" )->plain(),
-       ) ) . '</form>';
-
-       return $output;
-};
-
-$getAction = function( $action, $data = array(), $class = '' ) use ( $post, 
$self, $block ) {
-       $url = $self->generateUrl(
-               $block->getWorkflowId(),
-               $action,
-               array(
-                       $block->getName() . '[postId]' => 
$post->getPostId()->getHex(),
-               )
-       );
-       // Messages: flow-post-action-view, flow-post-action-post-history, 
flow-post-action-edit-post
-       return Html::element( 'a',
-               array(
-                       'href' => $url,
-                       'class' => $class,
-               ),
-               wfMessage( "flow-post-action-$action" )->plain()
-       );
-};
-
 $createReplyForm = function() use( $self, $block, $post, $editToken, $user ) {
        $replyForm = Html::openElement( 'form', array(
                        'method' => 'POST',
@@ -90,55 +46,9 @@
 $actions = array();
 $replyForm = '';
 
-// Build the actions for the post
-switch( $post->getModerationState() ) {
-case $post::MODERATED_NONE:
-       if ( $user->isAllowed( 'flow-hide' ) ) {
-               $actions['hide'] = $postAction( 'hide-post', array( 'postId' => 
$post->getPostId()->getHex() ), 'mw-ui-destructive' );
-       }
-       if ( $user->isAllowed( 'flow-delete' ) ) {
-               $actions['delete'] = $postAction( 'delete-post', array( 
'postId' => $post->getPostId()->getHex() ), 'mw-ui-destructive' );
-       }
-       if ( $user->isAllowed( 'flow-censor' ) ) {
-               $actions['censor'] = $postAction( 'censor-post', array( 
'postId' => $post->getPostId()->getHex() ), 'mw-ui-destructive' );
-       }
-       $actions['history'] = $getAction( 'post-history' );
-       $actions['edit-post'] = $getAction( 'edit-post' );
+if ( $post->getModerationState() == $post::MODERATED_NONE ) {
        $replyForm = $createReplyForm();
-       break;
-
-case $post::MODERATED_HIDDEN:
-       if ( $user->isAllowed( 'flow-hide' ) ) {
-               $actions['restore'] = $postAction( 'restore-post', array( 
'postId' => $post->getPostId()->getHex() ), 'mw-ui-constructive' );
-       }
-       if ( $user->isAllowed( 'flow-delete' ) ) {
-               $actions['delete'] = $postAction( 'delete-post', array( 
'postId' => $post->getPostId()->getHex() ), 'mw-ui-destructive' );
-       }
-       if ( $user->isAllowed( 'flow-censor' ) ) {
-               $actions['censor'] = $postAction( 'censor-post', array( 
'postId' => $post->getPostId()->getHex() ), 'mw-ui-destructive' );
-       }
-       $actions['history'] = $getAction( 'post-history' );
-       break;
-
-case $post::MODERATED_DELETED:
-       if ( $user->isAllowedAny( 'flow-delete', 'flow-censor' ) ) {
-               $actions['restore'] = $postAction( 'restore-post', array( 
'postId' => $post->getPostId()->getHex() ), 'mw-ui-constructive' );
-       }
-       $actions['history'] = $getAction( 'post-history' );
-       break;
-
-case $post::MODERATED_CENSORED:
-       if ( !$user->isAllowed( 'flow-censor' ) ) {
-               // no children, no nothing
-               return;
-       }
-       $actions['restore'] = $postAction( 'restore-post', array( 'postId' => 
$post->getPostId()->getHex() ), 'mw-ui-constructive' );
-       $actions['history'] = $getAction( 'post-history' );
-       break;
 }
-
-// Default always-available actions
-$actions['permalink'] = $getAction( 'view' );
 
 // The actual output
 echo Html::openElement( 'div', array(
@@ -180,8 +90,8 @@
                                <div class="flow-post-actionbox">
                                        <ul>
                                                <?php
-                                               foreach( $actions as $key => 
$action ) {
-                                                       echo '<li 
class="flow-action-'.$key.'">' . $action . "</li>\n";
+                                               foreach( $postActionMenu->get( 
$user, $block, $post, $editToken ) as $key => $action ) {
+                                                       echo "<li 
class=\"flow-action-$key\">$action</li>";
                                                }
                                                ?>
                                        </ul>

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

Gerrit-MessageType: merged
Gerrit-Change-Id: I56936c41e0dbee39a93d7dcecfc5ad04ecd8f801
Gerrit-PatchSet: 2
Gerrit-Project: mediawiki/extensions/Flow
Gerrit-Branch: master
Gerrit-Owner: Werdna <[email protected]>
Gerrit-Reviewer: EBernhardson (WMF) <[email protected]>
Gerrit-Reviewer: jenkins-bot

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

Reply via email to