Translate extension for MediaWiki
 
Loading...
Searching...
No Matches
SandboxMessageGroup.php
Go to the documentation of this file.
1<?php
10use MediaWiki\Context\IContextSource;
11use MediaWiki\Context\RequestContext;
16use MediaWiki\MediaWikiServices;
17use MediaWiki\Title\Title;
18
29 protected $namespace = false;
31 protected $language;
32
36 public function __construct() {
37 }
38
39 public function setLanguage( $code ) {
40 $this->language = $code;
41 }
42
43 public function getId() {
44 return '!sandbox';
45 }
46
47 public function getLabel( ?IContextSource $context = null ) {
48 // Should not be visible
49 return 'Sandbox messages';
50 }
51
52 public function getDescription( ?IContextSource $context = null ) {
53 // Should not be visible
54 return 'Suggests messages to translate for sandboxed users';
55 }
56
57 public function getDefinitions() {
58 global $wgTranslateSandboxLimit;
59
60 // This will contain the list of messages shown to the user
61 $list = [];
62
63 // Ugly
64 $store = new TranslationStashStorage(
65 MediaWikiServices::getInstance()->getDBLoadBalancer()->getConnection( DB_PRIMARY )
66 );
67 $user = RequestContext::getMain()->getUser();
68 $translations = $store->getTranslations( $user );
69
70 // Add messages the user has already translated first, so he
71 // can go back and correct them.
72 foreach ( $translations as $translation ) {
73 $title = $translation->getTitle();
74 $handle = new MessageHandle( $title );
75 $index = $title->getNamespace() . ':' . $handle->getKey();
76 $list[$index] = '';
77 }
78
79 // Get some random keys
80 $all = Services::getInstance()->getMessageIndex()->getKeys();
81 // In case there aren't any messages
82 if ( $all === [] ) {
83 return $list;
84 }
85 $min = 0;
86 $max = count( $all ) - 1; // Indexes are zero-based
87
88 // Get some message. Will be filtered to less below.
89 for ( $i = count( $list ); $i < 100; $i++ ) {
90 $list[$all[rand( $min, $max )]] = '';
91 }
92
93 // Fetch definitions, slowly, one by one
94 $count = 0;
95
96 // Provide twice the number of messages than the limit
97 // to have a buffer in case the user skips some messages
98 $messagesToProvide = $wgTranslateSandboxLimit * 2;
99
100 foreach ( $list as $index => &$translation ) {
101 [ $ns, $page ] = explode( ':', $index, 2 );
102 $title = Title::makeTitle( (int)$ns, "$page/{$this->language}" );
103 $handle = new MessageHandle( $title );
104
105 if ( MessageGroups::isTranslatableMessage( $handle, $this->language ) ) {
106 // Modified by reference
107 $translation = $this->getMessageContent( $handle );
108 if ( $translation === null ) {
109 // Something is not in sync or badly broken. Handle gracefully.
110 unset( $list[$index] );
111 wfWarn( "No message definition for $index while preparing the sandbox" );
112
113 continue;
114 }
115 } else {
116 // This might include messages that the user has already translated
117 // or just dated message index.
118 unset( $list[$index] );
119
120 continue;
121 }
122
123 $count++;
124
125 if ( $count === $messagesToProvide ) {
126 break;
127 }
128 }
129
130 // Remove the extra entries
131 $list = array_slice( $list, 0, $messagesToProvide );
132
133 return $list;
134 }
135
136 public function getValidator() {
137 return null;
138 }
139
145 public function getMessageContent( MessageHandle $handle ) {
146 $groupId = Services::getInstance()->getMessageIndex()->getPrimaryGroupId( $handle );
147 if ( $groupId === null ) {
148 return null;
149 }
150 $group = MessageGroups::getGroup( $groupId );
151 $key = $handle->getKey();
152
153 $source = $group->getMessage( $key, $group->getSourceLanguage() );
154 if ( $source !== null ) {
155 return $source;
156 }
157
158 // Try harder
159 $keys = $group->getKeys();
160
161 // Try to find the original key with correct case
162 foreach ( $keys as $realkey ) {
163 if ( $key === strtolower( $realkey ) ) {
164 $key = $realkey;
165 break;
166 }
167 }
168
169 return $group->getMessage( $key, $group->getSourceLanguage() );
170 }
171}
Factory class for accessing message groups individually by id or all of them as a list.
Class for pointing to messages, like Title class is for titles.
getKey()
Returns the identified or guessed message key.
Minimal service container.
Definition Services.php:59
getLabel(?IContextSource $context=null)
__construct()
setLanguage must be called before calling getDefinitions.
getDefinitions()
Fetch definitions from database.
getMessageContent(MessageHandle $handle)
Subpage language code, if any in the title, is ignored.
getDescription(?IContextSource $context=null)
Returns a longer description about the group.
getValidator()
Returns a message validator object or null.
Group for messages that can be controlled via a page in MediaWiki namespace.