ApiMove.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. <?php
  2. /*
  3. * Created on Oct 31, 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 ApiMove 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. if(is_null($params['reason']))
  38. $params['reason'] = '';
  39. $this->requireOnlyOneParameter($params, 'from', 'fromid');
  40. if(!isset($params['to']))
  41. $this->dieUsageMsg(array('missingparam', 'to'));
  42. if(!isset($params['token']))
  43. $this->dieUsageMsg(array('missingparam', 'token'));
  44. if(!$wgUser->matchEditToken($params['token']))
  45. $this->dieUsageMsg(array('sessionfailure'));
  46. if(isset($params['from']))
  47. {
  48. $fromTitle = Title::newFromText($params['from']);
  49. if(!$fromTitle)
  50. $this->dieUsageMsg(array('invalidtitle', $params['from']));
  51. }
  52. else if(isset($params['fromid']))
  53. {
  54. $fromTitle = Title::newFromID($params['fromid']);
  55. if(!$fromTitle)
  56. $this->dieUsageMsg(array('nosuchpageid', $params['fromid']));
  57. }
  58. if(!$fromTitle->exists())
  59. $this->dieUsageMsg(array('notanarticle'));
  60. $fromTalk = $fromTitle->getTalkPage();
  61. $toTitle = Title::newFromText($params['to']);
  62. if(!$toTitle)
  63. $this->dieUsageMsg(array('invalidtitle', $params['to']));
  64. $toTalk = $toTitle->getTalkPage();
  65. # Move the page
  66. $hookErr = null;
  67. $retval = $fromTitle->moveTo($toTitle, true, $params['reason'], !$params['noredirect']);
  68. if($retval !== true)
  69. $this->dieUsageMsg(reset($retval));
  70. $r = array('from' => $fromTitle->getPrefixedText(), 'to' => $toTitle->getPrefixedText(), 'reason' => $params['reason']);
  71. if(!$params['noredirect'] || !$wgUser->isAllowed('suppressredirect'))
  72. $r['redirectcreated'] = '';
  73. # Move the talk page
  74. if($params['movetalk'] && $fromTalk->exists() && !$fromTitle->isTalkPage())
  75. {
  76. $retval = $fromTalk->moveTo($toTalk, true, $params['reason'], !$params['noredirect']);
  77. if($retval === true)
  78. {
  79. $r['talkfrom'] = $fromTalk->getPrefixedText();
  80. $r['talkto'] = $toTalk->getPrefixedText();
  81. }
  82. // We're not gonna dieUsage() on failure, since we already changed something
  83. else
  84. {
  85. $parsed = $this->parseMsg(reset($retval));
  86. $r['talkmove-error-code'] = $parsed['code'];
  87. $r['talkmove-error-info'] = $parsed['info'];
  88. }
  89. }
  90. # Move subpages
  91. if($params['movesubpages'])
  92. {
  93. $r['subpages'] = $this->moveSubpages($fromTitle, $toTitle,
  94. $params['reason'], $params['noredirect']);
  95. $this->getResult()->setIndexedTagName($r['subpages'], 'subpage');
  96. if($params['movetalk'])
  97. {
  98. $r['subpages-talk'] = $this->moveSubpages($fromTalk, $toTalk,
  99. $params['reason'], $params['noredirect']);
  100. $this->getResult()->setIndexedTagName($r['subpages-talk'], 'subpage');
  101. }
  102. }
  103. # Watch pages
  104. if($params['watch'] || $wgUser->getOption('watchmoves'))
  105. {
  106. $wgUser->addWatch($fromTitle);
  107. $wgUser->addWatch($toTitle);
  108. }
  109. else if($params['unwatch'])
  110. {
  111. $wgUser->removeWatch($fromTitle);
  112. $wgUser->removeWatch($toTitle);
  113. }
  114. $this->getResult()->addValue(null, $this->getModuleName(), $r);
  115. }
  116. public function moveSubpages($fromTitle, $toTitle, $reason, $noredirect)
  117. {
  118. $retval = array();
  119. $success = $fromTitle->moveSubpages($toTitle, true, $reason, !$noredirect);
  120. if(isset($success[0]))
  121. return array('error' => $this->parseMsg($success));
  122. else
  123. {
  124. // At least some pages could be moved
  125. // Report each of them separately
  126. foreach($success as $oldTitle => $newTitle)
  127. {
  128. $r = array('from' => $oldTitle);
  129. if(is_array($newTitle))
  130. $r['error'] = $this->parseMsg(reset($newTitle));
  131. else
  132. // Success
  133. $r['to'] = $newTitle;
  134. $retval[] = $r;
  135. }
  136. }
  137. return $retval;
  138. }
  139. public function mustBePosted() { return true; }
  140. public function isWriteMode() {
  141. return true;
  142. }
  143. public function getAllowedParams() {
  144. return array (
  145. 'from' => null,
  146. 'fromid' => array(
  147. ApiBase::PARAM_TYPE => 'integer'
  148. ),
  149. 'to' => null,
  150. 'token' => null,
  151. 'reason' => null,
  152. 'movetalk' => false,
  153. 'movesubpages' => false,
  154. 'noredirect' => false,
  155. 'watch' => false,
  156. 'unwatch' => false
  157. );
  158. }
  159. public function getParamDescription() {
  160. return array (
  161. 'from' => 'Title of the page you want to move. Cannot be used together with fromid.',
  162. 'fromid' => 'Page ID of the page you want to move. Cannot be used together with from.',
  163. 'to' => 'Title you want to rename the page to.',
  164. 'token' => 'A move token previously retrieved through prop=info',
  165. 'reason' => 'Reason for the move (optional).',
  166. 'movetalk' => 'Move the talk page, if it exists.',
  167. 'movesubpages' => 'Move subpages, if applicable',
  168. 'noredirect' => 'Don\'t create a redirect',
  169. 'watch' => 'Add the page and the redirect to your watchlist',
  170. 'unwatch' => 'Remove the page and the redirect from your watchlist'
  171. );
  172. }
  173. public function getDescription() {
  174. return array(
  175. 'Move a page.'
  176. );
  177. }
  178. protected function getExamples() {
  179. return array (
  180. 'api.php?action=move&from=Exampel&to=Example&token=123ABC&reason=Misspelled%20title&movetalk&noredirect'
  181. );
  182. }
  183. public function getVersion() {
  184. return __CLASS__ . ': $Id: ApiMove.php 48091 2009-03-06 13:49:44Z catrope $';
  185. }
  186. }