Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
80.00% covered (warning)
80.00%
20 / 25
20.00% covered (danger)
20.00%
1 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
Workflow
80.00% covered (warning)
80.00%
20 / 25
20.00% covered (danger)
20.00%
1 / 5
13.15
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
 getConfig
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getLandingPageTitle
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 shouldInterceptPage
91.67% covered (success)
91.67%
11 / 12
0.00% covered (danger)
0.00%
0 / 1
7.03
 interceptIfNeeded
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2
3namespace ArticleCreationWorkflow;
4
5use MediaWiki\Config\Config;
6use MediaWiki\Context\IContextSource;
7use MediaWiki\Language\RawMessage;
8use MediaWiki\Title\Title;
9use MediaWiki\User\User;
10
11/**
12 * Contains this extension's business logic
13 */
14class Workflow {
15
16    public function __construct( private readonly Config $config ) {
17    }
18
19    /**
20     * @return Config
21     */
22    public function getConfig() {
23        return $this->config;
24    }
25
26    /**
27     * Returns the message defining the landing page
28     *
29     * @return Title|null
30     */
31    public function getLandingPageTitle() {
32        $landingPageName = $this->config->get( 'ArticleCreationLandingPage' );
33        return Title::newFromText( $landingPageName );
34    }
35
36    /**
37     * Check whether an attempt to visit a missing page should be intercepted and replaced by our
38     * workflow.
39     *
40     * @param Title $title The title $user attempts to create
41     * @param User $user The user trying to load the editor
42     * @return bool
43     */
44    public function shouldInterceptPage( Title $title, User $user ) {
45        // We are only interested in creation
46        if ( $title->exists() ) {
47            return false;
48        }
49
50        // Articles only
51        if ( !$title->inNamespace( NS_MAIN ) ) {
52            return false;
53        }
54
55        // User has perms, don't intercept
56        if ( $user->isAllowed( 'createpagemainns' ) ) {
57            return false;
58        }
59
60        // Only intercept users who can potentially create articles otherwise
61        if ( !$user->isAllowed( 'createpage' ) ) {
62            return false;
63        }
64
65        // Don't intercept if the landing page is not configured
66        $landingPage = $this->getLandingPageTitle();
67        if ( $landingPage === null || !$landingPage->exists() ) {
68            return false;
69        }
70
71        return true;
72    }
73
74    /**
75     * If a user without sufficient permissions attempts to view or create a missing page in the main
76     * namespace, display our workflow instead with a message defined on-wiki.
77     *
78     * @param Title $title
79     * @param User $user
80     * @param IContextSource $context
81     * @return bool Whether we intercepted the page view by displaying our own message
82     */
83    public function interceptIfNeeded( Title $title, User $user, IContextSource $context ) {
84        if ( $this->shouldInterceptPage( $title, $user ) ) {
85            // If the landing page didn't exist, we wouldn't have intercepted, so it's guaranteed to exist
86            // here ($landingPage is not null).
87            $landingPage = $this->getLandingPageTitle();
88            $output = $context->getOutput();
89            $output->disableClientCache();
90
91            // Transclude the landing page instead of redirecting. This allows for the deletion log snippet
92            // to be shown as usual, and for magic words like {{PAGENAME}} to be used in the message. (T204234)
93            $msg = new RawMessage( '{{:' . $landingPage->getPrefixedText() . '}}' );
94            $msg->page( $title );
95            $output->addHTML( $msg->parseAsBlock() );
96
97            return true;
98        }
99
100        return false;
101    }
102}