Translate extension for MediaWiki
 
Loading...
Searching...
No Matches
ManageTranslatorSandboxSpecialPage.php
1<?php
2declare( strict_types = 1 );
3
4namespace MediaWiki\Extension\Translate\TranslatorSandbox;
5
6use MediaWiki\Config\ServiceOptions;
7use MediaWiki\Html\Html;
8use MediaWiki\Json\FormatJson;
9use MediaWiki\Parser\Sanitizer;
10use MediaWiki\SpecialPage\SpecialPage;
11use MediaWiki\User\Options\UserOptionsLookup;
12use MediaWiki\User\User;
13use MediaWiki\Utils\MWTimestamp;
14
23class ManageTranslatorSandboxSpecialPage extends SpecialPage {
25 private $stash;
27 private $userOptionsLookup;
28 private TranslateSandbox $translateSandbox;
29
30 public const CONSTRUCTOR_OPTIONS = [
31 'TranslateUseSandbox',
32 ];
33
34 public function __construct(
36 UserOptionsLookup $userOptionsLookup,
37 TranslateSandbox $translateSandbox,
38 ServiceOptions $options
39 ) {
40 $this->stash = $stash;
41 $this->userOptionsLookup = $userOptionsLookup;
42 $this->translateSandbox = $translateSandbox;
43
44 parent::__construct(
45 'ManageTranslatorSandbox',
46 'translate-sandboxmanage',
47 $options->get( 'TranslateUseSandbox' )
48 );
49 }
50
51 public function doesWrites() {
52 return true;
53 }
54
55 protected function getGroupName() {
56 return 'translation';
57 }
58
59 public function execute( $params ) {
60 $this->setHeaders();
61 $this->checkPermissions();
62 $out = $this->getOutput();
63 $out->addModuleStyles(
64 [
65 'ext.translate.special.managetranslatorsandbox.styles',
66 'mediawiki.ui.button',
67 'jquery.uls.grid',
68 'mediawiki.codex.messagebox.styles',
69 ]
70 );
71 $out->addModules( 'ext.translate.special.managetranslatorsandbox' );
72
73 $this->showPage();
74 }
75
77 private function showPage(): void {
78 $out = $this->getOutput();
79
80 $nojs = Html::errorBox(
81 $this->msg( 'tux-nojs' )->escaped(),
82 '',
83 'tux-nojs'
84 );
85 $out->addHTML( $nojs );
86
87 $out->addHTML(
88 <<<HTML
89 <div class="grid tsb-container">
90 <div class="row">
91 <div class="nine columns pane filter">{$this->makeFilter()}</div>
92 <div class="three columns pane search">{$this->makeSearchBox()}</div>
93 </div>
94 <div class="row tsb-body">
95 <div class="four columns pane requests">
96 {$this->makeList()}
97 <div class="request-footer">
98 <span class="selected-counter">
99 {$this->msg( 'tsb-selected-count' )->numParams( 0 )->escaped()}
100 </span>
101 \u{00A0}
102 <a href="#" class="older-requests-indicator"></a>
103 </div>
104 </div>
105 <div class="eight columns pane details"></div>
106 </div>
107 </div>
108 HTML
109 );
110 }
111
112 private function makeFilter(): string {
113 return $this->msg( 'tsb-filter-pending' )->escaped();
114 }
115
116 private function makeSearchBox(): string {
117 return <<<HTML
118 <input class="request-filter-box right"
119 placeholder="{$this->msg( 'tsb-search-requests' )->escaped()}" type="search" />
120 HTML;
121 }
122
123 private function makeList(): string {
124 $items = [];
125 $requests = [];
126 $users = $this->translateSandbox->getUsers();
127
129 foreach ( $users as $user ) {
130 $reminders = $this->userOptionsLookup->getOption( $user, 'translate-sandbox-reminders' );
131 $reminders = $reminders ? explode( '|', $reminders ) : [];
132 $remindersCount = count( $reminders );
133 if ( $remindersCount ) {
134 $lastReminderTimestamp = new MWTimestamp( end( $reminders ) );
135 $lastReminderAgo = htmlspecialchars(
136 $this->getHumanTimestamp( $lastReminderTimestamp )
137 );
138 } else {
139 $lastReminderAgo = '';
140 }
141
142 $requests[] = [
143 'username' => $user->getName(),
144 'email' => $user->getEmail(),
145 'gender' => $this->userOptionsLookup->getOption( $user, 'gender' ),
146 'registrationdate' => $user->getRegistration(),
147 'translations' => count( $this->stash->getTranslations( $user ) ),
148 'languagepreferences' => FormatJson::decode(
149 $this->userOptionsLookup->getOption( $user, 'translate-sandbox' )
150 ),
151 'userid' => $user->getId(),
152 'reminderscount' => $remindersCount,
153 'lastreminder' => $lastReminderAgo,
154 ];
155 }
156
157 // Sort the requests based on translations and registration date
158 usort( $requests, [ $this, 'translatorRequestSort' ] );
159
160 foreach ( $requests as $request ) {
161 $items[] = $this->makeRequestItem( $request );
162 }
163
164 $requestsList = implode( "\n", $items );
165
166 return <<<HTML
167 <div class="row request-header">
168 <div class="four columns">
169 <button class="language-selector unselected">
170 {$this->msg( 'tsb-all-languages-button-label' )->escaped()}
171 </button>
172 </div>
173 <div class="five columns request-count"></div>
174 <div class="three columns text-center">
175 <input class="request-selector-all" name="request" type="checkbox" />
176 </div>
177 </div>
178 <div class="requests-list">
179 {$requestsList}
180 </div>
181 HTML;
182 }
183
184 private function makeRequestItem( array $request ): string {
185 $requestdataEnc = htmlspecialchars( FormatJson::encode( $request ) );
186 $nameEnc = htmlspecialchars( $request['username'] );
187 $nameEncForId =
188 htmlspecialchars(
189 Sanitizer::escapeIdForAttribute( 'tsb-request-' . $request['username'] )
190 );
191 $emailEnc = htmlspecialchars( $request['email'] );
192 $countEnc = htmlspecialchars( (string)$request['translations'] );
193 $timestamp = new MWTimestamp( $request['registrationdate'] );
194 $agoEnc = htmlspecialchars( $this->getHumanTimestamp( $timestamp ) );
195
196 return <<<HTML
197 <div class="row request" data-data="$requestdataEnc" id="$nameEncForId">
198 <div class="two columns amount">
199 <div class="translation-count">$countEnc</div>
200 </div>
201 <div class="seven columns request-info">
202 <div class="row username">$nameEnc</div>
203 <div class="row email" dir="ltr">$emailEnc</div>
204 </div>
205 <div class="three columns approval text-center">
206 <input class="row request-selector" name="request" type="checkbox" />
207 <div class="row signup-age">$agoEnc</div>
208 </div>
209 </div>
210 HTML;
211 }
212
213 private function getHumanTimestamp( MWTimestamp $ts ): string {
214 return $this->getLanguage()->getHumanTimestamp( $ts, null, $this->getUser() );
215 }
216
221 private function translatorRequestSort( array $a, array $b ): int {
222 return $b['translations'] <=> $a['translations']
223 ?: $b['registrationdate'] <=> $a['registrationdate']
224 ?: strnatcasecmp( $a['username'], $b['username'] );
225 }
226}
Utility class for the sandbox feature of Translate.