Matthias Mullie has uploaded a new change for review.

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

Change subject: Some changes to diff & hist links
......................................................................

Some changes to diff & hist links

* Diff link should check if there is something to diff against
* Added "prev" and "cur" links, that will be used in history-view
* Removed formatDiffHistPipeList in favor of 4 methods to fetch specific link

Change-Id: I40fef27d21f385905dfbf0b81fcc472aa9c8927a
---
M includes/Formatter/AbstractFormatter.php
M includes/Formatter/Contributions.php
M includes/Formatter/RecentChanges.php
M includes/Formatter/RevisionFormatter.php
4 files changed, 195 insertions(+), 42 deletions(-)


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

diff --git a/includes/Formatter/AbstractFormatter.php 
b/includes/Formatter/AbstractFormatter.php
index a961f75..df34ceb 100644
--- a/includes/Formatter/AbstractFormatter.php
+++ b/includes/Formatter/AbstractFormatter.php
@@ -117,35 +117,79 @@
        }
 
        /**
-        * Generate HTML for "(diff | hist)".  This will always contain both
-        * elements, they will be linked if the result from RevisionFormatter
-        * contains relevant links.
+        * Gets the "diff" link; linking to the diff against the previous 
revision,
+        * in a format that can be wrapped in an array and passed to
+        * formatLinksAsPipeList.
         *
         * @param array[][] Associative array containing (url, message) tuples
         * @param IContextSource $ctx
-        * @return string Html valid for user output
+        * @return array|Message
         */
-       protected function formatDiffHistPipeList( array $input, IContextSource 
$ctx ) {
-               $links = array();
-               if ( isset( $input['diff'] ) ) {
-                       $links[] = $input['diff'];
-               } else {
+       protected function getDiffLink( array $input, IContextSource $ctx ) {
+               if ( !isset( $input['diff'] ) ) {
                        // plain text with no link
-                       $links[] = $ctx->msg( 'diff' );
+                       return $ctx->msg( 'diff' );
                }
 
+               return $input['diff'];
+       }
+
+       /**
+        * Gets the "prev" link; linking to the diff against the previous 
revision,
+        * in a format that can be wrapped in an array and passed to
+        * formatLinksAsPipeList.
+        *
+        * @param array[][] Associative array containing (url, message) tuples
+        * @param IContextSource $ctx
+        * @return array|Message
+        */
+       protected function getDiffPrevLink( array $input, IContextSource $ctx ) 
{
+               if ( !isset( $input['diff-prev'] ) ) {
+                       // plain text with no link
+                       return $ctx->msg( 'last' );
+               }
+
+               return $input['diff-prev'];
+       }
+
+       /**
+        * Gets the "cur" link; linking to the diff against the current 
revision,
+        * in a format that can be wrapped in an array and passed to
+        * formatLinksAsPipeList.
+        *
+        * @param array[][] Associative array containing (url, message) tuples
+        * @param IContextSource $ctx
+        * @return array|Message
+        */
+       protected function getDiffCurLink( array $input, IContextSource $ctx ) {
+               if ( !isset( $input['diff-cur'] ) ) {
+                       // plain text with no link
+                       return $ctx->msg( 'cur' );
+               }
+
+               return $input['diff-cur'];
+       }
+
+       /**
+        * Gets the "hist" link; linking to the history of a certain element, 
in a
+        * format that can be wrapped in an array and passed to
+        * formatLinksAsPipeList.
+        *
+        * @param array[][] Associative array containing (url, message) tuples
+        * @param IContextSource $ctx
+        * @return array|Message
+        */
+       protected function getHistLink( array $input, IContextSource $ctx ) {
                if ( isset( $input['post-history'] ) ) {
-                       $links[] = $input['post-history'];
+                       return $input['post-history'];
                } elseif ( isset( $input['topic-history'] ) ) {
-                       $links[] = $input['topic-history'];
+                       return $input['topic-history'];
                } elseif ( isset( $input['board-history'] ) ) {
-                       $links[] = $input['board-history'];
+                       return $input['board-history'];
                } else {
                        // plain text with no link
-                       $links[] = $ctx->msg( 'hist' );
+                       return $ctx->msg( 'hist' );
                }
-
-               return $this->formatLinksAsPipeList( $links, $ctx );
        }
 
        /**
diff --git a/includes/Formatter/Contributions.php 
b/includes/Formatter/Contributions.php
index da6292b..9cd54cf 100644
--- a/includes/Formatter/Contributions.php
+++ b/includes/Formatter/Contributions.php
@@ -45,10 +45,14 @@
 
                $separator = $this->changeSeparator();
 
+               $links = array();
+               $links[] = $this->getDiffLink( $data['links'], $ctx );
+               $links[] = $this->getHistLink( $data['links'], $ctx );
+
                // Put it all together
                return
                        $this->formatTimestamp( $data ) . ' ' .
-                       $this->formatDiffHistPipeList( $data['links'], $ctx ) .
+                       $this->formatLinksAsPipeList( $links, $ctx ) .
                        $separator .
                        $charDiff .
                        $separator .
diff --git a/includes/Formatter/RecentChanges.php 
b/includes/Formatter/RecentChanges.php
index 09b51bf..4f43678 100644
--- a/includes/Formatter/RecentChanges.php
+++ b/includes/Formatter/RecentChanges.php
@@ -33,7 +33,11 @@
                // The ' . . ' text between elements
                $separator = $this->changeSeparator();
 
-               return $this->formatDiffHistPipeList( $data['links'], $ctx ) .
+               $links = array();
+               $links[] = $this->getDiffLink( $data['links'], $ctx );
+               $links[] = $this->getHistLink( $data['links'], $ctx );
+
+               return $this->formatLinksAsPipeList( $links, $ctx ) .
                        ' ' .
                        Linker::link( $row->workflow->getArticleTitle() ) .
                        $ctx->msg( 'semicolon-separator' )->escaped() .
diff --git a/includes/Formatter/RevisionFormatter.php 
b/includes/Formatter/RevisionFormatter.php
index 2dc1152..838618b 100644
--- a/includes/Formatter/RevisionFormatter.php
+++ b/includes/Formatter/RevisionFormatter.php
@@ -2,6 +2,8 @@
 
 namespace Flow\Formatter;
 
+use Flow\Collection\HeaderCollection;
+use Flow\Collection\PostCollection;
 use Flow\Container;
 use Flow\Data\ObjectManager;
 use Flow\Exception\FlowException;
@@ -204,35 +206,134 @@
                                        wfDebugLog( 'Flow', __METHOD__ . ': No 
revId available to render diff link' );
                                        break;
                                }
-                               $links['diff'] = array(
-                                       $this->urlGenerator->buildUrl(
-                                               $title,
-                                               'compare-header-revisions',
-                                               array(
-                                                       'workflow' => 
$workflowId->getAlphadecimal(),
-                                                       'header_newRevision' => 
$revId->getAlphadecimal(),
-                                               )
-                                       ),
-                                       wfMessage( 'diff' )
-                               );
+
+                               $collection = HeaderCollection::newFromId( 
$workflowId );
+
+                               /*
+                                * To diff against previous revision, we don't 
really need that
+                                * revision id; if no particular diff id is 
specified, it will
+                                * assume a diff against previous revision. 
However, we do want
+                                * to make sure that a previous revision 
actually exists to diff
+                                * against. This could result in a network 
request (fetching the
+                                * current revision), but it's likely being 
loaded anyways.
+                                */
+                               $revision = $collection->getRevision( $revId );
+                               if ( $revision->getPrevRevisionId() !== null ) {
+                                       $links['diff'] = array(
+                                               $this->urlGenerator->buildUrl(
+                                                       $title,
+                                                       
'compare-header-revisions',
+                                                       array(
+                                                               'workflow' => 
$workflowId->getAlphadecimal(),
+                                                               
'header_newRevision' => $revId->getAlphadecimal(),
+                                                               
'header_oldRevision' => $revision->getPrevRevisionId()->getAlphadecimal(),
+                                                       )
+                                               ),
+                                               wfMessage( 'diff' )
+                                       );
+
+                                       /*
+                                        * Different formatters have different 
terminology for the link
+                                        * that diffs a certain revision to the 
previous revision.
+                                        *
+                                        * E.g.: Special:Contributions has 
"diff" ($links['diff']),
+                                        * ?action=history has "prev" 
($links['prev']).
+                                        */
+                                       $links['diff-prev'] = array( 
$links['diff'][0], wfMessage( 'last' ) );
+                               }
+
+                               /*
+                                * To diff against the current revision, we 
need to know the id
+                                * of this last revision. This could be an 
additional network
+                                * request, though anything using formatter 
likely already needs
+                                * to request the most current revision (e.g. 
to check
+                                * permissions) so we should be able to get it 
from local cache.
+                                */
+                               $cur = $collection->getLastRevision();
+                               if ( !$revId->equals( $cur->getRevisionId() ) ) 
{
+                                       $links['diff-cur'] = array(
+                                               $this->urlGenerator->buildUrl(
+                                                       $title,
+                                                       
'compare-post-revisions',
+                                                       array(
+                                                               'workflow' => 
$workflowId->getAlphadecimal(),
+                                                               
'topic_newRevision' => $cur->getRevisionId()->getAlphadecimal(),
+                                                               
'topic_oldRevision' => $revId->getAlphadecimal(),
+                                                       )
+                                               ),
+                                               wfMessage( 'cur' )
+                                       );
+                               }
                                break;
 
                        case 'diff-post':
-                               if ( !$revId ) {
-                                       wfDebugLog( 'Flow', __METHOD__ . ': No 
revId available to render diff link' );
+                               if ( !$postId ) {
+                                       wfDebugLog( 'Flow', __METHOD__ . ': No 
postId available to render diff links' );
                                        break;
                                }
-                               $links['diff'] = array(
-                                       $this->urlGenerator->buildUrl(
-                                               $title,
-                                               'compare-post-revisions',
-                                               array(
-                                                       'workflow' => 
$workflowId->getAlphadecimal(),
-                                                       'topic_newRevision' => 
$revId->getAlphadecimal(),
-                                               )
-                                       ),
-                                       wfMessage( 'diff' )
-                               );
+
+                               if ( !$revId ) {
+                                       wfDebugLog( 'Flow', __METHOD__ . ': No 
revId available to render diff links' );
+                                       break;
+                               }
+
+                               $collection = PostCollection::newFromId( 
$postId );
+
+                               /*
+                                * To diff against previous revision, we don't 
really need that
+                                * revision id; if no particular diff id is 
specified, it will
+                                * assume a diff against previous revision. 
However, we do want
+                                * to make sure that a previous revision 
actually exists to diff
+                                * against. This could result in a network 
request (fetching the
+                                * current revision), but it's likely being 
loaded anyways.
+                                */
+                               $revision = $collection->getRevision( $revId );
+                               if ( $revision->getPrevRevisionId() !== null ) {
+                                       $links['diff'] = array(
+                                               $this->urlGenerator->buildUrl(
+                                                       $title,
+                                                       
'compare-post-revisions',
+                                                       array(
+                                                               'workflow' => 
$workflowId->getAlphadecimal(),
+                                                               
'topic_newRevision' => $revId->getAlphadecimal(),
+                                                               
'topic_oldRevision' => $revision->getPrevRevisionId()->getAlphadecimal(),
+                                                       )
+                                               ),
+                                               wfMessage( 'diff' )
+                                       );
+
+                                       /*
+                                        * Different formatters have different 
terminology for the link
+                                        * that diffs a certain revision to the 
previous revision.
+                                        *
+                                        * E.g.: Special:Contributions has 
"diff" ($links['diff']),
+                                        * ?action=history has "prev" 
($links['prev']).
+                                        */
+                                       $links['diff-prev'] = array( 
$links['diff'][0], wfMessage( 'last' ) );
+                               }
+
+                               /*
+                                * To diff against the current revision, we 
need to know the id
+                                * of this last revision. This could be an 
additional network
+                                * request, though anything using formatter 
likely already needs
+                                * to request the most current revision (e.g. 
to check
+                                * permissions) so we should be able to get it 
from local cache.
+                                */
+                               $cur = $collection->getLastRevision();
+                               if ( !$revId->equals( $cur->getRevisionId() ) ) 
{
+                                       $links['diff-cur'] = array(
+                                               $this->urlGenerator->buildUrl(
+                                                       $title,
+                                                       
'compare-post-revisions',
+                                                       array(
+                                                               'workflow' => 
$workflowId->getAlphadecimal(),
+                                                               
'topic_newRevision' => $cur->getRevisionId()->getAlphadecimal(),
+                                                               
'topic_oldRevision' => $revId->getAlphadecimal(),
+                                                       )
+                                               ),
+                                               wfMessage( 'cur' )
+                                       );
+                               }
                                break;
 
                        case 'workflow':

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I40fef27d21f385905dfbf0b81fcc472aa9c8927a
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Flow
Gerrit-Branch: master
Gerrit-Owner: Matthias Mullie <mmul...@wikimedia.org>

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

Reply via email to