-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathArticleProtection.hooks.php
220 lines (183 loc) · 5.09 KB
/
ArticleProtection.hooks.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
215
216
217
218
219
220
<?php
/**
* Static class for hooks handled by the Page Creation Notification extension.
*
* @since 0.1
*
* @file ArticleProtection.hooks.php
* @ingroup ArticleProtection
*
* @licence GNU GPL v3 or later
* @author Nischay Nahata < nischayn22@gmail.com >
*/
final class ArticleProtectionHooks {
/**
* Schema update to set up the needed database tables.
*
* @since 0.1
*
* @param DatabaseUpdater $updater
*
* @return true
*/
public static function onSchemaUpdate( /* DatabaseUpdater */ $updater = null ) {
global $wgDBtype;
if ( $wgDBtype == 'mysql' ) {
$updater->addExtensionUpdate( array(
'addTable',
'article_protection',
dirname( __FILE__ ) . '/ArticleProtection.sql',
true
) );
}
return true;
}
/**
* Called just after a new Article is created.
*
* @since 0.1
*/
public static function onArticleInsertComplete( &$article, User &$user, $text, $summary, $minoredit, $watchthis, $sectionanchor, &$flags, Revision
$revision ) {
global $articleProtectionNS;
if (!in_array( $article->getTitle()->getNamespace(), $articleProtectionNS ))
return true;
$dbw = wfGetDB( DB_MASTER );
$dbw->insert(
'article_protection',
array(
'article_id' => $article->getID(),
'user_name' => $user->getName(),
'owner' => 1,
'original_owner' => 1,
'edit_permission' => 0,
)
);
$logEntry = new ManualLogEntry( 'ArticleProtection', 'owner-created-permissions' );
$logEntry->setPerformer( $user ); // User object, the user who performed this action
$logEntry->setTarget( Title::newFromID( $article->getID() ) ); // The page that this log entry affects, a Title object
$logid = $logEntry->insert();
return true;
}
public static function onSkinTemplateNavigation( SkinTemplate &$sktemplate, array &$links ) {
global $wgTitle, $articleProtectionNS, $wgOut;
if (!in_array( $wgTitle->getNamespace(), $articleProtectionNS ))
return true;
$article_details = $sktemplate->makeArticleUrlDetails( Title::newFromText('Special:ArticleProtection/' . $wgTitle->getFullText() )->getFullText() );
$links['views']['protection'] = array(
'class' => false,
'text' => "View editors",
'href' => $article_details['href']
);
return true;
}
public static function BeforePageDisplay( OutputPage &$out, Skin &$skin ) {
$out->addModules( 'ext.articleprotection.pageview' );
}
public static function onTitleQuickPermissions( $title, $user, $action, &$errors, $doExpensiveQueries, $short ) {
// if ($action == 'edit') {
// $dbr = wfGetDB( DB_SLAVE );
// $article_info = $dbr->selectRow(
// 'article_protection',
// array(
// 'owner',
// 'edit_permission',
// 'view_permission'
// ),
// array(
// 'user_name' => $user->getName(),
// 'article_id' => $title->getArticleID()
// )
// );
// if (!$article_info) {
// return true;
// }
// if ($article_info->edit_permission != "1" ) {
// $errors = array( array( "not allowed" ) );
// return false;
// }
// }
return true;
}
public static function onUserGetRights( $user, &$aRights ) {
global $wgTitle, $articleProtectionNS;
if (!in_array( $wgTitle->getNamespace(), $articleProtectionNS )) {
return true;
}
$aRights = array_merge( array_diff($aRights, array("edit", "minoredit", "reupload", "upload")) );
if (!$user->isLoggedIn()) {
return true;
}
$dbr = wfGetDB( DB_SLAVE );
$article_infos = $dbr->select(
'article_protection',
array(
'owner',
'user_name',
'edit_permission'
),
array(
'article_id' => $wgTitle->getArticleID()
)
);
if ( !$article_infos->current() ) {
$aRights[] = "edit";
$aRights[] = "minoredit";
$aRights[] = "upload";
$aRights[] = "reupload";
return true;
}
foreach($article_infos as $article_info) {
if ($article_info->user_name != $user->getName()) {
continue;
}
if ($article_info->owner == "1" || $article_info->edit_permission == "1" ) {
$aRights[] = "edit";
$aRights[] = "minoredit";
$aRights[] = "upload";
$aRights[] = "reupload";
return true;
}
}
return true;
}
public static function onAPIEditBeforeSave( $editPage, $text, &$resultArr ) {
global $wgTitle, $wgUser, $articleProtectionNS;
if ( !in_array( $wgTitle->getNamespace(), $articleProtectionNS ) ) {
return true;
}
if ( !$wgUser->isLoggedIn() ) {
$resultArr = array(
'reason' => 'Anonymous edits not permitted for page.'
);
return false;
}
$dbr = wfGetDB( DB_SLAVE );
$article_infos = $dbr->select(
'article_protection',
array(
'owner',
'user_name',
'edit_permission'
),
array(
'article_id' => $wgTitle->getArticleID()
)
);
if ( !$article_infos->current() ) {
return true;
}
foreach ( $article_infos as $article_info ) {
if ( $article_info->user_name != $wgUser->getName() ) {
continue;
}
if ( $article_info->owner == "1" || $article_info->edit_permission == "1" ) {
return true;
}
}
$resultArr = array(
'reason' => 'User does not have access to edit page.'
);
return false;
}
}