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