summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/Translate/specials/SpecialAggregateGroups.php
blob: 582a71a83e1bd11aed3a2a07bf87076c83f62957 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
<?php
/**
 * Contains logic for special page Special:AggregateGroups.
 *
 * @file
 * @author Santhosh Thottingal
 * @author Niklas Laxström
 * @author Siebrand Mazeland
 * @author Kunal Grover
 * @license GPL-2.0-or-later
 */

class SpecialAggregateGroups extends SpecialPage {
	protected $hasPermission = false;

	public function __construct() {
		parent::__construct( 'AggregateGroups', 'translate-manage' );
	}

	protected function getGroupName() {
		return 'wiki';
	}

	public function execute( $parameters ) {
		$this->setHeaders();

		$out = $this->getOutput();
		$out->addModuleStyles( 'ext.translate.special.aggregategroups.styles' );

		// Check permissions
		if ( $this->getUser()->isAllowed( 'translate-manage' ) ) {
			$this->hasPermission = true;
		}

		$groupsPreload = array_merge(
			MessageGroups::getGroupsByType( WikiPageMessageGroup::class ),
			MessageGroups::getGroupsByType( AggregateMessageGroup::class )
		);
		TranslateMetadata::preloadGroups( array_keys( $groupsPreload ) );

		$groups = MessageGroups::getAllGroups();
		uasort( $groups, [ 'MessageGroups', 'groupLabelSort' ] );
		$aggregates = [];
		$pages = [];
		foreach ( $groups as $group ) {
			if ( $group instanceof WikiPageMessageGroup ) {
				$pages[] = $group;
			} elseif ( $group instanceof AggregateMessageGroup ) {
				$subgroups = TranslateMetadata::getSubgroups( $group->getId() );
				if ( $subgroups !== false ) {
					$aggregates[] = $group;
				}
			}
		}

		if ( !count( $pages ) ) {
			// @todo Use different message
			$out->addWikiMsg( 'tpt-list-nopages' );

			return;
		}

		$this->showAggregateGroups( $aggregates );
	}

	/**
	 * @param AggregateMessageGroup $group
	 * @return string
	 */
	protected function showAggregateGroup( $group ) {
		$out = '';
		$id = $group->getId();
		$label = $group->getLabel();
		$desc = $group->getDescription( $this->getContext() );

		$div = Html::openElement( 'div', [
			'class' => 'mw-tpa-group',
			'data-groupid' => $id,
			'data-id' => $this->htmlIdForGroup( $group ),
		] );

		$out .= $div;

		$edit = '';
		$remove = '';
		$editGroup = '';
		$select = '';
		$addButton = '';

		// Add divs for editing Aggregate Groups
		if ( $this->hasPermission ) {
			// Group edit and remove buttons
			$edit = Html::element( 'span', [ 'class' => 'tp-aggregate-edit-ag-button' ] );
			$remove = Html::element( 'span', [ 'class' => 'tp-aggregate-remove-ag-button' ] );

			// Edit group div
			$editGroupNameLabel = $this->msg( 'tpt-aggregategroup-edit-name' )->escaped();
			$editGroupName = Html::input(
				'tp-agg-name',
				$label,
				'text',
				[ 'class' => 'tp-aggregategroup-edit-name', 'maxlength' => '200' ]
			);
			$editGroupDescriptionLabel = $this->msg( 'tpt-aggregategroup-edit-description' )->escaped();
			$editGroupDescription = Html::input(
				'tp-agg-desc',
				$desc,
				'text',
				[ 'class' => 'tp-aggregategroup-edit-description' ]
			);
			$saveButton = Xml::submitButton(
				$this->msg( 'tpt-aggregategroup-update' )->text(),
				[ 'class' => 'tp-aggregategroup-update' ]
			);
			$cancelButton = Xml::submitButton(
				$this->msg( 'tpt-aggregategroup-update-cancel' )->text(),
				[ 'class' => 'tp-aggregategroup-update-cancel' ]
			);
			$editGroup = Html::rawElement(
				'div',
				[
					'class' => 'tp-edit-group hidden'
				],
				$editGroupNameLabel .
				$editGroupName . '<br />' .
				$editGroupDescriptionLabel .
				$editGroupDescription .
				$saveButton .
				$cancelButton
			);

			// Subgroups selector
			$select = Html::input(
				'tp-subgroups-input',
				'',
				'text',
				[ 'class' => 'tp-group-input' ]
			);
			$addButton = Html::element( 'input',
				[ 'type' => 'button',
					'value' => $this->msg( 'tpt-aggregategroup-add' )->text(),
					'class' => 'tp-aggregate-add-button' ]
			);
		}

		// Aggregate Group info div
		$groupName = Html::rawElement( 'h2',
			[ 'class' => 'tp-name' ],
			htmlspecialchars( $label ) . $edit . $remove
		);
		$groupDesc = Html::element( 'p',
			[ 'class' => 'tp-desc' ],
			$desc
		);
		$groupInfo = Html::rawElement( 'div',
			[ 'class' => 'tp-display-group' ],
			$groupName .
			$groupDesc
		);

		$out .= $groupInfo;
		$out .= $editGroup;
		$out .= $this->listSubgroups( $group );
		$out .= $select . $addButton;
		$out .= '</div>';

		return $out;
	}

	/**
	 * @param array $aggregates
	 */
	protected function showAggregateGroups( array $aggregates ) {
		$out = $this->getOutput();
		$out->addModules( 'ext.translate.special.aggregategroups' );

		$nojs = Html::element(
			'div',
			[ 'class' => 'tux-nojs errorbox' ],
			$this->msg( 'tux-nojs' )->plain()
		);

		$out->addHTML( $nojs );

		/**
		 * @var AggregateMessageGroup $group
		 */
		foreach ( $aggregates as $group ) {
			$out->addHTML( $this->showAggregateGroup( $group ) );
		}

		// Add new group if user has permissions
		if ( $this->hasPermission ) {
			$out->addHTML( "<br/><a class='tpt-add-new-group' href='#'>" .
				$this->msg( 'tpt-aggregategroup-add-new' )->escaped() .
				'</a>' );
			$newGroupNameLabel = $this->msg( 'tpt-aggregategroup-new-name' )->escaped();
			$newGroupName = Html::element(
				'input',
				[ 'class' => 'tp-aggregategroup-add-name', 'maxlength' => '200' ]
			);
			$newGroupDescriptionLabel = $this->msg( 'tpt-aggregategroup-new-description' )->escaped();
			$newGroupDescription = Html::element( 'input',
				[ 'class' => 'tp-aggregategroup-add-description' ]
			);
			$saveButton = Html::element( 'input', [
				'type' => 'button',
				'value' => $this->msg( 'tpt-aggregategroup-save' )->text(),
				'id' => 'tpt-aggregategroups-save',
				'class' => 'tp-aggregate-save-button'
			] );
			$newGroupDiv = Html::rawElement(
				'div',
				[ 'class' => 'tpt-add-new-group hidden' ],
				"$newGroupNameLabel $newGroupName<br />" .
				"$newGroupDescriptionLabel $newGroupDescription<br />$saveButton"
			);
			$out->addHTML( $newGroupDiv );
		}
	}

	/**
	 * @param AggregateMessageGroup $parent
	 * @return string
	 */
	protected function listSubgroups( AggregateMessageGroup $parent ) {
		$id = $this->htmlIdForGroup( $parent, 'mw-tpa-grouplist-' );
		$out = Html::openElement( 'ol', [ 'id' => $id ] );

		// Not calling $parent->getGroups() because it has done filtering already
		$subgroupIds = TranslateMetadata::getSubgroups( $parent->getId() );

		// Get the respective groups and sort them
		$subgroups = MessageGroups::getGroupsById( $subgroupIds );
		uasort( $subgroups, [ 'MessageGroups', 'groupLabelSort' ] );

		// Avoid potentially thousands of separate database queries
		$lb = new LinkBatch();
		foreach ( $subgroups as $group ) {
			$lb->addObj( $group->getTitle() );
		}
		$lb->setCaller( __METHOD__ );
		$lb->execute();

		// Add missing invalid group ids back, not returned by getGroupsById
		foreach ( $subgroupIds as $id ) {
			if ( !isset( $subgroups[$id] ) ) {
				$subgroups[$id] = null;
			}
		}

		foreach ( $subgroups as $id => $group ) {
			$remove = '';
			if ( $this->hasPermission ) {
				$remove = Html::element( 'span',
					[
						'class' => 'tp-aggregate-remove-button',
						'data-groupid' => $id,
					]
				);
			}

			if ( $group ) {
				$text = $this->getLinkRenderer()->makeKnownLink( $group->getTitle() );
				$note = MessageGroups::getPriority( $id );
			} else {
				$text = htmlspecialchars( $id );
				$note = $this->msg( 'tpt-aggregategroup-invalid-group' )->escaped();
			}

			$out .= Html::rawElement( 'li', [], "$text$remove $note" );
		}
		$out .= Html::closeElement( 'ol' );

		return $out;
	}

	/**
	 * @param MessageGroup $group
	 * @param string $prefix
	 * @return string
	 */
	protected function htmlIdForGroup( MessageGroup $group, $prefix = '' ) {
		$id = sha1( $group->getId() );
		$id = substr( $id, 5, 8 );

		return $prefix . $id;
	}
}