PageRenderTime 26ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/extensions/Nuke/Nuke_body.php

https://github.com/ChuguluGames/mediawiki-svn
PHP | 268 lines | 203 code | 38 blank | 27 comment | 29 complexity | dfc8c487f1a2a0525e81745a94fc5054 MD5 | raw file
  1. <?php
  2. class SpecialNuke extends SpecialPage {
  3. public function __construct() {
  4. parent::__construct( 'Nuke', 'nuke' );
  5. }
  6. public function execute( $par ){
  7. global $wgUser, $wgRequest;
  8. if( !$this->userCanExecute( $wgUser ) ){
  9. $this->displayRestrictionError();
  10. return;
  11. }
  12. $this->setHeaders();
  13. $this->outputHeader();
  14. if( $wgRequest->wasPosted() && $wgUser->matchEditToken( $wgRequest->getVal( 'wpEditToken' ) ) ) {
  15. $target = $wgRequest->getText( 'target', $par );
  16. // Normalise name
  17. if ( $target !== '' ) {
  18. $user = User::newFromName( $target );
  19. if ( $user ) $target = $user->getName();
  20. }
  21. $reason = $wgRequest->getText(
  22. 'wpReason',
  23. wfMsgForContent(
  24. 'nuke-defaultreason',
  25. $target === '' ? wfMsg( 'nuke-multiplepeople' ) : $target
  26. )
  27. );
  28. if ( $wgRequest->getVal( 'action' ) == 'delete' ) {
  29. $pages = $wgRequest->getArray( 'pages' );
  30. if( $pages ) {
  31. return $this->doDelete( $pages, $reason );
  32. }
  33. }
  34. else {
  35. $this->listForm( $target, $reason, $wgRequest->getInt( 'limit' ) );
  36. }
  37. }
  38. else {
  39. $this->promptForm();
  40. }
  41. }
  42. /**
  43. * Prompt for a username or IP address.
  44. */
  45. protected function promptForm() {
  46. global $wgOut, $wgUser;
  47. $wgOut->addWikiMsg( 'nuke-tools' );
  48. $wgOut->addHTML(
  49. Xml::openElement(
  50. 'form',
  51. array(
  52. 'action' => $this->getTitle()->getLocalURL( 'action=submit' ),
  53. 'method' => 'post'
  54. )
  55. )
  56. . '<table><tr>'
  57. . '<td>' . htmlspecialchars( wfMsg( 'nuke-userorip' ) ) . '</td>'
  58. . '<td>' . Xml::input( 'target', 40 ) . '</td>'
  59. . '</tr><tr>'
  60. . '<td>' . htmlspecialchars( wfMsg( 'nuke-maxpages' ) ) . '</td>'
  61. . '<td>' . Xml::input( 'limit', 7, '500' ) . '</td>'
  62. . '</tr><tr>'
  63. . '<td></td>'
  64. . '<td>' . Xml::submitButton( wfMsg( 'nuke-submit-user' ) ) . '</td>'
  65. .'</tr></table>'
  66. . Html::hidden( 'wpEditToken', $wgUser->editToken() )
  67. . Xml::closeElement( 'form' )
  68. );
  69. }
  70. /**
  71. * Display list of pages to delete.
  72. *
  73. * @param string $username
  74. * @param string $reason
  75. * @param integer $limit
  76. */
  77. protected function listForm( $username, $reason, $limit ) {
  78. global $wgUser, $wgOut, $wgLang;
  79. $pages = $this->getNewPages( $username, $limit );
  80. if( count( $pages ) == 0 ) {
  81. $wgOut->addWikiMsg( 'nuke-nopages', $username );
  82. return $this->promptForm();
  83. }
  84. if ( $username == '' ) {
  85. $wgOut->addWikiMsg( 'nuke-list-multiple' );
  86. }
  87. else {
  88. $wgOut->addWikiMsg( 'nuke-list', $username );
  89. }
  90. $nuke = $this->getTitle();
  91. $script = <<<JAVASCRIPT
  92. <script type="text/javascript">
  93. function selectPages( bool ) {
  94. var inputs = document.getElementsByTagName("input");
  95. for (i = 0; i < inputs.length; i++) {
  96. if (inputs[i].type == "checkbox") {
  97. inputs[i].checked = bool;
  98. }
  99. }
  100. }
  101. </script>
  102. JAVASCRIPT;
  103. $wgOut->addScript( $script );
  104. $wgOut->addHTML(
  105. Xml::openElement( 'form', array(
  106. 'action' => $nuke->getLocalURL( 'action=delete' ),
  107. 'method' => 'post',
  108. 'name' => 'nukelist')
  109. ) .
  110. Html::hidden( 'wpEditToken', $wgUser->editToken() ) .
  111. Xml::tags( 'p',
  112. null,
  113. Xml::inputLabel(
  114. wfMsg( 'deletecomment' ), 'wpReason', 'wpReason', 60, $reason
  115. )
  116. )
  117. );
  118. // Select: All, None
  119. $links = array();
  120. $links[] = '<a href="#" onclick="selectPages( true ); return false;">' .
  121. wfMsg( 'powersearch-toggleall' ) . '</a>';
  122. $links[] = '<a href="#" onclick="selectPages( false ); return false;">' .
  123. wfMsg( 'powersearch-togglenone' ) . '</a>';
  124. $wgOut->addHTML(
  125. Xml::tags( 'p',
  126. null,
  127. wfMsg( 'nuke-select', $wgLang->commaList( $links ) )
  128. )
  129. );
  130. // Delete button
  131. $wgOut->addHTML(
  132. Xml::submitButton( wfMsg( 'nuke-submit-delete' ) )
  133. );
  134. $wgOut->addHTML( '<ul>' );
  135. $sk = $wgUser->getSkin();
  136. foreach( $pages as $info ) {
  137. list( $title, $edits, $userName ) = $info;
  138. $image = $title->getNamespace() == NS_IMAGE ? wfLocalFile( $title ) : false;
  139. $thumb = $image && $image->exists() ? $image->transform( array( 'width' => 120, 'height' => 120 ), 0 ) : false;
  140. $changes = wfMsgExt( 'nchanges', 'parsemag', $wgLang->formatNum( $edits ) );
  141. $wgOut->addHTML( '<li>' .
  142. Xml::check( 'pages[]', true,
  143. array( 'value' => $title->getPrefixedDbKey() )
  144. ) .
  145. '&#160;' .
  146. ( $thumb ? $thumb->toHtml( array( 'desc-link' => true ) ) : '' ) .
  147. $sk->makeKnownLinkObj( $title ) .
  148. '&#160;(' .
  149. ( $userName ? wfMsgExt( 'nuke-editby', 'parseinline', $userName ) . ',&#160;' : '' ) .
  150. $sk->makeKnownLinkObj( $title, $changes, 'action=history' ) .
  151. ")</li>\n" );
  152. }
  153. $wgOut->addHTML(
  154. "</ul>\n" .
  155. Xml::submitButton( wfMsg( 'nuke-submit-delete' ) ) .
  156. "</form>"
  157. );
  158. }
  159. /**
  160. * Gets a list of new pages by the specified user or everyone when none is specified.
  161. *
  162. * @param string $username
  163. * @param integer $limit
  164. *
  165. * @return array
  166. */
  167. protected function getNewPages( $username, $limit ) {
  168. $dbr = wfGetDB( DB_SLAVE );
  169. $what = array(
  170. 'rc_namespace',
  171. 'rc_title',
  172. 'rc_timestamp',
  173. 'COUNT(*) AS edits'
  174. );
  175. $where = array( "(rc_new = 1) OR (rc_log_type = 'upload' AND rc_log_action = 'upload')" );
  176. if ( $username == '' ) {
  177. $what[] = 'rc_user_text';
  178. }
  179. else {
  180. $where['rc_user_text'] = $username;
  181. }
  182. $result = $dbr->select( 'recentchanges',
  183. $what,
  184. $where,
  185. __METHOD__,
  186. array(
  187. 'ORDER BY' => 'rc_timestamp DESC',
  188. 'GROUP BY' => 'rc_namespace, rc_title',
  189. 'LIMIT' => $limit
  190. )
  191. );
  192. $pages = array();
  193. foreach ( $result as $row ) {
  194. $pages[] = array(
  195. Title::makeTitle( $row->rc_namespace, $row->rc_title ),
  196. $row->edits,
  197. $username == '' ? $row->rc_user_text : false
  198. );
  199. }
  200. $dbr->freeResult( $result );
  201. return $pages;
  202. }
  203. /**
  204. * Does the actual deletion of the pages.
  205. *
  206. * @param array $pages The pages to delete
  207. * @param string $reason
  208. */
  209. protected function doDelete( array $pages, $reason ) {
  210. global $wgOut;
  211. $res = array();
  212. foreach( $pages as $page ) {
  213. $title = Title::newFromURL( $page );
  214. $file = $title->getNamespace() == NS_FILE ? wfLocalFile( $title ) : false;
  215. if ( $file ) {
  216. $oldimage = null; // Must be passed by reference
  217. $ok = FileDeleteForm::doDelete( $title, $file, $oldimage, $reason, false )->isOK();
  218. } else {
  219. $article = new Article( $title, 0 );
  220. $ok = $article->doDeleteArticle( $reason );
  221. }
  222. if ( $ok ) {
  223. $res[] = wfMsgExt( 'nuke-deleted', array( 'parseinline' ), $title->getPrefixedText() );
  224. } else {
  225. $res[] = wfMsgExt( 'nuke-not-deleted', array( 'parseinline' ), $title->getPrefixedText() );
  226. }
  227. }
  228. $wgOut->addHTML( "<ul>\n<li>" .implode( "</li>\n<li>", $res ) . "</li>\n</ul>\n" );
  229. }
  230. }