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                // @phan-suppress-next-line PhanUndeclaredMethod Type not correctly inferred
97                $row->revision->getPostId(),
98                $row->revision->getRevisionId()
99            );
100            $links['single-view']->setMessage( $title->getPrefixedText() );
101        } elseif ( $row->revision instanceof Header ) {
102            $links['single-view'] = $this->urlGenerator->headerRevisionLink(
103                $title,
104                $workflowId,
105                $row->revision->getRevisionId()
106            );
107            $links['single-view']->setMessage( $title->getPrefixedText() );
108        } elseif ( $row->revision instanceof PostSummary ) {
109            $links['single-view'] = $this->urlGenerator->summaryRevisionLink(
110                $title,
111                $workflowId,
112                $row->revision->getRevisionId()
113            );
114            $links['single-view']->setMessage( $title->getPrefixedText() );
115        } else {
116            wfDebugLog( 'Flow', __METHOD__ . ': Received unknown revision type ' . get_class( $row->revision ) );
117        }
118
119        if ( $row->revision->getPrevRevisionId() !== null ) {
120            $links['diff'] = $this->urlGenerator->diffLink(
121                $row->revision,
122                null,
123                $workflowId
124            );
125            $links['diff']->setMessage( $ctx->msg( 'diff' ) );
126        } else {
127            $links['diff'] = [
128                'url' => '',
129                'title' => ''
130            ];
131        }
132
133        $recentChange = $row->revision->getRecentChange();
134        if ( $recentChange !== null ) {
135            $user = $ctx->getUser();
136            if ( ChangesList::isUnpatrolled( $recentChange, $user ) ) {
137                $token = $user->getEditToken( $recentChange->getAttribute( 'rc_id' ) );
138                $links['markPatrolled'] = $this->urlGenerator->markRevisionPatrolledAction(
139                    $title,
140                    $workflowId,
141                    $recentChange,
142                    $token
143                );
144            }
145        }
146
147        return $links;
148    }
149
150}