Translate extension for MediaWiki
 
Loading...
Searching...
No Matches
SandboxMessageGroup.php
Go to the documentation of this file.
1<?php
12
18 /*
19 * Yes this is very ugly hack and should not be removed.
20 * @see \MediaWiki\Extension\Translate\MessageLoading\MessageCollection::getPages()
21 */
22 protected $namespace = false;
23 protected $language;
24
28 public function __construct() {
29 }
30
31 public function setLanguage( $code ) {
32 $this->language = $code;
33 }
34
35 public function getId() {
36 return '!sandbox';
37 }
38
39 public function getLabel( IContextSource $context = null ) {
40 // Should not be visible
41 return 'Sandbox messages';
42 }
43
44 public function getDescription( IContextSource $context = null ) {
45 // Should not be visible
46 return 'Suggests messages to translate for sandboxed users';
47 }
48
49 public function getDefinitions() {
50 global $wgTranslateSandboxLimit;
51
52 // This will contain the list of messages shown to the user
53 $list = [];
54
55 // Ugly
56 $store = new TranslationStashStorage( wfGetDB( DB_PRIMARY ) );
57 $user = RequestContext::getMain()->getUser();
58 $translations = $store->getTranslations( $user );
59
60 // Add messages the user has already translated first, so he
61 // can go back and correct them.
62 foreach ( $translations as $translation ) {
63 $title = $translation->getTitle();
64 $handle = new MessageHandle( $title );
65 $index = $title->getNamespace() . ':' . $handle->getKey();
66 $list[$index] = '';
67 }
68
69 // Get some random keys
70 $all = MessageIndex::singleton()->getKeys();
71 // In case there aren't any messages
72 if ( $all === [] ) {
73 return $list;
74 }
75 $min = 0;
76 $max = count( $all ) - 1; // Indexes are zero-based
77
78 // Get some message. Will be filtered to less below.
79 for ( $i = count( $list ); $i < 100; $i++ ) {
80 $list[$all[rand( $min, $max )]] = '';
81 }
82
83 // Fetch definitions, slowly, one by one
84 $count = 0;
85
86 // Provide twice the number of messages than the limit
87 // to have a buffer in case the user skips some messages
88 $messagesToProvide = $wgTranslateSandboxLimit * 2;
89
90 foreach ( $list as $index => &$translation ) {
91 [ $ns, $page ] = explode( ':', $index, 2 );
92 $title = Title::makeTitle( (int)$ns, "$page/{$this->language}" );
93 $handle = new MessageHandle( $title );
94
95 if ( MessageGroups::isTranslatableMessage( $handle, $this->language ) ) {
96 // Modified by reference
97 $translation = $this->getMessageContent( $handle );
98 if ( $translation === null ) {
99 // Something is not in sync or badly broken. Handle gracefully.
100 unset( $list[$index] );
101 wfWarn( "No message definition for $index while preparing the sandbox" );
102
103 continue;
104 }
105 } else {
106 // This might include messages that the user has already translated
107 // or just dated message index.
108 unset( $list[$index] );
109
110 continue;
111 }
112
113 $count++;
114
115 if ( $count === $messagesToProvide ) {
116 break;
117 }
118 }
119
120 // Remove the extra entries
121 $list = array_slice( $list, 0, $messagesToProvide );
122
123 return $list;
124 }
125
126 public function getValidator() {
127 return null;
128 }
129
135 public function getMessageContent( MessageHandle $handle ) {
136 $groupId = MessageIndex::getPrimaryGroupId( $handle );
137 if ( $groupId === null ) {
138 return null;
139 }
140 $group = MessageGroups::getGroup( $groupId );
141 $key = $handle->getKey();
142
143 $source = $group->getMessage( $key, $group->getSourceLanguage() );
144 if ( $source !== null ) {
145 return $source;
146 }
147
148 // Try harder
149 $keys = $group->getKeys();
150
151 // Try to find the original key with correct case
152 foreach ( $keys as $realkey ) {
153 if ( $key === strtolower( $realkey ) ) {
154 $key = $realkey;
155 break;
156 }
157 }
158
159 return $group->getMessage( $key, $group->getSourceLanguage() );
160 }
161}
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.
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.
getValidator()
Returns a message validator object or null.
getDescription(IContextSource $context=null)
Returns a longer description about the group.
Group for messages that can be controlled via a page in MediaWiki namespace.