ApiRollback.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. /*
  3. * Created on Jun 20, 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 ApiRollback extends ApiBase {
  31. public function __construct($main, $action) {
  32. parent :: __construct($main, $action);
  33. }
  34. public function execute() {
  35. $params = $this->extractRequestParams();
  36. $titleObj = NULL;
  37. if(!isset($params['title']))
  38. $this->dieUsageMsg(array('missingparam', 'title'));
  39. if(!isset($params['user']))
  40. $this->dieUsageMsg(array('missingparam', 'user'));
  41. if(!isset($params['token']))
  42. $this->dieUsageMsg(array('missingparam', 'token'));
  43. $titleObj = Title::newFromText($params['title']);
  44. if(!$titleObj)
  45. $this->dieUsageMsg(array('invalidtitle', $params['title']));
  46. if(!$titleObj->exists())
  47. $this->dieUsageMsg(array('notanarticle'));
  48. #We need to be able to revert IPs, but getCanonicalName rejects them
  49. $username = User::isIP($params['user'])
  50. ? $params['user']
  51. : User::getCanonicalName($params['user']);
  52. if(!$username)
  53. $this->dieUsageMsg(array('invaliduser', $params['user']));
  54. $articleObj = new Article($titleObj);
  55. $summary = (isset($params['summary']) ? $params['summary'] : "");
  56. $details = null;
  57. $retval = $articleObj->doRollback($username, $summary, $params['token'], $params['markbot'], $details);
  58. if($retval)
  59. // We don't care about multiple errors, just report one of them
  60. $this->dieUsageMsg(reset($retval));
  61. $info = array(
  62. 'title' => $titleObj->getPrefixedText(),
  63. 'pageid' => intval($details['current']->getPage()),
  64. 'summary' => $details['summary'],
  65. 'revid' => intval($titleObj->getLatestRevID()),
  66. 'old_revid' => intval($details['current']->getID()),
  67. 'last_revid' => intval($details['target']->getID())
  68. );
  69. $this->getResult()->addValue(null, $this->getModuleName(), $info);
  70. }
  71. public function mustBePosted() { return true; }
  72. public function isWriteMode() {
  73. return true;
  74. }
  75. public function getAllowedParams() {
  76. return array (
  77. 'title' => null,
  78. 'user' => null,
  79. 'token' => null,
  80. 'summary' => null,
  81. 'markbot' => false
  82. );
  83. }
  84. public function getParamDescription() {
  85. return array (
  86. 'title' => 'Title of the page you want to rollback.',
  87. 'user' => 'Name of the user whose edits are to be rolled back. If set incorrectly, you\'ll get a badtoken error.',
  88. 'token' => 'A rollback token previously retrieved through prop=revisions',
  89. 'summary' => 'Custom edit summary. If not set, default summary will be used.',
  90. 'markbot' => 'Mark the reverted edits and the revert as bot edits'
  91. );
  92. }
  93. public function getDescription() {
  94. return array(
  95. 'Undo the last edit to the page. If the last user who edited the page made multiple edits in a row,',
  96. 'they will all be rolled back.'
  97. );
  98. }
  99. protected function getExamples() {
  100. return array (
  101. 'api.php?action=rollback&title=Main%20Page&user=Catrope&token=123ABC',
  102. 'api.php?action=rollback&title=Main%20Page&user=217.121.114.116&token=123ABC&summary=Reverting%20vandalism&markbot=1'
  103. );
  104. }
  105. public function getVersion() {
  106. return __CLASS__ . ': $Id: ApiRollback.php 48122 2009-03-07 12:58:41Z catrope $';
  107. }
  108. }