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