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