/
MarkMajorChangeAction.php
215 lines (171 loc) · 5.22 KB
/
MarkMajorChangeAction.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
<?php
/**
* Class MajorChangeAction
*
* A lot of this is ripped from SpecialEditTags, which implements the EditTagsAction
* (which otherwise I could have simply extended, drat)
*/
class MajorChangeAction extends FormAction {
private $reason;
private $isSecondaryChange;
private $requiredRight = 'markmajorchange';
public function getRequiredRight() {
return $this->requiredRight;
}
public function show() {
// Additional security checking before parent::show()
$errors = $this->getTitle()->getUserPermissionsErrors(
$this->getRequiredRight(), $this->getUser()
);
if ( count( $errors ) ) {
throw new PermissionsError( $this->getRequiredRight(), $errors );
}
// Use jQuery.plugin.byteLimit to limit "reason" according to DB column (255B)
$this->getOutput()->addModules( 'mediawiki.action.majorchange' );
parent::show();
}
public function getName() {
return 'markmajorchange';
}
// We don't want a subtitle text here
protected function getDescription() {
return '';
}
protected function getPageTitle() {
return
$this->msg( 'markmajorchange-action-title' )->params( parent::getPageTitle() )->text();
}
public function getRestriction() {
return 'changetags';
}
protected function preText() {
return $this->msg( 'markmajorchange-form-desc' )->text();
}
protected function getFormFields() {
$fields = [];
if ( $this->hasArabicLangLink() ) {
$fields[ 'isSecondaryChange' ] = [
'type' => 'check',
'label-message' => 'markmajorchanges-field-issecondary',
// 'cssclass' => 'form-control'
];
}
$fields['reason'] = [
'type' => 'textarea',
'label-message' => 'markmajorchanges-field-reason',
'label' => 'What changed?',
'maxlength' => '200',
'size' => 60,
'cols' => 60,
'rows' => 2,
'required' => true,
// 'cssclass' => 'form-control' // Bootstrap3
];
return $fields;
}
public function onSubmit( $data ) {
// The HTMLForm class takes care of basic validation,
// such as required fields not being empty...
$this->reason = $data['reason'];
$this->isSecondaryChange = $data['isSecondaryChange'];
return true;
}
/* Copied from ChangeTags::updateTagsWithChecks() */
protected function logTagAdded( $tags, $revId, $user, $reason ) {
// log it
$logEntry = new ManualLogEntry( 'tag', 'update' );
$logEntry->setPerformer( $user );
$logEntry->setComment( $reason );
// find the appropriate target page
if ( $revId ) {
$rev = Revision::newFromId( $revId );
if ( $rev ) {
$logEntry->setTarget( $rev->getTitle() );
}
}
if ( !$logEntry->getTarget() ) {
// target is required, so we have to set something
$logEntry->setTarget( SpecialPage::getTitleFor( 'Tags' ) );
}
$logParams = [
'4::revid' => $revId,
'6:list:tagsAdded' => $tags,
'7:number:tagsAddedCount' => count( $tags ),
];
$logEntry->setParameters( $logParams );
$logEntry->setRelations( [ 'Tag' => $tags ] );
$dbw = wfGetDB( DB_MASTER );
$logId = $logEntry->insert( $dbw );
// Only send this to UDP, not RC, similar to patrol events
$logEntry->publish( $logId, 'udp' );
// $logEntry->publish( $logId );
}
protected function saveTags() {
$revId = $this->getTitle()->getLatestRevID();
$reason = $this->reason;
$user = $this->getUser();
$tags = [];
// Is this a major change, or just a secondary change? Mark both for major
if ( $this->hasArabicLangLink() ) {
$tags[] = MarkMajorChanges::getSecondaryTagName();
}
if ( !$this->isSecondaryChange ) {
$tags[] = MarkMajorChanges::getMainTagName();
}
// Should we use DeferredUpdates::addCallableUpdate?
$status = ChangeTags::addTags( $tags, null, $revId );
if ( $status === true ) {
$this->logTagAdded( $tags, $revId, $user, $reason );
// $this->getTitle()->isMajorChange == true;
return true;
}
return false;
}
public function onSuccess() {
$status = $this->saveTags();
// Let the user know
// @todo notify user according to actual status...
$this->getOutput()->setPageTitle( $this->msg( 'actioncomplete' ) );
$this->getOutput()->wrapWikiMsg( "<div class=\"successbox\">\n$1\n</div>",
'tags-edit-success' );
}
protected function alterForm( HTMLForm $form ) {
$form->setDisplayFormat( 'div' );
// Suppress default submit, so we can add one that is slightly nicer looking
$form->suppressDefaultSubmit();
$form->addButton(
'submit',
$this->msg( 'htmlform-submit' )->text(),
null,
[ 'class' => 'btn' ]
);
}
private function hasArabicLangLink() {
$langLinks = $this->getPageLankLinks();
return isset( $langLinks['ar'] );
}
/**
* Get an array of existing interlanguage links, with the language code in the key and the
* title in the value.
*
* Taken from Core's LinksUpdate::getExistingInterlangs() [includes/deferred/LinksUpdate.php]
*
* @return array
*/
private function getPageLankLinks() {
$dbr = wfGetDB( DB_REPLICA );
$res = $dbr->select(
'langlinks', [ 'll_lang', 'll_title' ],
[ 'll_from' => $this->getTitle()->getArticleID() ], __METHOD__
);
$arr = [];
foreach ( $res as $row ) {
$arr[$row->ll_lang] = $row->ll_title;
}
return $arr;
}
// @todo get existing change tags so no one tries to resubmit
private function getExistingChangeTags() {
// $tags = Revision::
}
}