Nikerabbit has uploaded a new change for review.
https://gerrit.wikimedia.org/r/70142
Change subject: Message suggester for sandboxed users
......................................................................
Message suggester for sandboxed users
Change-Id: Ie4fe8c4c49352efae433fe10570c8d1085452071
---
M MessageGroups.php
M Translate.php
M _autoload.php
M api/ApiQueryMessageGroups.php
M messagegroups/RecentAdditionsMessageGroup.php
A messagegroups/SandboxMessageGroup.php
6 files changed, 207 insertions(+), 35 deletions(-)
git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Translate
refs/changes/42/70142/1
diff --git a/MessageGroups.php b/MessageGroups.php
index 634104d..9ea8778 100644
--- a/MessageGroups.php
+++ b/MessageGroups.php
@@ -476,6 +476,7 @@
return array(
'!recent' => 'RecentMessageGroup',
'!additions' => 'RecentAdditionsMessageGroup',
+ '!sandbox' => 'SandboxMessageGroup',
);
}
@@ -662,4 +663,62 @@
return $groups;
}
+
+ /**
+ * Filters out messages that should not be translated under normal
+ * conditions.
+ *
+ * @param MessageHandle $handle Handle for the translation target.
+ * @return boolean
+ * @since 2013.06
+ */
+ public static function isTranslatableMessage( MessageHandle $handle ) {
+ static $cache = array();
+
+ $group = $handle->getGroup();
+ $groupId = $group->getId();
+ $language = $handle->getCode();
+ $cacheKey = "$groupId:$language";
+
+ if ( !isset( $cache[$cacheKey] ) ) {
+ $allowed = true;
+ $discouraged = false;
+
+ $whitelist = $group->getTranslatableLanguages();
+ if ( is_array( $whitelist ) && !isset(
$whitelist[$language] ) ) {
+ $allowed = false;
+ }
+
+ $discouraged = false;
+ if ( self::getPriority( $group ) === 'discouraged' ) {
+ $discouraged = true;
+ } else {
+ $priorityLanguages = TranslateMetadata::get(
$groupId, 'prioritylangs' );
+ if ( $priorityLanguages ) {
+ $map = array_flip( explode( ',',
$priorityLanguages ) );
+ if ( !isset( $map[$language] ) ) {
+ $discouraged = true;
+ }
+ }
+ }
+
+ $cache[$cacheKey] = array(
+ 'relevant' => $allowed && !$discouraged,
+ 'tags' => array(),
+ );
+
+ $groupTags = $group->getTags();
+ foreach ( array( 'ignored', 'optional' ) as $tag ) {
+ if ( isset( $groupTags[$tag] ) ) {
+ foreach ( $groupTags[$tag] as $key ) {
+ // TODO: ucfirst should not be
here
+
$cache[$cacheKey]['tags'][ucfirst( $key )] = true;
+ }
+ }
+ }
+ }
+
+ return $cache[$cacheKey]['relevant'] &&
+ !isset( $cache[$cacheKey]['tags'][ucfirst(
$handle->getKey() )] );
+ }
}
diff --git a/Translate.php b/Translate.php
index 996cc14..469f727 100644
--- a/Translate.php
+++ b/Translate.php
@@ -564,17 +564,23 @@
/**
* Whether to allow users to sign up via a sandbox. Sandboxed users cannot do
* much until approved and thus they can be get rid of easily.
- * @since 2013-04
+ * @since 2013.04
*/
$wgTranslateUseSandbox = false;
/**
* To which group the translators are promoted. If left at false, they will
just
* be removed from sandbox and become normal users.
- * @since 2013-04
+ * @since 2013.04
*/
$wgTranslateSandboxPromotedGroup = false;
+/**
+ * List of page names to always suggest for sandboxed users.
+ * @since 2013.06
+ */
+$wgTranslateSandboxSuggestions = array();
+
# </source>
# === Unsorted ===
# <source lang=php>
diff --git a/_autoload.php b/_autoload.php
index 72a9871..eb8f970 100644
--- a/_autoload.php
+++ b/_autoload.php
@@ -241,6 +241,7 @@
$wgAutoloadClasses['MessageGroupOld'] =
"$dir/messagegroups/MessageGroupOld.php";
$wgAutoloadClasses['RecentMessageGroup'] =
"$dir/messagegroups/RecentMessageGroup.php";
$wgAutoloadClasses['RecentAdditionsMessageGroup'] =
"$dir/messagegroups/RecentAdditionsMessageGroup.php";
+$wgAutoloadClasses['SandboxMessageGroup'] =
"$dir/messagegroups/SandboxMessageGroup.php";
$wgAutoloadClasses['SingleFileBasedMessageGroup'] =
"$dir/messagegroups/SingleFileBasedMessageGroup.php";
$wgAutoloadClasses['WikiMessageGroup'] =
"$dir/messagegroups/WikiMessageGroup.php";
$wgAutoloadClasses['WikiPageMessageGroup'] =
"$dir/messagegroups/WikiPageMessageGroup.php";
diff --git a/api/ApiQueryMessageGroups.php b/api/ApiQueryMessageGroups.php
index df6a94f..3333416 100644
--- a/api/ApiQueryMessageGroups.php
+++ b/api/ApiQueryMessageGroups.php
@@ -29,6 +29,11 @@
if ( $params['format'] === 'flat' ) {
$groups = MessageGroups::getAllGroups();
foreach ( MessageGroups::getDynamicGroups() as $id =>
$unused ) {
+ // Do not list the sandbox group. The code that
knows it
+ // exists can access it directly.
+ if ( $id === '!sandbox' ) {
+ continue;
+ }
$groups[$id] = MessageGroups::getGroup( $id );
}
diff --git a/messagegroups/RecentAdditionsMessageGroup.php
b/messagegroups/RecentAdditionsMessageGroup.php
index a7ae3f3..09bac8c 100644
--- a/messagegroups/RecentAdditionsMessageGroup.php
+++ b/messagegroups/RecentAdditionsMessageGroup.php
@@ -52,40 +52,10 @@
* as they are not displayed in other places.
*
* @see https://bugzilla.wikimedia.org/43030
- * @param MessageHandle $msg
+ * @param MessageHandle $handle
* @return boolean
*/
- protected function matchingMessage( MessageHandle $msg ) {
- $group = $msg->getGroup();
- $groupId = $group->getId();
-
- if ( !array_key_exists( $groupId, $this->groupInfoCache ) ) {
- $translatableLanguages =
$group->getTranslatableLanguages();
- $languageTranslatable = true;
-
- if ( is_array( $translatableLanguages ) &&
- !array_key_exists( $this->language,
$translatableLanguages )
- ) {
- $languageTranslatable = false;
- }
-
- $groupDiscouraged = MessageGroups::getPriority( $group
) !== 'discouraged';
- $this->groupInfoCache[$groupId] = array(
- 'relevant' => ( $languageTranslatable &&
$groupDiscouraged ),
- 'tags' => array(),
- );
-
- $groupTags = $group->getTags();
- foreach ( array( 'ignored', 'optional' ) as $tag ) {
- if ( isset( $groupTags[$tag] ) ) {
- foreach ( $groupTags[$tag] as $key ) {
-
$this->groupInfoCache[$groupId]['tags'][ucfirst( $key )] = true;
- }
- }
- }
- }
-
- return !isset(
$this->groupInfoCache[$groupId]['tags'][$msg->getKey()] ) &&
- $this->groupInfoCache[$groupId]['relevant'];
+ protected function matchingMessage( MessageHandle $handle ) {
+ return MessageGroups::isTranslatableMessage( $handle );
}
}
diff --git a/messagegroups/SandboxMessageGroup.php
b/messagegroups/SandboxMessageGroup.php
new file mode 100644
index 0000000..1a9fb83
--- /dev/null
+++ b/messagegroups/SandboxMessageGroup.php
@@ -0,0 +1,131 @@
+<?php
+/**
+ * This file contains an unmanaged message group implementation.
+ *
+ * @file
+ * @author Niklas Laxström
+ * @license GPL2+
+ */
+
+/**
+ * @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 $wgTranslateSandboxSuggestions;
+
+ $list = array();
+
+ $mi = MessageIndex::singleton();
+ foreach ( $wgTranslateSandboxSuggestions as $titleText ) {
+ $title = Title::newFromText( $titleText );
+ $handle = new MessageHandle( $title );
+ if ( $mi->getGroupIds( $handle ) !== array() ) {
+ $index = $title->getNamespace() . ':' .
$handle->getKey();
+ $list[$index] = '';
+ }
+ }
+
+ // Get some random ones
+ $all = array_keys( $mi->retrieve() );
+ // In case there aren't any messages
+ if ( $all === array() ) {
+ return array();
+ }
+ $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 )]] = '';
+ }
+
+ // Ugly
+ $store = new TranslationStashStorage( wfGetDB( DB_MASTER ) );
+ $user = RequestContext::getMain()->getUser();
+ $translations = $store->getTranslations( $user );
+
+ // Filter out the ones the user has already translated
+ foreach ( array_keys( $translations ) as $titleText ) {
+ $title = Title::newFromText( $titleText );
+ $handle = new MessageHandle( $title );
+ $index = $title->getNamespace() . ':' .
$handle->getKey();
+ unset( $list[$index] );
+ }
+
+ // Fetch definitions, slowly, one by one
+ $count = 0;
+ foreach ( $list as $index => &$translation ) {
+ list( $ns, $page ) = explode( ':', $index, 2 );
+ $title = Title::makeTitle( $ns,
"$page/{$this->language}" );
+ $handle = new MessageHandle( $title );
+
+ if ( MessageGroups::isTranslatableMessage( $handle ) ) {
+ $count++;
+ $translation = $this->getMessageContent(
$handle );
+ } else {
+ unset( $list[$index] );
+ }
+
+ // Only fetch 20 messages at once
+ if ( $count === 20 ) {
+ break;
+ }
+ }
+
+ // Remove the extra entries
+ $list = array_slice( $list, 0, 20 );
+
+ return $list;
+ }
+
+ public function getChecker() {
+ return null;
+ }
+
+ /**
+ * Subpage language code, if any in the title, is ignored.
+ */
+ public function getMessageContent( MessageHandle $handle ) {
+ $groupId = MessageIndex::getPrimaryGroupId( $handle );
+ $group = MessageGroups::getGroup( $groupId );
+ if ( $group ) {
+ return $group->getMessage( $handle->getKey(),
$group->getSourceLanguage() );
+ }
+
+ throw new MWException( 'Could not find group for ' .
$handle->getKey() );
+ }
+}
--
To view, visit https://gerrit.wikimedia.org/r/70142
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: Ie4fe8c4c49352efae433fe10570c8d1085452071
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Translate
Gerrit-Branch: master
Gerrit-Owner: Nikerabbit <[email protected]>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits