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

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


Change subject: New Topic action: edit-title
......................................................................

New Topic action: edit-title

Allows updating the title for a given topic

Change-Id: Iae0ef602aed62ee64954c9ae894b1ae26e821177
---
M includes/Block/Topic.php
M includes/Data/RevisionStorage.php
A templates/edit-title.html.php
M templates/topic.html.php
4 files changed, 83 insertions(+), 3 deletions(-)


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

diff --git a/includes/Block/Topic.php b/includes/Block/Topic.php
index 1266d03..7b5dcdc 100644
--- a/includes/Block/Topic.php
+++ b/includes/Block/Topic.php
@@ -14,10 +14,11 @@
 class TopicBlock extends AbstractBlock {
 
        protected $root;
+       protected $topicTitle;
        protected $rootLoader;
        protected $newRevision;
 
-       protected $supportedActions = array( 'reply', 'delete-topic', 
'delete-post', 'restore-post' );
+       protected $supportedActions = array( 'edit-title', 'reply', 
'delete-topic', 'delete-post', 'restore-post' );
 
        public function __construct( Workflow $workflow, ManagerGroup $storage, 
$root ) {
                parent::__construct( $workflow, $storage );
@@ -34,6 +35,10 @@
 
        protected function validate() {
                switch( $this->action ) {
+               case 'edit-title':
+                       $this->validateEditTitle();
+                       break;
+
                case 'reply':
                        $this->validateReply();
                        break;
@@ -53,6 +58,21 @@
 
                default:
                        throw new \MWException( "Unexpected action: 
{$this->action}" );
+               }
+       }
+
+       protected function validateEditTitle() {
+               if ( $this->workflow->isNew() ) {
+                       $this->errors['content'] = wfMessage( 
'flow-no-existing-workflow' );
+               } elseif ( empty( $this->submitted['content'] ) ) {
+                       $this->errors['content'] = wfMessage( 
'flow-missing-title-content' );
+               } else {
+                       $topicTitle = $this->loadTopicTitle();
+                       if ( !$topicTitle ) {
+                               throw new \Exception( 'No revision associated 
with workflow?' );
+                       }
+
+                       $this->topicTitle = $topicTitle->newNextRevision( 
$this->user, $this->submitted['content'] );
                }
        }
 
@@ -186,6 +206,10 @@
                        $this->storage->put( $this->newRevision );
                        break;
 
+               case 'edit-title':
+                       $this->storage->put( $this->topicTitle );
+                       break;
+
                case 'delete-topic':
                        $this->storage->put( $this->workflow );
                        break;
@@ -210,6 +234,13 @@
                                'topic' => $this->workflow,
                                'history' => $history,
                        ), $return );
+               } elseif ( $this->action === 'edit-title' ) {
+                       return $templating->render( "flow:edit-title.html.php", 
array(
+                               'block' => $this,
+                               'user' => $this->user,
+                               'topic' => $this->workflow,
+                               'topicTitle' => $this->loadTopicTitle(),
+                       ) );
                }
 
                $templating->getOutput()->addModules( 'ext.flow.base' );
@@ -355,7 +386,23 @@
                if ( $this->root !== null ) {
                        return $this->root;
                }
-               return $this->root = $this->rootLoader->get( 
$this->workflow->getId() );
+               // topicTitle is same as root, difference is root has children 
populated to full depth
+               return $this->topicTitle = $this->root = 
$this->rootLoader->get( $this->workflow->getId() );
+       }
+
+       // Loads only the title, as opposed to loadRootPost which gets the 
entire tree of posts.
+       protected function loadTopicTitle() {
+               if ( $this->topicTitle === null ) {
+                       $found = $this->storage->find(
+                               'PostRevision',
+                               array( 'tree_rev_descendant_id' => 
$this->workflow->getId() ),
+                               array( 'sort' => 'rev_id', 'order' => 'DESC', 
'limit' => 1 )
+                       );
+                       if ( $found ) {
+                               $this->topicTitle = reset( $found );
+                       }
+               }
+               return $this->topicTitle;
        }
 
        // Somehow the template has to know which post the errors go with
diff --git a/includes/Data/RevisionStorage.php 
b/includes/Data/RevisionStorage.php
index aa05ce4..e4568d5 100644
--- a/includes/Data/RevisionStorage.php
+++ b/includes/Data/RevisionStorage.php
@@ -449,8 +449,12 @@
                if ( $name === null ) {
                        $name = $fromKey;
                }
+               $ids = array();
                foreach ( $source as $row ) {
                        $ids[] = $row[$fromKey];
+               }
+               if ( !$ids ) {
+                       return $source;
                }
                $res = call_user_func( $callable, $ids );
                if ( $res === false ) {
@@ -472,11 +476,15 @@
                if ( $name === null ) {
                        $name = $fromKey;
                }
+               $ids = array();
                foreach ( $multiSource as $source ) {
                        foreach ( $source as $row ) {
                                $ids[] = $row[$fromKey];
                        }
                }
+               if ( !$ids ) {
+                       return $multiSource;
+               }
                $res = call_user_func( $callable, array_unique( $ids ) );
                if ( $res === false ) {
                        return false;
diff --git a/templates/edit-title.html.php b/templates/edit-title.html.php
new file mode 100644
index 0000000..f3f2ca4
--- /dev/null
+++ b/templates/edit-title.html.php
@@ -0,0 +1,21 @@
+<?php
+
+echo wfMessage( 'flow-action-edit-title' )->escaped();
+if ( $block->hasErrors() ) {
+       echo wfMessage( 'flow-action-errors' )->escaped(), '<ul>';
+       foreach ( $block->getErrors() as $error ) {
+               echo $error->escaped();
+       }
+       echo '</ul>';
+}
+
+echo Html::openElement( 'form', array(
+               'method' => 'POST',
+               // root post shares its uuid with the workflow
+               'action' => $this->generateUrl( $topicTitle->getPostId(), 
'edit-title' ),
+       ) ),
+       Html::element( 'input', array( 'type' => 'hidden', 'name' => 
'wpEditToken', 'value' => $user->getEditToken( 'flow' ) ) ),
+               Html::textarea( $block->getName() . '[content]', 
$topicTitle->getContent() ),
+               Html::element( 'input', array( 'type' => 'submit', 'value' => 
wfMessage( 'flow-action-edit-title' )->plain() ) ),
+        '</form>';
+
diff --git a/templates/topic.html.php b/templates/topic.html.php
index a0f83b1..2ae7044 100644
--- a/templates/topic.html.php
+++ b/templates/topic.html.php
@@ -83,7 +83,11 @@
        echo '</div>';
 };
 
-echo Html::element( 'h4', array(), $root->getContent() );
+echo Html::element( 'h4', array(), $root->getContent() ),
+       Html::rawElement( 'a', array(
+               'href' => $this->generateUrl( $root->getPostId(), 'edit-title' )
+       ), wfMessage( 'flow-action-edit-title' ) );
+
 foreach( $root->getChildren() as $child ) {
        $renderPost( $child );
 }

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: Iae0ef602aed62ee64954c9ae894b1ae26e821177
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Flow
Gerrit-Branch: master
Gerrit-Owner: EBernhardson (WMF) <[email protected]>

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

Reply via email to