Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 85
0.00% covered (danger)
0.00%
0 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
RevisionViewFormatter
0.00% covered (danger)
0.00%
0 / 85
0.00% covered (danger)
0.00%
0 / 4
210
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 setContentFormat
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 formatApi
0.00% covered (danger)
0.00%
0 / 26
0.00% covered (danger)
0.00%
0 / 1
12
 buildLinks
0.00% covered (danger)
0.00%
0 / 56
0.00% covered (danger)
0.00%
0 / 1
90
1<?php
2
3namespace Flow\Formatter;
4
5use Flow\Model\Header;
6use Flow\Model\PostRevision;
7use Flow\Model\PostSummary;
8use Flow\Model\UUID;
9use Flow\UrlGenerator;
10use MediaWiki\Context\IContextSource;
11use MediaWiki\RecentChanges\ChangesList;
12use MediaWiki\Utils\MWTimestamp;
13
14class RevisionViewFormatter {
15    /**
16     * @var UrlGenerator
17     */
18    protected $urlGenerator;
19
20    /**
21     * @var RevisionFormatter
22     */
23    protected $serializer;
24
25    public function __construct( UrlGenerator $urlGenerator, RevisionFormatter $serializer ) {
26        $this->urlGenerator = $urlGenerator;
27        $this->serializer = $serializer;
28    }
29
30    public function setContentFormat( $format, ?UUID $revisionId = null ) {
31        $this->serializer->setContentFormat( $format, $revisionId );
32    }
33
34    /**
35     * @param FormatterRow $row
36     * @param IContextSource $ctx
37     * @return array
38     */
39    public function formatApi( FormatterRow $row, IContextSource $ctx ) {
40        $res = $this->serializer->formatApi( $row, $ctx );
41        $res['rev_view_links'] = $this->buildLinks( $row, $ctx );
42        $res['human_timestamp'] = $ctx->getLanguage()->getHumanTimestamp(
43            new MWTimestamp( $res['timestamp'] )
44        );
45        if ( $row->revision instanceof PostRevision ) {
46            $res['properties']['topic-of-post'] = $this->serializer->processParam(
47                'topic-of-post',
48                $row->revision,
49                $row->workflow->getId(),
50                $ctx
51            );
52            $res['properties']['topic-of-post-text-from-html'] = $this->serializer->processParam(
53                'topic-of-post-text-from-html',
54                $row->revision,
55                $row->workflow->getId(),
56                $ctx
57            );
58        }
59        if ( $row->revision instanceof PostSummary ) {
60            $res['properties']['post-of-summary'] = $this->serializer->processParam(
61                'post-of-summary',
62                $row->revision,
63                $row->workflow->getId(),
64                $ctx
65            );
66        }
67        return $res;
68    }
69
70    /**
71     * Generate the links for single and diff view actions
72     *
73     * @param FormatterRow $row
74     * @param IContextSource $ctx
75     * @return array
76     */
77    public function buildLinks( FormatterRow $row, IContextSource $ctx ) {
78        $workflowId = $row->workflow->getId();
79
80        $boardTitle = $row->workflow->getOwnerTitle();
81        $title = $row->workflow->getArticleTitle();
82        $links = [
83            'hist' => $this->urlGenerator->boardHistoryLink( $title ),
84            'board' => $this->urlGenerator->boardLink( $boardTitle ),
85        ];
86
87        if ( $row->revision instanceof PostRevision || $row->revision instanceof PostSummary ) {
88            $links['root'] = $this->urlGenerator->topicLink( $row->workflow->getArticleTitle(), $workflowId );
89            $links['root']->setMessage( $title->getPrefixedText() );
90        }
91
92        if ( $row->revision instanceof PostRevision ) {
93            $links['single-view'] = $this->urlGenerator->postRevisionLink(
94                $title,
95                $workflowId,
96                $row->revision->getPostId(),
97                $row->revision->getRevisionId()
98            );
99            $links['single-view']->setMessage( $title->getPrefixedText() );
100        } elseif ( $row->revision instanceof Header ) {
101            $links['single-view'] = $this->urlGenerator->headerRevisionLink(
102                $title,
103                $workflowId,
104                $row->revision->getRevisionId()
105            );
106            $links['single-view']->setMessage( $title->getPrefixedText() );
107        } elseif ( $row->revision instanceof PostSummary ) {
108            $links['single-view'] = $this->urlGenerator->summaryRevisionLink(
109                $title,
110                $workflowId,
111                $row->revision->getRevisionId()
112            );
113            $links['single-view']->setMessage( $title->getPrefixedText() );
114        } else {
115            wfDebugLog( 'Flow', __METHOD__ . ': Received unknown revision type ' . get_class( $row->revision ) );
116        }
117
118        if ( $row->revision->getPrevRevisionId() !== null ) {
119            $links['diff'] = $this->urlGenerator->diffLink(
120                $row->revision,
121                null,
122                $workflowId
123            );
124            $links['diff']->setMessage( $ctx->msg( 'diff' ) );
125        } else {
126            $links['diff'] = [
127                'url' => '',
128                'title' => ''
129            ];
130        }
131
132        $recentChange = $row->revision->getRecentChange();
133        if ( $recentChange !== null ) {
134            $user = $ctx->getUser();
135            if ( ChangesList::isUnpatrolled( $recentChange, $user ) ) {
136                $token = $user->getEditToken( $recentChange->getAttribute( 'rc_id' ) );
137                $links['markPatrolled'] = $this->urlGenerator->markRevisionPatrolledAction(
138                    $title,
139                    $workflowId,
140                    $recentChange,
141                    $token
142                );
143            }
144        }
145
146        return $links;
147    }
148
149}