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