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