[ Index ]

PHP Cross Reference of MediaWiki-1.24.0

title

Body

[close]

/includes/api/ -> ApiProtect.php (source)

   1  <?php
   2  /**
   3   *
   4   *
   5   * Created on Sep 1, 2007
   6   *
   7   * Copyright © 2007 Roan Kattouw "<Firstname>.<Lastname>@gmail.com"
   8   *
   9   * This program is free software; you can redistribute it and/or modify
  10   * it under the terms of the GNU General Public License as published by
  11   * the Free Software Foundation; either version 2 of the License, or
  12   * (at your option) any later version.
  13   *
  14   * This program is distributed in the hope that it will be useful,
  15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17   * GNU General Public License for more details.
  18   *
  19   * You should have received a copy of the GNU General Public License along
  20   * with this program; if not, write to the Free Software Foundation, Inc.,
  21   * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  22   * http://www.gnu.org/copyleft/gpl.html
  23   *
  24   * @file
  25   */
  26  
  27  /**
  28   * @ingroup API
  29   */
  30  class ApiProtect extends ApiBase {
  31  	public function execute() {
  32          $params = $this->extractRequestParams();
  33  
  34          $pageObj = $this->getTitleOrPageId( $params, 'fromdbmaster' );
  35          $titleObj = $pageObj->getTitle();
  36  
  37          $errors = $titleObj->getUserPermissionsErrors( 'protect', $this->getUser() );
  38          if ( $errors ) {
  39              // We don't care about multiple errors, just report one of them
  40              $this->dieUsageMsg( reset( $errors ) );
  41          }
  42  
  43          $expiry = (array)$params['expiry'];
  44          if ( count( $expiry ) != count( $params['protections'] ) ) {
  45              if ( count( $expiry ) == 1 ) {
  46                  $expiry = array_fill( 0, count( $params['protections'] ), $expiry[0] );
  47              } else {
  48                  $this->dieUsageMsg( array(
  49                      'toofewexpiries',
  50                      count( $expiry ),
  51                      count( $params['protections'] )
  52                  ) );
  53              }
  54          }
  55  
  56          $restrictionTypes = $titleObj->getRestrictionTypes();
  57          $db = $this->getDB();
  58  
  59          $protections = array();
  60          $expiryarray = array();
  61          $resultProtections = array();
  62          foreach ( $params['protections'] as $i => $prot ) {
  63              $p = explode( '=', $prot );
  64              $protections[$p[0]] = ( $p[1] == 'all' ? '' : $p[1] );
  65  
  66              if ( $titleObj->exists() && $p[0] == 'create' ) {
  67                  $this->dieUsageMsg( 'create-titleexists' );
  68              }
  69              if ( !$titleObj->exists() && $p[0] != 'create' ) {
  70                  $this->dieUsageMsg( 'missingtitle-createonly' );
  71              }
  72  
  73              if ( !in_array( $p[0], $restrictionTypes ) && $p[0] != 'create' ) {
  74                  $this->dieUsageMsg( array( 'protect-invalidaction', $p[0] ) );
  75              }
  76              if ( !in_array( $p[1], $this->getConfig()->get( 'RestrictionLevels' ) ) && $p[1] != 'all' ) {
  77                  $this->dieUsageMsg( array( 'protect-invalidlevel', $p[1] ) );
  78              }
  79  
  80              if ( in_array( $expiry[$i], array( 'infinite', 'indefinite', 'infinity', 'never' ) ) ) {
  81                  $expiryarray[$p[0]] = $db->getInfinity();
  82              } else {
  83                  $exp = strtotime( $expiry[$i] );
  84                  if ( $exp < 0 || !$exp ) {
  85                      $this->dieUsageMsg( array( 'invalidexpiry', $expiry[$i] ) );
  86                  }
  87  
  88                  $exp = wfTimestamp( TS_MW, $exp );
  89                  if ( $exp < wfTimestampNow() ) {
  90                      $this->dieUsageMsg( array( 'pastexpiry', $expiry[$i] ) );
  91                  }
  92                  $expiryarray[$p[0]] = $exp;
  93              }
  94              $resultProtections[] = array(
  95                  $p[0] => $protections[$p[0]],
  96                  'expiry' => ( $expiryarray[$p[0]] == $db->getInfinity()
  97                      ? 'infinite'
  98                      : wfTimestamp( TS_ISO_8601, $expiryarray[$p[0]] )
  99                  )
 100              );
 101          }
 102  
 103          $cascade = $params['cascade'];
 104  
 105          if ( $params['watch'] ) {
 106              $this->logFeatureUsage( 'action=protect&watch' );
 107          }
 108          $watch = $params['watch'] ? 'watch' : $params['watchlist'];
 109          $this->setWatch( $watch, $titleObj, 'watchdefault' );
 110  
 111          $status = $pageObj->doUpdateRestrictions(
 112              $protections,
 113              $expiryarray,
 114              $cascade,
 115              $params['reason'],
 116              $this->getUser()
 117          );
 118  
 119          if ( !$status->isOK() ) {
 120              $this->dieStatus( $status );
 121          }
 122          $res = array(
 123              'title' => $titleObj->getPrefixedText(),
 124              'reason' => $params['reason']
 125          );
 126          if ( $cascade ) {
 127              $res['cascade'] = '';
 128          }
 129          $res['protections'] = $resultProtections;
 130          $result = $this->getResult();
 131          $result->setIndexedTagName( $res['protections'], 'protection' );
 132          $result->addValue( null, $this->getModuleName(), $res );
 133      }
 134  
 135  	public function mustBePosted() {
 136          return true;
 137      }
 138  
 139  	public function isWriteMode() {
 140          return true;
 141      }
 142  
 143  	public function getAllowedParams() {
 144          return array(
 145              'title' => array(
 146                  ApiBase::PARAM_TYPE => 'string',
 147              ),
 148              'pageid' => array(
 149                  ApiBase::PARAM_TYPE => 'integer',
 150              ),
 151              'protections' => array(
 152                  ApiBase::PARAM_ISMULTI => true,
 153                  ApiBase::PARAM_REQUIRED => true,
 154              ),
 155              'expiry' => array(
 156                  ApiBase::PARAM_ISMULTI => true,
 157                  ApiBase::PARAM_ALLOW_DUPLICATES => true,
 158                  ApiBase::PARAM_DFLT => 'infinite',
 159              ),
 160              'reason' => '',
 161              'cascade' => false,
 162              'watch' => array(
 163                  ApiBase::PARAM_DFLT => false,
 164                  ApiBase::PARAM_DEPRECATED => true,
 165              ),
 166              'watchlist' => array(
 167                  ApiBase::PARAM_DFLT => 'preferences',
 168                  ApiBase::PARAM_TYPE => array(
 169                      'watch',
 170                      'unwatch',
 171                      'preferences',
 172                      'nochange'
 173                  ),
 174              ),
 175          );
 176      }
 177  
 178  	public function getParamDescription() {
 179          $p = $this->getModulePrefix();
 180  
 181          return array(
 182              'title' => "Title of the page you want to (un)protect. Cannot be used together with {$p}pageid",
 183              'pageid' => "ID of the page you want to (un)protect. Cannot be used together with {$p}title",
 184              'protections' => 'List of protection levels, formatted action=group (e.g. edit=sysop)',
 185              'expiry' => array(
 186                  'Expiry timestamps. If only one timestamp is ' .
 187                      'set, it\'ll be used for all protections.',
 188                  'Use \'infinite\', \'indefinite\', \'infinity\' or \'never\', for a never-expiring protection.'
 189              ),
 190              'reason' => 'Reason for (un)protecting',
 191              'cascade' => array(
 192                  'Enable cascading protection (i.e. protect pages included in this page)',
 193                  'Ignored if not all protection levels are \'sysop\' or \'protect\''
 194              ),
 195              'watch' => 'If set, add the page being (un)protected to your watchlist',
 196              'watchlist' => 'Unconditionally add or remove the page from your ' .
 197                  'watchlist, use preferences or do not change watch',
 198          );
 199      }
 200  
 201  	public function getDescription() {
 202          return 'Change the protection level of a page.';
 203      }
 204  
 205  	public function needsToken() {
 206          return 'csrf';
 207      }
 208  
 209  	public function getExamples() {
 210          return array(
 211              'api.php?action=protect&title=Main%20Page&token=123ABC&' .
 212                  'protections=edit=sysop|move=sysop&cascade=&expiry=20070901163000|never',
 213              'api.php?action=protect&title=Main%20Page&token=123ABC&' .
 214                  'protections=edit=all|move=all&reason=Lifting%20restrictions'
 215          );
 216      }
 217  
 218  	public function getHelpUrls() {
 219          return 'https://www.mediawiki.org/wiki/API:Protect';
 220      }
 221  }


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