Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
25.00% covered (danger)
25.00%
6 / 24
20.00% covered (danger)
20.00%
1 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
Hooks
25.00% covered (danger)
25.00%
6 / 24
20.00% covered (danger)
20.00%
1 / 5
72.75
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 onTitleQuickPermissions
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
5
 onGetActionName
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
12
 onBeforeDisplayNoArticleText
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
2
 getWorkflow
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2
3namespace ArticleCreationWorkflow;
4
5use MediaWiki\Actions\Hook\GetActionNameHook;
6use MediaWiki\Config\ConfigFactory;
7use MediaWiki\Context\IContextSource;
8use MediaWiki\Page\Article;
9use MediaWiki\Page\Hook\BeforeDisplayNoArticleTextHook;
10use MediaWiki\Permissions\Hook\TitleQuickPermissionsHook;
11use MediaWiki\Title\Title;
12use MediaWiki\User\User;
13
14/**
15 * Hook handlers
16 */
17class Hooks implements
18    GetActionNameHook,
19    BeforeDisplayNoArticleTextHook,
20    TitleQuickPermissionsHook
21{
22    private ?Workflow $workflow = null;
23
24    public function __construct(
25        private readonly ConfigFactory $configFactory,
26    ) {
27    }
28
29    /**
30     * TitleQuickPermissions hook handler
31     * Prohibits creating pages in main namespace for users without a special permission
32     *
33     * @see https://www.mediawiki.org/wiki/Manual:Hooks/TitleQuickPermissions
34     * @param Title $title
35     * @param User $user
36     * @param string $action
37     * @param array &$errors
38     * @param bool $doExpensiveQueries
39     * @param bool $short
40     * @return bool
41     */
42    public function onTitleQuickPermissions( $title,
43        $user,
44        $action,
45        &$errors,
46        $doExpensiveQueries,
47        $short
48    ) {
49        if ( $action === 'create'
50            && $title->inNamespace( NS_MAIN )
51            && !$user->isAllowed( 'createpagemainns' )
52        ) {
53            $errors[] = !$user->isNamed() ? [ 'nocreatetext' ] : [ 'nocreate-loggedin' ];
54            return false;
55        }
56        return true;
57    }
58
59    /**
60     * GetActionName hook handler
61     *
62     * @param IContextSource $context Request context
63     * @param string &$action Default action name, reassign to change it
64     * @return void This hook must not abort, it must return no value
65     */
66    public function onGetActionName( IContextSource $context, string &$action ): void {
67        if ( $action !== 'edit' ) {
68            return;
69        }
70        $workflow = $this->getWorkflow();
71        $title = $context->getTitle();
72        $user = $context->getUser();
73        if ( $workflow->shouldInterceptPage( $title, $user ) ) {
74            // The user wouldn't be allowed to edit anyway, so pretend we're in the 'view' action,
75            // so that we can intercept it in onBeforeDisplayNoArticleText.
76            $action = 'view';
77        }
78    }
79
80    /**
81     * BeforeDisplayNoArticleText hook handler
82     *
83     * @param Article $article The (empty) article
84     * @return bool This hook can abort
85     */
86    public function onBeforeDisplayNoArticleText( $article ) {
87        $workflow = $this->getWorkflow();
88        $context = $article->getContext();
89        $user = $context->getUser();
90        $title = $article->getTitle();
91
92        $wasIntercepted = $workflow->interceptIfNeeded( $title, $user, $context );
93
94        // If we displayed our own message, abort the hook by returning `false`
95        // to suppress the default message, otherwise let it continue.
96        return !$wasIntercepted;
97    }
98
99    private function getWorkflow(): Workflow {
100        if ( !$this->workflow ) {
101            $config = $this->configFactory->makeConfig( 'ArticleCreationWorkflow' );
102            $this->workflow = new Workflow( $config );
103        }
104
105        return $this->workflow;
106    }
107}