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