/**
  * Generates the html snippet for ajax edit. Echoes it to the output and
  * disabled all other output.
  */
 public function execute()
 {
     global $wgServer, $wgScriptPath;
     $context = RequestContext::getMain();
     $context->getOutput()->disable();
     $data = $this->getEditInfo();
     $helpers = new TranslationHelpers($this->getTitle(), '');
     $id = "tm-target-{$helpers->dialogID()}";
     $helpers->setTextareaId($id);
     if ($this->suggestions === 'checks') {
         echo $helpers->getBoxes($this->suggestions);
         return;
     }
     $handle = new MessageHandle($this->getTitle());
     $groupId = MessageIndex::getPrimaryGroupId($handle);
     $translation = '';
     if ($groupId) {
         $translation = $helpers->getTranslation();
     }
     $targetLang = Language::factory($helpers->getTargetLanguage());
     $textareaParams = array('name' => 'text', 'class' => 'mw-translate-edit-area', 'id' => $id, 'lang' => $targetLang->getCode(), 'dir' => $targetLang->getDir());
     if (!$groupId || !$context->getUser()->isAllowed('translate')) {
         $textareaParams['readonly'] = 'readonly';
     }
     $extraInputs = '';
     Hooks::run('TranslateGetExtraInputs', array(&$translation, &$extraInputs));
     $textarea = Html::element('textarea', $textareaParams, $translation);
     $hidden = array();
     $hidden[] = Html::hidden('title', $this->getTitle()->getPrefixedDbKey());
     if (isset($data['revisions'][0]['timestamp'])) {
         $hidden[] = Html::hidden('basetimestamp', $data['revisions'][0]['timestamp']);
     }
     $hidden[] = Html::hidden('starttimestamp', $data['starttimestamp']);
     if (isset($data['edittoken'])) {
         $hidden[] = Html::hidden('token', $data['edittoken']);
     }
     $hidden[] = Html::hidden('format', 'json');
     $hidden[] = Html::hidden('action', 'edit');
     $summary = Xml::inputLabel($context->msg('translate-js-summary')->text(), 'summary', 'summary', 40);
     $save = Xml::submitButton($context->msg('translate-js-save')->text(), array('class' => 'mw-translate-save'));
     $saveAndNext = Xml::submitButton($context->msg('translate-js-next')->text(), array('class' => 'mw-translate-next'));
     $skip = Html::element('input', array('class' => 'mw-translate-skip', 'type' => 'button', 'value' => $context->msg('translate-js-skip')->text()));
     if ($this->getTitle()->exists()) {
         $history = Html::element('input', array('class' => 'mw-translate-history', 'type' => 'button', 'value' => $context->msg('translate-js-history')->text()));
     } else {
         $history = '';
     }
     $support = $this->getSupportButton($this->getTitle());
     if ($context->getUser()->isAllowed('translate')) {
         $bottom = "{$summary}{$save}{$saveAndNext}{$skip}{$history}{$support}";
     } else {
         $text = $context->msg('translate-edit-nopermission')->escaped();
         $button = $this->getPermissionPageButton();
         $bottom = "{$text} {$button}{$skip}{$history}{$support}";
     }
     // Use the api to submit edits
     $formParams = array('action' => "{$wgServer}{$wgScriptPath}/api.php", 'method' => 'post');
     $form = Html::rawElement('form', $formParams, implode("\n", $hidden) . "\n" . $helpers->getBoxes($this->suggestions) . "\n" . Html::rawElement('div', array('class' => 'mw-translate-inputs'), "{$textarea}\n{$extraInputs}") . "\n" . Html::rawElement('div', array('class' => 'mw-translate-bottom'), $bottom));
     echo Html::rawElement('div', array('class' => 'mw-ajax-dialog'), $form);
 }
	/**
	 * @return MessageIndex
	 */
	public static function singleton() {
		if ( self::$instance === null ) {
			global $wgTranslateMessageIndex;
			$params = $wgTranslateMessageIndex;
			$class = array_shift( $params );
			self::$instance = new $class( $params );
		}
		return self::$instance;
	}
 protected function setUp()
 {
     parent::setUp();
     global $wgHooks;
     $this->setMwGlobals(array('wgHooks' => $wgHooks, 'wgTranslateTranslationServices' => array(), 'wgTranslateCacheDirectory' => $this->getNewTempDirectory()));
     $wgHooks['TranslatePostInitGroups'] = array();
     $mg = MessageGroups::singleton();
     $mg->setCache(wfGetCache('hash'));
     $mg->recache();
     MessageIndex::setInstance(new HashMessageIndex());
     MessageIndex::singleton()->rebuild();
 }
 protected function setUp()
 {
     parent::setUp();
     global $wgHooks;
     $this->setMwGlobals(array('wgHooks' => $wgHooks, 'wgGroupPermissions' => array(), 'wgTranslateMessageNamespaces' => array(NS_MEDIAWIKI)));
     $wgHooks['TranslatePostInitGroups'] = array(array($this, 'getTestGroups'));
     $mg = MessageGroups::singleton();
     $mg->setCache(wfGetCache('hash'));
     $mg->recache();
     MessageIndex::setInstance(new HashMessageIndex());
     MessageIndex::singleton()->rebuild();
 }
 public function setUp()
 {
     parent::setUp();
     global $wgHooks;
     $this->setMwGlobals(array('wgHooks' => $wgHooks, 'wgTranslateTranslationServices' => array(), 'wgTranslateDelayedMessageIndexRebuild' => false));
     $wgHooks['TranslatePostInitGroups'] = array();
     $mg = MessageGroups::singleton();
     $mg->setCache(wfGetCache('hash'));
     $mg->recache();
     MessageIndex::setInstance(new HashMessageIndex());
     MessageIndex::singleton()->rebuild();
 }
 protected function setUp()
 {
     parent::setUp();
     global $wgHooks;
     $this->setMwGlobals(array('wgHooks' => $wgHooks, 'wgEnablePageTranslation' => true, 'wgTranslateTranslationServices' => array()));
     TranslateHooks::setupTranslate();
     $wgHooks['TranslatePostInitGroups'] = array('MessageGroups::getTranslatablePages');
     $mg = MessageGroups::singleton();
     $mg->setCache(wfGetCache('hash'));
     $mg->recache();
     MessageIndex::setInstance(new HashMessageIndex());
     MessageIndex::singleton()->rebuild();
 }
 protected function setUp()
 {
     parent::setUp();
     $conf = array(__DIR__ . '/data/ParentGroups.yaml');
     global $wgHooks;
     $this->setMwGlobals(array('wgHooks' => $wgHooks, 'wgTranslateGroupFiles' => $conf, 'wgTranslateTranslationServices' => array()));
     $wgHooks['TranslatePostInitGroups'] = array('MessageGroups::getConfiguredGroups');
     $mg = MessageGroups::singleton();
     $mg->setCache(wfGetCache('hash'));
     $mg->recache();
     MessageIndex::setInstance(new HashMessageIndex());
     MessageIndex::singleton()->rebuild();
 }
 protected function formatTranslation(StashedTranslation $translation)
 {
     $title = $translation->getTitle();
     $handle = new MessageHandle($title);
     // Prepare for the worst
     $definition = '';
     $comparison = '';
     if ($handle->isValid()) {
         $groupId = MessageIndex::getPrimaryGroupId($handle);
         $group = MessageGroups::getGroup($groupId);
         $key = $handle->getKey();
         $definition = $group->getMessage($key, $group->getSourceLanguage());
         $comparison = $group->getMessage($key, $handle->getCode());
     }
     return array('title' => $title->getPrefixedText(), 'definition' => $definition, 'translation' => $translation->getValue(), 'comparison' => $comparison, 'metadata' => $translation->getMetadata());
 }
 /**
  * Ajax-enabled message editing link.
  * @param $target Title: Title of the target message.
  * @param $text String: Link text for Linker::link()
  * @return string HTML link
  */
 public static function ajaxEditLink($target, $text)
 {
     $handle = new MessageHandle($target);
     $groupId = MessageIndex::getPrimaryGroupId($handle);
     $params = array();
     $params['action'] = 'edit';
     $params['loadgroup'] = $groupId;
     $jsEdit = TranslationEditPage::jsEdit($target, $groupId, 'dialog');
     return Linker::link($target, $text, $jsEdit, $params);
 }
 /**
  * Subpage language code, if any in the title, is ignored.
  * @param MessageHandle $handle
  * @return null|string
  * @throws MWException
  */
 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());
 }
	function run() {
		MessageIndex::singleton()->rebuild();
	}
 *
 * @author Niklas Laxstrom
 * @copyright Copyright © 2008-2011, Niklas Laxström
 * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License 2.0 or later
 * @file
 */

require( dirname( __FILE__ ) . '/cli.inc' );

function showUsage() {
	STDERR( <<<EOT
Message index creation command line script

Usage: php createMessageIndex.php [options...]

Options:
  --help            Show this help text
  --quiet           Only output errors

EOT
);
	exit( 1 );
}

if ( isset( $options['help'] ) ) {
	showUsage();
}

MessageGroups::clearCache();
MessageIndex::singleton()->rebuild();
Example #13
0
 /**
  * Subpage language of any in the title is not used.
  */
 public function getMessageContent(MessageHandle $handle, $code)
 {
     $groupId = MessageIndex::getPrimaryGroupId($handle);
     $group = MessageGroups::getGroup($groupId);
     if ($group) {
         return $group->getMessage($handle->getKey(), $code);
     }
 }
 public function testSearchableTTMServer()
 {
     global $wgTranslateTestTTMServer;
     if (!$wgTranslateTestTTMServer) {
         $this->markTestSkipped('No test TTMServer available');
     }
     $server = TTMServer::factory($wgTranslateTestTTMServer);
     $solarium = $server->getSolarium();
     // Empty it
     $update = $solarium->createUpdate();
     $update->addDeleteQuery('*:*');
     $update->addCommit();
     $solarium->update($update);
     // Check that it is empty indeed
     $select = $solarium->createSelect();
     $select->setRows(0);
     $select->setQuery('*:*');
     $result = $solarium->select($select);
     $this->assertEquals(0, $result->getNumFound());
     // Add one definition
     $title = Title::newFromText('MediaWiki:one/en');
     $page = WikiPage::factory($title);
     $content = ContentHandler::makeContent('1', $title);
     $page->doEditContent($content, __METHOD__);
     $select = $solarium->createSelect();
     $select->setRows(1);
     $select->setQuery('*:*');
     $result = $solarium->select($select);
     $this->assertEquals(1, $result->getNumFound());
     $doc = null;
     foreach ($result as $doc) {
         // @todo FIXME Empty foreach statement.
     }
     $this->assertEquals(wfWikiId(), $doc->wiki);
     $this->assertEquals('en', $doc->language);
     $this->assertEquals('1', $doc->content);
     $this->assertEquals(array('ttmserver-test'), $doc->group);
     // Add one translation
     $title = Title::newFromText('MediaWiki:one/fi');
     $page = WikiPage::factory($title);
     $content = ContentHandler::makeContent('yksi', $title);
     $page->doEditContent($content, __METHOD__);
     $select = $solarium->createSelect();
     $select->setRows(1);
     $select->setQuery('language:fi');
     $result = $solarium->select($select);
     $this->assertEquals(1, $result->getNumFound());
     $doc = null;
     foreach ($result as $doc) {
         // @todo FIXME Empty foreach statement.
     }
     $this->assertEquals('yksi', $doc->content);
     $this->assertEquals(array('ttmserver-test'), $doc->group);
     // Update definition
     $title = Title::newFromText('MediaWiki:one/en');
     $page = WikiPage::factory($title);
     $content = ContentHandler::makeContent('1-updated', $title);
     $page->doEditContent($content, __METHOD__);
     $select = $solarium->createSelect();
     $select->setQuery('language:en');
     $result = $solarium->select($select);
     $this->assertEquals(2, $result->getNumFound(), 'Old and new definition exists');
     // Translation is fuzzied
     $title = Title::newFromText('MediaWiki:one/fi');
     $page = WikiPage::factory($title);
     $content = ContentHandler::makeContent('!!FUZZY!!yksi', $title);
     $page->doEditContent($content, __METHOD__);
     $select = $solarium->createSelect();
     $select->setQuery('language:fi');
     $result = $solarium->select($select);
     $this->assertEquals(0, $result->getNumFound());
     // Translation is udpated
     $title = Title::newFromText('MediaWiki:one/fi');
     $page = WikiPage::factory($title);
     $content = ContentHandler::makeContent('yksi-päiv', $title);
     $page->doEditContent($content, __METHOD__);
     $select = $solarium->createSelect();
     $select->setQuery('language:fi');
     $result = $solarium->select($select);
     $this->assertEquals(1, $result->getNumFound());
     $doc = null;
     foreach ($result as $doc) {
         // @todo FIXME Empty foreach statement.
     }
     $this->assertEquals('yksi-päiv', $doc->content);
     // And now the messages should be orphaned
     global $wgHooks;
     $wgHooks['TranslatePostInitGroups'] = array();
     MessageGroups::singleton()->recache();
     MessageIndex::singleton()->rebuild();
     self::runJobs();
     $select = $solarium->createSelect();
     $select->setQuery('*:*');
     $result = $solarium->select($select);
     $this->assertEquals(2, $result->getNumFound(), 'One definition and one translation exists');
     foreach ($result as $doc) {
         $this->assertEquals(null, $doc->group, 'Messages are orphaned');
     }
     // And message deletion
     $title = Title::newFromText('MediaWiki:one/fi');
     $page = WikiPage::factory($title);
     $page->doDeleteArticle(__METHOD__);
     $select = $solarium->createSelect();
     $select->setQuery('language:fi');
     $result = $solarium->select($select);
     $this->assertEquals(0, $result->getNumFound());
 }
 /**
  * Returns all message group ids this message belongs to.
  * The primary message group id is always the first one.
  * If the handle does not correspond to any message, the returned array
  * is empty.
  * @return string[]
  */
 public function getGroupIds()
 {
     if ($this->groupIds === null) {
         $this->groupIds = MessageIndex::singleton()->getGroupIds($this);
     }
     return $this->groupIds;
 }
 /**
  * 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
     if (method_exists($group, 'getKeys')) {
         $keys = $group->getKeys();
     } else {
         $keys = array_keys($group->getDefinitions());
     }
     // 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());
 }
 /**
  * Override the global instance, for testing.
  * @since 2015.04
  */
 public static function setInstance(self $instance)
 {
     self::$instance = $instance;
 }
 /**
  * @param string $key Message key
  * @param string $code Language code
  * @return null|string
  */
 public function getMessage($key, $code)
 {
     /* Just hand over the message content retrieval to the primary message
      * group directly. This used to iterate over the subgroups looking for
      * the primary group, but that might actually be under some other
      * aggregate message group.
      * @todo Implement getMessageContent to avoid hardcoding the namespace
      * here.
      */
     $title = Title::makeTitle($this->getNamespace(), $key);
     $handle = new MessageHandle($title);
     $groupId = MessageIndex::getPrimaryGroupId($handle);
     if ($groupId === $this->getId()) {
         // Message key owned by aggregate group.
         // Should not ever happen, but it does.
         error_log("AggregateMessageGroup {$groupId} cannot be primary owner of key {$key}");
         return null;
     }
     $group = MessageGroups::getGroup($groupId);
     if ($group) {
         return $group->getMessage($key, $code);
     } else {
         return null;
     }
 }
Example #19
0
 /**
  * Ajax-enabled message editing link.
  * @param $target Title: Title of the target message.
  * @param $text String: Link text for Linker::link()
  * @return link
  */
 public static function ajaxEditLink($target, $text)
 {
     $handle = new MessageHandle($target);
     $groupId = MessageIndex::getPrimaryGroupId($handle);
     $params = array();
     $params['action'] = 'edit';
     $params['loadgroup'] = $groupId;
     $jsEdit = TranslationEditPage::jsEdit($target, $groupId);
     $linker = class_exists('DummyLinker') ? new DummyLinker() : new Linker();
     return $linker->link($target, $text, $jsEdit, $params);
 }
 function run()
 {
     MessageIndex::singleton()->rebuild();
     return true;
 }
 /**
  * Returns the all the groups message belongs to.
  * @param int $namespace
  * @param string $key
  * @return string[] Possibly empty list of group ids.
  */
 public static function messageKeyToGroups($namespace, $key)
 {
     $mi = MessageIndex::singleton()->retrieve();
     $normkey = self::normaliseKey($namespace, $key);
     if (isset($mi[$normkey])) {
         return (array) $mi[$normkey];
     } else {
         return array();
     }
 }
 public function execute()
 {
     MessageGroups::singleton()->recache();
     MessageIndex::singleton()->rebuild();
 }