2008-03-03 18:19:52 +00:00
< ? php
2010-02-22 12:18:42 +00:00
/**
2010-12-22 20:52:06 +00:00
*
2008-03-03 18:19:52 +00:00
*
2010-08-07 19:59:42 +00:00
* Created on August 16 , 2007
*
2012-07-15 20:13:02 +00:00
* Copyright © 2007 Iker Labarga " <Firstname><Lastname>@gmail.com "
2008-03-03 18:19:52 +00:00
*
* This program is free software ; you can redistribute it and / or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation ; either version 2 of the License , or
* ( at your option ) any later version .
*
* This program is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
* GNU General Public License for more details .
*
* You should have received a copy of the GNU General Public License along
* with this program ; if not , write to the Free Software Foundation , Inc . ,
2010-06-21 13:13:32 +00:00
* 51 Franklin Street , Fifth Floor , Boston , MA 02110 - 1301 , USA .
2008-03-03 18:19:52 +00:00
* http :// www . gnu . org / copyleft / gpl . html
2010-08-07 19:59:42 +00:00
*
* @ file
2008-03-03 18:19:52 +00:00
*/
/**
2008-09-04 13:40:33 +00:00
* A module that allows for editing and creating pages .
2008-03-03 18:19:52 +00:00
*
2008-09-04 13:40:33 +00:00
* Currently , this wraps around the EditPage class in an ugly way ,
* EditPage . php should be rewritten to provide a cleaner interface
WARNING: HUGE COMMIT
Doxygen documentation update:
* Changed alls @addtogroup to @ingroup. @addtogroup adds the comment to the group description, but doesn't add the file, class, function, ... to the group like @ingroup does. See for example http://svn.wikimedia.org/doc/group__SpecialPage.html where it's impossible to see related files, classes, ... that should belong to that group.
* Added @file to file description, it seems that it should be explicitely decalred for file descriptions, otherwise doxygen will think that the comment document the first class, variabled, function, ... that is in that file.
* Removed some empty comments
* Removed some ?>
Added following groups:
* ExternalStorage
* JobQueue
* MaintenanceLanguage
One more thing: there are still a lot of warnings when generating the doc.
2008-05-20 17:13:28 +00:00
* @ ingroup API
2008-03-03 18:19:52 +00:00
*/
class ApiEditPage extends ApiBase {
public function execute () {
2011-10-26 23:27:01 +00:00
$user = $this -> getUser ();
2008-03-03 18:19:52 +00:00
$params = $this -> extractRequestParams ();
2010-02-22 12:18:42 +00:00
2010-01-11 15:55:52 +00:00
if ( is_null ( $params [ 'text' ] ) && is_null ( $params [ 'appendtext' ] ) &&
2013-11-14 12:40:22 +00:00
is_null ( $params [ 'prependtext' ] ) &&
$params [ 'undo' ] == 0
) {
2011-05-19 17:51:16 +00:00
$this -> dieUsageMsg ( 'missingtext' );
2010-02-22 12:18:42 +00:00
}
2010-01-23 22:26:40 +00:00
2012-04-28 13:45:37 +00:00
$pageObj = $this -> getTitleOrPageId ( $params );
$titleObj = $pageObj -> getTitle ();
2011-06-20 22:18:11 +00:00
$apiResult = $this -> getResult ();
2011-06-20 22:13:45 +00:00
2010-12-30 17:06:09 +00:00
if ( $params [ 'redirect' ] ) {
2014-05-18 07:04:36 +00:00
if ( $params [ 'prependtext' ] === null && $params [ 'appendtext' ] === null && $params [ 'section' ] !== 'new' ) {
$this -> dieUsage ( 'You have attempted to edit using the "redirect"-following mode, which must be used in conjuction with section=new, prependtext, or appendtext.' , 'redirect-appendonly' );
}
2010-12-30 17:06:09 +00:00
if ( $titleObj -> isRedirect () ) {
2010-10-23 17:48:08 +00:00
$oldTitle = $titleObj ;
2010-12-30 17:06:09 +00:00
2012-09-05 15:50:13 +00:00
$titles = Revision :: newFromTitle ( $oldTitle , false , Revision :: READ_LATEST )
2013-11-14 12:40:22 +00:00
-> getContent ( Revision :: FOR_THIS_USER , $user )
-> getRedirectChain ();
2010-12-30 17:06:09 +00:00
// array_shift( $titles );
2010-10-23 17:48:08 +00:00
$redirValues = array ();
2013-03-11 03:45:51 +00:00
/** @var $newTitle Title */
2010-10-23 17:48:08 +00:00
foreach ( $titles as $id => $newTitle ) {
2010-12-30 17:06:09 +00:00
2013-01-12 06:50:48 +00:00
if ( ! isset ( $titles [ $id - 1 ] ) ) {
$titles [ $id - 1 ] = $oldTitle ;
2010-10-23 17:48:08 +00:00
}
2010-12-30 17:06:09 +00:00
2010-10-23 17:48:08 +00:00
$redirValues [] = array (
2013-01-12 06:50:48 +00:00
'from' => $titles [ $id - 1 ] -> getPrefixedText (),
2010-10-23 17:48:08 +00:00
'to' => $newTitle -> getPrefixedText ()
);
2010-12-30 17:06:09 +00:00
2010-10-23 17:48:08 +00:00
$titleObj = $newTitle ;
}
2010-12-30 17:06:09 +00:00
2011-06-20 22:18:11 +00:00
$apiResult -> setIndexedTagName ( $redirValues , 'r' );
$apiResult -> addValue ( null , 'redirects' , $redirValues );
2012-11-12 05:42:26 +00:00
// Since the page changed, update $pageObj
$pageObj = WikiPage :: factory ( $titleObj );
2010-08-08 01:14:48 +00:00
}
}
2010-02-22 12:18:42 +00:00
2012-11-12 05:42:26 +00:00
if ( ! isset ( $params [ 'contentmodel' ] ) || $params [ 'contentmodel' ] == '' ) {
$contentHandler = $pageObj -> getContentHandler ();
} else {
$contentHandler = ContentHandler :: getForModelID ( $params [ 'contentmodel' ] );
}
2013-11-16 19:09:17 +00:00
// @todo Ask handler whether direct editing is supported at all! make
// allowFlatEdit() method or some such
2012-11-12 05:42:26 +00:00
if ( ! isset ( $params [ 'contentformat' ] ) || $params [ 'contentformat' ] == '' ) {
$params [ 'contentformat' ] = $contentHandler -> getDefaultFormat ();
}
$contentFormat = $params [ 'contentformat' ];
if ( ! $contentHandler -> isSupportedFormat ( $contentFormat ) ) {
$name = $titleObj -> getPrefixedDBkey ();
$model = $contentHandler -> getModelID ();
2013-04-13 11:36:24 +00:00
$this -> dieUsage ( " The requested format $contentFormat is not supported for content model " .
2013-11-14 12:40:22 +00:00
" $model used by $name " , 'badformat' );
2012-11-12 05:42:26 +00:00
}
2010-02-22 12:18:42 +00:00
if ( $params [ 'createonly' ] && $titleObj -> exists () ) {
2011-05-19 17:51:16 +00:00
$this -> dieUsageMsg ( 'createonly-exists' );
2010-02-22 12:18:42 +00:00
}
if ( $params [ 'nocreate' ] && ! $titleObj -> exists () ) {
2011-05-19 17:51:16 +00:00
$this -> dieUsageMsg ( 'nocreate-missing' );
2010-02-22 12:18:42 +00:00
}
2008-04-14 07:45:50 +00:00
2008-03-03 18:19:52 +00:00
// Now let's check whether we're even allowed to do this
2011-10-26 23:27:01 +00:00
$errors = $titleObj -> getUserPermissionsErrors ( 'edit' , $user );
2010-02-22 12:18:42 +00:00
if ( ! $titleObj -> exists () ) {
2011-10-26 23:27:01 +00:00
$errors = array_merge ( $errors , $titleObj -> getUserPermissionsErrors ( 'create' , $user ) );
2010-02-22 12:18:42 +00:00
}
if ( count ( $errors ) ) {
2010-01-11 15:55:52 +00:00
$this -> dieUsageMsg ( $errors [ 0 ] );
2010-02-22 12:18:42 +00:00
}
2008-03-03 18:19:52 +00:00
2008-06-15 19:59:55 +00:00
$toMD5 = $params [ 'text' ];
2013-11-14 12:40:22 +00:00
if ( ! is_null ( $params [ 'appendtext' ] ) || ! is_null ( $params [ 'prependtext' ] ) ) {
2012-06-25 14:09:08 +00:00
$content = $pageObj -> getContent ();
if ( ! $content ) {
if ( $titleObj -> getNamespace () == NS_MEDIAWIKI ) {
2012-10-10 19:30:40 +00:00
# If this is a MediaWiki:x message, then load the messages
# and return the message value for x.
2012-06-25 14:09:08 +00:00
$text = $titleObj -> getDefaultMessageText ();
if ( $text === false ) {
$text = '' ;
}
2012-08-21 14:14:18 +00:00
try {
$content = ContentHandler :: makeContent ( $text , $this -> getTitle () );
} catch ( MWContentSerializationException $ex ) {
$this -> dieUsage ( $ex -> getMessage (), 'parseerror' );
2013-11-14 12:40:22 +00:00
2012-08-21 14:14:18 +00:00
return ;
}
2012-10-10 19:30:40 +00:00
} else {
# Otherwise, make a new empty content.
$content = $contentHandler -> makeEmptyContent ();
2012-06-25 14:09:08 +00:00
}
2010-02-22 12:18:42 +00:00
}
2013-05-15 01:12:35 +00:00
// @todo Add support for appending/prepending to the Content interface
2012-10-10 19:30:40 +00:00
if ( ! ( $content instanceof TextContent ) ) {
$mode = $contentHandler -> getModelID ();
$this -> dieUsage ( " Can't append to pages using content model $mode " , 'appendnotsupported' );
}
2010-02-22 12:18:42 +00:00
if ( ! is_null ( $params [ 'section' ] ) ) {
2012-06-25 14:09:08 +00:00
if ( ! $contentHandler -> supportsSections () ) {
2012-06-25 21:30:51 +00:00
$modelName = $contentHandler -> getModelID ();
2013-11-16 19:09:17 +00:00
$this -> dieUsage (
" Sections are not supported for this content model: $modelName . " ,
'sectionsnotsupported'
);
2012-06-25 14:09:08 +00:00
}
2013-08-05 15:26:49 +00:00
if ( $params [ 'section' ] == 'new' ) {
// DWIM if they're trying to prepend/append to a new section.
$content = null ;
} else {
// Process the content for section edits
2014-05-01 00:10:43 +00:00
$section = $params [ 'section' ];
2013-08-05 15:26:49 +00:00
$content = $content -> getSection ( $section );
2012-06-25 14:09:08 +00:00
2013-08-05 15:26:49 +00:00
if ( ! $content ) {
$this -> dieUsage ( " There is no section { $section } . " , 'nosuchsection' );
}
2010-02-22 12:18:42 +00:00
}
2009-07-14 20:14:47 +00:00
}
2012-06-25 14:09:08 +00:00
if ( ! $content ) {
$text = '' ;
} else {
$text = $content -> serialize ( $contentFormat );
}
2012-03-05 17:09:41 +00:00
$params [ 'text' ] = $params [ 'prependtext' ] . $text . $params [ 'appendtext' ];
2008-06-15 19:59:55 +00:00
$toMD5 = $params [ 'prependtext' ] . $params [ 'appendtext' ];
2008-06-12 13:05:07 +00:00
}
2010-02-22 12:18:42 +00:00
if ( $params [ 'undo' ] > 0 ) {
if ( $params [ 'undoafter' ] > 0 ) {
if ( $params [ 'undo' ] < $params [ 'undoafter' ] ) {
2010-01-11 15:55:52 +00:00
list ( $params [ 'undo' ], $params [ 'undoafter' ] ) =
2013-11-14 12:40:22 +00:00
array ( $params [ 'undoafter' ], $params [ 'undo' ] );
2010-02-22 12:18:42 +00:00
}
2010-01-11 15:55:52 +00:00
$undoafterRev = Revision :: newFromID ( $params [ 'undoafter' ] );
2009-01-26 13:51:03 +00:00
}
2010-01-11 15:55:52 +00:00
$undoRev = Revision :: newFromID ( $params [ 'undo' ] );
2010-06-27 19:22:46 +00:00
if ( is_null ( $undoRev ) || $undoRev -> isDeleted ( Revision :: DELETED_TEXT ) ) {
2010-01-11 15:55:52 +00:00
$this -> dieUsageMsg ( array ( 'nosuchrevid' , $params [ 'undo' ] ) );
2010-02-22 12:18:42 +00:00
}
2010-01-23 22:26:40 +00:00
2010-02-22 12:18:42 +00:00
if ( $params [ 'undoafter' ] == 0 ) {
2009-01-26 13:51:03 +00:00
$undoafterRev = $undoRev -> getPrevious ();
2010-02-22 12:18:42 +00:00
}
2010-06-27 19:22:46 +00:00
if ( is_null ( $undoafterRev ) || $undoafterRev -> isDeleted ( Revision :: DELETED_TEXT ) ) {
2010-01-11 15:55:52 +00:00
$this -> dieUsageMsg ( array ( 'nosuchrevid' , $params [ 'undoafter' ] ) );
2010-02-22 12:18:42 +00:00
}
2010-01-23 22:26:40 +00:00
2012-06-25 14:09:08 +00:00
if ( $undoRev -> getPage () != $pageObj -> getID () ) {
2013-11-16 19:09:17 +00:00
$this -> dieUsageMsg ( array ( 'revwrongpage' , $undoRev -> getID (),
$titleObj -> getPrefixedText () ) );
2010-02-22 12:18:42 +00:00
}
2012-06-25 14:09:08 +00:00
if ( $undoafterRev -> getPage () != $pageObj -> getID () ) {
2013-11-16 19:09:17 +00:00
$this -> dieUsageMsg ( array ( 'revwrongpage' , $undoafterRev -> getID (),
$titleObj -> getPrefixedText () ) );
2010-02-22 12:18:42 +00:00
}
2013-11-16 19:09:17 +00:00
$newContent = $contentHandler -> getUndoContent (
$pageObj -> getRevision (),
$undoRev ,
$undoafterRev
);
2012-08-21 14:14:18 +00:00
2012-06-25 14:09:08 +00:00
if ( ! $newContent ) {
2011-05-19 17:51:16 +00:00
$this -> dieUsageMsg ( 'undo-failure' );
2010-02-22 12:18:42 +00:00
}
2012-06-25 14:09:08 +00:00
$params [ 'text' ] = $newContent -> serialize ( $params [ 'contentformat' ] );
2009-01-26 13:51:03 +00:00
// If no summary was given and we only undid one rev,
// use an autosummary
2013-11-16 19:09:17 +00:00
if ( is_null ( $params [ 'summary' ] ) &&
$titleObj -> getNextRevisionID ( $undoafterRev -> getID () ) == $params [ 'undo' ]
) {
$params [ 'summary' ] = wfMessage ( 'undo-summary' )
-> params ( $params [ 'undo' ], $undoRev -> getUserText () ) -> inContentLanguage () -> text ();
2010-02-22 12:18:42 +00:00
}
2009-01-26 13:51:03 +00:00
}
2008-06-12 13:05:07 +00:00
2010-01-23 22:52:40 +00:00
// See if the MD5 hash checks out
2010-02-22 12:18:42 +00:00
if ( ! is_null ( $params [ 'md5' ] ) && md5 ( $toMD5 ) !== $params [ 'md5' ] ) {
2011-05-19 17:51:16 +00:00
$this -> dieUsageMsg ( 'hashcheckfailed' );
2010-02-22 12:18:42 +00:00
}
2008-03-03 18:19:52 +00:00
// EditPage wants to parse its stuff from a WebRequest
// That interface kind of sucks, but it's workable
2011-12-20 04:22:06 +00:00
$requestArray = array (
2010-02-22 12:18:42 +00:00
'wpTextbox1' => $params [ 'text' ],
2012-06-25 14:09:08 +00:00
'format' => $contentFormat ,
'model' => $contentHandler -> getModelID (),
2010-02-22 12:18:42 +00:00
'wpEditToken' => $params [ 'token' ],
2014-07-13 01:36:37 +00:00
'wpIgnoreBlankSummary' => '' ,
'wpIgnoreBlankArticle' => true
2008-03-03 18:19:52 +00:00
);
2010-01-23 22:26:40 +00:00
2010-02-22 12:18:42 +00:00
if ( ! is_null ( $params [ 'summary' ] ) ) {
2011-12-20 04:22:06 +00:00
$requestArray [ 'wpSummary' ] = $params [ 'summary' ];
2010-02-22 12:18:42 +00:00
}
2012-09-05 15:50:13 +00:00
2011-12-20 04:15:21 +00:00
if ( ! is_null ( $params [ 'sectiontitle' ] ) ) {
2011-12-20 04:22:06 +00:00
$requestArray [ 'wpSectionTitle' ] = $params [ 'sectiontitle' ];
2011-12-20 04:15:21 +00:00
}
2010-01-23 22:26:40 +00:00
2012-10-28 23:05:40 +00:00
// TODO: Pass along information from 'undoafter' as well
if ( $params [ 'undo' ] > 0 ) {
$requestArray [ 'wpUndidRevision' ] = $params [ 'undo' ];
}
2010-01-23 22:52:40 +00:00
// Watch out for basetimestamp == ''
// wfTimestamp() treats it as NOW, almost certainly causing an edit conflict
2010-06-27 19:22:46 +00:00
if ( ! is_null ( $params [ 'basetimestamp' ] ) && $params [ 'basetimestamp' ] != '' ) {
2011-12-20 04:22:06 +00:00
$requestArray [ 'wpEdittime' ] = wfTimestamp ( TS_MW , $params [ 'basetimestamp' ] );
2010-02-22 12:18:42 +00:00
} else {
2012-06-25 14:09:08 +00:00
$requestArray [ 'wpEdittime' ] = $pageObj -> getTimestamp ();
2010-02-22 12:18:42 +00:00
}
2010-01-23 22:26:40 +00:00
2010-03-25 22:15:08 +00:00
if ( ! is_null ( $params [ 'starttimestamp' ] ) && $params [ 'starttimestamp' ] != '' ) {
2011-12-20 04:22:06 +00:00
$requestArray [ 'wpStarttime' ] = wfTimestamp ( TS_MW , $params [ 'starttimestamp' ] );
2010-02-22 12:18:42 +00:00
} else {
2013-03-07 16:50:43 +00:00
$requestArray [ 'wpStarttime' ] = wfTimestampNow (); // Fake wpStartime
2010-02-22 12:18:42 +00:00
}
2010-01-23 22:26:40 +00:00
2013-04-26 14:42:31 +00:00
if ( $params [ 'minor' ] || ( ! $params [ 'notminor' ] && $user -> getOption ( 'minordefault' ) ) ) {
2011-12-20 04:22:06 +00:00
$requestArray [ 'wpMinoredit' ] = '' ;
2010-02-22 12:18:42 +00:00
}
2010-01-23 22:26:40 +00:00
2010-02-22 12:18:42 +00:00
if ( $params [ 'recreate' ] ) {
2011-12-20 04:22:06 +00:00
$requestArray [ 'wpRecreate' ] = '' ;
2010-02-22 12:18:42 +00:00
}
2010-01-23 22:26:40 +00:00
2010-02-22 12:18:42 +00:00
if ( ! is_null ( $params [ 'section' ] ) ) {
2014-05-01 00:10:43 +00:00
$section = $params [ 'section' ];
if ( ! preg_match ( '/^((T-)?\d+|new)$/' , $section ) ) {
$this -> dieUsage ( " The section parameter must be a valid section id or 'new' " , " invalidsection " );
2010-02-22 12:18:42 +00:00
}
2013-09-11 17:54:22 +00:00
$content = $pageObj -> getContent ();
2014-05-01 00:10:43 +00:00
if ( $section !== '0' && $section != 'new' && ( ! $content || ! $content -> getSection ( $section ) ) ) {
2013-09-11 17:54:22 +00:00
$this -> dieUsage ( " There is no section { $section } . " , 'nosuchsection' );
}
2011-12-20 04:22:06 +00:00
$requestArray [ 'wpSection' ] = $params [ 'section' ];
2010-02-22 12:18:42 +00:00
} else {
2011-12-20 04:22:06 +00:00
$requestArray [ 'wpSection' ] = '' ;
2010-02-22 12:18:42 +00:00
}
2008-04-14 07:45:50 +00:00
2010-04-12 10:44:45 +00:00
$watch = $this -> getWatchlistValue ( $params [ 'watchlist' ], $titleObj );
2010-03-25 22:15:08 +00:00
2009-07-14 20:48:00 +00:00
// Deprecated parameters
2010-02-22 12:18:42 +00:00
if ( $params [ 'watch' ] ) {
2014-08-14 18:15:23 +00:00
$this -> logFeatureUsage ( 'action=edit&watch' );
2008-03-03 18:19:52 +00:00
$watch = true ;
2010-02-22 12:18:42 +00:00
} elseif ( $params [ 'unwatch' ] ) {
2014-08-14 18:15:23 +00:00
$this -> logFeatureUsage ( 'action=edit&unwatch' );
2008-03-03 18:19:52 +00:00
$watch = false ;
2010-02-22 12:18:42 +00:00
}
2010-04-14 12:17:39 +00:00
if ( $watch ) {
2011-12-20 04:22:06 +00:00
$requestArray [ 'wpWatchthis' ] = '' ;
2010-02-22 12:18:42 +00:00
}
2008-03-03 18:19:52 +00:00
2013-07-24 06:26:37 +00:00
// Pass through anything else we might have been given, to support extensions
// This is kind of a hack but it's the best we can do to make extensions work
$requestArray += $this -> getRequest () -> getValues ();
2011-11-17 10:33:20 +00:00
global $wgTitle , $wgRequest ;
2011-12-20 04:22:06 +00:00
$req = new DerivativeRequest ( $this -> getRequest (), $requestArray , true );
2011-11-17 10:33:20 +00:00
// Some functions depend on $wgTitle == $ep->mTitle
// TODO: Make them not or check if they still do
$wgTitle = $titleObj ;
2013-02-17 10:59:41 +00:00
$articleContext = new RequestContext ;
$articleContext -> setRequest ( $req );
2013-02-24 10:34:50 +00:00
$articleContext -> setWikiPage ( $pageObj );
$articleContext -> setUser ( $this -> getUser () );
2013-03-11 03:45:51 +00:00
/** @var $articleObject Article */
2013-02-24 10:34:50 +00:00
$articleObject = Article :: newFromWikiPage ( $pageObj , $articleContext );
2013-02-17 10:59:41 +00:00
2012-06-25 14:09:08 +00:00
$ep = new EditPage ( $articleObject );
2012-03-09 17:23:05 +00:00
2012-09-12 11:43:52 +00:00
// allow editing of non-textual content.
$ep -> allowNonTextContent = true ;
2011-11-17 10:33:20 +00:00
$ep -> setContextTitle ( $titleObj );
2010-01-11 15:55:52 +00:00
$ep -> importFormData ( $req );
2013-07-26 11:43:08 +00:00
$content = $ep -> textbox1 ;
// The following is needed to give the hook the full content of the
// new revision rather than just the current section. (Bug 52077)
2013-11-16 19:09:17 +00:00
if ( ! is_null ( $params [ 'section' ] ) &&
$contentHandler -> supportsSections () && $titleObj -> exists ()
) {
2013-07-26 11:43:08 +00:00
// If sectiontitle is set, use it, otherwise use the summary as the section title (for
// backwards compatibility with old forms/bots).
if ( $ep -> sectiontitle !== '' ) {
$sectionTitle = $ep -> sectiontitle ;
} else {
$sectionTitle = $ep -> summary ;
}
2013-07-31 16:39:14 +00:00
$contentObj = $contentHandler -> unserializeContent ( $content , $contentFormat );
2013-07-26 11:43:08 +00:00
2013-11-16 19:09:17 +00:00
$fullContentObj = $articleObject -> replaceSectionContent (
$params [ 'section' ],
$contentObj ,
$sectionTitle
);
2013-07-26 11:43:08 +00:00
if ( $fullContentObj ) {
$content = $fullContentObj -> serialize ( $contentFormat );
} else {
// This most likely means we have an edit conflict which means that the edit
// wont succeed anyway.
$this -> dieUsageMsg ( 'editconflict' );
}
}
2008-03-03 18:19:52 +00:00
2011-11-23 19:09:57 +00:00
// Run hooks
// Handle APIEditBeforeSave parameters
2008-03-03 18:19:52 +00:00
$r = array ();
2013-07-26 11:43:08 +00:00
if ( ! wfRunHooks ( 'APIEditBeforeSave' , array ( $ep , $content , & $r ) ) ) {
2010-02-22 12:18:42 +00:00
if ( count ( $r ) ) {
$r [ 'result' ] = 'Failure' ;
2011-06-20 22:18:11 +00:00
$apiResult -> addValue ( null , $this -> getModuleName (), $r );
2013-11-14 12:40:22 +00:00
2008-04-14 07:45:50 +00:00
return ;
2010-02-22 12:18:42 +00:00
}
2013-11-17 19:54:11 +00:00
$this -> dieUsageMsg ( 'hookaborted' );
2008-03-03 18:19:52 +00:00
}
2008-04-14 07:45:50 +00:00
2010-01-23 22:52:40 +00:00
// Do the actual save
2012-06-25 14:09:08 +00:00
$oldRevId = $articleObject -> getRevIdFetched ();
2011-06-20 22:18:11 +00:00
$result = null ;
2010-01-23 22:52:40 +00:00
// Fake $wgRequest for some hooks inside EditPage
2011-05-17 22:03:20 +00:00
// @todo FIXME: This interface SUCKS
2008-05-28 09:14:35 +00:00
$oldRequest = $wgRequest ;
$wgRequest = $req ;
2011-10-26 23:27:01 +00:00
$status = $ep -> internalAttemptSave ( $result , $user -> isAllowed ( 'bot' ) && $params [ 'bot' ] );
2008-05-28 09:14:35 +00:00
$wgRequest = $oldRequest ;
2010-08-12 14:25:07 +00:00
2013-04-26 14:42:31 +00:00
switch ( $status -> value ) {
2008-03-03 18:19:52 +00:00
case EditPage :: AS_HOOK_ERROR :
case EditPage :: AS_HOOK_ERROR_EXPECTED :
2011-05-19 17:51:16 +00:00
$this -> dieUsageMsg ( 'hookaborted' );
2010-01-23 22:26:40 +00:00
2012-08-21 14:14:18 +00:00
case EditPage :: AS_PARSE_ERROR :
$this -> dieUsage ( $status -> getMessage (), 'parseerror' );
2008-03-03 18:19:52 +00:00
case EditPage :: AS_IMAGE_REDIRECT_ANON :
2011-05-19 17:51:16 +00:00
$this -> dieUsageMsg ( 'noimageredirect-anon' );
2010-01-23 22:26:40 +00:00
2008-03-03 18:19:52 +00:00
case EditPage :: AS_IMAGE_REDIRECT_LOGGED :
2011-05-19 17:51:16 +00:00
$this -> dieUsageMsg ( 'noimageredirect-logged' );
2010-01-23 22:26:40 +00:00
2008-03-03 18:19:52 +00:00
case EditPage :: AS_SPAM_ERROR :
2010-01-11 15:55:52 +00:00
$this -> dieUsageMsg ( array ( 'spamdetected' , $result [ 'spam' ] ) );
2010-01-23 22:26:40 +00:00
2008-03-03 18:19:52 +00:00
case EditPage :: AS_BLOCKED_PAGE_FOR_USER :
2011-05-19 17:51:16 +00:00
$this -> dieUsageMsg ( 'blockedtext' );
2010-01-23 22:26:40 +00:00
2008-03-03 18:19:52 +00:00
case EditPage :: AS_MAX_ARTICLE_SIZE_EXCEEDED :
case EditPage :: AS_CONTENT_TOO_BIG :
2014-01-24 02:51:11 +00:00
$this -> dieUsageMsg ( array ( 'contenttoobig' , $this -> getConfig () -> get ( 'MaxArticleSize' ) ) );
2010-01-23 22:26:40 +00:00
2008-03-03 18:19:52 +00:00
case EditPage :: AS_READ_ONLY_PAGE_ANON :
2011-05-19 17:51:16 +00:00
$this -> dieUsageMsg ( 'noedit-anon' );
2010-01-23 22:26:40 +00:00
2008-03-03 18:19:52 +00:00
case EditPage :: AS_READ_ONLY_PAGE_LOGGED :
2011-05-19 17:51:16 +00:00
$this -> dieUsageMsg ( 'noedit' );
2010-01-23 22:26:40 +00:00
2008-03-03 18:19:52 +00:00
case EditPage :: AS_READ_ONLY_PAGE :
2009-07-12 12:38:03 +00:00
$this -> dieReadOnly ();
2010-01-23 22:26:40 +00:00
2008-03-03 18:19:52 +00:00
case EditPage :: AS_RATE_LIMITED :
2011-05-19 17:51:16 +00:00
$this -> dieUsageMsg ( 'actionthrottledtext' );
2010-01-23 22:26:40 +00:00
2008-03-03 18:19:52 +00:00
case EditPage :: AS_ARTICLE_WAS_DELETED :
2011-05-19 17:51:16 +00:00
$this -> dieUsageMsg ( 'wasdeleted' );
2010-01-23 22:26:40 +00:00
2008-03-03 18:19:52 +00:00
case EditPage :: AS_NO_CREATE_PERMISSION :
2011-05-19 17:51:16 +00:00
$this -> dieUsageMsg ( 'nocreate-loggedin' );
2010-01-23 22:26:40 +00:00
2008-03-03 18:19:52 +00:00
case EditPage :: AS_BLANK_ARTICLE :
2011-05-19 17:51:16 +00:00
$this -> dieUsageMsg ( 'blankpage' );
2010-01-23 22:26:40 +00:00
2008-03-03 18:19:52 +00:00
case EditPage :: AS_CONFLICT_DETECTED :
2011-05-19 17:51:16 +00:00
$this -> dieUsageMsg ( 'editconflict' );
2010-01-23 22:26:40 +00:00
2010-01-23 22:52:40 +00:00
// case EditPage::AS_SUMMARY_NEEDED: Can't happen since we set wpIgnoreBlankSummary
2008-08-26 20:40:44 +00:00
case EditPage :: AS_TEXTBOX_EMPTY :
2011-05-19 17:51:16 +00:00
$this -> dieUsageMsg ( 'emptynewsection' );
2010-01-23 22:26:40 +00:00
2008-03-03 18:19:52 +00:00
case EditPage :: AS_SUCCESS_NEW_ARTICLE :
$r [ 'new' ] = '' ;
2013-03-11 03:45:51 +00:00
// fall-through
2010-07-06 13:15:59 +00:00
2008-03-03 18:19:52 +00:00
case EditPage :: AS_SUCCESS_UPDATE :
2010-02-22 12:18:42 +00:00
$r [ 'result' ] = 'Success' ;
2010-01-11 15:55:52 +00:00
$r [ 'pageid' ] = intval ( $titleObj -> getArticleID () );
2008-03-03 18:19:52 +00:00
$r [ 'title' ] = $titleObj -> getPrefixedText ();
2012-06-25 14:09:08 +00:00
$r [ 'contentmodel' ] = $titleObj -> getContentModel ();
$newRevId = $articleObject -> getLatest ();
2010-02-22 12:18:42 +00:00
if ( $newRevId == $oldRevId ) {
2008-03-03 18:19:52 +00:00
$r [ 'nochange' ] = '' ;
2010-02-22 12:18:42 +00:00
} else {
2010-01-11 15:55:52 +00:00
$r [ 'oldrevid' ] = intval ( $oldRevId );
$r [ 'newrevid' ] = intval ( $newRevId );
$r [ 'newtimestamp' ] = wfTimestamp ( TS_ISO_8601 ,
2012-06-25 14:09:08 +00:00
$pageObj -> getTimestamp () );
2008-03-03 18:19:52 +00:00
}
break ;
2010-07-23 07:33:40 +00:00
2010-06-27 19:22:46 +00:00
case EditPage :: AS_SUMMARY_NEEDED :
2011-05-19 17:51:16 +00:00
$this -> dieUsageMsg ( 'summaryrequired' );
2010-01-23 22:26:40 +00:00
2009-10-28 13:35:39 +00:00
case EditPage :: AS_END :
2012-05-10 04:48:16 +00:00
default :
2011-08-26 16:26:17 +00:00
// $status came from WikiPage::doEdit()
$errors = $status -> getErrorsArray ();
$this -> dieUsageMsg ( $errors [ 0 ] ); // TODO: Add new errors to message map
break ;
2008-03-03 18:19:52 +00:00
}
2011-06-20 22:18:11 +00:00
$apiResult -> addValue ( null , $this -> getModuleName (), $r );
2008-03-03 18:19:52 +00:00
}
2008-05-23 22:02:23 +00:00
public function mustBePosted () {
return true ;
}
2009-03-06 13:49:44 +00:00
public function isWriteMode () {
return true ;
}
2011-08-26 22:56:31 +00:00
public function getDescription () {
2014-03-09 20:22:47 +00:00
return 'Create and edit pages.' ;
2008-03-03 18:19:52 +00:00
}
2010-02-22 12:18:42 +00:00
2011-08-17 22:24:21 +00:00
public function getAllowedParams () {
2010-02-22 12:18:42 +00:00
return array (
2010-08-04 14:15:33 +00:00
'title' => array (
ApiBase :: PARAM_TYPE => 'string' ,
2012-04-05 20:54:18 +00:00
),
'pageid' => array (
ApiBase :: PARAM_TYPE => 'integer' ,
2010-08-04 14:15:33 +00:00
),
2008-03-12 13:25:12 +00:00
'section' => null ,
2011-12-20 04:15:21 +00:00
'sectiontitle' => array (
ApiBase :: PARAM_TYPE => 'string' ,
),
2008-03-03 18:19:52 +00:00
'text' => null ,
'summary' => null ,
'minor' => false ,
'notminor' => false ,
'bot' => false ,
'basetimestamp' => null ,
2008-10-04 13:43:17 +00:00
'starttimestamp' => null ,
2008-03-03 18:19:52 +00:00
'recreate' => false ,
2008-03-25 21:12:01 +00:00
'createonly' => false ,
2008-06-15 20:37:28 +00:00
'nocreate' => false ,
2009-10-28 00:56:07 +00:00
'watch' => array (
2010-02-22 12:18:42 +00:00
ApiBase :: PARAM_DFLT => false ,
ApiBase :: PARAM_DEPRECATED => true ,
2009-10-28 00:56:07 +00:00
),
'unwatch' => array (
2010-02-22 12:18:42 +00:00
ApiBase :: PARAM_DFLT => false ,
ApiBase :: PARAM_DEPRECATED => true ,
2009-10-28 00:56:07 +00:00
),
2009-07-14 20:48:00 +00:00
'watchlist' => array (
2010-02-22 12:18:42 +00:00
ApiBase :: PARAM_DFLT => 'preferences' ,
ApiBase :: PARAM_TYPE => array (
2010-01-11 15:55:52 +00:00
'watch' ,
'unwatch' ,
'preferences' ,
2009-07-14 20:48:00 +00:00
'nochange'
),
),
2008-05-28 09:22:40 +00:00
'md5' => null ,
2008-06-12 13:05:07 +00:00
'prependtext' => null ,
'appendtext' => null ,
2009-01-26 13:51:03 +00:00
'undo' => array (
2010-02-22 12:18:42 +00:00
ApiBase :: PARAM_TYPE => 'integer'
2009-01-26 13:51:03 +00:00
),
'undoafter' => array (
2010-02-22 12:18:42 +00:00
ApiBase :: PARAM_TYPE => 'integer'
2009-01-26 13:51:03 +00:00
),
2010-08-08 01:14:48 +00:00
'redirect' => array (
ApiBase :: PARAM_TYPE => 'boolean' ,
ApiBase :: PARAM_DFLT => false ,
),
2012-08-21 14:14:18 +00:00
'contentformat' => array (
ApiBase :: PARAM_TYPE => ContentHandler :: getAllContentFormats (),
),
'contentmodel' => array (
ApiBase :: PARAM_TYPE => ContentHandler :: getContentModels (),
)
2008-03-03 18:19:52 +00:00
);
}
2011-08-17 22:24:21 +00:00
public function getParamDescription () {
2010-05-11 22:30:18 +00:00
$p = $this -> getModulePrefix ();
2013-11-14 12:40:22 +00:00
2010-02-22 12:18:42 +00:00
return array (
2012-04-05 20:54:18 +00:00
'title' => " Title of the page you want to edit. Cannot be used together with { $p } pageid " ,
2012-04-07 17:55:49 +00:00
'pageid' => " Page ID of the page you want to edit. Cannot be used together with { $p } title " ,
2008-03-12 13:25:12 +00:00
'section' => 'Section number. 0 for the top section, \'new\' for a new section' ,
2011-12-20 04:15:21 +00:00
'sectiontitle' => 'The title for a new section' ,
2008-03-03 18:19:52 +00:00
'text' => 'Page content' ,
2013-11-16 19:09:17 +00:00
'token' => array (
2014-08-08 16:56:07 +00:00
/* Standard description is automatically prepended */
'The token should always be sent as the last parameter, or at ' .
2013-11-16 19:09:17 +00:00
" least, after the { $p } text parameter "
2011-06-17 14:55:03 +00:00
),
2013-11-16 19:09:17 +00:00
'summary'
=> " Edit summary. Also section title when { $p } section=new and { $p } sectiontitle is not set " ,
2008-03-03 18:19:52 +00:00
'minor' => 'Minor edit' ,
'notminor' => 'Non-minor edit' ,
'bot' => 'Mark this edit as bot' ,
2013-11-16 19:09:17 +00:00
'basetimestamp' => array (
'Timestamp of the base revision (obtained through prop=revisions&rvprop=timestamp).' ,
2013-11-14 12:40:22 +00:00
'Used to detect edit conflicts; leave unset to ignore conflicts'
2008-03-03 18:19:52 +00:00
),
2013-11-16 19:09:17 +00:00
'starttimestamp' => array (
2014-08-08 16:56:07 +00:00
'Timestamp when you began the editing process, e.g. when the current page content ' .
'was loaded for editing.' ,
2013-11-14 12:40:22 +00:00
'Used to detect edit conflicts; leave unset to ignore conflicts'
2008-10-04 13:43:17 +00:00
),
2008-03-03 18:19:52 +00:00
'recreate' => 'Override any errors about the article having been deleted in the meantime' ,
2008-06-15 20:37:28 +00:00
'createonly' => 'Don\'t edit the page if it exists already' ,
'nocreate' => 'Throw an error if the page doesn\'t exist' ,
2009-10-28 00:56:07 +00:00
'watch' => 'Add the page to your watchlist' ,
'unwatch' => 'Remove the page from your watchlist' ,
2013-11-16 19:09:17 +00:00
'watchlist' => 'Unconditionally add or remove the page from your ' .
'watchlist, use preferences or do not change watch' ,
'md5' => array (
" The MD5 hash of the { $p } text parameter, or the { $p } prependtext " .
" and { $p } appendtext parameters concatenated. " ,
'If set, the edit won\'t be done unless the hash is correct'
),
2010-05-11 22:30:18 +00:00
'prependtext' => " Add this text to the beginning of the page. Overrides { $p } text " ,
2012-04-28 18:59:01 +00:00
'appendtext' => array ( " Add this text to the end of the page. Overrides { $p } text. " ,
2013-11-14 12:40:22 +00:00
" Use { $p } section=new to append a new section " ),
2010-05-11 22:30:18 +00:00
'undo' => " Undo this revision. Overrides { $p } text, { $p } prependtext and { $p } appendtext " ,
2009-01-26 13:51:03 +00:00
'undoafter' => 'Undo all revisions from undo to this one. If not set, just undo one revision' ,
2010-08-08 01:14:48 +00:00
'redirect' => 'Automatically resolve redirects' ,
2012-08-21 14:14:18 +00:00
'contentformat' => 'Content serialization format used for the input text' ,
'contentmodel' => 'Content model of the new content' ,
2008-03-03 18:19:52 +00:00
);
}
2010-02-22 12:18:42 +00:00
2010-10-01 20:12:50 +00:00
public function needsToken () {
2014-08-08 16:56:07 +00:00
return 'csrf' ;
2010-02-14 22:20:27 +00:00
}
2008-03-03 18:19:52 +00:00
2011-08-17 22:24:21 +00:00
public function getExamples () {
2010-02-22 12:18:42 +00:00
return array (
2013-11-16 19:09:17 +00:00
'api.php?action=edit&title=Test&summary=test%20summary&' .
'text=article%20content&basetimestamp=20070824123454&token=%2B\\'
2011-12-27 16:22:35 +00:00
=> 'Edit a page (anonymous user)' ,
2013-11-16 19:09:17 +00:00
'api.php?action=edit&title=Test&summary=NOTOC&minor=&' .
'prependtext=__NOTOC__%0A&basetimestamp=20070824123454&token=%2B\\'
2011-12-27 16:22:35 +00:00
=> 'Prepend __NOTOC__ to a page (anonymous user)' ,
2013-11-16 19:09:17 +00:00
'api.php?action=edit&title=Test&undo=13585&undoafter=13579&' .
'basetimestamp=20070824123454&token=%2B\\'
2011-12-27 16:22:35 +00:00
=> 'Undo r13579 through r13585 with autosummary (anonymous user)' ,
2008-03-03 18:19:52 +00:00
);
}
2011-07-17 16:38:24 +00:00
public function getHelpUrls () {
2011-11-28 15:43:11 +00:00
return 'https://www.mediawiki.org/wiki/API:Edit' ;
2011-07-17 16:38:24 +00:00
}
2008-03-12 13:25:12 +00:00
}