* (bug 674) Allow users to be blocked from editing a specific article
[lhc/web/wiklou.git] / includes / specials / SpecialRestrictUser.php
1 <?php
2
3 function wfSpecialRestrictUser( $par = null ) {
4 global $wgOut, $wgRequest;
5 $user = $userOrig = null;
6 if( $par ) {
7 $userOrig = $par;
8 } elseif( $wgRequest->getVal( 'user' ) ) {
9 $userOrig = $wgRequest->getVal( 'user' );
10 } else {
11 $wgOut->addHTML( RestrictUserForm::selectUserForm() );
12 return;
13 }
14 $isIP = User::isIP( $userOrig );
15 if( $isIP )
16 $user = $userOrig;
17 else
18 $user = User::getCanonicalName( $userOrig );
19 $uid = User::idFromName( $user );
20 if( !$uid && !$isIP ) {
21 $err = '<strong class="error">' . wfMsgHtml( 'restrictuser-notfound' ) . '</strong>';
22 $wgOut->addHTML( RestrictUserForm::selectUserForm( $userOrig, $err ) );
23 return;
24 }
25 $wgOut->addHTML( RestrictUserForm::selectUserForm( $user ) );
26
27 UserRestriction::purgeExpired();
28 $old = UserRestriction::fetchForUser( $user, true );
29
30 RestrictUserForm::pageRestrictionForm( $uid, $user, $old );
31 RestrictUserForm::namespaceRestrictionForm( $uid, $user, $old );
32
33 $old = UserRestriction::fetchForUser( $user, true ); // Renew it after possible changes in previous two functions
34 if( $old ) {
35 $wgOut->addHTML( RestrictUserForm::existingRestrictions( $old ) );
36 }
37 }
38
39 class RestrictUserForm {
40 public static function selectUserForm( $val = null, $error = null ) {
41 global $wgScript, $wgTitle;
42 $legend = wfMsgHtml( 'restrictuser-userselect' );
43 $s = "<fieldset><legend>{$legend}</legend><form action=\"{$wgScript}\">";
44 if( $error )
45 $s .= '<p>' . $error . '</p>';
46 $s .= Xml::hidden( 'title', $wgTitle->getPrefixedDbKey() );
47 $form = array( 'restrictuser-user' => Xml::input( 'user', false, $val ) );
48 $s .= Xml::buildForm( $form, 'restrictuser-go' );
49 $s .= "</form></fieldset>";
50 return $s;
51 }
52
53 public static function existingRestrictions( $restrictions ) {
54 require_once( dirname( __FILE__ ) . '/SpecialListUserRestrictions.php' );
55 $legend = wfMsgHtml( 'restrictuser-existing' );
56 $s = "<fieldset><legend>{$legend}</legend><ul>";
57 foreach( $restrictions as $r )
58 $s .= UserRestrictionsPager::formatRestriction( $r );
59 $s .= "</ul></fieldset>";
60 return $s;
61 }
62
63 public static function pageRestrictionForm( $uid, $user, $oldRestrictions ) {
64 global $wgOut, $wgTitle, $wgRequest, $wgUser;
65 $error = '';
66 $success = false;
67 if( $wgRequest->wasPosted() && $wgRequest->getVal( 'type' ) == UserRestriction::PAGE &&
68 $wgUser->matchEditToken( $wgRequest->getVal( 'edittoken' ) ) ) {
69 $title = Title::newFromText( $wgRequest->getVal( 'page' ) );
70 if( !$title )
71 $error = wfMsgExt( 'restrictuser-badtitle', 'parseinline', $wgRequest->getVal( 'page' ) );
72 elseif( UserRestriction::convertExpiry( $wgRequest->getVal( 'expiry' ) ) === false )
73 $error = wfMsgExt( 'restrictuser-badexpiry', 'parseinline', $wgRequest->getVal( 'expiry' ) );
74 else
75 foreach( $oldRestrictions as $r )
76 if( $r->isPage() && $r->getPage()->equals( $title ) )
77 $error = wfMsgExt( 'restrictuser-duptitle', 'parse' );
78 if( !$error ) {
79 self::doPageRestriction( $uid, $user );
80 $success = true;
81 }
82 }
83 $useRequestValues = $wgRequest->getVal( 'type' ) == UserRestriction::PAGE;
84 $legend = wfMsgHtml( 'restrictuser-legend-page' );
85 $wgOut->addHTML( "<fieldset><legend>{$legend}</legend>" );
86 if( $error )
87 $wgOut->addHTML( '<strong class="error">' . $error . '</strong>' );
88 if( $success )
89 $wgOut->addHTML( '<strong class="success">' . wfMsgExt( 'restrictuser-success',
90 'parseinline', $user ) . '</strong>' );
91 $wgOut->addHTML( Xml::openElement( 'form', array( 'action' => $wgTitle->getLocalUrl(),
92 'method' => 'post' ) ) );
93 $wgOut->addHTML( Xml::hidden( 'type', UserRestriction::PAGE ) );
94 $wgOut->addHTML( Xml::hidden( 'edittoken', $wgUser->editToken() ) );
95 $wgOut->addHTML( Xml::hidden( 'user', $user ) );
96 $form = array();
97 $form['restrictuser-title'] = Xml::input( 'page', false,
98 $useRequestValues ? $wgRequest->getVal( 'page' ) : false );
99 $form['restrictuser-expiry'] = Xml::input( 'expiry', false,
100 $useRequestValues ? $wgRequest->getVal( 'expiry' ) : false );
101 $form['restrictuser-reason'] = Xml::input( 'reason', false,
102 $useRequestValues ? $wgRequest->getVal( 'reason' ) : false );
103 $wgOut->addHTML( Xml::buildForm( $form, 'restrictuser-sumbit' ) );
104 $wgOut->addHTML( "</form></fieldset>" );
105 }
106
107 public static function doPageRestriction( $uid, $user ) {
108 global $wgUser, $wgRequest;
109 $r = new UserRestriction();
110 $r->setType( UserRestriction::PAGE );
111 $r->setPage( Title::newFromText( $wgRequest->getVal( 'page' ) ) );
112 $r->setSubjectId( $uid );
113 $r->setSubjectText( $user );
114 $r->setBlockerId( $wgUser->getId() );
115 $r->setBlockerText( $wgUser->getName() );
116 $r->setReason( $wgRequest->getVal( 'reason' ) );
117 $r->setExpiry( UserRestriction::convertExpiry( $wgRequest->getVal( 'expiry' ) ) );
118 $r->setTimestamp( wfTimestampNow( TS_MW ) );
119 $r->commit();
120 $logExpiry = $wgRequest->getVal( 'expiry' ) ? $wgRequest->getVal( 'expiry' ) : Block::infinity();
121 $l = new LogPage( 'restrict' );
122 $l->addEntry( 'restrict', Title::makeTitle( NS_USER, $user ), $r->getReason(),
123 array( $r->getType(), $r->getPage()->getFullText(), $logExpiry) );
124 }
125
126 public static function namespaceRestrictionForm( $uid, $user, $oldRestrictions ) {
127 global $wgOut, $wgTitle, $wgRequest, $wgUser, $wgContLang;
128 $error = '';
129 $success = false;
130 if( $wgRequest->wasPosted() && $wgRequest->getVal( 'type' ) == UserRestriction::NAMESPACE &&
131 $wgUser->matchEditToken( $wgRequest->getVal( 'edittoken' ) ) ) {
132 $ns = $wgRequest->getVal( 'namespace' );
133 if( $wgContLang->getNsText( $ns ) === false )
134 $error = wfMsgExt( 'restrictuser-badnamespace', 'parseinline' );
135 elseif( UserRestriction::convertExpiry( $wgRequest->getVal( 'expiry' ) ) === false )
136 $error = wfMsgExt( 'restrictuser-badexpiry', 'parseinline', $wgRequest->getVal( 'expiry' ) );
137 else
138 foreach( $oldRestrictions as $r )
139 if( $r->isNamespace() && $r->getNamespace() == $ns )
140 $error = wfMsgExt( 'restrictuser-dupnamespace', 'parse' );
141 if( !$error ) {
142 self::doNamespaceRestriction( $uid, $user );
143 $success = true;
144 }
145 }
146 $useRequestValues = $wgRequest->getVal( 'type' ) == UserRestriction::NAMESPACE;
147 $legend = wfMsgHtml( 'restrictuser-legend-namespace' );
148 $wgOut->addHTML( "<fieldset><legend>{$legend}</legend>" );
149 if( $error )
150 $wgOut->addHTML( '<strong class="error">' . $error . '</strong>' );
151 if( $success )
152 $wgOut->addHTML( '<strong class="success">' . wfMsgExt( 'restrictuser-success',
153 'parseinline', $user ) . '</strong>' );
154 $wgOut->addHTML( Xml::openElement( 'form', array( 'action' => $wgTitle->getLocalUrl(),
155 'method' => 'post' ) ) );
156 $wgOut->addHTML( Xml::hidden( 'type', UserRestriction::NAMESPACE ) );
157 $wgOut->addHTML( Xml::hidden( 'edittoken', $wgUser->editToken() ) );
158 $wgOut->addHTML( Xml::hidden( 'user', $user ) );
159 $form = array();
160 $form['restrictuser-namespace'] = Xml::namespaceSelector( $wgRequest->getVal( 'namespace' ) );
161 $form['restrictuser-expiry'] = Xml::input( 'expiry', false,
162 $useRequestValues ? $wgRequest->getVal( 'expiry' ) : false );
163 $form['restrictuser-reason'] = Xml::input( 'reason', false,
164 $useRequestValues ? $wgRequest->getVal( 'reason' ) : false );
165 $wgOut->addHTML( Xml::buildForm( $form, 'restrictuser-sumbit' ) );
166 $wgOut->addHTML( "</form></fieldset>" );
167 }
168
169 public static function doNamespaceRestriction( $uid, $user ) {
170 global $wgUser, $wgRequest;
171 $r = new UserRestriction();
172 $r->setType( UserRestriction::NAMESPACE );
173 $r->setNamespace( $wgRequest->getVal( 'namespace' ) );
174 $r->setSubjectId( $uid );
175 $r->setSubjectText( $user );
176 $r->setBlockerId( $wgUser->getId() );
177 $r->setBlockerText( $wgUser->getName() );
178 $r->setReason( $wgRequest->getVal( 'reason' ) );
179 $r->setExpiry( UserRestriction::convertExpiry( $wgRequest->getVal( 'expiry' ) ) );
180 $r->setTimestamp( wfTimestampNow( TS_MW ) );
181 $r->commit();
182 $logExpiry = $wgRequest->getVal( 'expiry' ) ? $wgRequest->getVal( 'expiry' ) : Block::infinity();
183 $l = new LogPage( 'restrict' );
184 $l->addEntry( 'restrict', Title::makeTitle( NS_USER, $user ), $r->getReason(),
185 array( $r->getType(), $r->getNamespace(), $logExpiry ) );
186 }
187 }