ApiUndelete.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. /*
  3. * Created on Jul 3, 2007
  4. * API for MediaWiki 1.8+
  5. *
  6. * Copyright (C) 2007 Roan Kattouw <Firstname>.<Lastname>@home.nl
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along
  19. * with this program; if not, write to the Free Software Foundation, Inc.,
  20. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  21. * http://www.gnu.org/copyleft/gpl.html
  22. */
  23. if (!defined('MEDIAWIKI')) {
  24. // Eclipse helper - will be ignored in production
  25. require_once ("ApiBase.php");
  26. }
  27. /**
  28. * @ingroup API
  29. */
  30. class ApiUndelete extends ApiBase {
  31. public function __construct($main, $action) {
  32. parent :: __construct($main, $action);
  33. }
  34. public function execute() {
  35. global $wgUser;
  36. $params = $this->extractRequestParams();
  37. $titleObj = NULL;
  38. if(!isset($params['title']))
  39. $this->dieUsageMsg(array('missingparam', 'title'));
  40. if(!isset($params['token']))
  41. $this->dieUsageMsg(array('missingparam', 'token'));
  42. if(!$wgUser->isAllowed('undelete'))
  43. $this->dieUsageMsg(array('permdenied-undelete'));
  44. if($wgUser->isBlocked())
  45. $this->dieUsageMsg(array('blockedtext'));
  46. if(!$wgUser->matchEditToken($params['token']))
  47. $this->dieUsageMsg(array('sessionfailure'));
  48. $titleObj = Title::newFromText($params['title']);
  49. if(!$titleObj)
  50. $this->dieUsageMsg(array('invalidtitle', $params['title']));
  51. // Convert timestamps
  52. if(!isset($params['timestamps']))
  53. $params['timestamps'] = array();
  54. if(!is_array($params['timestamps']))
  55. $params['timestamps'] = array($params['timestamps']);
  56. foreach($params['timestamps'] as $i => $ts)
  57. $params['timestamps'][$i] = wfTimestamp(TS_MW, $ts);
  58. $pa = new PageArchive($titleObj);
  59. $dbw = wfGetDB(DB_MASTER);
  60. $dbw->begin();
  61. $retval = $pa->undelete((isset($params['timestamps']) ? $params['timestamps'] : array()), $params['reason']);
  62. if(!is_array($retval))
  63. $this->dieUsageMsg(array('cannotundelete'));
  64. if($retval[1])
  65. wfRunHooks( 'FileUndeleteComplete',
  66. array($titleObj, array(), $wgUser, $params['reason']) );
  67. $info['title'] = $titleObj->getPrefixedText();
  68. $info['revisions'] = intval($retval[0]);
  69. $info['fileversions'] = intval($retval[1]);
  70. $info['reason'] = intval($retval[2]);
  71. $this->getResult()->addValue(null, $this->getModuleName(), $info);
  72. }
  73. public function mustBePosted() { return true; }
  74. public function isWriteMode() {
  75. return true;
  76. }
  77. public function getAllowedParams() {
  78. return array (
  79. 'title' => null,
  80. 'token' => null,
  81. 'reason' => "",
  82. 'timestamps' => array(
  83. ApiBase :: PARAM_ISMULTI => true
  84. )
  85. );
  86. }
  87. public function getParamDescription() {
  88. return array (
  89. 'title' => 'Title of the page you want to restore.',
  90. 'token' => 'An undelete token previously retrieved through list=deletedrevs',
  91. 'reason' => 'Reason for restoring (optional)',
  92. 'timestamps' => 'Timestamps of the revisions to restore. If not set, all revisions will be restored.'
  93. );
  94. }
  95. public function getDescription() {
  96. return array(
  97. 'Restore certain revisions of a deleted page. A list of deleted revisions (including timestamps) can be',
  98. 'retrieved through list=deletedrevs'
  99. );
  100. }
  101. protected function getExamples() {
  102. return array (
  103. 'api.php?action=undelete&title=Main%20Page&token=123ABC&reason=Restoring%20main%20page',
  104. 'api.php?action=undelete&title=Main%20Page&token=123ABC&timestamps=20070703220045|20070702194856'
  105. );
  106. }
  107. public function getVersion() {
  108. return __CLASS__ . ': $Id: ApiUndelete.php 48091 2009-03-06 13:49:44Z catrope $';
  109. }
  110. }