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