r25309 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r25308‎ | r25309 | r25310 >
Date:11:02, 30 August 2007
Author:ilabarg1
Status:old
Tags:
Comment:
[ApiEditPage.php]-Class to handle edit action since API (only POST request allowed).
Modified paths:
  • /branches/ApiEdit_Vodafone/includes/api/ApiEditPage.php (added) (history)

Diff [purge]

Index: branches/ApiEdit_Vodafone/includes/api/ApiEditPage.php
@@ -0,0 +1,338 @@
 2+<?php
 3+/*
 4+ * Created on 17/07/2007
 5+ *
 6+ * To change the template for this generated file go to
 7+ * Window - Preferences - PHPeclipse - PHP - Code Templates
 8+ */
 9+?>
 10+
 11+<?php
 12+
 13+/*
 14+ * Created on August 16, 2007
 15+ *
 16+ * API for MediaWiki 1.8+
 17+ *
 18+ * Copyright (C) 2007 Iker Labarga <Firstname><Lastname>@gmail.com
 19+ *
 20+ * This program is free software; you can redistribute it and/or modify
 21+ * it under the terms of the GNU General Public License as published by
 22+ * the Free Software Foundation; either version 2 of the License, or
 23+ * (at your option) any later version.
 24+ *
 25+ * This program is distributed in the hope that it will be useful,
 26+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
 27+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 28+ * GNU General Public License for more details.
 29+ *
 30+ * You should have received a copy of the GNU General Public License along
 31+ * with this program; if not, write to the Free Software Foundation, Inc.,
 32+ * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 33+ * http://www.gnu.org/copyleft/gpl.html
 34+ */
 35+
 36+if (!defined('MEDIAWIKI')) {
 37+ // Eclipse helper - will be ignored in production
 38+ require_once ("ApiBase.php");
 39+}
 40+
 41+/**
 42+ * A query module to list all external URLs found on a given set of pages.
 43+ *
 44+ * @addtogroup API
 45+ */
 46+class ApiEditPage extends ApiBase {
 47+ //----------------------------------------
 48+ //**** APIEDITPAGE CONSTANTS (2xx) value ****
 49+ const BAD_LGTOKEN = 001;
 50+ const BAD_EDITTOKEN = 002;
 51+ const NO_POST_REQUEST = 003;
 52+ const AS_SUCCESS_UPDATE = 200;
 53+ const AS_SUCCESS_NEW_ARTICLE = 201;
 54+ const AS_HOOK_ERROR = 210;
 55+ const AS_FILTERING = 211;
 56+ const AS_EXIST_HOOK_ERROR_A = 212;
 57+ const AS_EXIST_HOOK_ERROR_B = 213;
 58+ const AS_EXIST_HOOK_ERROR_C = 214;
 59+ const AS_BLOCKED_PAGE_FOR_USER = 215;
 60+ const AS_CONTENT_TOO_BIG = 216;
 61+ const AS_USER_CANNOT_EDIT = 217;
 62+ const AS_READ_ONLY_PAGE_ANON = 218;
 63+ const AS_READ_ONLY_PAGE_LOGGED = 219;
 64+ const AS_READ_ONLY_PAGE = 220;
 65+ const AS_RATE_LIMITED = 221;
 66+ const AS_ARTICLE_WAS_DELETED = 222;
 67+ const AS_NO_CREATE_PERMISSION = 223;
 68+ const AS_BLANK_ARTICLE = 224;
 69+ const AS_CONFLICT_DETECTED = 225;
 70+ const AS_SUMMARY_NEEDED_A = 226;
 71+ const AS_SUMMARY_NEEDED_B = 227;
 72+ const AS_TEXTBOX_EMPTY = 228;
 73+ const AS_MAX_ARTICLE_SIZE_EXCEDED = 229;
 74+ const AS_OK = 230;
 75+ const AS_END = 231;
 76+ const AS_SPAM_ERROR = 232;
 77+ //----------------------------------------
 78+
 79+ public function __construct($query, $moduleName) {
 80+ parent :: __construct($query, $moduleName, 'ep');
 81+ }
 82+
 83+ public function execute() {
 84+ global $wgUser, $wgRequest;
 85+print "**** API EDIT PAGE BEGINS.. - EXECUTE (AEP-E)**** <br>";
 86+ $title = $text = $summary = $edittime = $lgtoken = $userid = $tokenid = null;
 87+
 88+ if( session_id() == '' ) {
 89+ wfSetupSession();
 90+ }
 91+
 92+ extract($this->extractRequestParams());
 93+
 94+print "-------------------------------<br>";
 95+print "AEP-E->Inner params :<br>";
 96+print_r($this->extractRequestParams())."<br>";
 97+print "FROM extract-title:" .$title."<br>";
 98+print "FROM extract-text:" .$text."<br>";
 99+print "FROM extract-summary:" .$summary."<br>";
 100+print "FROM extract-edittime:" .$edittime."<br>";
 101+print "FROM extract-lgtoken:" .$lgtoken."<br>";
 102+print "FROM extract-userid:" .$userid."<br>";
 103+print "FROM extract-tokenid:" .$tokenid."<br>";
 104+print "-------------------------------<br>";
 105+
 106+ $object_title = Title::newFromDBkey($title);
 107+ $myArticle = new Article($object_title);
 108+
 109+ // User creation since UserID number
 110+ if ($userid != 0){
 111+ $myUser = new User();
 112+ $myUser->setID($userid);
 113+ $myUser->loadFromId();
 114+ $myUser->setCookies();
 115+ $wgUser = $myUser;
 116+
 117+ if ($lgtoken != $_SESSION['wsToken']){
 118+ print 'LGTOKEN ARE DIFFERENT! - ERROR !!!!!!!!!!!<br>';
 119+ $value = BAD_LGTOKEN;
 120+ }
 121+ }
 122+
 123+ if ($value != 'BAD_LGTOKEN'){
 124+ $md5 = $wgUser->editToken();
 125+ // This is only to fast testing. So must be cleanned before a Release
 126+ $tokenid = $md5;
 127+
 128+ $params = new FauxRequest(array (
 129+ 'wpTitle' => $title,
 130+ 'wpTextbox1' => $text,
 131+ 'wpSummary' => $summary,
 132+ 'wpEdittime' => $edittime,
 133+ 'wplgToken' => $lgtoken,
 134+ 'wpUserID' => $userid,
 135+ 'wpEditToken' => $tokenid
 136+ ));
 137+
 138+ // APiEditPage only accepts POST requests
 139+ if (!$_SERVER['REQUEST_METHOD']){
 140+ $value = 'NO_POST_REQUEST';
 141+ }
 142+
 143+ else{
 144+ $params->wasPosted = true;
 145+
 146+ if ($md5 != $tokenid){
 147+ print 'TOKEN ARE DIFFERENT! - ERROR !!!!!!!!!!!<br>';
 148+ $value = BAD_EDITTOKEN;
 149+ }
 150+
 151+ else {
 152+print 'ApiEditPage-->Token ok ..<br>';
 153+ $editForm = new EditPage($myArticle);
 154+ $editForm->mTitle = $object_title;
 155+ $editForm->importFormData($params);
 156+
 157+print "-------------------------------<br>";
 158+print "AEP-E->Param sent by editPage->attemptSave: <br>";
 159+print "AEP-E->form->mTitle:" .$editForm->mTitle."<br>";
 160+print "AEP-E->form->textbox1:" .$editForm->textbox1."<br>";
 161+print "AEP-E->form->summary:" .$editForm->summary."<br>";
 162+print "AEP-E->form->revid:" .$editForm->revid."<br>";
 163+print "AEP-E->form->edittime:" .$editForm->edittime."<br>";
 164+
 165+ $value=$editForm->attemptSave();
 166+print "VALUE returned by attemptSave:".$value."<br>";
 167+ }
 168+ }
 169+ }
 170+print "VALUE tested in SWITCH:".$value."br";
 171+ switch ($value){
 172+ case self::AS_END:
 173+ $result['result'] = 'COnflict detected';
 174+ break;
 175+
 176+ case self::AS_SUCCESS_UPDATE:
 177+ $result['result'] = 'Success';
 178+ $result['title'] = $editForm->mTitle;
 179+ $result['id'] = $myArticle->getID();
 180+ $result['revid'] = $myArticle->getRevIdFetched();
 181+ $rtext['content'] = $editForm->textbox1;
 182+ break;
 183+
 184+ case self::AS_MAX_ARTICLE_SIZE_EXCEDED:
 185+ $result['result'] = 'Article too long';
 186+ break;
 187+
 188+ case self::AS_TEXTBOX_EMPTY:
 189+ $result['result'] = 'Blank edition';
 190+ break;
 191+
 192+ case self::AS_SUMMARY_NEEDED_B:
 193+ $result['result'] = 'Summary is mandatory (B)';
 194+ break;
 195+
 196+ case self::AS_SUMMARY_NEEDED_A:
 197+ $result['result'] = 'Summary is mandatory (A)';
 198+ break;
 199+
 200+ case self::AS_CONFLICT_DETECTED:
 201+ $result['result'] = 'Conflict detected';
 202+ break;
 203+
 204+ case self::AS_SUCCESS_NEW_ARTICLE:
 205+ $result['result'] = 'Success';
 206+ $result['title'] = $editForm->mTitle;
 207+ $result['id'] = $myArticle->getID();
 208+ $result['revid'] = $myArticle->getRevIdFetched();
 209+ $rtext['content'] = $editForm->textbox1;
 210+ break;
 211+
 212+ case self::AS_BLANK_ARTICLE:
 213+ $result['result'] = 'Blank article';
 214+ break;
 215+
 216+ case self::AS_NO_CREATE_PERMISSION;
 217+ $result['result'] = 'No create permission';
 218+ break;
 219+
 220+ case self::AS_ARTICLE_WAS_DELETED:
 221+ $result['result'] = 'Article was deleted before';
 222+ break;
 223+
 224+ case self::AS_RATE_LIMITED:
 225+ $result['result'] = 'Rate limit excedeed';
 226+ break;
 227+
 228+ case self::AS_READ_ONLY_PAGE:
 229+ $result['result'] = 'Read only page';
 230+ break;
 231+
 232+ case self::AS_READ_ONLY_PAGE_LOGGED:
 233+ $result['result'] = 'Read only allowed';
 234+ break;
 235+
 236+ case self::AS_READ_ONLY_PAGE_ANON:
 237+ $result['result'] = 'Read only allowed';
 238+ break;
 239+
 240+ case self::AS_CONTENT_TOO_BIG:
 241+ $result['result'] = 'Article too long';
 242+ break;
 243+
 244+ case self::AS_BLOCKED_PAGE_FOR_USER:
 245+ $result['result'] = 'Blocked page for the user';
 246+ break;
 247+
 248+ case self::AS_EXIST_HOOK_ERROR_A:
 249+ $result['result'] = 'Hook error detected(A)';
 250+ break;
 251+
 252+ case self::AS_SPAM_ERROR:
 253+ $result['result'] = 'Spam error detected';
 254+ break;
 255+
 256+ case self::AS_FILTERING:
 257+ $result['result'] = 'Filtering not passed';
 258+ break;
 259+
 260+ case self::AS_EXIST_HOOK_ERROR_B:
 261+ $result['result'] = 'Hook error detected(B)';
 262+ break;
 263+
 264+ case self::AS_EXIST_HOOK_ERROR_C:
 265+ $result['result'] = 'Hook error detected(C)';
 266+ break;
 267+
 268+ case self::NO_POST_REQUEST:
 269+ $result['result'] = 'Error.Only POST requests are allowed';
 270+ break;
 271+
 272+ case 'BAD_LGTOKEN':
 273+ $result['result'] = "Error.Login token is wrong";
 274+ break;
 275+
 276+ case 'BAD_EDITTOKEN':
 277+ $result['result'] = "Error.Edit token is wrong";
 278+ break;
 279+
 280+ default :
 281+ $result['result'] = 'Invalid';
 282+ break;
 283+ }
 284+
 285+ $this->getResult()->addValue(null, 'editpage', $result);
 286+ if (isset ($rtext['content'])) $this->getResult()->addValue('text', 'content', $rtext);
 287+
 288+ }
 289+
 290+ protected function getAllowedParams() {
 291+ return array (
 292+ 'title' => array(
 293+ ApiBase :: PARAM_TYPE => 'string'
 294+ ),
 295+ 'text' => array(
 296+ ApiBase :: PARAM_TYPE => 'string'
 297+ ),
 298+ 'summary' => array(
 299+ ApiBase :: PARAM_TYPE => 'string'
 300+ ),
 301+ 'userid' => array(
 302+ ApiBase :: PARAM_TYPE => 'string'
 303+ ),
 304+ 'edittime' => array(
 305+ ApiBase :: PARAM_TYPE => 'string'
 306+ ),
 307+ 'lgtoken' => array(
 308+ ApiBase :: PARAM_TYPE => 'string'
 309+ ),
 310+ 'tokenid' => array(
 311+ ApiBase :: PARAM_TYPE => 'string'
 312+ ),
 313+ );
 314+ }
 315+
 316+ protected function getDescription() {
 317+ return array (
 318+ 'title' => 'Title of article',
 319+ 'text' => 'text of article',
 320+ 'summary' => 'Summary of article',
 321+ 'userid' => 'ID of the user',
 322+ 'edittime' => 'Timestamp of base revision edited',
 323+ 'lgtoken' => 'Login token of the user'
 324+
 325+ );
 326+ }
 327+
 328+ protected function getExamples() {
 329+ return array (
 330+ "Edit a page (anonimous user)",
 331+ "http://localhost/WikiMob/api.php?action=edit&eptitle=Test&epsummary=test%20summary&eptext=article%20content&epedittime=20070824123454&eptokenid=+\\"
 332+ );
 333+ }
 334+
 335+ public function getVersion() {
 336+ return __CLASS__ . ': $Id: ApiEditPage.php 22289 2007-08-16 13:27:44Z ilabarg1 $';
 337+ }
 338+}
 339+?>

Status & tagging log