SandboxMessageGroup.php
Go to the documentation of this file.00001 <?php
00014 class SandboxMessageGroup extends WikiMessageGroup {
00015
00016
00017
00018
00019 protected $namespace = false;
00020
00021 protected $language;
00022
00026 public function __construct() {
00027 }
00028
00029 public function setLanguage( $code ) {
00030 $this->language = $code;
00031 }
00032
00033 public function getId() {
00034 return '!sandbox';
00035 }
00036
00037 public function getLabel( IContextSource $context = null ) {
00038
00039 return 'Sandbox messages';
00040 }
00041
00042 public function getDescription( IContextSource $context = null ) {
00043
00044 return 'Suggests messages to translate for sandboxed users';
00045 }
00046
00047 public function getDefinitions() {
00048 global $wgTranslateSandboxSuggestions, $wgTranslateSandboxLimit;
00049
00050
00051 $list = array();
00052
00053
00054 $store = new TranslationStashStorage( wfGetDB( DB_MASTER ) );
00055 $user = RequestContext::getMain()->getUser();
00056 $translations = $store->getTranslations( $user );
00057
00058
00059
00060 foreach ( $translations as $translation ) {
00061 $title = $translation->getTitle();
00062 $handle = new MessageHandle( $title );
00063 $index = $title->getNamespace() . ':' . $handle->getKey();
00064 $list[$index] = '';
00065 }
00066
00067
00068 foreach ( $wgTranslateSandboxSuggestions as $titleText ) {
00069 $title = Title::newFromText( $titleText );
00070 if ( !$title ) {
00071 wfWarn( "Invalid title in \$wgTranslateSandboxSuggestions: $titleText" );
00072 continue;
00073 }
00074
00075 $index = $title->getNamespace() . ':' . $handle->getKey();
00076
00077 $list[$index] = '';
00078 }
00079
00080
00081 $mi = MessageIndex::singleton();
00082
00083 $all = array_keys( $mi->retrieve() );
00084
00085 if ( $all === array() ) {
00086 return $list;
00087 }
00088 $min = 0;
00089 $max = count( $all ) - 1;
00090
00091
00092 for ( $i = count( $list ); $i < 100; $i++ ) {
00093 $list[$all[rand( $min, $max )]] = '';
00094 }
00095
00096
00097 $count = 0;
00098
00099
00100
00101 $messagesToProvide = $wgTranslateSandboxLimit * 2;
00102
00103 foreach ( $list as $index => &$translation ) {
00104 list( $ns, $page ) = explode( ':', $index, 2 );
00105 $title = Title::makeTitle( $ns, "$page/{$this->language}" );
00106 $handle = new MessageHandle( $title );
00107
00108 if ( MessageGroups::isTranslatableMessage( $handle ) ) {
00109
00110 $translation = $this->getMessageContent( $handle );
00111 if ( $translation === null ) {
00112
00113 unset( $list[$index] );
00114 wfWarn( "No message definition for $index while preparing the sandbox" );
00115
00116 continue;
00117 }
00118 } else {
00119
00120
00121
00122 unset( $list[$index] );
00123 wfWarn( "Unsuitable or unknown message $index while preparing sandbox" );
00124
00125 continue;
00126 }
00127
00128 $count++;
00129
00130 if ( $count === $messagesToProvide ) {
00131 break;
00132 }
00133 }
00134
00135
00136 $list = array_slice( $list, 0, $messagesToProvide );
00137
00138 return $list;
00139 }
00140
00141 public function getChecker() {
00142 return null;
00143 }
00144
00148 public function getMessageContent( MessageHandle $handle ) {
00149 $groupId = MessageIndex::getPrimaryGroupId( $handle );
00150 $group = MessageGroups::getGroup( $groupId );
00151 $key = $handle->getKey();
00152
00153 $source = $group->getMessage( $key, $group->getSourceLanguage() );
00154 if ( $source !== null ) {
00155 return $source;
00156 }
00157
00158
00159 $keys = array();
00160 if ( method_exists( $group, 'getKeys' ) ) {
00161 $keys = $group->getKeys();
00162 } else {
00163 $keys = array_keys( $group->getDefinitions() );
00164 }
00165
00166 foreach ( $keys as $realkey ) {
00167 if ( $key === strtolower( $realkey ) ) {
00168 $key = $realkey;
00169 break;
00170 }
00171 }
00172
00173 return $group->getMessage( $key, $group->getSourceLanguage() );
00174 }
00175 }