Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
48.39% covered (danger)
48.39%
15 / 31
25.00% covered (danger)
25.00%
1 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
Hooks
48.39% covered (danger)
48.39%
15 / 31
25.00% covered (danger)
25.00%
1 / 4
16.80
0.00% covered (danger)
0.00%
0 / 1
 onRecentChangesPurgeRows
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 onBeforePageDisplay
91.67% covered (success)
91.67%
11 / 12
0.00% covered (danger)
0.00%
0 / 1
4.01
 getFrontendConfiguration
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
2
 onRecentChange_save
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
2
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 MediaWiki\Hook\BeforePageDisplayHook;
20use MediaWiki\Hook\RecentChange_saveHook;
21use MediaWiki\Hook\RecentChangesPurgeRowsHook;
22use MediaWiki\Logger\LoggerFactory;
23use MediaWiki\Output\OutputPage;
24use ORES\ORESService;
25use ORES\Services\ORESServices;
26use RecentChange;
27use RequestContext;
28use Skin;
29
30class Hooks implements
31    BeforePageDisplayHook,
32    RecentChangesPurgeRowsHook,
33    RecentChange_saveHook
34{
35
36    /**
37     * Remove cached scores for revisions which were purged from recentchanges
38     *
39     * @param \stdClass[] $rows
40     */
41    public function onRecentChangesPurgeRows( $rows ): void {
42        $revIds = [];
43        foreach ( $rows as $row ) {
44            $revIds[] = $row->rc_this_oldid;
45        }
46        ORESServices::getScoreStorage()->purgeRows( $revIds );
47    }
48
49    /**
50     * Add CSS styles to output page
51     *
52     * @param OutputPage $out
53     * @param Skin $skin
54     */
55    public function onBeforePageDisplay( $out, $skin ): void {
56        if ( !Helpers::oresUiEnabled() ) {
57            return;
58        }
59
60        $oresData = $out->getProperty( 'oresData' );
61
62        if ( $oresData !== null ) {
63            $out->addJsConfigVars( 'oresData', $oresData );
64            $out->addJsConfigVars(
65                'oresThresholds',
66                [ 'damaging' => Helpers::getDamagingThresholds() ]
67            );
68            $out->addModuleStyles( 'ext.ores.styles' );
69            if ( Helpers::isHighlightEnabled( $out ) ) {
70                $out->addModules( 'ext.ores.highlighter' );
71            }
72        }
73    }
74
75    /**
76     * Returns an array of configuration for ORES API modules
77     *
78     * @return string[]
79     */
80    public static function getFrontendConfiguration() {
81        return [
82            'wikiId' => ORESService::getWikiID(),
83            'baseUrl' => ORESService::getFrontendBaseUrl(),
84            'apiVersion' => (string)ORESService::API_VERSION,
85        ];
86    }
87
88    /**
89     * @param RecentChange $rc
90     */
91    public function onRecentChange_save( $rc ) {
92        global $wgOresExcludeBots, $wgOresEnabledNamespaces, $wgOresModels;
93
94        $handler = new RecentChangeSaveHookHandler(
95            LoggerFactory::getInstance( 'ORES' ),
96            RequestContext::getMain()->getRequest()
97        );
98
99        $handler->handle(
100            $rc,
101            $wgOresModels,
102            $wgOresExcludeBots,
103            $wgOresEnabledNamespaces
104        );
105    }
106
107}