Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
51.22% covered (warning)
51.22%
21 / 41
0.00% covered (danger)
0.00%
0 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
ContributionsHooksHandler
51.22% covered (warning)
51.22%
21 / 41
0.00% covered (danger)
0.00%
0 / 5
78.18
0.00% covered (danger)
0.00%
0 / 1
 onContribsPager__getQueryInfo
87.50% covered (warning)
87.50%
7 / 8
0.00% covered (danger)
0.00%
0 / 1
3.02
 onSpecialContributions__formatRow__flags
77.78% covered (warning)
77.78%
7 / 9
0.00% covered (danger)
0.00%
0 / 1
6.40
 onContributionsLineEnding
77.78% covered (warning)
77.78%
7 / 9
0.00% covered (danger)
0.00%
0 / 1
7.54
 onSpecialContributions__getForm__filters
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
12
 hideNonDamagingPreference
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
12
1<?php
2/**
3 * This program is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, either version 3 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
15 */
16
17namespace ORES\Hooks;
18
19use ChangesList;
20use IContextSource;
21use MediaWiki\Hook\ContribsPager__getQueryInfoHook;
22use MediaWiki\Hook\ContributionsLineEndingHook;
23use MediaWiki\Hook\SpecialContributions__formatRow__flagsHook;
24use MediaWiki\Hook\SpecialContributions__getForm__filtersHook;
25use MediaWiki\MediaWikiServices;
26use MediaWiki\Pager\ContribsPager;
27use MediaWiki\Specials\SpecialContributions;
28
29class ContributionsHooksHandler implements
30    ContribsPager__getQueryInfoHook,
31    SpecialContributions__formatRow__flagsHook,
32    ContributionsLineEndingHook,
33    SpecialContributions__getForm__filtersHook
34{
35
36    /**
37     * Filter out non-damaging changes from Special:Contributions
38     *
39     * @param ContribsPager $pager
40     * @param array &$query
41     */
42    public function onContribsPager__getQueryInfo(
43        $pager,
44        &$query
45    ) {
46        if ( !Helpers::oresUiEnabled() ) {
47            return;
48        }
49
50        if ( Helpers::isModelEnabled( 'damaging' ) ) {
51            Helpers::joinWithOresTables( 'damaging', 'rev_id', $query['tables'], $query['fields'],
52                $query['join_conds'] );
53
54            Helpers::hideNonDamagingFilter( $query['fields'], $query['conds'],
55                self::hideNonDamagingPreference( $pager->getContext() ), $pager->getUser(),
56                $pager->getTitle() );
57        }
58    }
59
60    public function onSpecialContributions__formatRow__flags(
61        $context,
62        $row,
63        &$flags
64    ) {
65        if ( !Helpers::oresUiEnabled() ) {
66            return;
67        }
68
69        // Doesn't have ores score, skipping.
70        if (
71            !isset( $row->ores_damaging_score ) ||
72            !isset( $row->ores_damaging_threshold )
73        ) {
74            return;
75        }
76
77        Helpers::addRowData( $context, $row->rev_id, (float)$row->ores_damaging_score, 'damaging' );
78
79        if (
80            Helpers::isDamagingFlagEnabled( $context ) &&
81            $row->ores_damaging_score > $row->ores_damaging_threshold
82        ) {
83            // Prepend the "r" flag
84            array_unshift( $flags, ChangesList::flag( 'damaging' ) );
85        }
86    }
87
88    public function onContributionsLineEnding(
89        $pager,
90        &$ret,
91        $row,
92        &$classes,
93        &$attribs
94    ) {
95        if ( !Helpers::oresUiEnabled() ) {
96            return;
97        }
98
99        // Doesn't have ores score or threshold is not set properly, skipping.
100        if ( !isset( $row->ores_damaging_score ) || !isset( $row->ores_damaging_threshold ) ) {
101            return;
102        }
103
104        if ( $row->ores_damaging_score > $row->ores_damaging_threshold ) {
105            if ( Helpers::isHighlightEnabled( $pager ) ) {
106                $classes[] = 'ores-highlight';
107            }
108            if ( Helpers::isDamagingFlagEnabled( $pager ) ) {
109                $classes[] = 'damaging';
110            }
111        }
112    }
113
114    /**
115     * Hook into Special:Contributions filters
116     *
117     * @param SpecialContributions $page
118     * @param array[] &$filters HTML
119     */
120    public function onSpecialContributions__getForm__filters(
121        $page,
122        &$filters
123    ) {
124        if ( !Helpers::oresUiEnabled() || !Helpers::isModelEnabled( 'damaging' ) ) {
125            return;
126        }
127
128        $filters[] = [
129            'type' => 'check',
130            'id' => 'ores-hide-nondamaging',
131            'label' => $page->msg( 'ores-hide-nondamaging-filter' )->text(),
132            'name' => 'hidenondamaging',
133            'default' => self::hideNonDamagingPreference( $page->getContext() ),
134        ];
135    }
136
137    /**
138     * Get user preference for hiding non-damaging edits.
139     * - If form is submitted: filter is enabled if the hidenondamaging is set, disabled otherwise.
140     * - If Contributions page is opened regularly: filter is enabled if the parameter is set or
141     * the preference is enabled, disabled otherwise.
142     *
143     * @param IContextSource $context
144     * @return bool True if non damaging preference should be enabled
145     */
146    private static function hideNonDamagingPreference( IContextSource $context ) {
147        $checkbox = $context->getRequest()->getBool( 'hidenondamaging' );
148        $userOptionsLookup = MediaWikiServices::getInstance()->getUserOptionsLookup();
149        $preference = $userOptionsLookup->getOption( $context->getUser(), 'oresRCHideNonDamaging' );
150
151        // Unchecked options aren't submitted with HTML form, so we have hidenondamaging=1 or null.
152        // To distinguish when form on Special:Contributions is submitted, we check for
153        // hidden parameter on the Special:Contributions form, with name 'limit'.
154        // Watchlist special page defines similar hidden input field, called 'action'
155        // which is used in the same fashion as we are using 'limit' here.
156        if ( $context->getRequest()->getBool( 'limit' ) ) {
157            return $checkbox;
158        }
159
160        return $preference || $checkbox;
161    }
162
163}