Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
81.82% |
27 / 33 |
|
50.00% |
1 / 2 |
CRAP | |
0.00% |
0 / 1 |
EditPageImportFormDataHookHandler | |
81.82% |
27 / 33 |
|
50.00% |
1 / 2 |
16.35 | |
0.00% |
0 / 1 |
onEditPage__importFormData | |
76.00% |
19 / 25 |
|
0.00% |
0 / 1 |
12.67 | |||
swapTalkComments | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
4 |
1 | <?php |
2 | |
3 | // phpcs:disable MediaWiki.NamingConventions.LowerCamelFunctionsName.FunctionName |
4 | |
5 | namespace TwoColConflict\Hooks; |
6 | |
7 | use MediaWiki\EditPage\EditPage; |
8 | use MediaWiki\Hook\EditPage__importFormDataHook; |
9 | use MediaWiki\MediaWikiServices; |
10 | use MediaWiki\Request\WebRequest; |
11 | use TwoColConflict\ConflictFormValidator; |
12 | use TwoColConflict\SplitConflictMerger; |
13 | use TwoColConflict\TwoColConflictContext; |
14 | |
15 | /** |
16 | * @license GPL-2.0-or-later |
17 | */ |
18 | class EditPageImportFormDataHookHandler implements EditPage__importFormDataHook { |
19 | |
20 | /** |
21 | * @see https://www.mediawiki.org/wiki/Manual:Hooks/EditPage::importFormData |
22 | * |
23 | * @param EditPage $editPage |
24 | * @param WebRequest $request |
25 | */ |
26 | public function onEditPage__importFormData( $editPage, $request ) { |
27 | $contentRows = $request->getArray( 'mw-twocolconflict-split-content' ); |
28 | if ( $contentRows ) { |
29 | $extraLineFeeds = $request->getArray( 'mw-twocolconflict-split-linefeeds' ) ?? []; |
30 | $sideSelection = $request->getArray( 'mw-twocolconflict-side-selector' ) ?? []; |
31 | |
32 | if ( $request->getBool( 'mw-twocolconflict-single-column-view' ) ) { |
33 | if ( !( new ConflictFormValidator() )->validateRequest( $request ) ) { |
34 | // When the request is invalid, drop any selection to force the original conflict to |
35 | // be re-created, and not silently resolved or corrupted. |
36 | $sideSelection = []; |
37 | foreach ( $contentRows as $num => &$row ) { |
38 | // Make sure the merger can't fall back to "other", but allow other fallbacks |
39 | if ( is_array( $row ) && key( $row ) === 'other' ) { |
40 | unset( $row['other'] ); |
41 | if ( !$row ) { |
42 | unset( $contentRows[$num] ); |
43 | } |
44 | } |
45 | } |
46 | } elseif ( $request->getVal( 'mw-twocolconflict-reorder' ) === 'reverse' ) { |
47 | [ $contentRows, $extraLineFeeds ] = self::swapTalkComments( $contentRows, $extraLineFeeds ); |
48 | } |
49 | } |
50 | |
51 | $editPage->textbox1 = ( new SplitConflictMerger() )->mergeSplitConflictResults( |
52 | $contentRows, |
53 | $extraLineFeeds, |
54 | $sideSelection |
55 | ); |
56 | } |
57 | |
58 | if ( $request->getBool( 'mw-twocolconflict-disable-core-hint' ) ) { |
59 | $user = $editPage->getContext()->getUser(); |
60 | if ( $user->isNamed() ) { |
61 | $userOptionsManager = MediaWikiServices::getInstance()->getUserOptionsManager(); |
62 | $userOptionsManager->setOption( $user, TwoColConflictContext::HIDE_CORE_HINT_PREFERENCE, '1' ); |
63 | $userOptionsManager->saveOptions( $user ); |
64 | } |
65 | } |
66 | } |
67 | |
68 | private static function swapTalkComments( array $contentRows, array $extraLineFeeds ): array { |
69 | for ( $i = 0; $i < count( $contentRows ) - 1; $i++ ) { |
70 | if ( isset( $contentRows[$i]['other'] ) && isset( $contentRows[$i + 1]['your'] ) ) { |
71 | [ $contentRows[$i], $contentRows[$i + 1] ] = |
72 | [ $contentRows[$i + 1], $contentRows[$i] ]; |
73 | [ $extraLineFeeds[$i], $extraLineFeeds[$i + 1] ] = |
74 | [ $extraLineFeeds[$i + 1] ?? 0, $extraLineFeeds[$i] ?? 0 ]; |
75 | $i++; |
76 | } |
77 | } |
78 | |
79 | return [ $contentRows, $extraLineFeeds ]; |
80 | } |
81 | |
82 | } |