API: Adding action=edit module
authorRoan Kattouw <catrope@users.mediawiki.org>
Mon, 3 Mar 2008 18:08:40 +0000 (18:08 +0000)
committerRoan Kattouw <catrope@users.mediawiki.org>
Mon, 3 Mar 2008 18:08:40 +0000 (18:08 +0000)
RELEASE-NOTES
docs/hooks.txt
includes/AutoLoader.php
includes/api/ApiEditPage.php [new file with mode: 0644]
includes/api/ApiMain.php

index c6ae9bc..86016dd 100644 (file)
@@ -72,6 +72,7 @@ it from source control: http://www.mediawiki.org/wiki/Download_from_SVN
 * (bug 12394) Added rctitles parameter to list=recentchanges, making rcid retrieval easier
 * (bug 13218) Fix inclusion of " character in hyperlinks
 * Added watch and unwatch parameters to action=delete and action=move
+* Added action=edit
 
 === Languages updated in 1.13 ===
 
index db132f7..979e8ab 100644 (file)
@@ -268,6 +268,14 @@ before showing the edit form ( EditPage::edit() ). This is triggered
 on &action=edit.
 $EditPage : the EditPage object
 
+'APIEditBeforeSave': before saving a page with api.php?action=edit,
+after processing request parameters. Return false to let the request
+fail, returning an error message or an <edit result="Failure"> tag
+if $resultArr was filled.
+$EditPage : the EditPage object
+$text : the new text of the article (has yet to be saved)
+$resultArr : data in this array will be added to the API result
+
 'ArticleAfterFetchContent': after fetching content of an article from the database
 $article: the article (object) being loaded from the database
 $content: the content (string) of the article
index 2e2083b..8ac63ff 100644 (file)
@@ -370,6 +370,7 @@ function __autoload($className) {
                #'ApiChangeRights' => 'includes/api/ApiChangeRights.php',
                # Disabled for now
                'ApiDelete' => 'includes/api/ApiDelete.php',
+               'ApiEditPage' => 'includes/api/ApiEditPage.php',
                'ApiMove' => 'includes/api/ApiMove.php',
                'ApiProtect' => 'includes/api/ApiProtect.php',
                'ApiQueryBlocks' => 'includes/api/ApiQueryBlocks.php',
diff --git a/includes/api/ApiEditPage.php b/includes/api/ApiEditPage.php
new file mode 100644 (file)
index 0000000..b9a1120
--- /dev/null
@@ -0,0 +1,252 @@
+<?php\r
+\r
+/*\r
+ * Created on August 16, 2007\r
+ *\r
+ * API for MediaWiki 1.8+\r
+ *\r
+ * Copyright (C) 2007 Iker Labarga <Firstname><Lastname>@gmail.com\r
+ *\r
+ * This program is free software; you can redistribute it and/or modify\r
+ * it under the terms of the GNU General Public License as published by\r
+ * the Free Software Foundation; either version 2 of the License, or\r
+ * (at your option) any later version.\r
+ *\r
+ * This program is distributed in the hope that it will be useful,\r
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\r
+ * GNU General Public License for more details.\r
+ *\r
+ * You should have received a copy of the GNU General Public License along\r
+ * with this program; if not, write to the Free Software Foundation, Inc.,\r
+ * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.\r
+ * http://www.gnu.org/copyleft/gpl.html\r
+ */\r
+\r
+if (!defined('MEDIAWIKI')) {\r
+    // Eclipse helper - will be ignored in production\r
+    require_once ("ApiBase.php");\r
+}\r
+\r
+/**\r
+ * A query module to list all external URLs found on a given set of pages.\r
+ *\r
+ * @addtogroup API\r
+ */\r
+class ApiEditPage extends ApiBase {\r
+\r
+       public function __construct($query, $moduleName) {\r
+               parent :: __construct($query, $moduleName);\r
+       }\r
+\r
+       public function execute() {\r
+               global $wgUser;\r
+               $this->getMain()->requestWriteMode();\r
+\r
+               $params = $this->extractRequestParams();\r
+               if(is_null($params['title']))\r
+                       $this->dieUsageMsg(array('missingparam', 'title'));\r
+               if(is_null($params['text']))\r
+                       $this->dieUsageMsg(array('missingparam', 'text'));\r
+               if(is_null($params['token']))\r
+                       $this->dieUsageMsg(array('missingparam', 'token'));\r
+               if(!$wgUser->matchEditToken($params['token']))\r
+                       $this->dieUsageMsg(array('sessionfailure'));\r
+               \r
+               $titleObj = Title::newFromText($params['title']);\r
+               if(!$titleObj)\r
+                       $this->dieUsageMsg(array('invalidtitle', $params['title']));\r
+               \r
+               // Now let's check whether we're even allowed to do this\r
+               $errors = $titleObj->getUserPermissionsErrors('edit', $wgUser);\r
+               if(!$titleObj->exists())\r
+                       $errors = array_merge($errors, $titleObj->getUserPermissionsErrors('create', $wgUser));\r
+               if(!empty($errors))\r
+                       $this->dieUsageMsg($errors[0]);\r
+\r
+               $articleObj = new Article($titleObj);\r
+               $ep = new EditPage($articleObj);\r
+\r
+               // EditPage wants to parse its stuff from a WebRequest\r
+               // That interface kind of sucks, but it's workable\r
+               $reqArr = array('wpTextbox1' => $params['text'],\r
+                               'wpEdittoken' => $params['token'],\r
+                               'wpIgnoreBlankSummary' => ''\r
+               );\r
+               if(!is_null($params['summary']))\r
+                       $reqArr['wpSummary'] = $params['summary'];\r
+               # Watch out for basetimestamp == ''\r
+               # wfTimestamp() treats it as NOW, almost certainly causing an edit conflict\r
+               if(!is_null($params['basetimestamp']) && $params['basetimestamp'] != '')\r
+                       $reqArr['wpEdittime'] = wfTimestamp(TS_MW, $params['basetimestamp']);\r
+               else\r
+                       $reqArr['wpEdittime'] = $articleObj->getTimestamp();\r
+               # Fake wpStartime\r
+               $reqArr['wpStarttime'] = $reqArr['wpEdittime'];\r
+               if($params['minor'] || (!$params['notminor'] && $wgUser->getOption('minordefault')))\r
+                       $reqArr['wpMinoredit'] = '';\r
+               if($params['recreate'])\r
+                       $reqArr['wpRecreate'] = '';\r
+               if(!is_null($params['captchaid']))\r
+                       $reqArr['wpCaptchaId'] = $params['captchaid'];\r
+               if(!is_null($params['captchaword']))\r
+                       $reqArr['wpCaptchaWord'] = $params['captchaword'];\r
+               \r
+               if($params['watch'])\r
+                       $watch = true;\r
+               else if($params['unwatch'])\r
+                       $watch = false;\r
+               else if($titleObj->userIsWatching())\r
+                       $watch = true;\r
+               else if($wgUser->getOption('watchdefault'))\r
+                       $watch = true;\r
+               else if($wgUser->getOption('watchcreations') && !$titleObj->exists())\r
+                       $watch = true;\r
+               else\r
+                       $watch = false;\r
+               if($watch)\r
+                       $reqArr['wpWatchthis'] = '';\r
+\r
+               $req = new FauxRequest($reqArr, true);\r
+               $ep->importFormData($req);\r
+\r
+               # Run hooks\r
+               # We need to fake $wgRequest for some of them\r
+               global $wgRequest;\r
+               $wgRequest = $req;\r
+               $r = array();\r
+               if(!wfRunHooks('APIEditBeforeSave', array(&$ep, $ep->textbox1, &$r)))\r
+               {\r
+                       if(!empty($r))\r
+                       {\r
+                               $r['result'] = "Failure";\r
+                               $this->getResult()->addValue(null, $this->getModuleName(), $r);\r
+                               return; \r
+                       }       \r
+                       else\r
+                               $this->dieUsageMsg(array('hookaborted'));\r
+               }\r
+               \r
+               # Do the actual save\r
+               $oldRevId = $articleObj->getRevIdFetched();\r
+               $result = null;\r
+               # *Something* is setting $wgTitle to a title corresponding to "Msg",\r
+               # but that breaks API mode detection through is_null($wgTitle)\r
+               global $wgTitle;\r
+               $wgTitle = null;\r
+               $retval = $ep->internalAttemptSave($result, $wgUser->isAllowed('bot') && $params['bot']);\r
+               switch($retval)\r
+               {\r
+                       case EditPage::AS_HOOK_ERROR:\r
+                       case EditPage::AS_HOOK_ERROR_EXPECTED:\r
+                               $this->dieUsageMsg(array('hookaborted'));\r
+                       case EditPage::AS_IMAGE_REDIRECT_ANON:\r
+                               $this->dieUsageMsg(array('noimageredirect-anon'));\r
+                       case EditPage::AS_IMAGE_REDIRECT_LOGGED:\r
+                               $this->dieUsageMsg(array('noimageredirect-logged'));\r
+                       case EditPage::AS_SPAM_ERROR:\r
+                               $this->dieUsageMsg(array('spamdetected', $result['spam']));\r
+                       case EditPage::AS_FILTERING:\r
+                               $this->dieUsageMsg(array('filtered'));\r
+                       case EditPage::AS_BLOCKED_PAGE_FOR_USER:\r
+                               $this->dieUsageMsg(array('blockedtext'));\r
+                       case EditPage::AS_MAX_ARTICLE_SIZE_EXCEEDED:\r
+                       case EditPage::AS_CONTENT_TOO_BIG:\r
+                               global $wgMaxArticleSize;\r
+                               $this->dieUsageMsg(array('contenttoobig', $wgMaxArticleSize));\r
+                       case EditPage::AS_READ_ONLY_PAGE_ANON:\r
+                               $this->dieUsageMsg(array('noedit-anon'));\r
+                       case EditPage::AS_READ_ONLY_PAGE_LOGGED:\r
+                               $this->dieUsageMsg(array('noedit'));\r
+                       case EditPage::AS_READ_ONLY_PAGE:\r
+                               $this->dieUsageMsg(array('readonlytext'));\r
+                       case EditPage::AS_RATE_LIMITED:\r
+                               $this->dieUsageMsg(array('actionthrottledtext'));\r
+                       case EditPage::AS_ARTICLE_WAS_DELETED:\r
+                               $this->dieUsageMsg(array('wasdeleted'));\r
+                       case EditPage::AS_NO_CREATE_PERMISSION:\r
+                               $this->dieUsageMsg(array('nocreate-loggedin'));\r
+                       case EditPage::AS_BLANK_ARTICLE:\r
+                               $this->dieUsageMsg(array('blankpage'));\r
+                       case EditPage::AS_CONFLICT_DETECTED:\r
+                               $this->dieUsageMsg(array('editconflict'));\r
+                       #case EditPage::AS_SUMMARY_NEEDED: Can't happen since we set wpIgnoreBlankSummary\r
+                       #case EditPage::AS_TEXTBOX_EMPTY: Can't happen since we don't do sections\r
+                       case EditPage::AS_END:\r
+                               # This usually means some kind of race condition\r
+                               # or DB weirdness occurred. Throw an unknown error here.\r
+                               $this->dieUsageMsg(array('unknownerror', 'AS_END'));\r
+                       case EditPage::AS_SUCCESS_NEW_ARTICLE:\r
+                               $r['new'] = '';\r
+                       case EditPage::AS_SUCCESS_UPDATE:\r
+                               $r['result'] = "Success";\r
+                               $r['pageid'] = $titleObj->getArticleID();\r
+                               $r['title'] = $titleObj->getPrefixedText();\r
+                               $newRevId = $titleObj->getLatestRevId();\r
+                               if($newRevId == $oldRevId)\r
+                                       $r['nochange'] = '';\r
+                               else\r
+                               {\r
+                                       $r['oldrevid'] = $oldRevId;\r
+                                       $r['newrevid'] = $newRevId;\r
+                               }\r
+                               break;\r
+                       default:\r
+                               $this->dieUsageMsg(array('unknownerror', $retval));\r
+               }\r
+               $this->getResult()->addValue(null, $this->getModuleName(), $r);\r
+       }\r
+\r
+       protected function getDescription() {\r
+               return 'Create and edit pages.';\r
+       }\r
+\r
+       protected function getAllowedParams() {\r
+               return array (\r
+                       'title' => null,\r
+                       'text' => null,\r
+                       'token' => null,\r
+                       'summary' => null,\r
+                       'minor' => false,\r
+                       'notminor' => false,\r
+                       'bot' => false,\r
+                       'basetimestamp' => null,\r
+                       'recreate' => false,\r
+                       'captchaword' => null,\r
+                       'captchaid' => null,\r
+                       'watch' => false,\r
+                       'unwatch' => false,\r
+               );\r
+       }\r
+\r
+       protected function getParamDescription() {\r
+               return array (\r
+                       'title' => 'Page title',\r
+                       'text' => 'Page content',\r
+                       'token' => 'Edit token. You can get one of these through prop=info',\r
+                       'summary' => 'Edit summary',\r
+                       'minor' => 'Minor edit',\r
+                       'notminor' => 'Non-minor edit',\r
+                       'bot' => 'Mark this edit as bot',\r
+                       'basetimestamp' => array('Timestamp of the base revision (gotten through prop=revisions&rvprop=timestamp).',\r
+                                               'Used to detect edit conflicts; leave unset to ignore conflicts.'\r
+                       ),\r
+                       'recreate' => 'Override any errors about the article having been deleted in the meantime',\r
+                       'watch' => 'Add the page to your watchlist',\r
+                       'unwatch' => 'Remove the page from your watchlist',\r
+                       'captchaid' => 'CAPTCHA ID from previous request',\r
+                       'captchaword' => 'Answer to the CAPTCHA',\r
+               );\r
+       }\r
+\r
+       protected function getExamples() {\r
+               return array (\r
+                       "Edit a page (anonymous user):",\r
+                       "    api.php?action=edit&eptitle=Test&epsummary=test%20summary&eptext=article%20content&epedittime=20070824123454&eptokenid=+%5C"\r
+               );\r
+       }\r
+\r
+       public function getVersion() {\r
+               return __CLASS__ . ': $Id: ApiEditPage.php 22289 2007-08-16 13:27:44Z ilabarg1 $';\r
+       }\r
+}
\ No newline at end of file
index 0711a24..0526a24 100644 (file)
@@ -71,6 +71,7 @@ class ApiMain extends ApiBase {
                'block' => 'ApiBlock',
                'unblock' => 'ApiUnblock',
                'move' => 'ApiMove',
+               'edit' => 'ApiEditPage',
                #'changerights' => 'ApiChangeRights'
                # Disabled for now
        );