Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
78.26% covered (warning)
78.26%
18 / 23
20.00% covered (danger)
20.00%
1 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
Workflow
78.26% covered (warning)
78.26%
18 / 23
20.00% covered (danger)
20.00%
1 / 5
13.48
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%
7 / 7
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2
3namespace ArticleCreationWorkflow;
4
5use Config;
6use IContextSource;
7use Title;
8use User;
9
10/**
11 * Contains this extension's business logic
12 */
13class Workflow {
14
15    /** @var Config */
16    private $config;
17
18    /**
19     * @param Config $config Configuration to use
20     */
21    public function __construct( Config $config ) {
22        $this->config = $config;
23    }
24
25    /**
26     * @return Config
27     */
28    public function getConfig() {
29        return $this->config;
30    }
31
32    /**
33     * Returns the message defining the landing page
34     *
35     * @return Title|null
36     */
37    public function getLandingPageTitle() {
38        $landingPageName = $this->config->get( 'ArticleCreationLandingPage' );
39        return Title::newFromText( $landingPageName );
40    }
41
42    /**
43     * Checks whether an attempt to edit a page should be intercepted and redirected to our workflow
44     *
45     * @param Title $title The title $user attempts to create
46     * @param User $user The user trying to load the editor
47     * @return bool
48     */
49    public function shouldInterceptPage( Title $title, User $user ) {
50        // We are only interested in creation
51        if ( $title->exists() ) {
52            return false;
53        }
54
55        // Articles only
56        if ( !$title->inNamespace( NS_MAIN ) ) {
57            return false;
58        }
59
60        // User has perms, don't intercept
61        if ( $user->isAllowed( 'createpagemainns' ) ) {
62            return false;
63        }
64
65        // Only intercept users who can potentially create articles otherwise
66        if ( !$user->isAllowed( 'createpage' ) ) {
67            return false;
68        }
69
70        // Don't intercept if the landing page is not configured
71        $landingPage = $this->getLandingPageTitle();
72        if ( $landingPage === null || !$landingPage->exists() ) {
73            return false;
74        }
75
76        return true;
77    }
78
79    /**
80     * If a user without sufficient permissions attempts to create a page in the main namespace
81     *
82     * @param Title $title
83     * @param User $user
84     * @param IContextSource $context
85     * @return bool
86     */
87    public function interceptIfNeeded( Title $title, User $user, IContextSource $context ) {
88        if ( $this->shouldInterceptPage( $title, $user ) ) {
89            // If the landing page didn't exist, we wouldn't have intercepted.
90            $redirTo = $this->getLandingPageTitle();
91            $output = $context->getOutput();
92            $output->redirect( $redirTo->getFullURL(
93                [ 'page' => $title->getPrefixedText() ]
94            ) );
95
96            return true;
97        }
98        return false;
99    }
100}