EBernhardson (WMF) has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/78933


Change subject: edit-post action for Topic block
......................................................................

edit-post action for Topic block

Change-Id: Ie62ff67ae09ec131ae49acce3ffe779311203842
---
M Flow.php
M container.php
M includes/Block/Topic.php
A templates/edit-post.html.php
M templates/topic.html.php
5 files changed, 142 insertions(+), 27 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Flow 
refs/changes/33/78933/1

diff --git a/Flow.php b/Flow.php
index 6df3ccb..f5be5aa 100755
--- a/Flow.php
+++ b/Flow.php
@@ -40,7 +40,7 @@
        'descriptionmsg' => 'flow-desc',
 );
 
-$dir = dirname( __FILE__ ) . '/';
+$dir = __DIR__ . '/';
 $wgExtensionMessagesFiles['Flow'] = $dir . 'Flow.i18n.php';
 
 // Classes fulfilling the mediawiki extension architecture
diff --git a/container.php b/container.php
index d2665cc..401f24f 100644
--- a/container.php
+++ b/container.php
@@ -33,7 +33,11 @@
 $c['templating.namespaces'] = array(
        'flow' => __DIR__ . '/templates',
 );
-$c['templating.global_variables'] = array();
+$c['templating.global_variables'] = $c->share( function( $c ) {
+       return array(
+               'user' => $c['user'],
+       );
+} );
 $c['templating'] = $c->share( function( $c ) {
        return new Flow\Templating(
                $c['url_generator'],
diff --git a/includes/Block/Topic.php b/includes/Block/Topic.php
index 1266d03..32d0f3b 100644
--- a/includes/Block/Topic.php
+++ b/includes/Block/Topic.php
@@ -16,8 +16,12 @@
        protected $root;
        protected $rootLoader;
        protected $newRevision;
+       protected $requestedPost;
 
-       protected $supportedActions = array( 'reply', 'delete-topic', 
'delete-post', 'restore-post' );
+       protected $supportedActions = array(
+               'delete-post', 'restore-post', 'edit-post',
+               'reply', 'delete-topic',
+       );
 
        public function __construct( Workflow $workflow, ManagerGroup $storage, 
$root ) {
                parent::__construct( $workflow, $storage );
@@ -49,6 +53,10 @@
 
                case 'restore-post':
                        $this->validateRestorePost();
+                       break;
+
+               case 'edit-post':
+                       $this->validateEditPost();
                        break;
 
                default:
@@ -133,6 +141,28 @@
                }
        }
 
+       protected function validateEditPost() {
+               if ( empty( $this->submitted['postId'] ) ) {
+                       $this->errors['edit-post'] = wfMessage( 
'flow-no-post-provided' );
+                       return;
+               }
+               if ( empty( $this->submitted['content'] ) ) {
+                       $this->errors['content'] = wfMessage( 
'flow-missing-post-content' );
+               } else {
+                       $this->parsedContent = $this->convertWikitextToHtml5( 
$this->submitted['content'] );
+                       if ( empty( $this->parsedContent ) ) {
+                               $this->errors['content'] = wfMessage( 
'flow-empty-parsoid-result' );
+                               return;
+                       }
+               }
+               $post = $this->loadRequestedPost( $this->submitted['postId'] );
+               if ( $post ) {
+                       $this->newRevision = $post->newNextRevision( 
$this->user, $this->parsedContent );
+               } else {
+                       $this->errors['edit-post'] = wfMessage( 
'flow-post-not-found' );
+               }
+       }
+
        // @todo: I assume not only topic reply, but also TopicListBlock & 
SummaryBlock's content need to be converted?
        protected function convertWikitextToHtml5( $wikitext ) {
                global $wgFlowUseParsoid;
@@ -180,6 +210,7 @@
                case 'reply':
                case 'delete-post':
                case 'restore-post':
+               case 'edit-post':
                        if ( $this->newRevision === null ) {
                                throw new \MWException( 'Attempt to save null 
revision' );
                        }
@@ -196,24 +227,47 @@
        }
 
        public function render( Templating $templating, array $options, $return 
= false ) {
-               if ( $this->action === 'post-history' ) {
-                       if ( empty( $options['postId'] ) ) {
-                               var_dump( $this->getName() );
-                               var_dump( $options );
-                               throw new \Exception( 'No postId specified' );
-                               $history = array();
-                       } else {
-                               $history = $this->getHistory( 
$options['postId'] );
-                       }
-                       return $templating->render( 
"flow:post-history.html.php", array(
-                               'block' => $this,
-                               'topic' => $this->workflow,
-                               'history' => $history,
-                       ), $return );
-               }
-
                $templating->getOutput()->addModules( 'ext.flow.base' );
+               switch( $this->action ) {
+               case 'post-history':
+                       return $this->renderPostHistory( $templating, $options, 
$return );
 
+               case 'edit-post':
+                       return $this->renderEditPost( $templating, $options, 
$return );
+
+               default:
+                       return $this->renderGeneric( $templating, $options, 
$return );
+               }
+       }
+
+       protected function renderPostHistory( Templating $templating, array 
$options, $return = false ) {
+               if ( empty( $options['postId'] ) ) {
+                       var_dump( $this->getName() );
+                       var_dump( $options );
+                       throw new \Exception( 'Could not locate post' );
+                       $history = array();
+               } else {
+                       $history = $this->getHistory( $options['postId'] );
+               }
+               return $templating->render( "flow:post-history.html.php", array(
+                       'block' => $this,
+                       'topic' => $this->workflow,
+                       'history' => $history,
+               ), $return );
+       }
+
+       protected function renderEditPost( Templating $templating, array 
$options, $return = false ) {
+               if ( !isset( $options['postId'] ) ) {
+                       throw new \Exception( 'No postId provided' );
+               }
+               return $templating->render( "flow:edit-post.html.php", array(
+                       'block' => $this,
+                       'topic' => $this->workflow,
+                       'post' => $this->loadRequestedPost( $options['postId'] 
),
+               ), $return );
+       }
+
+       protected function renderGeneric( Templating $templating, array 
$options, $return = false ) {
                return $templating->render( "flow:topic.html.php", array(
                        'block' => $this,
                        'topic' => $this->workflow,
@@ -358,6 +412,24 @@
                return $this->root = $this->rootLoader->get( 
$this->workflow->getId() );
        }
 
+       protected function loadRequestedPost( $postId ) {
+               if ( !isset( $this->requestedPost[$postId] ) ) {
+                       $found = $this->storage->find(
+                               'PostRevision',
+                               array( 'tree_rev_descendant_id' => $postId ),
+                               array( 'sort' => 'rev_id', 'order' => 'DESC', 
'limit' => 1 )
+                       );
+                       if ( $found ) {
+                               $this->requestedPost[$postId] = reset( $found );
+                       } else {
+                               // meh, signals that its not found, dont look 
again
+                               $this->requestedPost[$postId] = false;
+                       }
+               }
+               // catches the === false and returns null as expected
+               return $this->requestedPost[$postId] ?: null;
+       }
+
        // Somehow the template has to know which post the errors go with
        public function getRepliedTo() {
                return isset( $this->submitted['replyTo'] ) ? 
$this->submitted['replyTo'] : null;
@@ -370,7 +442,7 @@
 
        // The prefix used for form data
        public function getName() {
-               return 'topic_list';
+               return 'topic';
        }
 
 }
diff --git a/templates/edit-post.html.php b/templates/edit-post.html.php
new file mode 100644
index 0000000..c607c14
--- /dev/null
+++ b/templates/edit-post.html.php
@@ -0,0 +1,32 @@
+<?php
+
+echo Html::openElement( 'form', array(
+       'method' => 'POST',
+       'action' => $this->generateUrl( $topic->getId(), 'edit-post' ),
+) );
+$editToken = $user->getEditToken( 'flow' );
+if ( $block->hasErrors() ) {
+       echo '<ul>';
+       foreach ( $block->getErrors() as $error ) {
+               echo '<li>', $error->text() . '</li>'; // the pain ...
+       }
+       echo '</ul>';
+}
+
+echo Html::element( 'input', array(
+               'type' => 'hidden',
+               'name' => 'wpEditToken',
+               'value' => $user->getEditToken( 'flow' ),
+       ) ),
+       Html::element( 'input', array(
+               'type' => 'hidden',
+               'name' => $block->getName() . '[postId]',
+               'value' => $post->getPostId()->getHex(),
+       ) ),
+       Html::textarea( $block->getName() . '[content]', $post->getContent() ),
+       Html::element( 'input', array(
+               'type' => 'submit',
+               'value' => wfMessage( 'flow-action-edit-post' )->plain()
+       ) ),
+       '</form>';
+
diff --git a/templates/topic.html.php b/templates/topic.html.php
index a0f83b1..2b7c1e8 100644
--- a/templates/topic.html.php
+++ b/templates/topic.html.php
@@ -48,13 +48,20 @@
                        . wfMessage( 'flow-content' ) . $post->getContent()
                        . '<ul>';
                $postAction( 'delete-post', array( 'postId' => 
$post->getPostId()->getHex() ) );
-               echo '<li>' . Html::element( 'a', array(
-                       'href' => $self->generateUrl( $root->getPostId(), 
'post-history', array(
-                               $block->getName() . '[postId]' => 
$post->getPostId()->getHex(),
-                       ) ),
-               ), wfMessage( 'flow-post-action-history' )->plain() ) . '<li>';
-               echo '</ul>'
-                       . Html::openElement( 'form', array(
+               echo '<li>',
+                               Html::element( 'a', array(
+                                       'href' => $self->generateUrl( 
$root->getPostId(), 'edit-post', array(
+                                               $block->getName() . '[postId]' 
=> $post->getPostId()->getHex(),
+                               ) ) ), wfMessage( 'flow-post-action-edit-post' 
)->plain() ) .
+                       '</li>',
+                       '<li>',
+                               Html::element( 'a', array(
+                                       'href' => $self->generateUrl( 
$root->getPostId(), 'post-history', array(
+                                               $block->getName() . '[postId]' 
=> $post->getPostId()->getHex(),
+                               ) ) ), wfMessage( 'flow-post-action-history' 
)->plain() ) .
+                       '</li>',
+                       '</ul>',
+                       Html::openElement( 'form', array(
                                'method' => 'POST',
                                // root post id is same as topic workflow id
                                'action' => $self->generateUrl( 
$root->getPostId(), 'reply' ),

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ie62ff67ae09ec131ae49acce3ffe779311203842
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Flow
Gerrit-Branch: master
Gerrit-Owner: EBernhardson (WMF) <ebernhard...@wikimedia.org>

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

Reply via email to