Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 11 |
|
0.00% |
0 / 4 |
CRAP | |
0.00% |
0 / 1 |
| TagHooks | |
0.00% |
0 / 11 |
|
0.00% |
0 / 4 |
42 | |
0.00% |
0 / 1 |
| onChangeTagsListActive | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| onListDefinedTags | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| onRecentChange_save | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
12 | |||
| getDiscussionToolsTagsFromRequest | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * DiscussionTools tag hooks |
| 4 | * |
| 5 | * @file |
| 6 | * @ingroup Extensions |
| 7 | * @license MIT |
| 8 | */ |
| 9 | |
| 10 | namespace MediaWiki\Extension\DiscussionTools\Hooks; |
| 11 | |
| 12 | use MediaWiki\ChangeTags\Hook\ChangeTagsListActiveHook; |
| 13 | use MediaWiki\ChangeTags\Hook\ListDefinedTagsHook; |
| 14 | use MediaWiki\Context\RequestContext; |
| 15 | use MediaWiki\RecentChanges\Hook\RecentChange_saveHook; |
| 16 | use MediaWiki\RecentChanges\RecentChange; |
| 17 | |
| 18 | class TagHooks implements |
| 19 | ChangeTagsListActiveHook, |
| 20 | ListDefinedTagsHook, |
| 21 | RecentChange_saveHook |
| 22 | { |
| 23 | private const TAGS = [ |
| 24 | 'discussiontools', |
| 25 | // Features: |
| 26 | 'discussiontools-reply', |
| 27 | 'discussiontools-edit', |
| 28 | 'discussiontools-newtopic', |
| 29 | 'discussiontools-added-comment', |
| 30 | // Input methods: |
| 31 | 'discussiontools-source', |
| 32 | 'discussiontools-visual', |
| 33 | // Temporary input method: |
| 34 | 'discussiontools-source-enhanced', |
| 35 | ]; |
| 36 | |
| 37 | /** |
| 38 | * @param string[] &$tags List of all active tags. Append to this array. |
| 39 | * @return bool|void True or no return value to continue or false to abort |
| 40 | */ |
| 41 | public function onChangeTagsListActive( &$tags ) { |
| 42 | $this->onListDefinedTags( $tags ); |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * Populate core Special:Tags with the change tags in use by DiscussionTools. |
| 47 | * |
| 48 | * @param string[] &$tags List of tags |
| 49 | * @return bool|void True or no return value to continue or false to abort |
| 50 | */ |
| 51 | public function onListDefinedTags( &$tags ) { |
| 52 | $tags = array_merge( $tags, static::TAGS ); |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Implements the RecentChange_save hook, to add an allowed set of changetags |
| 57 | * to edits. |
| 58 | * |
| 59 | * @param RecentChange $recentChange |
| 60 | * @return bool |
| 61 | */ |
| 62 | public function onRecentChange_save( $recentChange ) { |
| 63 | // only apply to api edits, since there's no case where discussiontools |
| 64 | // should be using the form-submit method. |
| 65 | if ( !defined( 'MW_API' ) ) { |
| 66 | return true; |
| 67 | } |
| 68 | |
| 69 | $tags = static::getDiscussionToolsTagsFromRequest(); |
| 70 | if ( $tags ) { |
| 71 | $recentChange->addTags( $tags ); |
| 72 | } |
| 73 | |
| 74 | return true; |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Get DT tags from the dttags param in the request, and validate against known tags. |
| 79 | */ |
| 80 | public static function getDiscussionToolsTagsFromRequest(): array { |
| 81 | $request = RequestContext::getMain()->getRequest(); |
| 82 | $tags = explode( ',', $request->getText( 'dttags' ) ); |
| 83 | return array_values( array_intersect( $tags, static::TAGS ) ); |
| 84 | } |
| 85 | } |