Debian Codegraph @

extensions/DiscussionTools/includes/Hooks/TagHooks.php

raw symbols SCIP
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\Hook\RecentChange_saveHook;
16 use 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 // phpcs:disable MediaWiki.NamingConventions.LowerCamelFunctionsName.FunctionName
56
57 /**
58 * Implements the RecentChange_save hook, to add an allowed set of changetags
59 * to edits.
60 *
61 * @param RecentChange $recentChange
62 * @return bool
63 */
64 public function onRecentChange_save( $recentChange ) {
65 // only apply to api edits, since there's no case where discussiontools
66 // should be using the form-submit method.
67 if ( !defined( 'MW_API' ) ) {
68 return true;
69 }
70
71 $tags = static::getDiscussionToolsTagsFromRequest();
72 if ( $tags ) {
73 $recentChange->addTags( $tags );
74 }
75
76 return true;
77 }
78
79 /**
80 * Get DT tags from the dttags param in the request, and validate against known tags.
81 */
82 public static function getDiscussionToolsTagsFromRequest(): array {
83 $request = RequestContext::getMain()->getRequest();
84 $tags = explode( ',', $request->getText( 'dttags' ) );
85 return array_values( array_intersect( $tags, static::TAGS ) );
86 }
87 }