Translate extension for MediaWiki
 
Loading...
Searching...
No Matches
JsSelectToInput.php
1<?php
2declare( strict_types=1 );
3
4namespace MediaWiki\Extension\Translate\Utilities;
5
6use MWException;
7use RequestContext;
8use Xml;
9use XmlSelect;
10
18 protected $targetId;
20 protected $sourceId;
22 protected $select;
24 protected $buttonId;
26 protected $msg = 'translate-jssti-add';
27
28 public function __construct( XmlSelect $select = null ) {
29 $this->select = $select;
30 }
31
32 public function getSourceId(): string {
33 return $this->sourceId;
34 }
35
36 public function setTargetId( string $id ) {
37 $this->targetId = $id;
38 }
39
40 public function getTargetId(): string {
41 return $this->targetId;
42 }
43
45 public function setMessage( string $message ): void {
46 $this->msg = $message;
47 }
48
50 public function getMessage(): string {
51 return $this->msg;
52 }
53
59 public function getHtmlAndPrepareJS(): string {
60 $this->sourceId = $this->select->getAttribute( 'id' );
61
62 if ( !is_string( $this->sourceId ) ) {
63 throw new MWException( 'ID needs to be specified for the selector' );
64 }
65
66 self::injectJs();
67 $html = $this->select->getHTML();
68 $html .= $this->getButton( $this->msg, $this->sourceId, $this->targetId );
69
70 return $html;
71 }
72
80 protected function getButton( string $msg, string $source, string $target ): string {
81 $html = Xml::element( 'input', [
82 'type' => 'button',
83 'value' => wfMessage( $msg )->text(),
84 'class' => 'mw-translate-jssti',
85 'data-translate-jssti-sourceid' => $source,
86 'data-translate-jssti-targetid' => $target
87 ] );
88
89 return $html;
90 }
91
93 public static function injectJs(): void {
94 static $done = false;
95 if ( $done ) {
96 return;
97 }
98
99 RequestContext::getMain()->getOutput()->addModules( 'ext.translate.selecttoinput' );
100 }
101}
return[ 'Translate:ConfigHelper'=> static function():ConfigHelper { return new ConfigHelper();}, 'Translate:CsvTranslationImporter'=> static function(MediaWikiServices $services):CsvTranslationImporter { return new CsvTranslationImporter( $services->getWikiPageFactory());}, 'Translate:EntitySearch'=> static function(MediaWikiServices $services):EntitySearch { return new EntitySearch($services->getMainWANObjectCache(), $services->getCollationFactory() ->makeCollation( 'uca-default-u-kn'), MessageGroups::singleton(), $services->getNamespaceInfo(), $services->get( 'Translate:MessageIndex'), $services->getTitleParser(), $services->getTitleFormatter());}, 'Translate:ExternalMessageSourceStateImporter'=> static function(MediaWikiServices $services):ExternalMessageSourceStateImporter { return new ExternalMessageSourceStateImporter($services->getMainConfig(), $services->get( 'Translate:GroupSynchronizationCache'), $services->getJobQueueGroup(), LoggerFactory::getInstance( 'Translate.GroupSynchronization'), MessageIndex::singleton());}, 'Translate:GroupSynchronizationCache'=> static function(MediaWikiServices $services):GroupSynchronizationCache { return new GroupSynchronizationCache( $services->get( 'Translate:PersistentCache'));}, 'Translate:MessageBundleStore'=> static function(MediaWikiServices $services):MessageBundleStore { return new MessageBundleStore(new RevTagStore(), $services->getJobQueueGroup(), $services->getLanguageNameUtils(), $services->get( 'Translate:MessageIndex'));}, 'Translate:MessageGroupReview'=> static function(MediaWikiServices $services):MessageGroupReview { return new MessageGroupReview($services->getDBLoadBalancer(), $services->getHookContainer());}, 'Translate:MessageIndex'=> static function(MediaWikiServices $services):MessageIndex { $params=$services->getMainConfig() ->get( 'TranslateMessageIndex');if(is_string( $params)) { $params=(array) $params;} $class=array_shift( $params);return new $class( $params);}, 'Translate:ParsingPlaceholderFactory'=> static function():ParsingPlaceholderFactory { return new ParsingPlaceholderFactory();}, 'Translate:PersistentCache'=> static function(MediaWikiServices $services):PersistentCache { return new PersistentDatabaseCache($services->getDBLoadBalancer(), $services->getJsonCodec());}, 'Translate:ProgressStatsTableFactory'=> static function(MediaWikiServices $services):ProgressStatsTableFactory { return new ProgressStatsTableFactory($services->getLinkRenderer(), $services->get( 'Translate:ConfigHelper'));}, 'Translate:SubpageListBuilder'=> static function(MediaWikiServices $services):SubpageListBuilder { return new SubpageListBuilder($services->get( 'Translate:TranslatableBundleFactory'), $services->getLinkBatchFactory());}, 'Translate:TranslatableBundleFactory'=> static function(MediaWikiServices $services):TranslatableBundleFactory { return new TranslatableBundleFactory($services->get( 'Translate:TranslatablePageStore'), $services->get( 'Translate:MessageBundleStore'));}, 'Translate:TranslatableBundleMover'=> static function(MediaWikiServices $services):TranslatableBundleMover { return new TranslatableBundleMover($services->getMovePageFactory(), $services->getJobQueueGroup(), $services->getLinkBatchFactory(), $services->get( 'Translate:TranslatableBundleFactory'), $services->get( 'Translate:SubpageListBuilder'), $services->getMainConfig() ->get( 'TranslatePageMoveLimit'));}, 'Translate:TranslatablePageParser'=> static function(MediaWikiServices $services):TranslatablePageParser { return new TranslatablePageParser($services->get( 'Translate:ParsingPlaceholderFactory'));}, 'Translate:TranslatablePageStore'=> static function(MediaWikiServices $services):TranslatablePageStore { return new TranslatablePageStore($services->get( 'Translate:MessageIndex'), $services->getJobQueueGroup(), new RevTagStore(), $services->getDBLoadBalancer());}, 'Translate:TranslationStashReader'=> static function(MediaWikiServices $services):TranslationStashReader { $db=$services->getDBLoadBalancer() ->getConnectionRef(DB_REPLICA);return new TranslationStashStorage( $db);}, 'Translate:TranslationStatsDataProvider'=> static function(MediaWikiServices $services):TranslationStatsDataProvider { return new TranslationStatsDataProvider(new ServiceOptions(TranslationStatsDataProvider::CONSTRUCTOR_OPTIONS, $services->getMainConfig()), $services->getObjectFactory());}, 'Translate:TranslationUnitStoreFactory'=> static function(MediaWikiServices $services):TranslationUnitStoreFactory { return new TranslationUnitStoreFactory( $services->getDBLoadBalancer());}, 'Translate:TranslatorActivity'=> static function(MediaWikiServices $services):TranslatorActivity { $query=new TranslatorActivityQuery($services->getMainConfig(), $services->getDBLoadBalancer());return new TranslatorActivity($services->getMainObjectStash(), $query, $services->getJobQueueGroup());}, 'Translate:TtmServerFactory'=> static function(MediaWikiServices $services):TtmServerFactory { $config=$services->getMainConfig();$default=$config->get( 'TranslateTranslationDefaultService');if( $default===false) { $default=null;} return new TtmServerFactory( $config->get( 'TranslateTranslationServices'), $default);}]
@phpcs-require-sorted-array
Code for JavaScript enhanced <option> selectors.
getButton(string $msg, string $source, string $target)
Constructs the append button.
$targetId
Id of the text field where stuff is appended.
getHtmlAndPrepareJS()
Returns the whole input element and injects needed JavaScript.
static injectJs()
Inject needed JavaScript in the page.