API: sinumberingroup now gives correct size of 'user' group, and omits size of implic...
[lhc/web/wiklou.git] / includes / api / ApiEditPage.php
index 3879f75..8742f8b 100644 (file)
@@ -1,10 +1,9 @@
 <?php
-
 /**
- * Created on August 16, 2007
- *
  * API for MediaWiki 1.8+
  *
+ * Created on August 16, 2007
+ *
  * Copyright © 2007 Iker Labarga <Firstname><Lastname>@gmail.com
  *
  * This program is free software; you can redistribute it and/or modify
  *
  * 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.,
- * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  * http://www.gnu.org/copyleft/gpl.html
+ *
+ * @file
  */
 
 if ( !defined( 'MEDIAWIKI' ) ) {
@@ -45,10 +46,6 @@ class ApiEditPage extends ApiBase {
                global $wgUser;
                $params = $this->extractRequestParams();
 
-               if ( is_null( $params['title'] ) ) {
-                       $this->dieUsageMsg( array( 'missingparam', 'title' ) );
-               }
-
                if ( is_null( $params['text'] ) && is_null( $params['appendtext'] ) &&
                                is_null( $params['prependtext'] ) &&
                                $params['undo'] == 0 )
@@ -61,6 +58,17 @@ class ApiEditPage extends ApiBase {
                        $this->dieUsageMsg( array( 'invalidtitle', $params['title'] ) );
                }
 
+               if( $params['redirect'] && $titleObj->isRedirect() ) {
+                       $pageSet = new ApiPageSet( $this->getQuery(), true ); // Or true, true to also do variant conversion of titles
+                       $pageSet->populateFromTitles( array( $titleObj ) );
+                       foreach ( $pageSet->getRedirectTitles() as $from => $to ) {
+                               $redirsValues[] = array( 'from' => $from, 'to' => $to );
+                       }
+
+                       $this->getResult()->setIndexedTagName( $redirValues, 'r' );
+                       $this->getResult()->addValue( null, 'redirects', $redirValues );
+               }
+
                // Some functions depend on $wgTitle == $ep->mTitle
                global $wgTitle;
                $wgTitle = $titleObj;
@@ -117,16 +125,14 @@ class ApiEditPage extends ApiBase {
                                $undoafterRev = Revision::newFromID( $params['undoafter'] );
                        }
                        $undoRev = Revision::newFromID( $params['undo'] );
-                       if ( is_null( $undoRev ) || $undoRev->isDeleted( Revision::DELETED_TEXT ) )
-                       {
+                       if ( is_null( $undoRev ) || $undoRev->isDeleted( Revision::DELETED_TEXT ) ) {
                                $this->dieUsageMsg( array( 'nosuchrevid', $params['undo'] ) );
                        }
 
                        if ( $params['undoafter'] == 0 ) {
                                $undoafterRev = $undoRev->getPrevious();
                        }
-                       if ( is_null( $undoafterRev ) || $undoafterRev->isDeleted( Revision::DELETED_TEXT ) )
-                       {
+                       if ( is_null( $undoafterRev ) || $undoafterRev->isDeleted( Revision::DELETED_TEXT ) ) {
                                $this->dieUsageMsg( array( 'nosuchrevid', $params['undoafter'] ) );
                        }
 
@@ -144,8 +150,7 @@ class ApiEditPage extends ApiBase {
                        $params['text'] = $newtext;
                        // If no summary was given and we only undid one rev,
                        // use an autosummary
-                       if ( is_null( $params['summary'] ) && $titleObj->getNextRevisionID( $undoafterRev->getID() ) == $params['undo'] )
-                       {
+                       if ( is_null( $params['summary'] ) && $titleObj->getNextRevisionID( $undoafterRev->getID() ) == $params['undo'] ) {
                                $params['summary'] = wfMsgForContent( 'undo-summary', $params['undo'], $undoRev->getUserText() );
                        }
                }
@@ -170,8 +175,7 @@ class ApiEditPage extends ApiBase {
 
                // Watch out for basetimestamp == ''
                // wfTimestamp() treats it as NOW, almost certainly causing an edit conflict
-               if ( !is_null( $params['basetimestamp'] ) && $params['basetimestamp'] != '' )
-               {
+               if ( !is_null( $params['basetimestamp'] ) && $params['basetimestamp'] != '' ) {
                        $reqArr['wpEdittime'] = wfTimestamp( TS_MW, $params['basetimestamp'] );
                } else {
                        $reqArr['wpEdittime'] = $articleObj->getTimestamp();
@@ -180,10 +184,10 @@ class ApiEditPage extends ApiBase {
                if ( !is_null( $params['starttimestamp'] ) && $params['starttimestamp'] != '' ) {
                        $reqArr['wpStarttime'] = wfTimestamp( TS_MW, $params['starttimestamp'] );
                } else {
-                       $reqArr['wpStarttime'] = $reqArr['wpEdittime']; // Fake wpStartime
+                       $reqArr['wpStarttime'] = wfTimestampNow();      // Fake wpStartime
                }
 
-               if ( $params['minor'] || ( !$params['notminor'] && $wgUser->getOption( 'minordefault' ) ) )     {
+               if ( $params['minor'] ) {
                        $reqArr['wpMinoredit'] = '';
                }
 
@@ -193,8 +197,7 @@ class ApiEditPage extends ApiBase {
 
                if ( !is_null( $params['section'] ) ) {
                        $section = intval( $params['section'] );
-                       if ( $section == 0 && $params['section'] != '0' && $params['section'] != 'new' )
-                       {
+                       if ( $section == 0 && $params['section'] != '0' && $params['section'] != 'new' ) {
                                $this->dieUsage( "The section parameter must be set to an integer or 'new'", "invalidsection" );
                        }
                        $reqArr['wpSection'] = $params['section'];
@@ -229,8 +232,7 @@ class ApiEditPage extends ApiBase {
                }
 
                $r = array();
-               if ( !wfRunHooks( 'APIEditBeforeSave', array( $ep, $ep->textbox1, &$r ) ) )
-               {
+               if ( !wfRunHooks( 'APIEditBeforeSave', array( $ep, $ep->textbox1, &$r ) ) ) {
                        if ( count( $r ) ) {
                                $r['result'] = 'Failure';
                                $this->getResult()->addValue( null, $this->getModuleName(), $r );
@@ -250,6 +252,8 @@ class ApiEditPage extends ApiBase {
 
                $retval = $ep->internalAttemptSave( $result, $wgUser->isAllowed( 'bot' ) && $params['bot'] );
                $wgRequest = $oldRequest;
+               global $wgMaxArticleSize;
+
                switch( $retval ) {
                        case EditPage::AS_HOOK_ERROR:
                        case EditPage::AS_HOOK_ERROR_EXPECTED:
@@ -272,7 +276,6 @@ class ApiEditPage extends ApiBase {
 
                        case EditPage::AS_MAX_ARTICLE_SIZE_EXCEEDED:
                        case EditPage::AS_CONTENT_TOO_BIG:
-                               global $wgMaxArticleSize;
                                $this->dieUsageMsg( array( 'contenttoobig', $wgMaxArticleSize ) );
 
                        case EditPage::AS_READ_ONLY_PAGE_ANON:
@@ -305,6 +308,7 @@ class ApiEditPage extends ApiBase {
 
                        case EditPage::AS_SUCCESS_NEW_ARTICLE:
                                $r['new'] = '';
+
                        case EditPage::AS_SUCCESS_UPDATE:
                                $r['result'] = 'Success';
                                $r['pageid'] = intval( $titleObj->getArticleID() );
@@ -325,15 +329,17 @@ class ApiEditPage extends ApiBase {
                                }
                                break;
 
+                       case EditPage::AS_SUMMARY_NEEDED:
+                               $this->dieUsageMsg( array( 'summaryrequired' ) );
+
                        case EditPage::AS_END:
                                // This usually means some kind of race condition
-                               // or DB weirdness occurred. 
-                               if ( is_array( $result ) && count( $result ) > 0 ) {
-                                       $this->dieUsageMsg( array( 'unknownerror', $result[0][0] ) );                                   
-                               }
-                               
-                               // Unknown error, but no specific error message
-                               // Fall through
+                               // or DB weirdness occurred. Fall through to throw an unknown
+                               // error.
+
+                               // This needs fixing higher up, as Article::doEdit should be
+                               // used rather than Article::updateArticle, so that specific
+                               // error conditions can be returned
                        default:
                                $this->dieUsageMsg( array( 'unknownerror', $retval ) );
                }
@@ -356,7 +362,6 @@ class ApiEditPage extends ApiBase {
                global $wgMaxArticleSize;
 
                return array_merge( parent::getPossibleErrors(), array(
-                       array( 'missingparam', 'title' ),
                        array( 'missingtext' ),
                        array( 'invalidtitle', 'title' ),
                        array( 'createonly-exists' ),
@@ -370,6 +375,7 @@ class ApiEditPage extends ApiBase {
                        array( 'noimageredirect-anon' ),
                        array( 'noimageredirect-logged' ),
                        array( 'spamdetected', 'spam' ),
+                       array( 'summaryrequired' ),
                        array( 'filtered' ),
                        array( 'blockedtext' ),
                        array( 'contenttoobig', $wgMaxArticleSize ),
@@ -389,7 +395,10 @@ class ApiEditPage extends ApiBase {
 
        protected function getAllowedParams() {
                return array(
-                       'title' => null,
+                       'title' => array(
+                               ApiBase::PARAM_TYPE => 'string',
+                               ApiBase::PARAM_REQUIRED => true
+                       ),
                        'section' => null,
                        'text' => null,
                        'token' => null,
@@ -430,6 +439,10 @@ class ApiEditPage extends ApiBase {
                        'undoafter' => array(
                                ApiBase::PARAM_TYPE => 'integer'
                        ),
+                       'redirect' => array(
+                               ApiBase::PARAM_TYPE => 'boolean',
+                               ApiBase::PARAM_DFLT => false,
+                       ),
                );
        }
 
@@ -464,6 +477,7 @@ class ApiEditPage extends ApiBase {
                        'appendtext' => "Add this text to the end of the page. Overrides {$p}text",
                        'undo' => "Undo this revision. Overrides {$p}text, {$p}prependtext and {$p}appendtext",
                        'undoafter' => 'Undo all revisions from undo to this one. If not set, just undo one revision',
+                       'redirect' => 'Automatically resolve redirects',
                );
        }