Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 60 |
|
0.00% |
0 / 10 |
CRAP | |
0.00% |
0 / 1 |
| PoliciesGuidelines | |
0.00% |
0 / 60 |
|
0.00% |
0 / 10 |
156 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| serverRendered | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| acceptsFocusedSubPath | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getHeaderText | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getSubheaderText | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getModules | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getModuleStyles | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getBody | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
6 | |||
| getAccordion | |
0.00% |
0 / 18 |
|
0.00% |
0 / 1 |
2 | |||
| getStepsHtml | |
0.00% |
0 / 26 |
|
0.00% |
0 / 1 |
6 | |||
| 1 | <?php |
| 2 | |
| 3 | declare( strict_types = 1 ); |
| 4 | |
| 5 | namespace MediaWiki\Extension\ModeratorToolkit\Modules; |
| 6 | |
| 7 | use MediaWiki\Context\IContextSource; |
| 8 | use MediaWiki\Extension\PersonalDashboard\Modules\BaseModule; |
| 9 | use MediaWiki\Html\Html; |
| 10 | use Wikimedia\Codex\Component\Accordion; |
| 11 | use Wikimedia\Codex\Component\HtmlSnippet; |
| 12 | use Wikimedia\Codex\Localization\MediaWikiLocalization; |
| 13 | use Wikimedia\Codex\Utility\Codex; |
| 14 | |
| 15 | /** |
| 16 | * Core content policies, each a Codex card that is also a <details> accordion: |
| 17 | * the title is the summary, and the definition plus a worked-examples walkthrough |
| 18 | * disclose inside it. |
| 19 | * |
| 20 | * Rendered server-side and CSS-only, so a no-JS visitor gets the whole thing: the |
| 21 | * accordions open and close with no script. A per-policy subpath opens the |
| 22 | * matching accordion on load, so a deep link composes the full dashboard with that |
| 23 | * policy already open; the client router owns anything deeper (which example is |
| 24 | * showing). |
| 25 | */ |
| 26 | class PoliciesGuidelines extends BaseModule { |
| 27 | |
| 28 | /** |
| 29 | * Policies in display order, each mapped to the status icon per example step. |
| 30 | * The single source the card body and its accordion steps read from. |
| 31 | */ |
| 32 | private const array POLICIES = [ |
| 33 | 'neutral-point-of-view' => [ 'error', 'success' ], |
| 34 | 'no-original-research' => [ 'error', 'error' ], |
| 35 | 'verifiability' => [ 'warning', 'error', 'success' ], |
| 36 | 'assume-good-faith' => [ 'error', 'success' ], |
| 37 | ]; |
| 38 | |
| 39 | private Codex $codex; |
| 40 | |
| 41 | public function __construct( IContextSource $context ) { |
| 42 | parent::__construct( $context ); |
| 43 | $this->codex = new Codex( new MediaWikiLocalization( $context ) ); |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * The body is server HTML, CSS-only: the accordions need no behavior module. |
| 48 | * @return bool |
| 49 | */ |
| 50 | protected function serverRendered(): bool { |
| 51 | return true; |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * A per-policy subpath opens the matching accordion server-side, so this |
| 56 | * module reads one. |
| 57 | * @return bool |
| 58 | */ |
| 59 | public function acceptsFocusedSubPath(): bool { |
| 60 | return true; |
| 61 | } |
| 62 | |
| 63 | /** @inheritDoc */ |
| 64 | protected function getHeaderText(): string { |
| 65 | return $this->msg( 'moderator-toolkit-policies-guidelines-title' )->text(); |
| 66 | } |
| 67 | |
| 68 | /** @inheritDoc */ |
| 69 | protected function getSubheaderText(): string { |
| 70 | return $this->msg( 'moderator-toolkit-policies-guidelines-body' )->text(); |
| 71 | } |
| 72 | |
| 73 | /** @inheritDoc */ |
| 74 | protected function getModules(): array { |
| 75 | // The accordions are CSS-only, so the no-JS baseline needs no behavior |
| 76 | // module; the one-example-at-a-time enhancement is a follow-up. |
| 77 | return []; |
| 78 | } |
| 79 | |
| 80 | /** @inheritDoc */ |
| 81 | protected function getModuleStyles(): array { |
| 82 | return [ 'ext.moderatorToolkit.policiesGuidelines.styles' ]; |
| 83 | } |
| 84 | |
| 85 | /** @inheritDoc */ |
| 86 | protected function getBody(): string { |
| 87 | $html = ''; |
| 88 | foreach ( self::POLICIES as $name => $steps ) { |
| 89 | $html .= $this->getAccordion( $name, $steps )->getHtml(); |
| 90 | } |
| 91 | return Html::rawElement( |
| 92 | 'div', |
| 93 | [ 'class' => 'mw-moderator-toolkit-policies-guidelines__list' ], |
| 94 | $html |
| 95 | ); |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * One policy as a Codex CSS-only accordion: the title and definition are the |
| 100 | * summary header, and the worked-example steps disclose inside. Collapsed by |
| 101 | * default; the policy named by the focused subpath opens on load, the |
| 102 | * server-side deep link a no-JS visitor lands on. |
| 103 | * |
| 104 | * @param string $name Policy key |
| 105 | * @param string[] $steps Status icon per example step |
| 106 | * @return Accordion |
| 107 | */ |
| 108 | private function getAccordion( string $name, array $steps ): Accordion { |
| 109 | // Only the policy the subpath names opens; everything else stays collapsed, |
| 110 | // on the focused page too. The point is not to overwhelm a new user, so we |
| 111 | // leave the walkthroughs closed until they choose one. The first subpath |
| 112 | // segment is the policy; anything below it (an example) is the client |
| 113 | // router's, so match on that segment alone. |
| 114 | $open = explode( '/', $this->getFocusedSubPath(), 2 )[0] === $name; |
| 115 | |
| 116 | return $this->codex->accordion() |
| 117 | ->setId( $name ) |
| 118 | ->setOpen( $open ) |
| 119 | // Messages used here include: |
| 120 | // * moderator-toolkit-policies-guidelines-neutral-point-of-view-title |
| 121 | // * moderator-toolkit-policies-guidelines-no-original-research-title |
| 122 | // * moderator-toolkit-policies-guidelines-verifiability-title |
| 123 | // * moderator-toolkit-policies-guidelines-assume-good-faith-title |
| 124 | ->setTitle( $this->msg( "moderator-toolkit-policies-guidelines-$name-title" )->text() ) |
| 125 | // Messages used here include: |
| 126 | // * moderator-toolkit-policies-guidelines-neutral-point-of-view-definition |
| 127 | // * moderator-toolkit-policies-guidelines-no-original-research-definition |
| 128 | // * moderator-toolkit-policies-guidelines-verifiability-definition |
| 129 | // * moderator-toolkit-policies-guidelines-assume-good-faith-definition |
| 130 | ->setDescription( $this->msg( "moderator-toolkit-policies-guidelines-$name-definition" )->text() ) |
| 131 | ->setContentHtml( $this->getStepsHtml( $name, $steps ) ) |
| 132 | ->build(); |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * The examples walkthrough stacked inside a policy's accordion: each step's |
| 137 | * prompt, the example text, and a status icon. |
| 138 | * |
| 139 | * @param string $name Policy key |
| 140 | * @param string[] $steps Status icon per example step |
| 141 | * @return HtmlSnippet |
| 142 | */ |
| 143 | private function getStepsHtml( string $name, array $steps ): HtmlSnippet { |
| 144 | $html = ''; |
| 145 | foreach ( $steps as $index => $icon ) { |
| 146 | $step = $index + 1; |
| 147 | $html .= Html::rawElement( 'div', [ 'class' => 'mw-moderator-toolkit-policies-guidelines__step' ], |
| 148 | Html::element( 'h4', [], |
| 149 | $this->msg( 'moderator-toolkit-policies-guidelines-examples-header' )->params( $step )->text() |
| 150 | ) . |
| 151 | Html::element( 'p', [], |
| 152 | // Messages used here include: |
| 153 | // * moderator-toolkit-policies-guidelines-neutral-point-of-view-example-1 |
| 154 | // * moderator-toolkit-policies-guidelines-neutral-point-of-view-example-2 |
| 155 | // * moderator-toolkit-policies-guidelines-no-original-research-example-1 |
| 156 | // * moderator-toolkit-policies-guidelines-no-original-research-example-2 |
| 157 | // * moderator-toolkit-policies-guidelines-verifiability-example-1 |
| 158 | // * moderator-toolkit-policies-guidelines-verifiability-example-2 |
| 159 | // * moderator-toolkit-policies-guidelines-verifiability-example-3 |
| 160 | // * moderator-toolkit-policies-guidelines-assume-good-faith-example-1 |
| 161 | // * moderator-toolkit-policies-guidelines-assume-good-faith-example-2 |
| 162 | $this->msg( "moderator-toolkit-policies-guidelines-$name-example-$step" )->text() |
| 163 | ) . |
| 164 | Html::rawElement( 'div', [ 'class' => 'mw-moderator-toolkit-policies-guidelines__answer' ], |
| 165 | Html::element( 'span', [ 'class' => [ |
| 166 | 'mw-moderator-toolkit-policies-guidelines__step-icon', |
| 167 | // The following CSS classes are used here: |
| 168 | // * mw-moderator-toolkit-policies-guidelines__step-icon--success |
| 169 | // * mw-moderator-toolkit-policies-guidelines__step-icon--warning |
| 170 | // * mw-moderator-toolkit-policies-guidelines__step-icon--error |
| 171 | 'mw-moderator-toolkit-policies-guidelines__step-icon--' . $icon, |
| 172 | ] ] ) . |
| 173 | Html::rawElement( 'div', [ 'class' => 'mw-moderator-toolkit-policies-guidelines__answer__text' ], |
| 174 | Html::element( 'strong', [], |
| 175 | // Messages used here include: |
| 176 | // * moderator-toolkit-policies-guidelines-neutral-point-of-view-answer-1-label |
| 177 | // * moderator-toolkit-policies-guidelines-neutral-point-of-view-answer-2-label |
| 178 | // * moderator-toolkit-policies-guidelines-no-original-research-answer-1-label |
| 179 | // * moderator-toolkit-policies-guidelines-no-original-research-answer-2-label |
| 180 | // * moderator-toolkit-policies-guidelines-verifiability-answer-1-label |
| 181 | // * moderator-toolkit-policies-guidelines-verifiability-answer-2-label |
| 182 | // * moderator-toolkit-policies-guidelines-verifiability-answer-3-label |
| 183 | // * moderator-toolkit-policies-guidelines-assume-good-faith-answer-1-label |
| 184 | // * moderator-toolkit-policies-guidelines-assume-good-faith-answer-2-label |
| 185 | $this->msg( "moderator-toolkit-policies-guidelines-$name-answer-$step-label" )->text() |
| 186 | ) . ' ' . |
| 187 | // Messages used here include: |
| 188 | // * moderator-toolkit-policies-guidelines-neutral-point-of-view-answer-1-text |
| 189 | // * moderator-toolkit-policies-guidelines-neutral-point-of-view-answer-2-text |
| 190 | // * moderator-toolkit-policies-guidelines-no-original-research-answer-1-text |
| 191 | // * moderator-toolkit-policies-guidelines-no-original-research-answer-2-text |
| 192 | // * moderator-toolkit-policies-guidelines-verifiability-answer-1-text |
| 193 | // * moderator-toolkit-policies-guidelines-verifiability-answer-2-text |
| 194 | // * moderator-toolkit-policies-guidelines-verifiability-answer-3-text |
| 195 | // * moderator-toolkit-policies-guidelines-assume-good-faith-answer-1-text |
| 196 | // * moderator-toolkit-policies-guidelines-assume-good-faith-answer-2-text |
| 197 | $this->msg( "moderator-toolkit-policies-guidelines-$name-answer-$step-text" )->escaped() |
| 198 | ) |
| 199 | ) |
| 200 | ); |
| 201 | } |
| 202 | return $this->codex->htmlSnippet() |
| 203 | ->setContent( $html ) |
| 204 | ->build(); |
| 205 | } |
| 206 | } |