Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
50.00% |
28 / 56 |
|
50.00% |
1 / 2 |
CRAP | |
0.00% |
0 / 1 |
AutocommentFormatter | |
50.00% |
28 / 56 |
|
50.00% |
1 / 2 |
48.00 | |
0.00% |
0 / 1 |
formatAutocomment | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
4 | |||
parseAutocomment | |
40.43% |
19 / 47 |
|
0.00% |
0 / 1 |
42.45 |
1 | <?php |
2 | |
3 | declare( strict_types = 1 ); |
4 | |
5 | namespace EntitySchema\Presentation; |
6 | |
7 | use EntitySchema\DataAccess\MediaWikiRevisionEntitySchemaInserter; |
8 | use EntitySchema\DataAccess\MediaWikiRevisionEntitySchemaUpdater; |
9 | use MediaWiki\MediaWikiServices; |
10 | use MediaWiki\User\User; |
11 | use RequestContext; |
12 | |
13 | /** |
14 | * @license GPL-2.0-or-later |
15 | */ |
16 | class AutocommentFormatter { |
17 | |
18 | /** |
19 | * @param bool $pre Whether any text appears in the summary before this autocomment. |
20 | * If true, we insert the autocomment-prefix before the autocomment |
21 | * (outside the two <span>s) to separate it from that. |
22 | * @param string $auto The autocomment content (without the surrounding comment marks) |
23 | * @param bool $post Whether any text appears in the summary after this autocomment. |
24 | * If true, we append the colon-separator after the autocomment |
25 | * (still inside the two <span>s) to separate it from that. |
26 | * |
27 | * @return string|null |
28 | */ |
29 | public function formatAutocomment( bool $pre, string $auto, bool $post ): ?string { |
30 | $comment = $this->parseAutocomment( $auto ); |
31 | if ( $comment === null ) { |
32 | return null; |
33 | } |
34 | |
35 | if ( $post ) { |
36 | $comment .= wfMessage( 'colon-separator' )->escaped(); |
37 | } |
38 | |
39 | $comment = '<span dir="auto"><span class="autocomment">' . $comment . '</span></span>'; |
40 | |
41 | if ( $pre ) { |
42 | $comment = wfMessage( 'autocomment-prefix' )->escaped() . $comment; |
43 | } |
44 | |
45 | return $comment; |
46 | } |
47 | |
48 | private function parseAutocomment( string $auto ): ?string { |
49 | $commentParts = explode( ':', $auto, 2 ); |
50 | $context = RequestContext::getMain(); |
51 | |
52 | switch ( $commentParts[0] ) { |
53 | case MediaWikiRevisionEntitySchemaInserter::AUTOCOMMENT_NEWSCHEMA: |
54 | $comment = wfMessage( 'entityschema-summary-newschema-nolabel' ); |
55 | break; |
56 | case MediaWikiRevisionEntitySchemaUpdater::AUTOCOMMENT_UPDATED_SCHEMATEXT: |
57 | $comment = wfMessage( 'entityschema-summary-update-schema-text' ); |
58 | break; |
59 | case MediaWikiRevisionEntitySchemaUpdater::AUTOCOMMENT_UPDATED_NAMEBADGE: |
60 | $languageName = MediaWikiServices::getInstance()->getLanguageNameUtils()->getLanguageName( |
61 | $commentParts[1], |
62 | $context->getLanguage()->getCode() |
63 | ); |
64 | $comment = wfMessage( 'entityschema-summary-update-schema-namebadge' ) |
65 | ->params( $languageName ); |
66 | break; |
67 | case MediaWikiRevisionEntitySchemaUpdater::AUTOCOMMENT_UPDATED_LABEL: |
68 | $languageName = MediaWikiServices::getInstance()->getLanguageNameUtils()->getLanguageName( |
69 | $commentParts[1], |
70 | $context->getLanguage()->getCode() |
71 | ); |
72 | $comment = wfMessage( 'entityschema-summary-update-schema-label' ) |
73 | ->params( $languageName ); |
74 | break; |
75 | case MediaWikiRevisionEntitySchemaUpdater::AUTOCOMMENT_UPDATED_DESCRIPTION: |
76 | $languageName = MediaWikiServices::getInstance()->getLanguageNameUtils()->getLanguageName( |
77 | $commentParts[1], |
78 | $context->getLanguage()->getCode() |
79 | ); |
80 | $comment = wfMessage( 'entityschema-summary-update-schema-description' ) |
81 | ->params( $languageName ); |
82 | break; |
83 | case MediaWikiRevisionEntitySchemaUpdater::AUTOCOMMENT_UPDATED_ALIASES: |
84 | $languageName = MediaWikiServices::getInstance()->getLanguageNameUtils()->getLanguageName( |
85 | $commentParts[1], |
86 | $context->getLanguage()->getCode() |
87 | ); |
88 | $comment = wfMessage( 'entityschema-summary-update-schema-aliases' ) |
89 | ->params( $languageName ); |
90 | break; |
91 | case MediaWikiRevisionEntitySchemaUpdater::AUTOCOMMENT_RESTORE: |
92 | [ $revId, $username ] = explode( ':', $commentParts[1], 2 ); |
93 | $user = User::newFromName( $username ) ?: $username; |
94 | $comment = wfMessage( 'entityschema-summary-restore-autocomment' ) |
95 | ->params( $revId, $user ); |
96 | break; |
97 | case MediaWikiRevisionEntitySchemaUpdater::AUTOCOMMENT_UNDO: |
98 | [ $revId, $username ] = explode( ':', $commentParts[1], 2 ); |
99 | $user = User::newFromName( $username ) ?: $username; |
100 | $comment = wfMessage( 'entityschema-summary-undo-autocomment' ) |
101 | ->params( $revId, $user ); |
102 | break; |
103 | default: |
104 | return null; |
105 | } |
106 | |
107 | return $comment->escaped(); |
108 | } |
109 | |
110 | } |