/** * Generates the whole page html and appends it to output */ protected function showPage() { $out = $this->getOutput(); $user = $this->getUser(); $count = count($this->stash->getTranslations($user)); if ($count === 0) { $progress = $this->msg('translate-translationstash-initialtranslation')->parse(); } else { $progress = $this->msg('translate-translationstash-translations')->numParams($count)->parse(); } $out->addHtml(<<<HTML <div class="grid"> \t<div class="row translate-welcome-header"> \t\t<h1> \t\t\t{$this->msg('translate-translationstash-welcome', $user->getName())->parse()} \t\t</h1> \t\t<p> \t\t\t{$this->msg('translate-translationstash-welcome-note')->parse()} \t\t</p> \t</div> \t<div class="row translate-stash-control"> \t\t<div class="six columns stash-stats"> \t\t\t{$progress} \t\t</div> \t\t<div class="six columns ext-translate-language-selector right"> \t\t\t{$this->tuxLanguageSelector()} \t\t</div> \t</div> \t{$this->getMessageTable()} \t<div class="row limit-reached hide"></div> </div> HTML ); }
public function testAdd() { $storage = new TranslationStashStorage(wfGetDB(DB_MASTER)); $translation1 = new StashedTranslation(User::newFromId(1), Title::makeTitle(NS_MAIN, __METHOD__), 'test value', array('kissa', 'kala')); $translation2 = new StashedTranslation(User::newFromId(2), Title::makeTitle(NS_MAIN, __METHOD__), 'test value 2', array('kissa', 'kala')); $storage->addTranslation($translation1); $storage->addTranslation($translation2); $ret = $storage->getTranslations(User::newFromId(1)); $this->assertCount(1, $ret, 'One stashed translation for this user'); // AssertSame required same reference, assert equals only same content $this->assertEquals($translation1, $ret[0], 'Data roundtrips'); }
protected function makeList() { $items = array(); $requests = array(); $users = TranslateSandbox::getUsers(); /** @var User $user */ foreach ($users as $user) { $reminders = $user->getOption('translate-sandbox-reminders'); $reminders = $reminders ? explode('|', $reminders) : array(); $remindersCount = count($reminders); if ($remindersCount) { $lastReminderTimestamp = new MWTimestamp(end($reminders)); $lastReminderAgo = htmlspecialchars($lastReminderTimestamp->getHumanTimestamp()); } else { $lastReminderAgo = ''; } $requests[] = array('username' => $user->getName(), 'email' => $user->getEmail(), 'gender' => $user->getOption('gender'), 'registrationdate' => $user->getRegistration(), 'translations' => count($this->stash->getTranslations($user)), 'languagepreferences' => FormatJson::decode($user->getOption('translate-sandbox')), 'userid' => $user->getId(), 'reminderscount' => $remindersCount, 'lastreminder' => $lastReminderAgo); } // Sort the requests based on translations and registration date usort($requests, array(__CLASS__, 'translatorRequestSort')); foreach ($requests as $request) { $items[] = $this->makeRequestItem($request); } $requestsList = implode("\n", $items); return <<<HTML <div class="row request-header"> \t<div class="four columns"> \t\t<button class="language-selector unselected"> \t\t\t{$this->msg("tsb-all-languages-button-label")->escaped()} \t\t</button> \t</div> \t<div class="five columns request-count"></div> \t<div class="three columns center"> \t\t<input class="request-selector-all" name="request" type="checkbox" /> \t</div> </div> <div class="requests-list"> \t{$requestsList} </div> HTML; }
public function execute() { $params = $this->extractRequestParams(); // The user we are operating on, not necessarly the user making the request $user = $this->getUser(); if (isset($params['username'])) { if ($this->getUser()->isAllowed('translate-sandboxmanage')) { $user = User::newFromName($params['username']); if (!$user) { $this->dieUsageMsg(array('invalidparam', 'username')); } } else { $this->dieUsageMsg(array('invalidparam', 'username')); } } $stash = new TranslationStashStorage(wfGetDB(DB_MASTER)); $action = $params['subaction']; if ($action === 'add') { if (!isset($params['title'])) { $this->dieUsageMsg(array('missingparam', 'title')); } if (!isset($params['translation'])) { $this->dieUsageMsg(array('missingparam', 'translation')); } // @todo: Return value of Title::newFromText not checked $translation = new StashedTranslation($user, Title::newFromText($params['title']), $params['translation'], FormatJson::decode($params['metadata'], true)); $stash->addTranslation($translation); } if ($action === 'query') { $output['translations'] = array(); $translations = $stash->getTranslations($user); foreach ($translations as $translation) { $output['translations'][] = $this->formatTranslation($translation); } } // If we got this far, nothing has failed $output['result'] = 'ok'; $this->getResult()->addValue(null, $this->getModuleName(), $output); }
public function getDefinitions() { global $wgTranslateSandboxSuggestions, $wgTranslateSandboxLimit; // This will contain the list of messages shown to the user $list = array(); // 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] = ''; } // Always add the regular suggestions foreach ($wgTranslateSandboxSuggestions as $titleText) { $title = Title::newFromText($titleText); if (!$title) { wfWarn("Invalid title in \$wgTranslateSandboxSuggestions: {$titleText}"); continue; } $index = $title->getNamespace() . ':' . $handle->getKey(); // This index might already exist, but that is okay $list[$index] = ''; } // Message index of all known messages $mi = MessageIndex::singleton(); // Get some random keys $all = array_keys($mi->retrieve()); // In case there aren't any messages if ($all === array()) { 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) { list($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 messages given in $wgTranslateSandboxSuggestions 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; }