You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

321 lines
12KB

  1. <?php
  2. # version 0.41
  3. class UserRightsList extends UserrightsPage {
  4. function __construct(){
  5. SpecialPage::__construct('UserRightsList');
  6. # SpecialPage::SpecialPage("UserRightsList", 'createaccount');
  7. self::loadMessages();
  8. $this->offset=0;
  9. $this->limit=50;
  10. list ($this->user_table,$this->user_groups_table) = wfGetDB(DB_MASTER)->tableNamesN('user','user_groups');
  11. return true;
  12. }
  13. public function userCanExecute( $user ) {
  14. global $egUserRightsListChGrp, $wgAddGroups, $wgRemoveGroups;
  15. if (!isset($egUserRightsListChGrp)) return true;
  16. foreach ($egUserRightsListChGrp as $group=>$chgrps){
  17. foreach ($chgrps as $grp) $wgAddGroups[$group][] = $grp;
  18. foreach ($chgrps as $grp) $wgRemoveGroups[$group][] = $grp;
  19. }
  20. return parent::userCanExecute( $user );
  21. }
  22. function execute( $par ) {
  23. global $wgRequest, $wgOut, $wgUser;
  24. $this->setHeaders();
  25. if( !$this->userCanExecute( $wgUser ) ) {
  26. // fixme... there may be intermediate groups we can mention.
  27. global $wgOut;
  28. $wgOut->showPermissionsErrorPage( array(
  29. $wgUser->isAnon()
  30. ? 'userrights-nologin'
  31. : 'userrights-notallowed' ) );
  32. return true;
  33. }
  34. # Get request data from, e.g.
  35. $fields = array('yearfrom','yearto','monthfrom','monthto','username','offset','limit','group');
  36. foreach($fields as $field){
  37. if (!is_null($wgRequest->getVal($field))) $this->$field = $wgRequest->getVal($field);
  38. }
  39. if ($wgRequest->getText('act') == 'save') $this->save_rights();
  40. $output = $this->make_form($this->findMyUsers());
  41. $wgOut->addHTML( $output );
  42. return true;
  43. }
  44. function save_rights(){
  45. global $wgRequest;
  46. $users = $this->findMyUsers();
  47. foreach ($users as $user){
  48. $u = User::newFromId($user['user_id']);
  49. if(is_object($u)) {
  50. $oldGroups = $u->getGroups();
  51. $newGroups = $wgRequest->getArray('user_'.$user['user_id']);
  52. if(is_null($wgRequest->getArray('user_'.$user['user_id']))) $newGroups = array();;
  53. // remove then add groups
  54. $removegroup = array_diff($oldGroups, $newGroups);
  55. $addgroup = array_diff($newGroups, $oldGroups);
  56. if (count(array_merge($removegroup, $addgroup)) == 0) continue;
  57. # for 1.13
  58. $wgRequest->data['user'] = $u->getName();
  59. $wgRequest->data['wpEditToken'] = $u->editToken();
  60. foreach ($newGroups as $group) $wgRequest->data['wpGroup-'.$group] = 1;
  61. UserrightsPage::saveUserGroups( $u->getName(), $removegroup, $addgroup);
  62. }
  63. }
  64. return true;
  65. }
  66. /*
  67. # functions from Special::Userrights
  68. function fetchUser( $username ) {
  69. $user = UserrightsPage::fetchUser( $username );
  70. return $user;
  71. }
  72. function getAllGroups() {
  73. return User::getAllGroups();
  74. }
  75. function addLogEntry( $user, $oldGroups, $newGroups ) {
  76. return UserrightsPage::addLogEntry( $user, $oldGroups, $newGroups ) ;
  77. }
  78. function changeableByGroup( $group ) {
  79. global $wgAddGroups, $wgRemoveGroups;
  80. return UserrightsPage::changeableByGroup($group);
  81. }
  82. function changeableGroups( ) {
  83. return UserrightsPage::changeableGroups();
  84. }
  85. function makeGroupNameList( $ids ) {
  86. return implode( ', ', $ids );
  87. }
  88. */
  89. # takes an array of users where each user is a hash
  90. # user_id, user_name, log_timestamp
  91. function make_form($users){
  92. global $wgUser;
  93. $form = $this->pageTop();
  94. if (count($users) == 0) return $form.wfMsg('nousersfound');
  95. $form .= $this->navLinks();
  96. $form .= "<br/><form method='post'><table>\n";
  97. $row = 1; $style = array('',"bgcolor = '#dddddd'");
  98. $changeable = UserrightsPage::changeableGroups();
  99. $changeable_groups = array_unique($changeable['add']+$changeable['remove']+$changeable['add-self']+$changeable['remove-self']);
  100. foreach ($users as $user){
  101. $mwUser = User::newFromId($user['user_id']);
  102. $mwUser->loadFromId();
  103. $form .= "<tr valign='bottom' ".$style[$row]."><td>".$user['user_name'].":</td>";
  104. foreach ($changeable_groups as $group){
  105. if (in_array($group, User::getAllGroups())){
  106. $checked = '';
  107. if (in_array($group, $mwUser->getGroups())) $checked = 'checked';
  108. $form .= "<td><input name='user.".$user['user_id']."[]' id='".$user['user_id']."' type='checkbox' value = '$group' $checked>$group</input></td>";
  109. }
  110. }
  111. $form .= "</tr>\n";
  112. $row++;
  113. $row = $row%2;
  114. }
  115. $form .= "</table>";
  116. # Preserve params
  117. if( isset($this->offset) )
  118. $form .= wfElement( 'input', array( 'type' => 'hidden', 'name' => 'offset', 'value' => $this->offset ) );
  119. if( isset($this->limit) )
  120. $form .= wfElement( 'input', array( 'type' => 'hidden', 'name' => 'limit', 'value' => $this->limit ) );
  121. if( isset($this->yearfrom) )
  122. $form .= wfElement( 'input', array( 'type' => 'hidden', 'name' => 'yearfrom', 'value' => $this->yearfrom ) );
  123. if( isset($this->monthfrom) )
  124. $form .= wfElement( 'input', array( 'type' => 'hidden', 'name' => 'monthfrom', 'value' => $this->monthfrom ) );
  125. if( isset($this->yearto) )
  126. $form .= wfElement( 'input', array( 'type' => 'hidden', 'name' => 'yearto', 'value' => $this->yearto ) );
  127. if( isset($this->monthto) )
  128. $form .= wfElement( 'input', array( 'type' => 'hidden', 'name' => 'monthto', 'value' => $this->monthto ) );
  129. if( isset($this->username) )
  130. $form .= wfElement( 'input', array( 'type' => 'hidden', 'name' => 'username', 'value' => $this->username ) );
  131. if( isset($this->group) )
  132. $form .= wfElement( 'input', array( 'type' => 'hidden', 'name' => 'group', 'value' => $this->group ) );
  133. $form .="
  134. <input name='act' type='submit' value='save'>
  135. </form>\n";
  136. $form .= $this->navLinks();
  137. return $form;
  138. }
  139. function pageTop(){
  140. $self = $this->getTitle();
  141. $out = '<p>';
  142. # Form tag
  143. $out .= wfOpenElement( 'form', array( 'method' => 'post', 'action' => $self->getLocalUrl() ) );
  144. # Group drop-down list
  145. $out .= wfElement( 'label', array( 'for' => 'group' ), wfMsg( 'group' ) ) . ' ';
  146. $out .= wfOpenElement( 'select', array( 'name' => 'group' ) );
  147. $out .= wfElement( 'option', array( 'value' => '' ), wfMsg( 'group-all' ) ); # Item for "all groups"
  148. $groups = User::getAllGroups();
  149. foreach( $groups as $group ) {
  150. $attribs = array( 'value' => $group );
  151. if( isset($this->group) && $group == $this->group ) $attribs['selected'] = 'selected';
  152. $out .= wfElement( 'option', $attribs, User::getGroupName( $group ) );
  153. }
  154. $out .= wfCloseElement( 'select' ) . ' ';# . wfElement( 'br' );
  155. # Username field
  156. $out .= wfElement( 'label', array( 'for' => 'username' ), wfMsg( 'usernamelike' ) ) . '</td><td>';
  157. $out .= wfElement( 'input', array( 'type' => 'text', 'id' => 'username', 'name' => 'username',
  158. 'value' => @$this->username ) ) . ' ';
  159. $out .= wfElement( 'label', array( 'for' => 'year' ), wfMsg( 'regafter' ) ) . ' ';
  160. $years = $this->getYears();
  161. $out .= $this->yearMenu($years, 'yearfrom');
  162. $out .= $this->monthMenu('monthfrom').' ';
  163. $out .= wfElement( 'label', array( 'for' => 'year' ), wfMsg( 'regbefore' ) ) . ' ';
  164. $out .= $this->yearMenu($years, 'yearto');
  165. $out .= $this->monthMenu('monthto');
  166. # Submit button and form bottom
  167. $out .= wfElement( 'input', array( 'type' => 'submit', 'value' => wfMsg( 'allpagessubmit' ) ) );
  168. $out .= wfCloseElement( 'form' );
  169. $out .= '</p>';
  170. $out .= "<hr>";
  171. return $out;
  172. }
  173. function getYears(){
  174. $dbr =& wfGetDB( DB_SLAVE );
  175. $years = array();
  176. $result = $dbr->selectRow(
  177. $this->user_table,
  178. 'user_registration',
  179. 'user_registration IS NOT NULL',
  180. __METHOD__,
  181. array('ORDER BY' => 'user_registration')
  182. );
  183. $y = 2000;
  184. $thisyear = date("Y");
  185. if (is_object($result)) $y = substr(wfTimeStamp(TS_MW, $result->user_registration),0,4);
  186. for ($year = $y; $year <= $thisyear; $year++) $years[] = $year;
  187. return $years;
  188. }
  189. # Year drop-down list
  190. function yearMenu($years, $item = 'yearfrom'){
  191. $out = wfOpenElement( 'select', array( 'name' => $item ) );
  192. $out .= wfElement( 'option', array( 'value' => '' ), wfMsg( 'group-all' ) ); # Item for "all years"
  193. foreach( $years as $year ) {
  194. $attribs = array( 'value' => $year );
  195. if( isset ($this->$item) && $year == $this->$item )
  196. $attribs['selected'] = 'selected';
  197. $out .= wfElement( 'option', $attribs, $year );
  198. }
  199. $out .= wfCloseElement( 'select' ) . ' ';# . wfElement( 'br' );
  200. return $out;
  201. }
  202. function monthMenu($item){
  203. global $wgContLang;
  204. $out = wfOpenElement( 'select', array( 'name' => $item ) );
  205. $out .= wfElement( 'option', array( 'value' => '' ), wfMsg( 'group-all' ) ); # Item for "all months"
  206. for( $i = 1; $i <= 12; $i++ ) {
  207. $month = str_pad($i,2,'0',STR_PAD_LEFT);
  208. $monthName = $wgContLang->getMonthAbbreviation( $i );
  209. $attribs = array( 'value' => $month );
  210. if( isset ($this->$item) && $month == $this->$item )
  211. $attribs['selected'] = 'selected';
  212. $out .= wfElement( 'option', $attribs, $monthName );
  213. }
  214. $out .= wfCloseElement( 'select' ) . ' ';# . wfElement( 'br' );
  215. return $out;
  216. }
  217. function navLinks(){
  218. global $wgContLang;
  219. $atend = $this->num < $this->limit;
  220. $params = array();
  221. if( isset($this->yearfrom) ) $params['yearfrom'] = $this->yearfrom;
  222. if( isset($this->monthfrom) ) $params['monthfrom'] = $this->monthfrom;
  223. if( isset($this->yearto) ) $params['yearto'] = $this->yearto;
  224. if( isset($this->monthto) ) $params['monthto'] = $this->monthto;
  225. if( isset($this->username) ) $params['username'] = $this->username;
  226. if( isset($this->group) ) $params['group'] = $this->group;
  227. return wfViewPrevNext(
  228. $this->offset,
  229. $this->limit ,
  230. $wgContLang->specialPage( $this->getName() ),
  231. wfArrayToCGI( $params ),
  232. $atend );
  233. }
  234. function findMyUsers(){
  235. global $wgUser, $wgDBprefix;
  236. $dbr =& wfGetDB( DB_SLAVE );
  237. $vars = array('user_id', 'user_name', 'user_registration');
  238. if($wgUser->isAllowed('userrights')){
  239. $table = array($this->user_table);
  240. $conds = array();
  241. }else{
  242. $table = array($this->user_table,'logging');
  243. $conds = array('log_title = user_name',
  244. "log_type = 'newusers'",
  245. "log_user = '".$wgUser->getID()."'");
  246. }
  247. if (isset($this->group) && $this->group !=''){
  248. $table[] = $this->user_groups_table;
  249. $conds = array_merge($conds, array(" ug_user = user_id", "ug_group = '".$this->group."'"));
  250. }
  251. if (isset($this->username) && !is_null($this->username) && $this->username != ''){
  252. $conds = array_merge($conds, array("user_name LIKE'".mysql_real_escape_string($this->username)."' "));
  253. }
  254. if (isset($this->yearfrom) && !is_null($this->yearfrom) && $this->yearfrom != ''){
  255. $month = '00';
  256. if (!is_null($this->monthfrom )) $month = $this->monthfrom;
  257. $fromdate = $dbr->timestamp(str_pad($this->yearfrom.$month, 14, '0', STR_PAD_RIGHT));
  258. $conds = array_merge($conds, array("user_registration >='$fromdate' "));
  259. }
  260. if (isset($this->yearto) && !is_null($this->yearto) && $this->yearto != ''){
  261. $year = $this->yearto;
  262. $month = '99';
  263. if (!is_null($this->monthto ) ) $month = $this->monthto;
  264. $todate = $dbr->timestamp(str_pad($year.$month, 14, '9', STR_PAD_RIGHT));
  265. $conds = array_merge($conds, array("user_registration <= '$todate'"));
  266. }
  267. $options["ORDER BY"] = "user_name";
  268. $options["LIMIT"] = $this->limit;
  269. $options["OFFSET"] = $this->offset;
  270. $results = $dbr->select($table, $vars, $conds, __METHOD__, $options);
  271. $this->num = $dbr->numRows($results);
  272. if (!$results) return array();
  273. while( $x = $dbr->fetchObject ( $results ) ) {
  274. $arr[] = get_object_vars($x);
  275. }
  276. #echo "<pre>";print_r($conds);print_r($dbr->lastQuery());echo "</pre>";
  277. return $arr;
  278. }
  279. function loadMessages() {
  280. static $messagesLoaded = false;
  281. global $wgMessageCache;
  282. if ( $messagesLoaded ) return true;
  283. $messagesLoaded = true;
  284. require( dirname( __FILE__ ) . '/SpecialUserRightsList.i18n.php' );
  285. foreach ( $allMessages as $lang => $langMessages ) {
  286. // $wgMessageCache->addMessages( $langMessages, $lang );
  287. }
  288. return true;
  289. }
  290. }
  291. ?>