Translate extension for MediaWiki
 
Loading...
Searching...
No Matches
CleanChangesFilters.php
1<?php
2declare( strict_types = 1 );
3
4namespace MediaWiki\Extension\Translate\TranslatorInterface;
5
6use MediaWiki\Config\Config;
7use MediaWiki\Extension\CLDR\LanguageNames;
8use MediaWiki\Hook\FetchChangesListHook;
9use MediaWiki\Hook\SpecialRecentChangesPanelHook;
10use MediaWiki\Html\Html;
11use MediaWiki\Languages\LanguageNameUtils;
12use MediaWiki\SpecialPage\Hook\ChangesListSpecialPageQueryHook;
13use MediaWiki\Xml\Xml;
14use Wikimedia\Rdbms\ILoadBalancer;
15
22class CleanChangesFilters implements
23 FetchChangesListHook,
24 ChangesListSpecialPageQueryHook,
25 SpecialRecentChangesPanelHook
26{
27 private Config $config;
28 private LanguageNameUtils $languageNameUtils;
29 private ILoadBalancer $loadBalancer;
30
31 public function __construct( LanguageNameUtils $languageNameUtils, ILoadBalancer $loadBalancer, Config $config ) {
32 $this->languageNameUtils = $languageNameUtils;
33 $this->loadBalancer = $loadBalancer;
34 $this->config = $config;
35 }
36
42 $name, &$tables, &$fields, &$conds, &$query_options, &$join_conds, $opts
43 ) {
44 global $wgRequest;
45 if ( !$this->config->get( 'TranslateRecentChangesLanguageFilter' ) ) {
46 return;
47 }
48
49 $opts->add( 'trailer', '' );
50 $trailer = $wgRequest->getVal( 'trailer', '' );
51 if ( $trailer === null ) {
52 return;
53 }
54
55 $dbr = $this->loadBalancer->getConnection( DB_REPLICA );
56 $conds[] = 'rc_title ' . $dbr->buildLike( $dbr->anyString(), $trailer );
57 $opts->setValue( 'trailer', $trailer );
58 }
59
64 public function onSpecialRecentChangesPanel( &$extraOpts, $opts ) {
65 global $wgLang, $wgRequest;
66 if ( !$this->config->get( 'TranslateRecentChangesLanguageFilter' ) ) {
67 return;
68 }
69
70 // TODO the query is parsed (and unknown options are discarded) before we got a chance to define our option.
71 // SEE https://gerrit.wikimedia.org/r/c/mediawiki/extensions/Translate/+/1053288/comment/5303b6a3_aaef9399/
72 $default = $wgRequest->getVal( 'trailer', '' );
73
74 if ( is_callable( [ LanguageNames::class, 'getNames' ] ) ) {
75 // cldr extension
76 $languages = LanguageNames::getNames(
77 $wgLang->getCode(),
78 LanguageNames::FALLBACK_NORMAL
79 );
80 } else {
81 $languages = $this->languageNameUtils->getLanguageNames();
82 }
83 ksort( $languages );
84 $optionAttributes = [ 'value' => '' ];
85 if ( $default === '' ) {
86 $optionAttributes[ 'selected' ] = 'selected';
87 }
88
89 $options = Html::element(
90 'option',
91 $optionAttributes,
92 wfMessage( 'tpt-cleanchanges-language-na' )->text()
93 );
94
95 foreach ( $languages as $code => $name ) {
96 $selected = ( "/$code" === $default );
97 $optionAttributes = [ 'value' => "/$code" ];
98 if ( $selected ) {
99 $optionAttributes[ 'selected' ] = 'selected';
100 }
101 $options .= Html::element( 'option', $optionAttributes, "$code - $name" ) . "\n";
102 }
103 $str =
104 Xml::openElement( 'select',
105 [
106 'name' => 'trailer',
107 'class' => 'mw-language-selector',
108 'id' => 'tpt-rc-language',
109 ] ) .
110 $options .
111 Xml::closeElement( 'select' );
112
113 $extraOpts['tailer'] = [ wfMessage( 'tpt-cleanchanges-language' )->escaped(), $str ];
114 }
115
120 public function onFetchChangesList( $user, $skin, &$list, $groups ) {
121 if ( $this->config->get( 'TranslateRecentChangesLanguageFilter' ) && defined( 'ULS_VERSION' ) ) {
122 $skin->getOutput()->addModules( 'ext.translate.cleanchanges' );
123 }
124 }
125}
This class adds a language filter to Special:RecentChanges.
onChangesListSpecialPageQuery( $name, &$tables, &$fields, &$conds, &$query_options, &$join_conds, $opts)
Hook: ChangesListSpecialPageQuery @inheritDoc.
onSpecialRecentChangesPanel(&$extraOpts, $opts)
Hook: SpecialRecentChangesPanel @inheritDoc.
onFetchChangesList( $user, $skin, &$list, $groups)
Hook: FetchChangesList @inheritDoc.