SpecialActiveUsers.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <?php
  2. /**
  3. * Implements Special:Activeusers
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along
  16. * with this program; if not, write to the Free Software Foundation, Inc.,
  17. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. * http://www.gnu.org/copyleft/gpl.html
  19. *
  20. * @file
  21. * @ingroup SpecialPage
  22. */
  23. /**
  24. * Implements Special:Activeusers
  25. *
  26. * @ingroup SpecialPage
  27. */
  28. class SpecialActiveUsers extends SpecialPage {
  29. public function __construct() {
  30. parent::__construct( 'Activeusers' );
  31. }
  32. /**
  33. * @param string|null $par Parameter passed to the page or null
  34. */
  35. public function execute( $par ) {
  36. $out = $this->getOutput();
  37. $this->setHeaders();
  38. $this->outputHeader();
  39. $opts = new FormOptions();
  40. $opts->add( 'username', '' );
  41. $opts->add( 'groups', [] );
  42. $opts->add( 'excludegroups', [] );
  43. // Backwards-compatibility with old URLs
  44. $opts->add( 'hidebots', false, FormOptions::BOOL );
  45. $opts->add( 'hidesysops', false, FormOptions::BOOL );
  46. $opts->fetchValuesFromRequest( $this->getRequest() );
  47. if ( $par !== null ) {
  48. $opts->setValue( 'username', $par );
  49. }
  50. $pager = new ActiveUsersPager( $this->getContext(), $opts );
  51. $usersBody = $pager->getBody();
  52. $this->buildForm();
  53. if ( $usersBody ) {
  54. $out->addHTML(
  55. $pager->getNavigationBar() .
  56. Html::rawElement( 'ul', [], $usersBody ) .
  57. $pager->getNavigationBar()
  58. );
  59. $out->addModuleStyles( 'mediawiki.interface.helpers.styles' );
  60. } else {
  61. $out->addWikiMsg( 'activeusers-noresult' );
  62. }
  63. }
  64. /**
  65. * Generate and output the form
  66. */
  67. protected function buildForm() {
  68. $groups = User::getAllGroups();
  69. $options = [];
  70. foreach ( $groups as $group ) {
  71. $msg = htmlspecialchars( UserGroupMembership::getGroupName( $group ) );
  72. $options[$msg] = $group;
  73. }
  74. asort( $options );
  75. // Backwards-compatibility with old URLs
  76. $req = $this->getRequest();
  77. $excludeDefault = [];
  78. if ( $req->getCheck( 'hidebots' ) ) {
  79. $excludeDefault[] = 'bot';
  80. }
  81. if ( $req->getCheck( 'hidesysops' ) ) {
  82. $excludeDefault[] = 'sysop';
  83. }
  84. $formDescriptor = [
  85. 'username' => [
  86. 'type' => 'user',
  87. 'name' => 'username',
  88. 'label-message' => 'activeusers-from',
  89. ],
  90. 'groups' => [
  91. 'type' => 'multiselect',
  92. 'dropdown' => true,
  93. 'flatlist' => true,
  94. 'name' => 'groups',
  95. 'label-message' => 'activeusers-groups',
  96. 'options' => $options,
  97. ],
  98. 'excludegroups' => [
  99. 'type' => 'multiselect',
  100. 'dropdown' => true,
  101. 'flatlist' => true,
  102. 'name' => 'excludegroups',
  103. 'label-message' => 'activeusers-excludegroups',
  104. 'options' => $options,
  105. 'default' => $excludeDefault,
  106. ],
  107. ];
  108. HTMLForm::factory( 'ooui', $formDescriptor, $this->getContext() )
  109. // For the 'multiselect' field values to be preserved on submit
  110. ->setFormIdentifier( 'specialactiveusers' )
  111. ->setIntro( $this->getIntroText() )
  112. ->setWrapperLegendMsg( 'activeusers' )
  113. ->setSubmitTextMsg( 'activeusers-submit' )
  114. // prevent setting subpage and 'username' parameter at the same time
  115. ->setAction( $this->getPageTitle()->getLocalURL() )
  116. ->setMethod( 'get' )
  117. ->prepareForm()
  118. ->displayForm( false );
  119. }
  120. /**
  121. * Return introductory message.
  122. * @return string
  123. */
  124. protected function getIntroText() {
  125. $days = $this->getConfig()->get( 'ActiveUserDays' );
  126. $intro = $this->msg( 'activeusers-intro' )->numParams( $days )->parse();
  127. // Mention the level of cache staleness...
  128. $dbr = wfGetDB( DB_REPLICA, 'recentchanges' );
  129. $rcMax = $dbr->selectField( 'recentchanges', 'MAX(rc_timestamp)', '', __METHOD__ );
  130. if ( $rcMax ) {
  131. $cTime = $dbr->selectField( 'querycache_info',
  132. 'qci_timestamp',
  133. [ 'qci_type' => 'activeusers' ],
  134. __METHOD__
  135. );
  136. if ( $cTime ) {
  137. $secondsOld = wfTimestamp( TS_UNIX, $rcMax ) - wfTimestamp( TS_UNIX, $cTime );
  138. } else {
  139. $rcMin = $dbr->selectField( 'recentchanges', 'MIN(rc_timestamp)' );
  140. $secondsOld = time() - wfTimestamp( TS_UNIX, $rcMin );
  141. }
  142. if ( $secondsOld > 0 ) {
  143. $intro .= $this->msg( 'cachedspecial-viewing-cached-ttl' )
  144. ->durationParams( $secondsOld )->parseAsBlock();
  145. }
  146. }
  147. return $intro;
  148. }
  149. protected function getGroupName() {
  150. return 'users';
  151. }
  152. }