summaryrefslogtreecommitdiff
blob: df33e67b0edccf08ab4d6ffa89b977bc57dd73b6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
<?php
/**
 * This file contains an unmanaged message group implementation.
 *
 * @file
 * @author Niklas Laxström
 * @license GPL-2.0-or-later
 */

use MediaWiki\Extension\Translate\TranslatorSandbox\TranslationStashStorage;

/**
 * @since 2013.06
 * @ingroup MessageGroup
 */
class SandboxMessageGroup extends WikiMessageGroup {
	/*
	 * Yes this is very ugly hack and should not be removed.
	 * @see MessageCollection::getPages()
	 */
	protected $namespace = false;
	protected $language;

	/**
	 * #setLanguage must be called before calling getDefinitions.
	 */
	public function __construct() {
	}

	public function setLanguage( $code ) {
		$this->language = $code;
	}

	public function getId() {
		return '!sandbox';
	}

	public function getLabel( IContextSource $context = null ) {
		// Should not be visible
		return 'Sandbox messages';
	}

	public function getDescription( IContextSource $context = null ) {
		// Should not be visible
		return 'Suggests messages to translate for sandboxed users';
	}

	public function getDefinitions() {
		global $wgTranslateSandboxLimit;

		// This will contain the list of messages shown to the user
		$list = [];

		// Ugly
		$store = new TranslationStashStorage( wfGetDB( DB_MASTER ) );
		$user = RequestContext::getMain()->getUser();
		$translations = $store->getTranslations( $user );

		// Add messages the user has already translated first, so he
		// can go back and correct them.
		foreach ( $translations as $translation ) {
			$title = $translation->getTitle();
			$handle = new MessageHandle( $title );
			$index = $title->getNamespace() . ':' . $handle->getKey();
			$list[$index] = '';
		}

		// Get some random keys
		$all = MessageIndex::singleton()->getKeys();
		// In case there aren't any messages
		if ( $all === [] ) {
			return $list;
		}
		$min = 0;
		$max = count( $all ) - 1; // Indexes are zero-based

		// Get some message. Will be filtered to less below.
		for ( $i = count( $list ); $i < 100; $i++ ) {
			$list[$all[rand( $min, $max )]] = '';
		}

		// Fetch definitions, slowly, one by one
		$count = 0;

		// Provide twice the number of messages than the limit
		// to have a buffer in case the user skips some messages
		$messagesToProvide = $wgTranslateSandboxLimit * 2;

		foreach ( $list as $index => &$translation ) {
			[ $ns, $page ] = explode( ':', $index, 2 );
			$title = Title::makeTitle( $ns, "$page/{$this->language}" );
			$handle = new MessageHandle( $title );

			if ( MessageGroups::isTranslatableMessage( $handle ) ) {
				// Modified by reference
				$translation = $this->getMessageContent( $handle );
				if ( $translation === null ) {
					// Something is not in sync or badly broken. Handle gracefully.
					unset( $list[$index] );
					wfWarn( "No message definition for $index while preparing the sandbox" );

					continue;
				}
			} else {
				// This might include messages that the user has already translated
				// or just dated message index.
				unset( $list[$index] );

				continue;
			}

			$count++;

			if ( $count === $messagesToProvide ) {
				break;
			}
		}

		// Remove the extra entries
		$list = array_slice( $list, 0, $messagesToProvide );

		return $list;
	}

	public function getValidator() {
		return null;
	}

	/**
	 * Subpage language code, if any in the title, is ignored.
	 * @param MessageHandle $handle
	 * @return null|string
	 */
	public function getMessageContent( MessageHandle $handle ) {
		$groupId = MessageIndex::getPrimaryGroupId( $handle );
		$group = MessageGroups::getGroup( $groupId );
		$key = $handle->getKey();

		$source = $group->getMessage( $key, $group->getSourceLanguage() );
		if ( $source !== null ) {
			return $source;
		}

		// Try harder
		$keys = $group->getKeys();

		// Try to find the original key with correct case
		foreach ( $keys as $realkey ) {
			if ( $key === strtolower( $realkey ) ) {
				$key = $realkey;
				break;
			}
		}

		return $group->getMessage( $key, $group->getSourceLanguage() );
	}
}