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 MediaWiki\Context\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;
26
27class ContributionsHooksHandler implements
28    ContribsPager__getQueryInfoHook,
29    SpecialContributions__formatRow__flagsHook,
30    ContributionsLineEndingHook,
31    SpecialContributions__getForm__filtersHook
32{
33
34    /**
35     * Filter out non-damaging changes from Special:Contributions
36     *
37     * @inheritDoc
38     */
39    public function onContribsPager__getQueryInfo(
40        $pager,
41        &$query
42    ) {
43        if ( !Helpers::oresUiEnabled() ) {
44            return;
45        }
46
47        if ( Helpers::isModelEnabled( 'damaging' ) ) {
48            Helpers::joinWithOresTables( 'damaging', 'rev_id', $query['tables'], $query['fields'],
49                $query['join_conds'] );
50
51            Helpers::hideNonDamagingFilter( $query['fields'], $query['conds'],
52                self::hideNonDamagingPreference( $pager->getContext() ), $pager->getUser(),
53                $pager->getTitle() );
54        }
55    }
56
57    /**
58     * @inheritDoc
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    /**
89     * @inheritDoc
90     */
91    public function onContributionsLineEnding(
92        $pager,
93        &$ret,
94        $row,
95        &$classes,
96        &$attribs
97    ) {
98        if ( !Helpers::oresUiEnabled() ) {
99            return;
100        }
101
102        // Doesn't have ores score or threshold is not set properly, skipping.
103        if ( !isset( $row->ores_damaging_score ) || !isset( $row->ores_damaging_threshold ) ) {
104            return;
105        }
106
107        if ( $row->ores_damaging_score > $row->ores_damaging_threshold ) {
108            if ( Helpers::isHighlightEnabled( $pager ) ) {
109                $classes[] = 'ores-highlight';
110            }
111            if ( Helpers::isDamagingFlagEnabled( $pager ) ) {
112                $classes[] = 'damaging';
113            }
114        }
115    }
116
117    /**
118     * Hook into Special:Contributions filters
119     *
120     * @inheritDoc
121     */
122    public function onSpecialContributions__getForm__filters(
123        $page,
124        &$filters
125    ) {
126        if ( !Helpers::oresUiEnabled() || !Helpers::isModelEnabled( 'damaging' ) ) {
127            return;
128        }
129
130        $filters[] = [
131            'type' => 'check',
132            'id' => 'ores-hide-nondamaging',
133            'label' => $page->msg( 'ores-hide-nondamaging-filter' )->text(),
134            'name' => 'hidenondamaging',
135            'default' => self::hideNonDamagingPreference( $page->getContext() ),
136        ];
137    }
138
139    /**
140     * Get user preference for hiding non-damaging edits.
141     * - If form is submitted: filter is enabled if the hidenondamaging is set, disabled otherwise.
142     * - If Contributions page is opened regularly: filter is enabled if the parameter is set or
143     * the preference is enabled, disabled otherwise.
144     *
145     * @param IContextSource $context
146     * @return bool True if non damaging preference should be enabled
147     */
148    private static function hideNonDamagingPreference( IContextSource $context ) {
149        $checkbox = $context->getRequest()->getBool( 'hidenondamaging' );
150        $userOptionsLookup = MediaWikiServices::getInstance()->getUserOptionsLookup();
151        $preference = $userOptionsLookup->getOption( $context->getUser(), 'oresRCHideNonDamaging' );
152
153        // Unchecked options aren't submitted with HTML form, so we have hidenondamaging=1 or null.
154        // To distinguish when form on Special:Contributions is submitted, we check for
155        // hidden parameter on the Special:Contributions form, with name 'limit'.
156        // Watchlist special page defines similar hidden input field, called 'action'
157        // which is used in the same fashion as we are using 'limit' here.
158        if ( $context->getRequest()->getBool( 'limit' ) ) {
159            return $checkbox;
160        }
161
162        return $preference || $checkbox;
163    }
164
165}