SandboxMessageGroup.php

Go to the documentation of this file.
00001 <?php
00014 class SandboxMessageGroup extends WikiMessageGroup {
00015     /*
00016      * Yes this is very ugly hack and should not be removed.
00017      * @see MessageCollection::getPages()
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         // Should not be visible
00039         return 'Sandbox messages';
00040     }
00041 
00042     public function getDescription( IContextSource $context = null ) {
00043         // Should not be visible
00044         return 'Suggests messages to translate for sandboxed users';
00045     }
00046 
00047     public function getDefinitions() {
00048         global $wgTranslateSandboxSuggestions, $wgTranslateSandboxLimit;
00049 
00050         // This will contain the list of messages shown to the user
00051         $list = array();
00052 
00053         // Ugly
00054         $store = new TranslationStashStorage( wfGetDB( DB_MASTER ) );
00055         $user = RequestContext::getMain()->getUser();
00056         $translations = $store->getTranslations( $user );
00057 
00058         // Add messages the user has already translated first, so he
00059         // can go back and correct them.
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         // Always add the regular suggestions
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             // This index might already exist, but that is okay
00077             $list[$index] = '';
00078         }
00079 
00080         // Message index of all known messages
00081         $mi = MessageIndex::singleton();
00082         // Get some random keys
00083         $all = array_keys( $mi->retrieve() );
00084         // In case there aren't any messages
00085         if ( $all === array() ) {
00086             return $list;
00087         }
00088         $min = 0;
00089         $max = count( $all ) - 1; // Indexes are zero-based
00090 
00091         // Get some message. Will be filtered to less below.
00092         for ( $i = count( $list ); $i < 100; $i++ ) {
00093             $list[$all[rand( $min, $max )]] = '';
00094         }
00095 
00096         // Fetch definitions, slowly, one by one
00097         $count = 0;
00098 
00099         // Provide twice the number of messages than the limit
00100         // to have a buffer in case the user skips some messages
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                 // Modified by reference
00110                 $translation = $this->getMessageContent( $handle );
00111                 if ( $translation === null ) {
00112                     // Something is not in sync or badly broken. Handle gracefully.
00113                     unset( $list[$index] );
00114                     wfWarn( "No message definition for $index while preparing the sandbox" );
00115 
00116                     continue;
00117                 }
00118             } else {
00119                 // This might include messages that the user has already translated
00120                 // or messages given in $wgTranslateSandboxSuggestions or just dated
00121                 // message index.
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         // Remove the extra entries
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         // Try harder
00159         $keys = array();
00160         if ( method_exists( $group, 'getKeys' ) ) {
00161             $keys = $group->getKeys();
00162         } else {
00163             $keys = array_keys( $group->getDefinitions() );
00164         }
00165         // Try to find the original key with correct case
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 }
Generated on Tue Oct 29 00:00:24 2013 for MediaWiki Translate Extension by  doxygen 1.6.3