SpecialListUserRestrictions.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <?php
  2. function wfSpecialListUserRestrictions() {
  3. global $wgOut, $wgRequest;
  4. $wgOut->addWikiMsg( 'listuserrestrictions-intro' );
  5. $f = new SpecialListUserRestrictionsForm();
  6. $wgOut->addHTML( $f->getHTML() );
  7. if( !mt_rand( 0, 10 ) )
  8. UserRestriction::purgeExpired();
  9. $pager = new UserRestrictionsPager( $f->getConds() );
  10. if( $pager->getNumRows() )
  11. $wgOut->addHTML( $pager->getNavigationBar() .
  12. Xml::tags( 'ul', null, $pager->getBody() ) .
  13. $pager->getNavigationBar()
  14. );
  15. elseif( $f->getConds() )
  16. $wgOut->addWikiMsg( 'listuserrestrictions-notfound' );
  17. else
  18. $wgOut->addWikiMsg( 'listuserrestrictions-empty' );
  19. }
  20. class SpecialListUserRestrictionsForm {
  21. public function getHTML() {
  22. global $wgRequest, $wgScript, $wgTitle;
  23. $action = htmlspecialchars( $wgScript );
  24. $s = '';
  25. $s .= Xml::fieldset( wfMsg( 'listuserrestrictions-legend' ) );
  26. $s .= "<form action=\"{$action}\">";
  27. $s .= Xml::hidden( 'title', $wgTitle->getPrefixedDbKey() );
  28. $s .= Xml::label( wfMsgHtml( 'listuserrestrictions-type' ), 'type' ) . '&nbsp;' .
  29. self::typeSelector( 'type', $wgRequest->getVal( 'type' ), 'type' );
  30. $s .= '&nbsp;';
  31. $s .= Xml::inputLabel( wfMsgHtml( 'listuserrestrictions-user' ), 'user', 'user',
  32. false, $wgRequest->getVal( 'user' ) );
  33. $s .= '<p>';
  34. $s .= Xml::label( wfMsgHtml( 'listuserrestrictions-namespace' ), 'namespace' ) . '&nbsp;' .
  35. Xml::namespaceSelector( $wgRequest->getVal( 'namespace' ), '', 'namespace' );
  36. $s .= '&nbsp;';
  37. $s .= Xml::inputLabel( wfMsgHtml( 'listuserrestrictions-page' ), 'page', 'page',
  38. false, $wgRequest->getVal( 'page' ) );
  39. $s .= Xml::submitButton( wfMsg( 'listuserrestrictions-submit' ) );
  40. $s .= "</p></form></fieldset>";
  41. return $s;
  42. }
  43. public static function typeSelector( $name = 'type', $value = '', $id = false ) {
  44. $s = new XmlSelect( $name, $id, $value );
  45. $s->addOption( wfMsg( 'userrestrictiontype-none' ), '' );
  46. $s->addOption( wfMsg( 'userrestrictiontype-page' ), UserRestriction::PAGE );
  47. $s->addOption( wfMsg( 'userrestrictiontype-namespace' ), UserRestriction::NAMESPACE );
  48. return $s->getHTML();
  49. }
  50. public function getConds() {
  51. global $wgRequest;
  52. $conds = array();
  53. $type = $wgRequest->getVal( 'type' );
  54. if( in_array( $type, array( UserRestriction::PAGE, UserRestriction::NAMESPACE ) ) )
  55. $conds['ur_type'] = $type;
  56. $user = $wgRequest->getVal( 'user' );
  57. if( $user )
  58. $conds['ur_user_text'] = $user;
  59. $namespace = $wgRequest->getVal( 'namespace' );
  60. if( $namespace || $namespace === '0' )
  61. $conds['ur_namespace'] = $namespace;
  62. $page = $wgRequest->getVal( 'page' );
  63. $title = Title::newFromText( $page );
  64. if( $title ) {
  65. $conds['ur_page_namespace'] = $title->getNamespace();
  66. $conds['ur_page_title'] = $title->getDBKey();
  67. }
  68. return $conds;
  69. }
  70. }
  71. class UserRestrictionsPager extends ReverseChronologicalPager {
  72. public $mConds;
  73. public function __construct( $conds = array() ) {
  74. $this->mConds = $conds;
  75. parent::__construct();
  76. }
  77. public function getStartBody() {
  78. # Copied from Special:Ipblocklist
  79. wfProfileIn( __METHOD__ );
  80. # Do a link batch query
  81. $this->mResult->seek( 0 );
  82. $lb = new LinkBatch;
  83. # Faster way
  84. # Usernames and titles are in fact related by a simple substitution of space -> underscore
  85. # The last few lines of Title::secureAndSplit() tell the story.
  86. foreach( $this->mResult as $row ) {
  87. $name = str_replace( ' ', '_', $row->ur_by_text );
  88. $lb->add( NS_USER, $name );
  89. $lb->add( NS_USER_TALK, $name );
  90. $name = str_replace( ' ', '_', $row->ur_user_text );
  91. $lb->add( NS_USER, $name );
  92. $lb->add( NS_USER_TALK, $name );
  93. if( $row->ur_type == UserRestriction::PAGE )
  94. $lb->add( $row->ur_page_namespace, $row->ur_page_title );
  95. }
  96. $lb->execute();
  97. wfProfileOut( __METHOD__ );
  98. return '';
  99. }
  100. public function getQueryInfo() {
  101. return array(
  102. 'tables' => 'user_restrictions',
  103. 'fields' => '*',
  104. 'conds' => $this->mConds,
  105. );
  106. }
  107. public function formatRow( $row ) {
  108. return self::formatRestriction( UserRestriction::newFromRow( $row ) );
  109. }
  110. // Split off for use on Special:RestrictUser
  111. public static function formatRestriction( $r ) {
  112. global $wgUser, $wgLang;
  113. $sk = $wgUser->getSkin();
  114. $timestamp = $wgLang->timeanddate( $r->getTimestamp(), true );
  115. $blockerlink = $sk->userLink( $r->getBlockerId(), $r->getBlockerText() ) .
  116. $sk->userToolLinks( $r->getBlockerId(), $r->getBlockerText() );
  117. $subjlink = $sk->userLink( $r->getSubjectId(), $r->getSubjectText() ) .
  118. $sk->userToolLinks( $r->getSubjectId(), $r->getSubjectText() );
  119. $expiry = is_numeric( $r->getExpiry() ) ?
  120. wfMsg( 'listuserrestrictions-row-expiry', $wgLang->timeanddate( $r->getExpiry() ) ) :
  121. wfMsg( 'ipbinfinite' );
  122. $msg = '';
  123. if( $r->isNamespace() ) {
  124. $msg = wfMsgHtml( 'listuserrestrictions-row-ns', $subjlink,
  125. $wgLang->getDisplayNsText( $r->getNamespace() ), $expiry );
  126. }
  127. if( $r->isPage() ) {
  128. $pagelink = $sk->link( $r->getPage() );
  129. $msg = wfMsgHtml( 'listuserrestrictions-row-page', $subjlink,
  130. $pagelink, $expiry );
  131. }
  132. $reason = $sk->commentBlock( $r->getReason() );
  133. $removelink = '';
  134. if( $wgUser->isAllowed( 'restrict' ) ) {
  135. $removelink = '(' . $sk->link( SpecialPage::getTitleFor( 'RemoveRestrictions' ),
  136. wfMsgHtml( 'listuserrestrictions-remove' ), array(), array( 'id' => $r->getId() ) ) . ')';
  137. }
  138. return "<li>{$timestamp}, {$blockerlink} {$msg} {$reason} {$removelink}</li>\n";
  139. }
  140. public function getIndexField() {
  141. return 'ur_timestamp';
  142. }
  143. }