Index: branches/ApiEdit_Vodafone/includes/api/ApiTalk.php |
— | — | @@ -0,0 +1,389 @@ |
| 2 | +<?php
|
| 3 | +/*
|
| 4 | + * Created on 24/09/2007
|
| 5 | + *
|
| 6 | + * API for MediaWiki 1.8+
|
| 7 | + *
|
| 8 | + * Copyright (C) 2007 Iker Labarga <Firstname><Lastname>@gmail.com
|
| 9 | + *
|
| 10 | + * This program is free software; you can redistribute it and/or modify
|
| 11 | + * it under the terms of the GNU General Public License as published by
|
| 12 | + * the Free Software Foundation; either version 2 of the License, or
|
| 13 | + * (at your option) any later version.
|
| 14 | + *
|
| 15 | + * This program is distributed in the hope that it will be useful,
|
| 16 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
| 17 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
| 18 | + * GNU General Public License for more details.
|
| 19 | + *
|
| 20 | + * You should have received a copy of the GNU General Public License along
|
| 21 | + * with this program; if not, write to the Free Software Foundation, Inc.,
|
| 22 | + * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
| 23 | + * http://www.gnu.org/copyleft/gpl.html
|
| 24 | + */
|
| 25 | +
|
| 26 | +if (!defined('MEDIAWIKI')) {
|
| 27 | + // Eclipse helper - will be ignored in production
|
| 28 | + require_once ("ApiBase.php");
|
| 29 | +}
|
| 30 | +
|
| 31 | +/**
|
| 32 | + * This module allows create and edit discussion and comments for article and user pages.
|
| 33 | + *
|
| 34 | + * @addtogroup API
|
| 35 | + */
|
| 36 | +class ApiTalk extends ApiBase {
|
| 37 | + const BAD_LGTOKEN = 001;
|
| 38 | + const BAD_EDITTOKEN = 002;
|
| 39 | + const NO_POST_REQUEST = 003;
|
| 40 | +
|
| 41 | + const PAGE_NOT_EXIST = 004;
|
| 42 | + const DISCUSSION_NOT_EXIST = 005;
|
| 43 | + const WRONG_REQUEST = 006;
|
| 44 | + const USER_NOT_EXIST = 007;
|
| 45 | +
|
| 46 | + const AS_SUCCESS_UPDATE = 200;
|
| 47 | + const AS_SUCCESS_NEW_ARTICLE = 201;
|
| 48 | + const AS_HOOK_ERROR = 210;
|
| 49 | + const AS_FILTERING = 211;
|
| 50 | + const AS_HOOK_ERROR_EXPECTED = 212;
|
| 51 | + const AS_BLOCKED_PAGE_FOR_USER = 215;
|
| 52 | + const AS_CONTENT_TOO_BIG = 216;
|
| 53 | + const AS_USER_CANNOT_EDIT = 217;
|
| 54 | + const AS_READ_ONLY_PAGE_ANON = 218;
|
| 55 | + const AS_READ_ONLY_PAGE_LOGGED = 219;
|
| 56 | + const AS_READ_ONLY_PAGE = 220;
|
| 57 | + const AS_RATE_LIMITED = 221;
|
| 58 | + const AS_ARTICLE_WAS_DELETED = 222;
|
| 59 | + const AS_NO_CREATE_PERMISSION = 223;
|
| 60 | + const AS_BLANK_ARTICLE = 224;
|
| 61 | + const AS_CONFLICT_DETECTED = 225;
|
| 62 | + const AS_SUMMARY_NEEDED = 226;
|
| 63 | + const AS_TEXTBOX_EMPTY = 228;
|
| 64 | + const AS_MAX_ARTICLE_SIZE_EXCEDED = 229;
|
| 65 | + const AS_OK = 230;
|
| 66 | + const AS_END = 231;
|
| 67 | + const AS_SPAM_ERROR = 232;
|
| 68 | +
|
| 69 | + public function __construct($query, $moduleName) {
|
| 70 | + parent :: __construct($query, $moduleName, 'ta');
|
| 71 | + }
|
| 72 | +
|
| 73 | + public function execute() {
|
| 74 | + global $wgUser, $wgRequest;
|
| 75 | + $title = $text = $summary = $type = $edittime = $lgtoken = $userid = $tokenid = $value = null;
|
| 76 | + $section = 'no';
|
| 77 | +
|
| 78 | + if( session_id() == '' ) {
|
| 79 | + wfSetupSession();
|
| 80 | + }
|
| 81 | +
|
| 82 | + extract($this->extractRequestParams());
|
| 83 | +
|
| 84 | + // Ensure the correct timestamp format
|
| 85 | + $edittime =eregi_replace("[-,a-z,:]","",$edittime);
|
| 86 | + $page_title = '';
|
| 87 | +
|
| 88 | + if ($type == 'talk'){
|
| 89 | + $page_title = $title;
|
| 90 | + $object_article_title = Title::newFromDBkey($page_title);
|
| 91 | +
|
| 92 | + // Test if asociated article exist
|
| 93 | + $myArticle = new Article($object_article_title);
|
| 94 | + if (!$myArticle->exists()){
|
| 95 | + $value = PAGE_NOT_EXIST;
|
| 96 | + }
|
| 97 | + else{
|
| 98 | + $object_discussion_title = Title::newFromDBkey($title);
|
| 99 | + $object_discussion_title->mNamespace=1;
|
| 100 | + $myDiscussion = new Article($object_discussion_title);
|
| 101 | + if ($section == 'yes'){
|
| 102 | + $section_value = 'new';
|
| 103 | + }
|
| 104 | + }
|
| 105 | + }
|
| 106 | +
|
| 107 | + else if ($type == 'user'){
|
| 108 | + $user_owner = new User();
|
| 109 | +
|
| 110 | + if ($user_owner->idFromName($title) == null){
|
| 111 | + $value = USER_NOT_EXIST;
|
| 112 | + }
|
| 113 | + else{
|
| 114 | + $page_title = 'User:'.$title;
|
| 115 | + $object_article_title = Title::newFromDBkey($page_title);
|
| 116 | +
|
| 117 | + // Test if user page exist
|
| 118 | + $myArticle = new Article($object_article_title);
|
| 119 | + if (!$myArticle->exists()){
|
| 120 | + $value = PAGE_NOT_EXIST;
|
| 121 | + }
|
| 122 | + else {
|
| 123 | + $object_discussion_title = Title::newFromDBkey($title);
|
| 124 | + $object_discussion_title->mNamespace=3;
|
| 125 | + $myDiscussion = new Article($object_discussion_title);
|
| 126 | + if ($section == 'yes'){
|
| 127 | + $section_value = 'new';
|
| 128 | + }
|
| 129 | + }
|
| 130 | + }
|
| 131 | + }
|
| 132 | +
|
| 133 | + else {
|
| 134 | + $value = WRONG_REQUEST;
|
| 135 | + }
|
| 136 | +
|
| 137 | + if (($value != 'PAGE_NOT_EXIST') && ($value != 'USER_NOT_EXIST') && ($value != 'WRONG_REQUEST')){
|
| 138 | + // User creation since UserID number
|
| 139 | + if ($userid != 0){
|
| 140 | + $myUser = new User();
|
| 141 | + $myUser->setID($userid);
|
| 142 | + $myUser->loadFromId();
|
| 143 | + $myUser->setCookies();
|
| 144 | + $wgUser = $myUser;
|
| 145 | + if ($lgtoken != $_SESSION['wsToken']){
|
| 146 | + $value = BAD_LGTOKEN;
|
| 147 | + }
|
| 148 | + }
|
| 149 | +
|
| 150 | + if ((!$myDiscussion->exists()) && ($section == 'yes')){
|
| 151 | + $value = DISCUSSION_NOT_EXIST;
|
| 152 | + }
|
| 153 | +
|
| 154 | + if (($value != 'BAD_LGTOKEN') && ($value != 'DISCUSSION_NOT_EXIST')){
|
| 155 | + $md5 = $wgUser->editToken();
|
| 156 | + // This is only to fast testing. So must be cleanned before a Release
|
| 157 | + $tokenid = $md5;
|
| 158 | +
|
| 159 | + $params = new FauxRequest(array (
|
| 160 | + 'wpTitle' => $myDiscussion->getTitle(),
|
| 161 | + 'wpTextbox1' => $text,
|
| 162 | + 'wpSummary' => $summary,
|
| 163 | + 'wpEdittime' => $edittime,
|
| 164 | + 'wplgToken' => $lgtoken,
|
| 165 | + 'wpUserID' => $userid,
|
| 166 | + 'wpEditToken' => $tokenid,
|
| 167 | + 'wpSection' => $section_value,
|
| 168 | + ));
|
| 169 | +
|
| 170 | + // APiEditPage only accepts POST requests
|
| 171 | + if (!$_SERVER['REQUEST_METHOD']){
|
| 172 | + $value = 'NO_POST_REQUEST';
|
| 173 | + }
|
| 174 | +
|
| 175 | + else{
|
| 176 | + $params->wasPosted = true;
|
| 177 | + if ($md5 != $tokenid){
|
| 178 | + $value = BAD_EDITTOKEN;
|
| 179 | + }
|
| 180 | +
|
| 181 | + else {
|
| 182 | + $editForm = new EditPage($myDiscussion);
|
| 183 | + $editForm->mTitle = $object_discussion_title;
|
| 184 | + $editForm->importFormData($params);
|
| 185 | + $value=$editForm->attemptSave();
|
| 186 | + }
|
| 187 | + }
|
| 188 | + }
|
| 189 | + }
|
| 190 | +
|
| 191 | + switch ($value){
|
| 192 | + case 'WRONG_REQUEST':
|
| 193 | + $result['result'] = 'Error. Wrong request';
|
| 194 | + break;
|
| 195 | +
|
| 196 | + case 'PAGE_NOT_EXIST':
|
| 197 | + $result['result'] = 'Error. Page not exist';
|
| 198 | + break;
|
| 199 | +
|
| 200 | + case 'USER_NOT_EXIST':
|
| 201 | + $result['result'] = 'Error. User not exist';
|
| 202 | + break;
|
| 203 | +
|
| 204 | + case 'DISCUSSION_NOT_EXIST':
|
| 205 | + $result['result'] = 'Error. Page has not discussion yet';
|
| 206 | + break;
|
| 207 | +
|
| 208 | + case self::AS_END:
|
| 209 | + $result['result'] = 'Conflict detected';
|
| 210 | + break;
|
| 211 | +
|
| 212 | + case self::AS_SUCCESS_UPDATE:
|
| 213 | + $result['result'] = 'Success';
|
| 214 | + $result['title'] = $editForm->mTitle;
|
| 215 | + $result['id'] = $myDiscussion->getID();
|
| 216 | + $result['revid'] = $myDiscussion->getRevIdFetched();
|
| 217 | + $rtext['content'] = $editForm->textbox1;
|
| 218 | + break;
|
| 219 | +
|
| 220 | + case self::AS_MAX_ARTICLE_SIZE_EXCEDED:
|
| 221 | + $result['result'] = 'Article too long';
|
| 222 | + break;
|
| 223 | +
|
| 224 | + case self::AS_TEXTBOX_EMPTY:
|
| 225 | + $result['result'] = 'Blank edition';
|
| 226 | + break;
|
| 227 | +
|
| 228 | + case self::AS_SUMMARY_NEEDED:
|
| 229 | + $result['result'] = 'Summary is mandatory';
|
| 230 | + break;
|
| 231 | +
|
| 232 | + case self::AS_CONFLICT_DETECTED:
|
| 233 | + $result['result'] = 'Conflict detected';
|
| 234 | + break;
|
| 235 | +
|
| 236 | + case self::AS_SUCCESS_NEW_ARTICLE:
|
| 237 | + $result['result'] = 'Success';
|
| 238 | + $result['title'] = $editForm->mTitle;
|
| 239 | + $result['id'] = $myDiscussion->getID();
|
| 240 | + $result['revid'] = $myDiscussion->getRevIdFetched();
|
| 241 | + $rtext['content'] = $editForm->textbox1;
|
| 242 | + break;
|
| 243 | +
|
| 244 | + case self::AS_BLANK_ARTICLE:
|
| 245 | + $result['result'] = 'Blank article';
|
| 246 | + break;
|
| 247 | +
|
| 248 | + case self::AS_NO_CREATE_PERMISSION;
|
| 249 | + $result['result'] = 'No create permission';
|
| 250 | + break;
|
| 251 | +
|
| 252 | + case self::AS_ARTICLE_WAS_DELETED:
|
| 253 | + $result['result'] = 'Article was deleted before';
|
| 254 | + break;
|
| 255 | +
|
| 256 | + case self::AS_RATE_LIMITED:
|
| 257 | + $result['result'] = 'Rate limit excedeed';
|
| 258 | + break;
|
| 259 | +
|
| 260 | + case self::AS_READ_ONLY_PAGE:
|
| 261 | + $result['result'] = 'Read only page';
|
| 262 | + break;
|
| 263 | +
|
| 264 | + case self::AS_READ_ONLY_PAGE_LOGGED:
|
| 265 | + $result['result'] = 'Read only allowed';
|
| 266 | + break;
|
| 267 | +
|
| 268 | + case self::AS_READ_ONLY_PAGE_ANON:
|
| 269 | + $result['result'] = 'Read only allowed';
|
| 270 | + break;
|
| 271 | +
|
| 272 | + case self::AS_CONTENT_TOO_BIG:
|
| 273 | + $result['result'] = 'Article too long';
|
| 274 | + break;
|
| 275 | +
|
| 276 | + case self::AS_BLOCKED_PAGE_FOR_USER:
|
| 277 | + $result['result'] = 'Blocked page for the user';
|
| 278 | + break;
|
| 279 | +
|
| 280 | + case self::AS_HOOK_ERROR:
|
| 281 | + $result['result'] = 'Hook error detected';
|
| 282 | + break;
|
| 283 | +
|
| 284 | + case self::AS_SPAM_ERROR:
|
| 285 | + $result['result'] = 'Spam error detected';
|
| 286 | + break;
|
| 287 | +
|
| 288 | + case self::AS_FILTERING:
|
| 289 | + $result['result'] = 'Filtering not passed';
|
| 290 | + break;
|
| 291 | +
|
| 292 | + case self::AS_HOOK_ERROR_EXPECTED:
|
| 293 | + $result['result'] = 'Hook error detected';
|
| 294 | + break;
|
| 295 | +
|
| 296 | + case self::NO_POST_REQUEST:
|
| 297 | + $result['result'] = 'Error.Only POST requests are allowed';
|
| 298 | + break;
|
| 299 | +
|
| 300 | + case 'BAD_LGTOKEN':
|
| 301 | + $result['result'] = "Error.Login token is wrong";
|
| 302 | + break;
|
| 303 | +
|
| 304 | + case 'BAD_EDITTOKEN':
|
| 305 | + $result['result'] = "Error.Edit token is wrong";
|
| 306 | + break;
|
| 307 | +
|
| 308 | + default :
|
| 309 | + $result['result'] = 'Invalid';
|
| 310 | + break;
|
| 311 | + }
|
| 312 | +
|
| 313 | + $this->getResult()->addValue(null, 'editpage', $result);
|
| 314 | + if (isset ($rtext['content'])) $this->getResult()->addValue('text', 'content', $rtext);
|
| 315 | + }
|
| 316 | +
|
| 317 | + protected function getAllowedParams() {
|
| 318 | + return array (
|
| 319 | + 'title' => array(
|
| 320 | + ApiBase :: PARAM_TYPE => 'string'
|
| 321 | + ),
|
| 322 | + 'text' => array(
|
| 323 | + ApiBase :: PARAM_TYPE => 'string'
|
| 324 | + ),
|
| 325 | + 'summary' => array(
|
| 326 | + ApiBase :: PARAM_TYPE => 'string'
|
| 327 | + ),
|
| 328 | + 'userid' => array(
|
| 329 | + ApiBase :: PARAM_TYPE => 'string'
|
| 330 | + ),
|
| 331 | + 'type' => array(
|
| 332 | + ApiBase :: PARAM_TYPE => 'string'
|
| 333 | + ),
|
| 334 | + 'section' => array(
|
| 335 | + ApiBase :: PARAM_TYPE => 'string'
|
| 336 | + ),
|
| 337 | + 'edittime' => array(
|
| 338 | + ApiBase :: PARAM_TYPE => 'string'
|
| 339 | + ),
|
| 340 | + 'lgtoken' => array(
|
| 341 | + ApiBase :: PARAM_TYPE => 'string'
|
| 342 | + ),
|
| 343 | + 'tokenid' => array(
|
| 344 | + ApiBase :: PARAM_TYPE => 'string'
|
| 345 | + ),
|
| 346 | + );
|
| 347 | + }
|
| 348 | +
|
| 349 | + protected function getDescription() {
|
| 350 | + return 'This module allows create and edit discussion and comments for article and user pages.';
|
| 351 | + }
|
| 352 | +
|
| 353 | + protected function getParamDescription() {
|
| 354 | + return array (
|
| 355 | + 'title' => 'Title of article',
|
| 356 | + 'text' => 'text of article',
|
| 357 | + 'summary' => 'Summary of article',
|
| 358 | + 'type' => 'Type of TALK action, talk (article talk) or user (user talk)',
|
| 359 | + 'section' => 'true or false to comment(true) or discussion(false)',
|
| 360 | + 'userid' => 'ID of the user',
|
| 361 | + 'edittime' => 'Timestamp of base revision edited',
|
| 362 | + 'lgtoken' => 'Login token of the user',
|
| 363 | + 'tokenid' => 'Edit token (ignored)'
|
| 364 | +
|
| 365 | + );
|
| 366 | + }
|
| 367 | +
|
| 368 | +
|
| 369 | + protected function getExamples() {
|
| 370 | + return array (
|
| 371 | + "Multipart post request: api.php ? action=talk ",
|
| 372 | + "Post Parameters:",
|
| 373 | + " tatile= article or user page title ",
|
| 374 | + " tasummary= talk summary",
|
| 375 | + " tatext= content",
|
| 376 | + " tatype= talk/user",
|
| 377 | + " tasection= no/yes",
|
| 378 | + " tauserid= userID",
|
| 379 | + " talgtoken= user lgtoken",
|
| 380 | + " taedittime= page version edittime",
|
| 381 | + " tatokenid= edit token",
|
| 382 | + );
|
| 383 | + }
|
| 384 | +
|
| 385 | + public function getVersion() {
|
| 386 | + return __CLASS__ . ': $Id: ApiTalk.php 22289 2007-09-24 10:20:23Z ilabarg1 $';
|
| 387 | + }
|
| 388 | +}
|
| 389 | +?>
|
| 390 | +
|