Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 70 |
|
0.00% |
0 / 10 |
CRAP | |
0.00% |
0 / 1 |
SpecialManageShortUrls | |
0.00% |
0 / 70 |
|
0.00% |
0 / 10 |
506 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
execute | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
6 | |||
getDisplayFormat | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getFormFields | |
0.00% |
0 / 20 |
|
0.00% |
0 / 1 |
2 | |||
isListed | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
onSubmit | |
0.00% |
0 / 35 |
|
0.00% |
0 / 1 |
156 | |||
checkExecutePermissions | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
doesWrites | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getGroupName | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
onSuccess | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 |
1 | <?php |
2 | /** |
3 | * A special page that manages short urls, delete and restore is possible |
4 | * |
5 | * @file |
6 | * @ingroup Extensions |
7 | * @author Ladsgroup |
8 | * @copyright © 2019 Amir Sarabadani |
9 | * @license Apache-2.0 |
10 | */ |
11 | |
12 | namespace MediaWiki\Extension\UrlShortener; |
13 | |
14 | use ManualLogEntry; |
15 | use MediaWiki\SpecialPage\FormSpecialPage; |
16 | use MediaWiki\User\User; |
17 | |
18 | class SpecialManageShortUrls extends FormSpecialPage { |
19 | |
20 | public function __construct() { |
21 | parent::__construct( 'ManageShortUrls', 'urlshortener-manage-url' ); |
22 | } |
23 | |
24 | /** |
25 | * @inheritDoc |
26 | */ |
27 | public function execute( $par ) { |
28 | $this->addHelpLink( 'Help:UrlShortener' ); |
29 | |
30 | if ( $this->getConfig()->get( 'UrlShortenerReadOnly' ) ) { |
31 | $this->setHeaders(); |
32 | $this->getOutput()->addWikiMsg( 'urlshortener-disabled' ); |
33 | } else { |
34 | $this->getOutput()->addWikiMsg( 'urlshortener-manage-text' ); |
35 | parent::execute( $par ); |
36 | } |
37 | } |
38 | |
39 | /** |
40 | * @inheritDoc |
41 | */ |
42 | protected function getDisplayFormat() { |
43 | return 'ooui'; |
44 | } |
45 | |
46 | /** |
47 | * @return array[] |
48 | */ |
49 | protected function getFormFields() { |
50 | return [ |
51 | 'shortCodeDelete' => [ |
52 | 'type' => 'text', |
53 | 'default' => '', |
54 | 'label-message' => 'urlshortener-enter-short-code-delete', |
55 | 'name' => 'shortCodeDelete' |
56 | ], |
57 | 'shortCodeRestore' => [ |
58 | 'type' => 'text', |
59 | 'default' => '', |
60 | 'label-message' => 'urlshortener-enter-short-code-restore', |
61 | 'name' => 'shortCodeRestore' |
62 | ], |
63 | 'reason' => [ |
64 | 'type' => 'text', |
65 | 'default' => '', |
66 | 'label-message' => 'urlshortener-manage-reason', |
67 | 'name' => 'reason' |
68 | ], |
69 | ]; |
70 | } |
71 | |
72 | /** |
73 | * Don't list this page if in read only mode |
74 | * |
75 | * @return bool |
76 | */ |
77 | public function isListed() { |
78 | return !$this->getConfig()->get( 'UrlShortenerReadOnly' ); |
79 | } |
80 | |
81 | /** |
82 | * @param array $formData |
83 | * |
84 | * @return array|bool |
85 | */ |
86 | public function onSubmit( array $formData ) { |
87 | $errors = []; |
88 | $success = false; |
89 | $delete = $formData['shortCodeDelete']; |
90 | $restore = $formData['shortCodeRestore']; |
91 | $reason = $formData['reason']; |
92 | |
93 | if ( !$delete && !$restore ) { |
94 | return [ 'urlshortener-manage-not-enough-data' ]; |
95 | } |
96 | |
97 | if ( $delete ) { |
98 | $url = UrlShortenerUtils::getURL( $delete ); |
99 | if ( $url === false ) { |
100 | $errors[] = [ 'urlshortener-short-code-not-found' ]; |
101 | } |
102 | $success = UrlShortenerUtils::deleteURL( $delete ); |
103 | if ( !$success ) { |
104 | $errors[] = [ 'urlshortener-manage-delete-failed' ]; |
105 | } |
106 | } |
107 | |
108 | if ( $restore ) { |
109 | $deleted = UrlShortenerUtils::isURLDeleted( $restore ); |
110 | if ( !$deleted ) { |
111 | $errors[] = [ 'urlshortener-short-code-is-not-deleted' ]; |
112 | } |
113 | $success = UrlShortenerUtils::restoreURL( $restore ); |
114 | if ( !$success ) { |
115 | $errors[] = [ 'urlshortener-manage-restore-failed' ]; |
116 | } |
117 | } |
118 | |
119 | if ( $errors === [] ) { |
120 | $subtype = $delete ? 'delete' : 'restore'; |
121 | $target = $delete ?: $restore; |
122 | |
123 | // Log the action |
124 | $logEntry = new ManualLogEntry( 'urlshortener', $subtype ); |
125 | $logEntry->setPerformer( $this->getUser() ); |
126 | // Set some dummy title. It is required to be set, otherwise we're not using it. |
127 | $logEntry->setTarget( $this->getTitleFor( 'UrlShortener' ) ); |
128 | $logEntry->setComment( $reason ); |
129 | $logEntry->setParameters( |
130 | [ '4::realtarget' => $target ] |
131 | ); |
132 | $logId = $logEntry->insert(); |
133 | $logEntry->publish( $logId ); |
134 | |
135 | return $success; |
136 | } |
137 | |
138 | return $errors; |
139 | } |
140 | |
141 | /** |
142 | * Require users to be logged in and have the rights |
143 | * |
144 | * @param User $user |
145 | */ |
146 | protected function checkExecutePermissions( User $user ) { |
147 | parent::checkExecutePermissions( $user ); |
148 | $this->requireLogin(); |
149 | } |
150 | |
151 | /** |
152 | * @inheritDoc |
153 | */ |
154 | public function doesWrites() { |
155 | return true; |
156 | } |
157 | |
158 | /** |
159 | * @inheritDoc |
160 | */ |
161 | protected function getGroupName() { |
162 | return 'pagetools'; |
163 | } |
164 | |
165 | public function onSuccess() { |
166 | $this->getOutput()->addWikiMsg( 'urlshortener-action-done' ); |
167 | $this->getOutput()->returnToMain(); |
168 | } |
169 | } |