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 === $this->getId() ) {
136 // Message key owned by aggregate group.
137 // Should not ever happen, but it does.
138 error_log( "AggregateMessageGroup $groupId cannot be primary owner of key $key" );
139
140 return null;
141 }
142
143 $group = MessageGroups::getGroup( $groupId );
144 if ( $group ) {
145 return $group->getMessage( $key, $code );
146 } else {
147 return null;
148 }
149 }
150
152 public function getTags( $type = null ) {
153 $tags = [];
154
155 foreach ( $this->getGroups() as $group ) {
156 $tags = array_merge_recursive( $tags, $group->getTags( $type ) );
157 }
158
159 return $tags;
160 }
161
163 public function getKeys() {
164 $keys = [];
165 foreach ( $this->getGroups() as $group ) {
166 // Array merge is *really* slow (tested in PHP 7.1), so avoiding it. A loop
167 // followed by array_unique (which we need anyway) is magnitudes faster.
168 foreach ( $group->getKeys() as $key ) {
169 $keys[] = $key;
170 }
171 }
172
173 return array_values( array_unique( $keys ) );
174 }
175}
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.