[ Index ]

PHP Cross Reference of MediaWiki-1.24.0

title

Body

[close]

/extensions/Nuke/ -> Nuke_body.php (source)

   1  <?php
   2  
   3  class SpecialNuke extends SpecialPage {
   4  
   5  	public function __construct() {
   6          parent::__construct( 'Nuke', 'nuke' );
   7      }
   8  
   9  	public function execute( $par ) {
  10          if ( !$this->userCanExecute( $this->getUser() ) ) {
  11              $this->displayRestrictionError();
  12          }
  13          $this->setHeaders();
  14          $this->outputHeader();
  15  
  16          if ( $this->getUser()->isBlocked() ) {
  17              $block = $this->getUser()->getBlock();
  18              throw new UserBlockedError( $block );
  19          }
  20  
  21          if ( method_exists( $this, 'checkReadOnly' ) ) {
  22              // checkReadOnly was introduced only in 1.19
  23              $this->checkReadOnly();
  24          }
  25  
  26          $req = $this->getRequest();
  27  
  28          $target = trim( $req->getText( 'target', $par ) );
  29  
  30          // Normalise name
  31          if ( $target !== '' ) {
  32              $user = User::newFromName( $target );
  33              if ( $user ) {
  34                  $target = $user->getName();
  35              }
  36          }
  37  
  38          $msg = $target === '' ?
  39              $this->msg( 'nuke-multiplepeople' )->inContentLanguage()->text() :
  40              $this->msg( 'nuke-defaultreason', $target )->
  41                  inContentLanguage()->text();
  42          $reason = $req->getText( 'wpReason', $msg );
  43  
  44          $limit = $req->getInt( 'limit', 500 );
  45          $namespace = $req->getVal( 'namespace' );
  46          $namespace = ctype_digit( $namespace ) ? (int)$namespace : null;
  47  
  48          if ( $req->wasPosted()
  49              && $this->getUser()->matchEditToken( $req->getVal( 'wpEditToken' ) ) ) {
  50  
  51              if ( $req->getVal( 'action' ) == 'delete' ) {
  52                  $pages = $req->getArray( 'pages' );
  53  
  54                  if ( $pages ) {
  55                      $this->doDelete( $pages, $reason );
  56                      return;
  57                  }
  58              } elseif ( $req->getVal( 'action' ) == 'submit' ) {
  59                  $this->listForm( $target, $reason, $limit, $namespace );
  60              } else {
  61                  $this->promptForm();
  62              }
  63          } elseif ( $target === '' ) {
  64              $this->promptForm();
  65          } else {
  66              $this->listForm( $target, $reason, $limit, $namespace );
  67          }
  68      }
  69  
  70      /**
  71       * Prompt for a username or IP address.
  72       *
  73       * @param $userName string
  74       */
  75  	protected function promptForm( $userName = '' ) {
  76          $out = $this->getOutput();
  77  
  78          $out->addWikiMsg( 'nuke-tools' );
  79  
  80          $out->addHTML(
  81              Xml::openElement(
  82                  'form',
  83                  array(
  84                      'action' => $this->getTitle()->getLocalURL( 'action=submit' ),
  85                      'method' => 'post'
  86                  )
  87              )
  88              . '<table><tr>'
  89                  . '<td>' . Xml::label( $this->msg( 'nuke-userorip' )->text(), 'nuke-target' ) . '</td>'
  90                  . '<td>' . Xml::input( 'target', 40, $userName, array( 'id' => 'nuke-target', 'autofocus' => true ) ) . '</td>'
  91              . '</tr><tr>'
  92                  . '<td>' . Xml::label( $this->msg( 'nuke-pattern' )->text(), 'nuke-pattern' ) . '</td>'
  93                  . '<td>' . Xml::input( 'pattern', 40, '', array( 'id' => 'nuke-pattern' ) ) . '</td>'
  94                  . '</tr><tr>'
  95                  . '<td>' . Xml::label( $this->msg( 'nuke-namespace' )->text(), 'nuke-namespace' ) . '</td>'
  96                  . '<td>' . Html::namespaceSelector( array( 'all' => 'all' ), array( 'name' => 'namespace' ) ) . '</td>'
  97              . '</tr><tr>'
  98                  . '<td>' . Xml::label( $this->msg( 'nuke-maxpages' )->text(), 'nuke-limit' ) . '</td>'
  99                  . '<td>' . Xml::input( 'limit', 7, '500', array( 'id' => 'nuke-limit' ) ) . '</td>'
 100              . '</tr><tr>'
 101                  . '<td></td>'
 102                  . '<td>' . Xml::submitButton( $this->msg( 'nuke-submit-user' )->text() ) . '</td>'
 103              . '</tr></table>'
 104              . Html::hidden( 'wpEditToken', $this->getUser()->getEditToken() )
 105              . Xml::closeElement( 'form' )
 106          );
 107      }
 108  
 109      /**
 110       * Display list of pages to delete.
 111       *
 112       * @param string $username
 113       * @param string $reason
 114       * @param integer $limit
 115       * @param integer|null $namespace
 116       */
 117  	protected function listForm( $username, $reason, $limit, $namespace = null ) {
 118          $out = $this->getOutput();
 119  
 120          $pages = $this->getNewPages( $username, $limit, $namespace );
 121  
 122          if ( count( $pages ) == 0 ) {
 123              if ( $username === '' ) {
 124                  $out->addWikiMsg( 'nuke-nopages-global' );
 125              } else {
 126                  $out->addWikiMsg( 'nuke-nopages', $username );
 127              }
 128  
 129              $this->promptForm( $username );
 130              return;
 131          }
 132  
 133          if ( $username === '' ) {
 134              $out->addWikiMsg( 'nuke-list-multiple' );
 135          } else {
 136              $out->addWikiMsg( 'nuke-list', $username );
 137          }
 138  
 139          $nuke = $this->getTitle();
 140  
 141          $out->addModules( 'ext.nuke' );
 142  
 143          $out->addHTML(
 144              Xml::openElement( 'form', array(
 145                  'action' => $nuke->getLocalURL( 'action=delete' ),
 146                  'method' => 'post',
 147                  'name' => 'nukelist' )
 148              ) .
 149              Html::hidden( 'wpEditToken', $this->getUser()->getEditToken() ) .
 150              Xml::tags( 'p',
 151                  null,
 152                  Xml::inputLabel(
 153                      $this->msg( 'deletecomment' )->text(), 'wpReason', 'wpReason', 70, $reason
 154                  )
 155              )
 156          );
 157  
 158          // Select: All, None
 159          $links = array();
 160          $links[] = '<a href="#" id="toggleall">' .
 161              $this->msg( 'powersearch-toggleall' )->text() . '</a>';
 162          $links[] = '<a href="#" id="togglenone">' .
 163              $this->msg( 'powersearch-togglenone' )->text() . '</a>';
 164          $out->addHTML(
 165              Xml::tags( 'p',
 166                  null,
 167                  $this->msg( 'nuke-select', $this->getLanguage()->commaList( $links ) )->text()
 168              )
 169          );
 170  
 171          // Delete button
 172          $out->addHTML(
 173              Xml::submitButton( $this->msg( 'nuke-submit-delete' )->text() )
 174          );
 175  
 176          $out->addHTML( '<ul>' );
 177  
 178          $wordSeparator = $this->msg( 'word-separator' )->text();
 179          $commaSeparator = $this->msg( 'comma-separator' )->text();
 180  
 181          foreach ( $pages as $info ) {
 182              /**
 183               * @var $title Title
 184               */
 185              list( $title, $userName ) = $info;
 186  
 187              $image = $title->getNamespace() == NS_IMAGE ? wfLocalFile( $title ) : false;
 188              $thumb = $image && $image->exists() ? $image->transform( array( 'width' => 120, 'height' => 120 ), 0 ) : false;
 189  
 190              $userNameText = $userName ? $this->msg( 'nuke-editby', $userName )->parse() . $commaSeparator : '';
 191              $changesLink = Linker::linkKnown(
 192                  $title,
 193                  $this->msg( 'nuke-viewchanges' )->text(),
 194                  array(),
 195                  array( 'action' => 'history' )
 196              );
 197              $out->addHTML( '<li>' .
 198                  Xml::check(
 199                      'pages[]',
 200                      true,
 201                      array( 'value' =>  $title->getPrefixedDbKey() )
 202                  ) . '&#160;' .
 203                  ( $thumb ? $thumb->toHtml( array( 'desc-link' => true ) ) : '' ) .
 204                  Linker::linkKnown( $title ) . $wordSeparator .
 205                  $this->msg( 'parentheses' )->rawParams( $userNameText . $changesLink )->escaped() .
 206                  "</li>\n" );
 207          }
 208  
 209          $out->addHTML(
 210              "</ul>\n" .
 211              Xml::submitButton( wfMessage( 'nuke-submit-delete' )->text() ) .
 212              '</form>'
 213          );
 214      }
 215  
 216      /**
 217       * Gets a list of new pages by the specified user or everyone when none is specified.
 218       *
 219       * @param string $username
 220       * @param integer $limit
 221       * @param integer|null $namespace
 222       *
 223       * @return array
 224       */
 225  	protected function getNewPages( $username, $limit, $namespace = null ) {
 226          $dbr = wfGetDB( DB_SLAVE );
 227  
 228          $what = array(
 229              'rc_namespace',
 230              'rc_title',
 231              'rc_timestamp',
 232          );
 233  
 234          $where = array( "(rc_new = 1) OR (rc_log_type = 'upload' AND rc_log_action = 'upload')" );
 235  
 236          if ( $username === '' ) {
 237              $what[] = 'rc_user_text';
 238          } else {
 239              $where['rc_user_text'] = $username;
 240          }
 241  
 242          if ( $namespace !== null ) {
 243              $where['rc_namespace'] = $namespace;
 244          }
 245  
 246          $pattern = $this->getRequest()->getText( 'pattern' );
 247          if ( !is_null( $pattern ) && trim( $pattern ) !== '' ) {
 248              $where[] = 'rc_title LIKE ' . $dbr->addQuotes( $pattern );
 249          }
 250          $group = implode( ', ', $what );
 251  
 252          $result = $dbr->select( 'recentchanges',
 253              $what,
 254              $where,
 255              __METHOD__,
 256              array(
 257                  'ORDER BY' => 'rc_timestamp DESC',
 258                  'GROUP BY' => $group,
 259                  'LIMIT' => $limit
 260              )
 261          );
 262  
 263          $pages = array();
 264  
 265          foreach ( $result as $row ) {
 266              $pages[] = array(
 267                  Title::makeTitle( $row->rc_namespace, $row->rc_title ),
 268                  $username === '' ? $row->rc_user_text : false
 269              );
 270          }
 271  
 272          return $pages;
 273      }
 274  
 275      /**
 276       * Does the actual deletion of the pages.
 277       *
 278       * @param array $pages The pages to delete
 279       * @param string $reason
 280       * @throws PermissionsError
 281       */
 282  	protected function doDelete( array $pages, $reason ) {
 283          $res = array();
 284  
 285          foreach ( $pages as $page ) {
 286              $title = Title::newFromURL( $page );
 287              $file = $title->getNamespace() == NS_FILE ? wfLocalFile( $title ) : false;
 288  
 289              $permission_errors = $title->getUserPermissionsErrors( 'delete', $this->getUser() );
 290  
 291              if ( $permission_errors !== array() ) {
 292                  throw new PermissionsError( 'delete', $permission_errors );
 293              }
 294  
 295              if ( $file ) {
 296                  $oldimage = null; // Must be passed by reference
 297                  $ok = FileDeleteForm::doDelete( $title, $file, $oldimage, $reason, false )->isOK();
 298              } else {
 299                  $article = new Article( $title, 0 );
 300                  $ok = $article->doDeleteArticle( $reason );
 301              }
 302  
 303              if ( $ok ) {
 304                  $res[] = wfMessage( 'nuke-deleted', $title->getPrefixedText() )->parse();
 305              } else {
 306                  $res[] = wfMessage( 'nuke-not-deleted', $title->getPrefixedText() )->parse();
 307              }
 308          }
 309  
 310          $this->getOutput()->addHTML( "<ul>\n<li>" . implode( "</li>\n<li>", $res ) . "</li>\n</ul>\n" );
 311          $this->getOutput()->addWikiMsg( 'nuke-delete-more' );
 312      }
 313  }


Generated: Fri Nov 28 14:03:12 2014 Cross-referenced by PHPXref 0.7.1