Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 42
0.00% covered (danger)
0.00%
0 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
PageTranslationHookHandler
0.00% covered (danger)
0.00%
0 / 42
0.00% covered (danger)
0.00%
0 / 5
90
0.00% covered (danger)
0.00%
0 / 1
 onSpecialPrefixIndexGetFormFilters
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
2
 onSpecialPrefixIndexQuery
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
6
 onLonelyPagesQuery
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
2
 onSpecialPageBeforeFormDisplay
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
6
 onSpecialWhatLinksHereQuery
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 1
12
1<?php
2declare( strict_types = 1 );
3
4namespace MediaWiki\Extension\Translate\PageTranslation;
5
6use MediaWiki\Context\IContextSource;
7use MediaWiki\Hook\LonelyPagesQueryHook;
8use MediaWiki\Hook\SpecialPrefixIndexGetFormFiltersHook;
9use MediaWiki\Hook\SpecialPrefixIndexQueryHook;
10use MediaWiki\Hook\SpecialWhatLinksHereQueryHook;
11use MediaWiki\HTMLForm\Field\HTMLCheckField;
12use MediaWiki\SpecialPage\Hook\SpecialPageBeforeFormDisplayHook;
13use Wikimedia\Rdbms\SelectQueryBuilder;
14
15class PageTranslationHookHandler implements
16    SpecialPrefixIndexGetFormFiltersHook,
17    SpecialPrefixIndexQueryHook,
18    LonelyPagesQueryHook,
19    SpecialPageBeforeFormDisplayHook,
20    SpecialWhatLinksHereQueryHook
21{
22
23    public function onSpecialPrefixIndexGetFormFilters( IContextSource $contextSource, array &$filters ) {
24        $filters[ 'translate-hidetranslations' ] = [
25            'class' => HTMLCheckField::class,
26            'name' => 'translate-hidetranslations',
27            'label-message' => 'translate-hidetranslations',
28        ];
29    }
30
31    public function onSpecialPrefixIndexQuery( array $fieldData, SelectQueryBuilder $queryBuilder ) {
32        if ( $fieldData[ 'translate-hidetranslations' ] === true ) {
33            $queryBuilder->leftJoin(
34                'page_props',
35                'translate_pp',
36                [
37                    'translate_pp.pp_page=page_id',
38                    'translate_pp.pp_propname' => 'translate-is-translation'
39                ]
40            )->andWhere( [ 'translate_pp.pp_value' => null ] );
41        }
42    }
43
44    public function onLonelyPagesQuery( &$tables, &$conds, &$joinConds ) {
45        $tables[ 'translate_pp' ] = 'page_props';
46        $joinConds['translate_pp'] = [
47            'LEFT JOIN', [
48                'translate_pp.pp_page=page_id',
49                'translate_pp.pp_propname' => 'translate-is-translation'
50            ]
51        ];
52        $conds['translate_pp.pp_value'] = null;
53    }
54
55    public function onSpecialPageBeforeFormDisplay( $name, $form ): void {
56        if ( $name === 'Whatlinkshere' ) {
57            $form->addFields( [
58                'translate-hidetranslations' => [
59                    'type' => 'check',
60                    'name' => 'translate-hidetranslations',
61                    'label-message' => 'translate-hidetranslations',
62                    'section' => 'whatlinkshere-filter',
63                ]
64            ] );
65        }
66    }
67
68    public function onSpecialWhatLinksHereQuery( $table, $data, $queryBuilder ) {
69        $isSupportedTable = in_array( $table, [ 'pagelinks', 'templatelinks', 'imagelinks' ] );
70
71        if ( $data[ 'translate-hidetranslations' ] && $isSupportedTable ) {
72            $queryBuilder->leftJoin(
73                'page_props',
74                'translate_pp',
75                [
76                    'translate_pp.pp_page=page_id',
77                    'translate_pp.pp_propname' => 'translate-is-translation',
78                ]
79            )
80            ->andWhere( [ 'translate_pp.pp_value' => null ] );
81        }
82    }
83}