Translate extension for MediaWiki
 
Loading...
Searching...
No Matches
AggregateGroupsSpecialPage.php
1<?php
2declare( strict_types = 1 );
3
4namespace MediaWiki\Extension\Translate\MessageGroupProcessing;
5
7use MediaWiki\Html\Html;
8use MediaWiki\Html\TemplateParser;
9use MediaWiki\Page\LinkBatchFactory;
10use MediaWiki\SpecialPage\SpecialPage;
11
21class AggregateGroupsSpecialPage extends SpecialPage {
22
23 private TemplateParser $templateParser;
24
25 public function __construct(
26 private readonly LinkBatchFactory $linkBatchFactory,
27 private readonly MessageGroupMetadata $messageGroupMetadata,
28 private readonly AggregateGroupManager $aggregateGroupManager,
29 ) {
30 parent::__construct( 'AggregateGroups' );
31 $this->templateParser = new TemplateParser( __DIR__ . '/templates' );
32 }
33
34 protected function getGroupName(): string {
35 return 'translation';
36 }
37
39 public function execute( $parameters ): void {
40 $this->setHeaders();
41 $this->addHelpLink( 'Help:Extension:Translate/Page translation administration' );
42
43 // Check permissions
44 $hasPermission = $this->getUser()->isAllowed( 'translate-manage' );
45
46 $out = $this->getOutput();
47
48 $out->addModuleStyles( 'ext.translate.specialpages.styles' );
49 $out->addModules( 'ext.translate.special.aggregategroups' );
50
51 $out->addHTML(
52 Html::element(
53 'div',
54 [
55 'id' => 'ext-translate-aggregategroups',
56 'data-haspermission' => $hasPermission ? 'true' : null,
57 ]
58 )
59 );
60
61 $aggregateGroups = $this->aggregateGroupManager->getAll();
62 if ( $aggregateGroups === [] ) {
63 $out->addHTML(
64 Html::noticeBox(
65 $this->msg( 'tpt-aggregategroup-no-groups' )->escaped(),
66 'tpt-aggregategroup-nogroups'
67 )
68 );
69 }
70
71 $msgCache = [
72 'editLabel' => $this->msg( 'tpt-aggregategroup-edit' )->text(),
73 'deleteLabel' => $this->msg( 'tpt-aggregategroup-delete' )->text(),
74 'removeItemLabel' => $this->msg( 'tpt-aggregategroup-remove-item' )->text(),
75 ];
76 foreach ( $aggregateGroups as $aggregateGroup ) {
77 $subGroupIds = $this->messageGroupMetadata->getSubgroups( $aggregateGroup->getId() );
78
79 $html = $this->templateParser->processTemplate( 'AggregateGroupTemplate', [
80 'id' => $aggregateGroup->getId(),
81 'name' => $aggregateGroup->getLabel(),
82 'description' => $aggregateGroup->getDescription(),
83 'subGroups' => $this->getSubGroupInfoForTemplate( $subGroupIds ),
84 'shouldExpand' => count( $subGroupIds ) <= 3,
85 'editLabel' => $msgCache['editLabel'],
86 'deleteLabel' => $msgCache['deleteLabel'],
87 'removeItemLabel' => $msgCache['removeItemLabel'],
88 'hasManageRights' => $hasPermission,
89 ] );
90
91 $this->getOutput()->addHTML( $html );
92 }
93 }
94
95 private function getSubGroupInfoForTemplate( array $subGroupIds ): array {
96 $subGroups = MessageGroups::getGroupsById( $subGroupIds );
97 uasort( $subGroups, [ MessageGroups::class, 'groupLabelSort' ] );
98
99 $groupCache = $this->getGroupCache( $subGroups );
100
101 $subGroupInfo = [];
102 foreach ( $subGroupIds as $id ) {
103 $group = $subGroups[$id] ?? null;
104 if ( $group ) {
105 $text = $this->getLinkRenderer()->makeKnownLink( $groupCache[$group->getId()], $group->getLabel() );
106 $note = htmlspecialchars( MessageGroups::getPriority( $id ) );
107 } else {
108 $text = htmlspecialchars( $id );
109 $note = $this->msg( 'tpt-aggregategroup-invalid-group' )->escaped();
110 }
111 $subGroupInfo[] = [
112 'id' => $id,
113 'labelHtml' => $text,
114 'note' => $note,
115 ];
116 }
117
118 return $subGroupInfo;
119 }
120
125 private function getGroupCache( array $subGroups ): array {
126 $groupCache = [];
127 $lb = $this->linkBatchFactory->newLinkBatch();
128 foreach ( $subGroups as $group ) {
129 $subGroupId = $group->getId();
130 $groupCache[$subGroupId] = $this->aggregateGroupManager->getTargetTitleByGroupId( $subGroupId );
131 $lb->addObj( $groupCache[$subGroupId] );
132 }
133 $lb->setCaller( __METHOD__ );
134 $lb->execute();
135
136 return $groupCache;
137 }
138}
Contains logic to manage aggregate groups and their subgroups.
static getPriority(MessageGroup|string $group)
We want to de-emphasize time sensitive groups like news for 2009.
Offers functionality for reading and updating Translate group related metadata.