Translate extension for MediaWiki
 
Loading...
Searching...
No Matches
AggregateMessageGroup.php
Go to the documentation of this file.
1<?php
14use MediaWiki\Title\Title;
15
26 private $groups;
27
29 public function exists() {
30 // Group exists if there are any subgroups.
31 return (bool)$this->conf['GROUPS'];
32 }
33
35 public function load( $code ) {
36 $messages = [];
37
38 foreach ( $this->getGroups() as $group ) {
39 $messages += $group->load( $code );
40 }
41
42 return $messages;
43 }
44
46 public function getMangler() {
47 if ( $this->mangler === null ) {
48 $this->mangler = new StringMatcher();
49 }
50
51 return $this->mangler;
52 }
53
58 public function getGroups(): array {
59 if ( $this->groups === null ) {
60 $groups = [];
61 $ids = (array)$this->conf['GROUPS'];
62 $ids = MessageGroups::expandWildcards( $ids );
63
64 foreach ( $ids as $id ) {
65 // Do not try to include self and go to infinite loop.
66 if ( $id === $this->getId() ) {
67 continue;
68 }
69
70 $group = MessageGroups::getGroup( $id );
71 if ( $group === null ) {
72 error_log( "Invalid group id in {$this->getId()}: $id" );
73 continue;
74 }
75
76 if ( MessageGroups::getPriority( $group ) === 'discouraged' ) {
77 continue;
78 }
79
80 $groups[$id] = $group;
81 }
82
83 $this->groups = $groups;
84 }
85
86 return $this->groups;
87 }
88
89 protected function loadMessagesFromCache( $groups ) {
90 $messages = [];
91 foreach ( $groups as $group ) {
92 if ( $group instanceof self ) {
93 $messages += $this->loadMessagesFromCache( $group->getGroups() );
94 continue;
95 }
96
97 if ( $group instanceof FileBasedMessageGroup ) {
98 $cache = $group->getMessageGroupCache( $group->getSourceLanguage() );
99 if ( $cache->exists() ) {
100 foreach ( $cache->getKeys() as $key ) {
101 $messages[$key] = $cache->get( $key );
102 }
103 }
104 continue;
105 }
106
107 $messages += $group->getDefinitions();
108 }
109
110 return $messages;
111 }
112
114 public function initCollection( $code ) {
115 $messages = $this->loadMessagesFromCache( $this->getGroups() );
116 $namespace = $this->getNamespace();
117 $definitions = new MessageDefinitions( $messages, $namespace );
118 $collection = MessageCollection::newFromDefinitions( $definitions, $code );
119
120 $this->setTags( $collection );
121
122 return $collection;
123 }
124
126 public function getMessage( $key, $code ) {
127 /* Just hand over the message content retrieval to the primary message
128 * group directly. This used to iterate over the subgroups looking for
129 * the primary group, but that might actually be under some other
130 * aggregate message group.
131 * @todo Implement getMessageContent to avoid hardcoding the namespace
132 * here.
133 */
134 $title = Title::makeTitle( $this->getNamespace(), $key );
135 $handle = new MessageHandle( $title );
136 $groupId = Services::getInstance()->getMessageIndex()->getPrimaryGroupId( $handle );
137 if ( $groupId === null ) {
138 error_log( "Could not determine groupId for MessageHandle of key $key" );
139 return null;
140 }
141 if ( $groupId === $this->getId() ) {
142 // Message key owned by aggregate group.
143 // Should not ever happen, but it does.
144 error_log( "AggregateMessageGroup $groupId cannot be primary owner of key $key" );
145
146 return null;
147 }
148
149 $group = MessageGroups::getGroup( $groupId );
150 if ( $group ) {
151 return $group->getMessage( $key, $code );
152 } else {
153 return null;
154 }
155 }
156
158 public function getTags( $type = null ) {
159 $tags = [];
160
161 foreach ( $this->getGroups() as $group ) {
162 $tags = array_merge_recursive( $tags, $group->getTags( $type ) );
163 }
164
165 return $tags;
166 }
167
169 public function getKeys() {
170 $keys = [];
171 foreach ( $this->getGroups() as $group ) {
172 // Array merge is *really* slow (tested in PHP 7.1), so avoiding it. A loop
173 // followed by array_unique (which we need anyway) is magnitudes faster.
174 foreach ( $group->getKeys() as $key ) {
175 $keys[] = $key;
176 }
177 }
178
179 return array_values( array_unique( $keys ) );
180 }
181}
Groups multiple message groups together as one group.
initCollection( $code)
@inheritDoc
getMessage( $key, $code)
@inheritDoc
getGroups()
Returns a list of message groups that this group consists of.
getTags( $type=null)
@inheritDoc
This class implements default behavior for file based message groups.
Factory class for accessing message groups individually by id or all of them as a list.
This file contains the class for core message collections implementation.
Wrapper for message definitions, just to beauty the code.
Class for pointing to messages, like Title class is for titles.
The versatile default implementation of StringMangler interface.
Minimal service container.
Definition Services.php:58
This class implements some basic functions that wrap around the YAML message group configurations.
getId()
Returns the unique identifier for this group.