Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 61 |
|
0.00% |
0 / 7 |
CRAP | |
0.00% |
0 / 1 |
IRCLineUrlFormatter | |
0.00% |
0 / 61 |
|
0.00% |
0 / 7 |
156 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
getHistoryType | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
associate | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getLine | |
0.00% |
0 / 18 |
|
0.00% |
0 / 1 |
6 | |||
serializeRcRevision | |
0.00% |
0 / 14 |
|
0.00% |
0 / 1 |
6 | |||
formatDescription | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
format | |
0.00% |
0 / 23 |
|
0.00% |
0 / 1 |
20 |
1 | <?php |
2 | |
3 | namespace Flow\Formatter; |
4 | |
5 | use Flow\Container; |
6 | use Flow\RevisionActionPermissions; |
7 | use FormatJson; |
8 | use IContextSource; |
9 | use MediaWiki\Logger\LoggerFactory; |
10 | use RCFeedFormatter; |
11 | use RecentChange; |
12 | use SplObjectStorage; |
13 | |
14 | /** |
15 | * Generates URL's to be inserted into the IRC |
16 | * recent changes feed. |
17 | */ |
18 | class IRCLineUrlFormatter extends AbstractFormatter implements RCFeedFormatter { |
19 | /** |
20 | * @var SplObjectStorage |
21 | */ |
22 | protected $data; |
23 | |
24 | public function __construct( RevisionActionPermissions $permissions, RevisionFormatter $serializer ) { |
25 | parent::__construct( $permissions, $serializer ); |
26 | $this->data = new SplObjectStorage; |
27 | } |
28 | |
29 | protected function getHistoryType() { |
30 | return 'irc'; |
31 | } |
32 | |
33 | public function associate( RecentChange $rc, array $metadata ) { |
34 | $this->data[$rc] = $metadata; |
35 | } |
36 | |
37 | /** |
38 | * Allows us to set the rc_comment field |
39 | */ |
40 | |
41 | /** |
42 | * @param array $feed |
43 | * @param RecentChange $rc |
44 | * @param null|string $actionComment |
45 | * @return string|null Text for IRC line, or null on failure |
46 | */ |
47 | public function getLine( array $feed, RecentChange $rc, $actionComment ) { |
48 | $ctx = \RequestContext::getMain(); |
49 | |
50 | $serialized = $this->serializeRcRevision( $rc, $ctx ); |
51 | if ( !$serialized ) { |
52 | LoggerFactory::getInstance( 'Flow' )->debug( |
53 | __METHOD__ . ': Failed to obtain serialized RC revision.', |
54 | [ |
55 | 'rc_attributes' => FormatJson::encode( $rc->getAttributes(), true ), |
56 | 'user_id' => $ctx->getUser()->getId(), |
57 | ] |
58 | ); |
59 | return null; |
60 | } |
61 | |
62 | $rcAttribs = $rc->getAttributes(); |
63 | $rcAttribs['rc_comment'] = $this->formatDescription( $serialized, $ctx ); |
64 | $rcAttribs['rc_comment_text'] = $rcAttribs['rc_comment']; |
65 | $rcAttribs['rc_comment_data'] = null; |
66 | $rc->setAttribs( $rcAttribs ); |
67 | |
68 | /** @var RCFeedFormatter $formatter */ |
69 | $formatter = new $feed['original_formatter'](); |
70 | return $formatter->getLine( $feed, $rc, $actionComment ); |
71 | } |
72 | |
73 | /** |
74 | * Gets the formatted RC revision, or returns null if this revision is not to be |
75 | * shown in RC, or on failure. |
76 | * |
77 | * @param RecentChange $rc |
78 | * @param IContextSource $ctx |
79 | * @return array|false Array of data, or false on failure |
80 | * |
81 | * @fixme this looks slow, likely a better way |
82 | */ |
83 | protected function serializeRcRevision( RecentChange $rc, IContextSource $ctx ) { |
84 | /** @var RecentChangesQuery $query */ |
85 | $query = Container::get( 'query.changeslist' ); |
86 | $query->loadMetadataBatch( [ (object)$rc->getAttributes() ] ); |
87 | $rcRow = $query->getResult( null, $rc ); |
88 | if ( !$rcRow ) { |
89 | LoggerFactory::getInstance( 'Flow' )->debug( |
90 | __METHOD__ . ': Failed to load result.', |
91 | [ |
92 | 'rc_attributes' => FormatJson::encode( $rc->getAttributes(), true ), |
93 | 'user_id' => $ctx->getUser()->getId() |
94 | ] |
95 | ); |
96 | return false; |
97 | } |
98 | |
99 | $this->serializer->setIncludeHistoryProperties( true ); |
100 | return $this->serializer->formatApi( $rcRow, $ctx, 'recentchanges' ); |
101 | } |
102 | |
103 | /** |
104 | * Generate a plaintext revision description suitable for IRC consumption |
105 | * |
106 | * @param array $data |
107 | * @param \IContextSource $ctx not used |
108 | * @return string |
109 | */ |
110 | protected function formatDescription( array $data, \IContextSource $ctx ) { |
111 | $msg = $this->getDescription( $data, $ctx ); |
112 | return $msg->inLanguage( 'en' )->text(); |
113 | } |
114 | |
115 | /** |
116 | * @param RecentChange $rc |
117 | * @return string|null |
118 | */ |
119 | public function format( RecentChange $rc ) { |
120 | // commit metadata provided via self::associate |
121 | if ( !isset( $this->data[$rc] ) ) { |
122 | wfDebugLog( 'Flow', __METHOD__ . ': Nothing pre-loaded about rc ' . $rc->getAttribute( 'rc_id' ) ); |
123 | return null; |
124 | } |
125 | $metadata = $this->data[$rc]; |
126 | |
127 | $row = new FormatterRow; |
128 | $row->revision = $metadata['revision']; |
129 | $row->currentRevision = $row->revision; |
130 | $row->workflow = $metadata['workflow']; |
131 | $links = $this->serializer->buildLinks( $row ); |
132 | |
133 | // Listed in order of preference |
134 | $accept = [ |
135 | 'diff', |
136 | 'post-history', 'topic-history', 'board-history', |
137 | 'post', 'topic', |
138 | 'workflow' |
139 | ]; |
140 | |
141 | foreach ( $accept as $key ) { |
142 | if ( isset( $links[$key] ) ) { |
143 | return $links[$key]->getCanonicalURL(); |
144 | } |
145 | } |
146 | |
147 | wfDebugLog( 'Flow', __METHOD__ |
148 | . ': No url generated for action ' . $row->workflow->getType() |
149 | . ' on revision ' . $row->revision->getRevisionId() |
150 | ); |
151 | return null; |
152 | } |
153 | } |