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