summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/Translate/utils/RcFilter.php
diff options
context:
space:
mode:
Diffstat (limited to 'www/wiki/extensions/Translate/utils/RcFilter.php')
-rw-r--r--www/wiki/extensions/Translate/utils/RcFilter.php191
1 files changed, 172 insertions, 19 deletions
diff --git a/www/wiki/extensions/Translate/utils/RcFilter.php b/www/wiki/extensions/Translate/utils/RcFilter.php
index dd2d7f82..7c2334f2 100644
--- a/www/wiki/extensions/Translate/utils/RcFilter.php
+++ b/www/wiki/extensions/Translate/utils/RcFilter.php
@@ -6,7 +6,7 @@
* @file
* @author Niklas Laxström
* @copyright Copyright © 2010, Niklas Laxström
- * @license GPL-2.0+
+ * @license GPL-2.0-or-later
*/
/**
@@ -20,20 +20,20 @@ class TranslateRcFilter {
*
* Appends SQL filter conditions into $conds.
* @param string $pageName
- * @param array $tables
- * @param array $fields
- * @param array $conds
- * @param array $query_options
- * @param array $join_conds
+ * @param array &$tables
+ * @param array &$fields
+ * @param array &$conds
+ * @param array &$query_options
+ * @param array &$join_conds
* @param FormOptions $opts
* @return bool true
*/
- public static function translationFilter( $pageName, &$tables, &$fields, &$conds, &$query_options,
- &$join_conds, FormOptions $opts
+ public static function translationFilter( $pageName, &$tables, &$fields, &$conds,
+ &$query_options, &$join_conds, FormOptions $opts
) {
- global $wgTranslateMessageNamespaces, $wgTranslateRcFilterDefault;
+ global $wgTranslateRcFilterDefault;
- if ( $pageName !== 'Recentchanges' ) {
+ if ( $pageName !== 'Recentchanges' || self::isStructuredFilterUiEnabled() ) {
return true;
}
@@ -42,14 +42,9 @@ class TranslateRcFilter {
$opts->add( 'translations', $wgTranslateRcFilterDefault );
$opts->setValue( 'translations', $translations );
- $dbr = wfGetDB( DB_SLAVE );
+ $dbr = wfGetDB( DB_REPLICA );
- $namespaces = array();
-
- foreach ( $wgTranslateMessageNamespaces as $index ) {
- $namespaces[] = $index;
- $namespaces[] = $index + 1; // Talk too
- }
+ $namespaces = self::getTranslateNamespaces();
if ( $translations === 'only' ) {
$conds[] = 'rc_namespace IN (' . $dbr->makeList( $namespaces ) . ')';
@@ -64,16 +59,32 @@ class TranslateRcFilter {
return true;
}
+ private static function getTranslateNamespaces() {
+ global $wgTranslateMessageNamespaces;
+ $namespaces = [];
+
+ foreach ( $wgTranslateMessageNamespaces as $index ) {
+ $namespaces[] = $index;
+ $namespaces[] = $index + 1; // Include Talk namespaces
+ }
+
+ return $namespaces;
+ }
+
/**
* Hooks SpecialRecentChangesPanel. See the hook documentation for
* documentation of the function parameters.
*
* Adds a HTMl selector into $items
- * @param $items
+ * @param array &$items
* @param FormOptions $opts
* @return bool true
*/
public static function translationFilterForm( &$items, $opts ) {
+ if ( self::isStructuredFilterUiEnabled() ) {
+ return true;
+ }
+
$opts->consumeValue( 'translations' );
$default = $opts->getValue( 'translations' );
@@ -93,8 +104,150 @@ class TranslateRcFilter {
);
$select->addOption( wfMessage( 'translate-rc-translation-filter-site' )->text(), 'site' );
- $items['translations'] = array( $label, $select->getHTML() );
+ $items['translations'] = [ $label, $select->getHTML() ];
+
+ return true;
+ }
+
+ private static function isStructuredFilterUiEnabled() {
+ $context = RequestContext::getMain();
+
+ // This assumes usage only on RC page
+ $page = new SpecialRecentChanges();
+ $page->setContext( $context );
+
+ // isStructuredFilterUiEnabled used to be a protected method in older versions :(
+ return is_callable( [ $page, 'isStructuredFilterUiEnabled' ] ) &&
+ $page->isStructuredFilterUiEnabled();
+ }
+
+ /**
+ * Hooks ChangesListSpecialPageStructuredFilters. See the hook documentation for
+ * documentation of the function parameters.
+ *
+ * Adds translations filters to structured UI
+ * @param ChangesListSpecialPage $special
+ * @return bool true
+ */
+ public static function onChangesListSpecialPageStructuredFilters(
+ ChangesListSpecialPage $special
+ ) {
+ global $wgTranslateRcFilterDefault;
+ $defaultFilter = $wgTranslateRcFilterDefault !== 'noaction' ?
+ $wgTranslateRcFilterDefault :
+ ChangesListStringOptionsFilterGroup::NONE;
+
+ $translationsGroup = new ChangesListStringOptionsFilterGroup(
+ [
+ 'name' => 'translations',
+ 'title' => 'translate-rcfilters-translations',
+ 'priority' => -7,
+ 'default' => $defaultFilter,
+ 'isFullCoverage' => true,
+ 'filters' => [
+ [
+ 'name' => 'only',
+ 'label' => 'translate-rcfilters-translations-only-label',
+ 'description' => 'translate-rcfilters-translations-only-desc',
+ 'cssClassSuffix' => 'only',
+ 'isRowApplicableCallable' => function ( $ctx, $rc ) {
+ $namespaces = self::getTranslateNamespaces();
+
+ return in_array( $rc->getAttribute( 'rc_namespace' ), $namespaces ) &&
+ strpos( $rc->getAttribute( 'rc_title' ), '/' ) !== false;
+ }
+ ],
+ [
+ 'name' => 'site',
+ 'label' => 'translate-rcfilters-translations-site-label',
+ 'description' => 'translate-rcfilters-translations-site-desc',
+ 'cssClassSuffix' => 'site',
+ 'isRowApplicableCallable' => function ( $ctx, $rc ) {
+ $namespaces = self::getTranslateNamespaces();
+
+ return in_array( $rc->getAttribute( 'rc_namespace' ), $namespaces ) &&
+ strpos( $rc->getAttribute( 'rc_title' ), '/' ) === false;
+ }
+ ],
+ [
+ 'name' => 'filter',
+ 'label' => 'translate-rcfilters-translations-filter-label',
+ 'description' => 'translate-rcfilters-translations-filter-desc',
+ 'cssClassSuffix' => 'filter',
+ 'isRowApplicableCallable' => function ( $ctx, $rc ) {
+ $namespaces = self::getTranslateNamespaces();
+
+ return !in_array( $rc->getAttribute( 'rc_namespace' ), $namespaces );
+ }
+ ],
+ ],
+ 'queryCallable' => function ( $specialClassName, $ctx, $dbr, &$tables,
+ &$fields, &$conds, &$query_options, &$join_conds, $selectedValues
+ ) {
+ $fields[] = 'rc_title';
+ $fields[] = 'rc_namespace';
+
+ $namespaces = self::getTranslateNamespaces();
+ $inNamespaceCond = 'rc_namespace IN (' .
+ $dbr->makeList( $namespaces ) . ')';
+ $notInNamespaceCond = 'rc_namespace NOT IN (' .
+ $dbr->makeList( $namespaces ) . ')';
+
+ $onlyCond = $dbr->makeList( [
+ $inNamespaceCond,
+ 'rc_title ' .
+ $dbr->buildLike( $dbr->anyString(), '/', $dbr->anyString() )
+ ], LIST_AND );
+ $siteCond = $dbr->makeList( [
+ $inNamespaceCond,
+ 'rc_title NOT' .
+ $dbr->buildLike( $dbr->anyString(), '/', $dbr->anyString() )
+ ], LIST_AND );
+
+ if ( count( $selectedValues ) === 3 ) {
+ // no filters
+ return;
+ }
+
+ if ( $selectedValues === [ 'filter', 'only' ] ) {
+ $conds[] = $dbr->makeList( [
+ $notInNamespaceCond,
+ $onlyCond
+ ], LIST_OR );
+ return;
+ }
+
+ if ( $selectedValues === [ 'filter', 'site' ] ) {
+ $conds[] = $dbr->makeList( [
+ $notInNamespaceCond,
+ $siteCond
+ ], LIST_OR );
+ return;
+ }
+
+ if ( $selectedValues === [ 'only', 'site' ] ) {
+ $conds[] = $inNamespaceCond;
+ return;
+ }
+
+ if ( $selectedValues === [ 'filter' ] ) {
+ $conds[] = $notInNamespaceCond;
+ return;
+ }
+
+ if ( $selectedValues === [ 'only' ] ) {
+ $conds[] = $onlyCond;
+ return;
+ }
+
+ if ( $selectedValues === [ 'site' ] ) {
+ $conds[] = $siteCond;
+ }
+ }
+ ]
+ );
+ $special->registerFilterGroup( $translationsGroup );
return true;
}
}