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