[ Index ]

PHP Cross Reference of MediaWiki-1.24.0

title

Body

[close]

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

   1  <?php
   2  /**
   3   *
   4   *
   5   * Created on March 5, 2011
   6   *
   7   * Copyright © 2011 Bryan Tong Minh <[email protected]>
   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 ApiFileRevert extends ApiBase {
  31      /** @var LocalFile */
  32      protected $file;
  33  
  34      /** @var string */
  35      protected $archiveName;
  36  
  37      /** @var array */
  38      protected $params;
  39  
  40  	public function execute() {
  41          $this->params = $this->extractRequestParams();
  42          // Extract the file and archiveName from the request parameters
  43          $this->validateParameters();
  44  
  45          // Check whether we're allowed to revert this file
  46          $this->checkPermissions( $this->getUser() );
  47  
  48          $sourceUrl = $this->file->getArchiveVirtualUrl( $this->archiveName );
  49          $status = $this->file->upload(
  50              $sourceUrl,
  51              $this->params['comment'],
  52              $this->params['comment'],
  53              0,
  54              false,
  55              false,
  56              $this->getUser()
  57          );
  58  
  59          if ( $status->isGood() ) {
  60              $result = array( 'result' => 'Success' );
  61          } else {
  62              $result = array(
  63                  'result' => 'Failure',
  64                  'errors' => $this->getResult()->convertStatusToArray( $status ),
  65              );
  66          }
  67  
  68          $this->getResult()->addValue( null, $this->getModuleName(), $result );
  69      }
  70  
  71      /**
  72       * Checks that the user has permissions to perform this revert.
  73       * Dies with usage message on inadequate permissions.
  74       * @param User $user The user to check.
  75       */
  76  	protected function checkPermissions( $user ) {
  77          $title = $this->file->getTitle();
  78          $permissionErrors = array_merge(
  79              $title->getUserPermissionsErrors( 'edit', $user ),
  80              $title->getUserPermissionsErrors( 'upload', $user )
  81          );
  82  
  83          if ( $permissionErrors ) {
  84              $this->dieUsageMsg( $permissionErrors[0] );
  85          }
  86      }
  87  
  88      /**
  89       * Validate the user parameters and set $this->archiveName and $this->file.
  90       * Throws an error if validation fails
  91       */
  92  	protected function validateParameters() {
  93          // Validate the input title
  94          $title = Title::makeTitleSafe( NS_FILE, $this->params['filename'] );
  95          if ( is_null( $title ) ) {
  96              $this->dieUsageMsg( array( 'invalidtitle', $this->params['filename'] ) );
  97          }
  98          $localRepo = RepoGroup::singleton()->getLocalRepo();
  99  
 100          // Check if the file really exists
 101          $this->file = $localRepo->newFile( $title );
 102          if ( !$this->file->exists() ) {
 103              $this->dieUsageMsg( 'notanarticle' );
 104          }
 105  
 106          // Check if the archivename is valid for this file
 107          $this->archiveName = $this->params['archivename'];
 108          $oldFile = $localRepo->newFromArchiveName( $title, $this->archiveName );
 109          if ( !$oldFile->exists() ) {
 110              $this->dieUsageMsg( 'filerevert-badversion' );
 111          }
 112      }
 113  
 114  	public function mustBePosted() {
 115          return true;
 116      }
 117  
 118  	public function isWriteMode() {
 119          return true;
 120      }
 121  
 122  	public function getAllowedParams() {
 123          return array(
 124              'filename' => array(
 125                  ApiBase::PARAM_TYPE => 'string',
 126                  ApiBase::PARAM_REQUIRED => true,
 127              ),
 128              'comment' => array(
 129                  ApiBase::PARAM_DFLT => '',
 130              ),
 131              'archivename' => array(
 132                  ApiBase::PARAM_TYPE => 'string',
 133                  ApiBase::PARAM_REQUIRED => true,
 134              ),
 135          );
 136      }
 137  
 138  	public function getParamDescription() {
 139          return array(
 140              'filename' => 'Target filename without the File: prefix',
 141              'comment' => 'Upload comment',
 142              'archivename' => 'Archive name of the revision to revert to',
 143          );
 144      }
 145  
 146  	public function getDescription() {
 147          return array(
 148              'Revert a file to an old version.'
 149          );
 150      }
 151  
 152  	public function needsToken() {
 153          return 'csrf';
 154      }
 155  
 156  	public function getExamples() {
 157          return array(
 158              'api.php?action=filerevert&filename=Wiki.png&comment=Revert&' .
 159                  'archivename=20110305152740!Wiki.png&token=123ABC'
 160                  => 'Revert Wiki.png to the version of 20110305152740',
 161          );
 162      }
 163  }


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