Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 41
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
TestWikiRC
0.00% covered (danger)
0.00%
0 / 41
0.00% covered (danger)
0.00%
0 / 3
110
0.00% covered (danger)
0.00%
0 / 1
 getValues
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
12
 onChangesListSpecialPageQuery
0.00% covered (danger)
0.00%
0 / 17
0.00% covered (danger)
0.00%
0 / 1
20
 onSpecialRecentChangesPanel
0.00% covered (danger)
0.00%
0 / 14
0.00% covered (danger)
0.00%
0 / 1
12
1<?php
2
3namespace MediaWiki\Extension\WikimediaIncubator;
4
5use MediaWiki\Hook\SpecialRecentChangesPanelHook;
6use MediaWiki\Html\FormOptions;
7use MediaWiki\MediaWikiServices;
8use MediaWiki\SpecialPage\Hook\ChangesListSpecialPageQueryHook;
9use MediaWiki\User\User;
10use RequestContext;
11use Xml;
12use XmlSelect;
13
14/**
15 * Recent changes for a specific test wiki, or for all project changes (or normal display)
16 *
17 * @file
18 * @ingroup Extensions
19 * @author Robin Pepermans (SPQRobin)
20 */
21
22class TestWikiRC implements
23    ChangesListSpecialPageQueryHook,
24    SpecialRecentChangesPanelHook
25{
26    /**
27     * Get values
28     *
29     * @param User $user
30     * @return array
31     */
32    private static function getValues( User $user ) {
33        global $wmincPref, $wgRequest;
34        $url = WikimediaIncubator::getUrlParam();
35        $userOptionsLookup = MediaWikiServices::getInstance()->getUserOptionsLookup();
36        $projectPref = $userOptionsLookup->getOption( $user, $wmincPref . '-project' ) ?? '';
37        $codePref = $userOptionsLookup->getOption( $user, $wmincPref . '-code' ) ?? '';
38        $projectvalue = $url ? $url['project'] : $projectPref;
39        $codevalue = $url ? $url['lang'] : $codePref;
40        $projectvalue = strtolower( $wgRequest->getVal( 'rc-testwiki-project', $projectvalue ) );
41        $codevalue = strtolower( $wgRequest->getVal( 'rc-testwiki-code', $codevalue ) );
42        return [ $projectvalue, $codevalue ];
43    }
44
45    /**
46     * ChangesListSpecialPageQuery hook
47     *
48     * @param string $pageName
49     * @param array &$tables
50     * @param array &$fields
51     * @param array &$conds
52     * @param array &$query_options
53     * @param array &$join_conds
54     * @param FormOptions $opts
55     */
56    public function onChangesListSpecialPageQuery( $pageName, &$tables, &$fields, &$conds, &$query_options,
57        &$join_conds, $opts
58    ) {
59        global $wmincProjectSite, $wmincTestWikiNamespaces;
60
61        if ( $pageName !== 'Recentchanges' ) {
62            return;
63        }
64
65        [ $projectvalue, $codevalue ] = self::getValues( RequestContext::getMain()->getUser() );
66        $prefix = WikimediaIncubator::displayPrefix( $projectvalue, $codevalue );
67        $opts->add( 'rc-testwiki-project', false );
68        $opts->setValue( 'rc-testwiki-project', $projectvalue );
69        $opts->add( 'rc-testwiki-code', false );
70        $opts->setValue( 'rc-testwiki-code', $codevalue );
71
72        if ( $projectvalue == $wmincProjectSite['short'] ) {
73            // If project site is selected, display all changes except test wiki changes
74            $dbr = MediaWikiServices::getInstance()->getDBLoadBalancerFactory()->getReplicaDatabase();
75            $conds[] = 'rc_title NOT ' . $dbr->buildLike( 'W', $dbr->anyChar(), '/', $dbr->anyString() );
76        } elseif ( WikimediaIncubator::validatePrefix( $prefix, true ) ) {
77            // Else, display changes to the selected test wiki in the appropriate namespaces
78            $dbr = MediaWikiServices::getInstance()->getDBLoadBalancerFactory()->getReplicaDatabase();
79            // The next line (the phan-suppress one) is buggy some times. If phan-docker
80            // complains, try re-adding it by adding/removing the initial @.
81            // phan-suppress-next-line PhanPossiblyUndeclaredVariable
82            $conds['rc_namespace'] = $wmincTestWikiNamespaces;
83            $conds[] = 'rc_title ' . $dbr->buildLike( $prefix . '/', $dbr->anyString() ) .
84            ' OR rc_title = ' . $dbr->addQuotes( $prefix );
85        }
86        // If "none" is selected, display normal recent changes
87    }
88
89    public function onSpecialRecentChangesPanel( &$items, $opts ) {
90        global $wmincProjects, $wmincProjectSite, $wmincLangCodeLength;
91
92        [ $projectvalue, $codevalue ] = self::getValues( RequestContext::getMain()->getUser() );
93        $opts->consumeValue( 'rc-testwiki-project' );
94        $opts->consumeValue( 'rc-testwiki-code' );
95        $label = Xml::label( wfMessage( 'wminc-testwiki' )->text(), 'rc-testwiki' );
96        $select = new XmlSelect( 'rc-testwiki-project', 'rc-testwiki-project', $projectvalue );
97        $select->addOption( wfMessage( 'wminc-testwiki-none' )->text(), 'none' );
98        foreach ( $wmincProjects as $prefix => $metadata ) {
99            if ( !$metadata['sister'] ) {
100                $select->addOption( $metadata['name'], $prefix );
101            }
102        }
103        $select->addOption( $wmincProjectSite['name'], $wmincProjectSite['short'] );
104        $langcode = Xml::input( 'rc-testwiki-code', (int)$wmincLangCodeLength, $codevalue,
105            [ 'id' => 'rc-testwiki-code', 'maxlength' => (int)$wmincLangCodeLength ] );
106        $items['testwiki'] = [ $label, $select->getHTML() . ' ' . $langcode ];
107    }
108}