Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
55.84% |
43 / 77 |
|
62.50% |
5 / 8 |
CRAP | |
0.00% |
0 / 1 |
SpecialNewsletterCreate | |
55.84% |
43 / 77 |
|
62.50% |
5 / 8 |
30.87 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
execute | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
1 | |||
alterForm | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getFormFields | |
100.00% |
22 / 22 |
|
100.00% |
1 / 1 |
1 | |||
onSubmit | |
21.95% |
9 / 41 |
|
0.00% |
0 / 1 |
30.30 | |||
onSuccess | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
doesWrites | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getDisplayFormat | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 |
1 | <?php |
2 | |
3 | namespace MediaWiki\Extension\Newsletter\Specials; |
4 | |
5 | use HTMLForm; |
6 | use MediaWiki\Extension\Newsletter\Content\NewsletterContentHandler; |
7 | use MediaWiki\Extension\Newsletter\NewsletterValidator; |
8 | use MediaWiki\MediaWikiServices; |
9 | use MediaWiki\SpecialPage\FormSpecialPage; |
10 | use MediaWiki\SpecialPage\SpecialPage; |
11 | use MediaWiki\Status\Status; |
12 | use MediaWiki\Title\Title; |
13 | use ThrottledError; |
14 | |
15 | /** |
16 | * Special page for creating newsletters |
17 | * |
18 | * @license GPL-2.0-or-later |
19 | * @author Tina Johnson |
20 | */ |
21 | class SpecialNewsletterCreate extends FormSpecialPage { |
22 | |
23 | /** |
24 | * @var string |
25 | */ |
26 | protected $newsletterName; |
27 | |
28 | public function __construct() { |
29 | parent::__construct( 'NewsletterCreate', 'newsletter-create' ); |
30 | } |
31 | |
32 | public function execute( $par ) { |
33 | $this->requireLogin(); |
34 | parent::execute( $par ); |
35 | $this->getOutput()->setSubtitle( |
36 | $this->getLinkRenderer()->makeKnownLink( |
37 | SpecialPage::getTitleFor( 'Newsletters' ), |
38 | $this->msg( 'newsletter-subtitlelinks-list' )->text() |
39 | ) |
40 | ); |
41 | $this->addHelpLink( 'Help:Extension:Newsletter' ); |
42 | } |
43 | |
44 | /** |
45 | * @param HTMLForm $form |
46 | */ |
47 | protected function alterForm( HTMLForm $form ) { |
48 | $form->setSubmitTextMsg( 'newsletter-create-submit' ); |
49 | } |
50 | |
51 | /** |
52 | * @return array |
53 | */ |
54 | protected function getFormFields() { |
55 | return [ |
56 | 'name' => [ |
57 | 'name' => 'newsletter', |
58 | 'type' => 'text', |
59 | 'required' => true, |
60 | 'label-message' => 'newsletter-name', |
61 | 'maxlength' => 120, |
62 | ], |
63 | 'mainpage' => [ |
64 | 'exists' => true, |
65 | 'type' => 'title', |
66 | 'required' => true, |
67 | 'label-message' => 'newsletter-title', |
68 | ], |
69 | 'description' => [ |
70 | 'type' => 'textarea', |
71 | 'required' => true, |
72 | 'label-message' => 'newsletter-desc', |
73 | 'rows' => 15, |
74 | 'maxlength' => 600000, |
75 | ], |
76 | ]; |
77 | } |
78 | |
79 | /** |
80 | * Do input validation, error handling and create a new newletter. |
81 | * |
82 | * @param array $input The data entered by user in the form |
83 | * @throws ThrottledError |
84 | * @return Status |
85 | */ |
86 | public function onSubmit( array $input ) { |
87 | $data = [ |
88 | 'Name' => trim( $input['name'] ), |
89 | 'Description' => trim( $input['description'] ), |
90 | 'MainPage' => Title::newFromText( $input['mainpage'] ), |
91 | ]; |
92 | |
93 | $validator = new NewsletterValidator( $data ); |
94 | $validation = $validator->validate( true ); |
95 | if ( !$validation->isGood() ) { |
96 | // Invalid input was entered |
97 | return $validation; |
98 | } |
99 | |
100 | $mainPageId = $data['MainPage']->getArticleID(); |
101 | |
102 | $dbr = MediaWikiServices::getInstance()->getDBLoadBalancer()->getConnection( DB_REPLICA ); |
103 | $rows = $dbr->newSelectQueryBuilder() |
104 | ->select( [ 'nl_name', 'nl_main_page_id', 'nl_active' ] ) |
105 | ->from( 'nl_newsletters' ) |
106 | ->where( |
107 | $dbr->expr( 'nl_name', '=', $data['Name'] )->orExpr( |
108 | $dbr->expr( 'nl_main_page_id', '=', $mainPageId )->and( 'nl_active', '=', 1 ) |
109 | ) |
110 | ) |
111 | ->caller( __METHOD__ ) |
112 | ->fetchResultSet(); |
113 | // Check whether another existing newsletter has the same name or main page |
114 | foreach ( $rows as $row ) { |
115 | if ( $row->nl_name === $data['Name'] ) { |
116 | return Status::newFatal( 'newsletter-exist-error', $data['Name'] ); |
117 | } elseif ( (int)$row->nl_main_page_id === $mainPageId && (int)$row->nl_active === 1 ) { |
118 | return Status::newFatal( 'newsletter-mainpage-in-use' ); |
119 | } |
120 | } |
121 | |
122 | $user = $this->getUser(); |
123 | if ( $user->pingLimiter( 'newsletter' ) ) { |
124 | // Default user access level for creating a newsletter is quite low |
125 | // so add a throttle here to prevent abuse (eg. mass vandalism spree) |
126 | throw new ThrottledError; |
127 | } |
128 | |
129 | $this->newsletterName = $data['Name']; |
130 | $title = Title::makeTitleSafe( NS_NEWSLETTER, $data['Name'] ); |
131 | $editSummaryMsg = $this->msg( 'newsletter-create-editsummary' ); |
132 | $result = NewsletterContentHandler::edit( |
133 | $title, |
134 | $data['Description'], |
135 | $input['mainpage'], |
136 | [ $user->getName() ], |
137 | $editSummaryMsg->inContentLanguage()->plain(), |
138 | $this->getContext() |
139 | ); |
140 | return $result; |
141 | } |
142 | |
143 | public function onSuccess() { |
144 | $this->getOutput()->addWikiMsg( 'newsletter-create-confirmation', $this->newsletterName ); |
145 | } |
146 | |
147 | public function doesWrites() { |
148 | return true; |
149 | } |
150 | |
151 | protected function getDisplayFormat() { |
152 | return 'ooui'; |
153 | } |
154 | |
155 | } |