Call Linker methods statically
[lhc/web/wiklou.git] / includes / api / ApiEditPage.php
index 83d9c16..4cfa343 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /**
- * API for MediaWiki 1.8+
+ *
  *
  * Created on August 16, 2007
  *
@@ -50,7 +50,7 @@ class ApiEditPage extends ApiBase {
                                is_null( $params['prependtext'] ) &&
                                $params['undo'] == 0 )
                {
-                       $this->dieUsageMsg( array( 'missingtext' ) );
+                       $this->dieUsageMsg( 'missingtext' );
                }
 
                $titleObj = Title::newFromText( $params['title'] );
@@ -58,15 +58,33 @@ 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 );
-                       }
+               $apiResult = $this->getResult();
+
+               if ( $params['redirect'] ) {
+                       if ( $titleObj->isRedirect() ) {
+                               $oldTitle = $titleObj;
+
+                               $titles = Title::newFromRedirectArray( Revision::newFromTitle( $oldTitle )->getText( Revision::FOR_THIS_USER ) );
+                               // array_shift( $titles );
+
+                               $redirValues = array();
+                               foreach ( $titles as $id => $newTitle ) {
+
+                                       if ( !isset( $titles[ $id - 1 ] ) ) {
+                                               $titles[ $id - 1 ] = $oldTitle;
+                                       }
+
+                                       $redirValues[] = array(
+                                               'from' => $titles[ $id - 1 ]->getPrefixedText(),
+                                               'to' => $newTitle->getPrefixedText()
+                                       );
+
+                                       $titleObj = $newTitle;
+                               }
 
-                       $this->getResult()->setIndexedTagName( $redirValues, 'r' );
-                       $this->getResult()->addValue( null, 'redirects', $redirValues );
+                               $apiResult->setIndexedTagName( $redirValues, 'r' );
+                               $apiResult->addValue( null, 'redirects', $redirValues );
+                       }
                }
 
                // Some functions depend on $wgTitle == $ep->mTitle
@@ -74,10 +92,10 @@ class ApiEditPage extends ApiBase {
                $wgTitle = $titleObj;
 
                if ( $params['createonly'] && $titleObj->exists() ) {
-                       $this->dieUsageMsg( array( 'createonly-exists' ) );
+                       $this->dieUsageMsg( 'createonly-exists' );
                }
                if ( $params['nocreate'] && !$titleObj->exists() ) {
-                       $this->dieUsageMsg( array( 'nocreate-missing' ) );
+                       $this->dieUsageMsg( 'nocreate-missing' );
                }
 
                // Now let's check whether we're even allowed to do this
@@ -145,7 +163,7 @@ class ApiEditPage extends ApiBase {
 
                        $newtext = $articleObj->getUndoText( $undoRev, $undoafterRev );
                        if ( $newtext === false ) {
-                               $this->dieUsageMsg( array( 'undo-failure' ) );
+                               $this->dieUsageMsg( 'undo-failure' );
                        }
                        $params['text'] = $newtext;
                        // If no summary was given and we only undid one rev,
@@ -157,10 +175,12 @@ class ApiEditPage extends ApiBase {
 
                // See if the MD5 hash checks out
                if ( !is_null( $params['md5'] ) && md5( $toMD5 ) !== $params['md5'] ) {
-                       $this->dieUsageMsg( array( 'hashcheckfailed' ) );
+                       $this->dieUsageMsg( 'hashcheckfailed' );
                }
 
                $ep = new EditPage( $articleObj );
+               $ep->setContextTitle( $titleObj );
+
                // EditPage wants to parse its stuff from a WebRequest
                // That interface kind of sucks, but it's workable
                $reqArr = array(
@@ -187,7 +207,7 @@ class ApiEditPage extends ApiBase {
                        $reqArr['wpStarttime'] = wfTimestampNow();      // Fake wpStartime
                }
 
-               if ( $params['minor'] ) {
+               if ( $params['minor'] || ( !$params['notminor'] && $wgUser->getOption( 'minordefault' ) ) )     {
                        $reqArr['wpMinoredit'] = '';
                }
 
@@ -235,10 +255,10 @@ class ApiEditPage extends ApiBase {
                if ( !wfRunHooks( 'APIEditBeforeSave', array( $ep, $ep->textbox1, &$r ) ) ) {
                        if ( count( $r ) ) {
                                $r['result'] = 'Failure';
-                               $this->getResult()->addValue( null, $this->getModuleName(), $r );
+                               $apiResult->addValue( null, $this->getModuleName(), $r );
                                return;
                        } else {
-                               $this->dieUsageMsg( array( 'hookaborted' ) );
+                               $this->dieUsageMsg( 'hookaborted' );
                        }
                }
 
@@ -246,65 +266,65 @@ class ApiEditPage extends ApiBase {
                $oldRevId = $articleObj->getRevIdFetched();
                $result = null;
                // Fake $wgRequest for some hooks inside EditPage
-               // FIXME: This interface SUCKS
+               // @todo FIXME: This interface SUCKS
                $oldRequest = $wgRequest;
                $wgRequest = $req;
 
-               $retval = $ep->internalAttemptSave( $result, $wgUser->isAllowed( 'bot' ) && $params['bot'] );
+               $status = $ep->internalAttemptSave( $result, $wgUser->isAllowed( 'bot' ) && $params['bot'] );
                $wgRequest = $oldRequest;
                global $wgMaxArticleSize;
 
-               switch( $retval ) {
+               switch( $status->value ) {
                        case EditPage::AS_HOOK_ERROR:
                        case EditPage::AS_HOOK_ERROR_EXPECTED:
-                               $this->dieUsageMsg( array( 'hookaborted' ) );
+                               $this->dieUsageMsg( 'hookaborted' );
 
                        case EditPage::AS_IMAGE_REDIRECT_ANON:
-                               $this->dieUsageMsg( array( 'noimageredirect-anon' ) );
+                               $this->dieUsageMsg( 'noimageredirect-anon' );
 
                        case EditPage::AS_IMAGE_REDIRECT_LOGGED:
-                               $this->dieUsageMsg( array( 'noimageredirect-logged' ) );
+                               $this->dieUsageMsg( 'noimageredirect-logged' );
 
                        case EditPage::AS_SPAM_ERROR:
                                $this->dieUsageMsg( array( 'spamdetected', $result['spam'] ) );
 
                        case EditPage::AS_FILTERING:
-                               $this->dieUsageMsg( array( 'filtered' ) );
+                               $this->dieUsageMsg( 'filtered' );
 
                        case EditPage::AS_BLOCKED_PAGE_FOR_USER:
-                               $this->dieUsageMsg( array( 'blockedtext' ) );
+                               $this->dieUsageMsg( 'blockedtext' );
 
                        case EditPage::AS_MAX_ARTICLE_SIZE_EXCEEDED:
                        case EditPage::AS_CONTENT_TOO_BIG:
                                $this->dieUsageMsg( array( 'contenttoobig', $wgMaxArticleSize ) );
 
                        case EditPage::AS_READ_ONLY_PAGE_ANON:
-                               $this->dieUsageMsg( array( 'noedit-anon' ) );
+                               $this->dieUsageMsg( 'noedit-anon' );
 
                        case EditPage::AS_READ_ONLY_PAGE_LOGGED:
-                               $this->dieUsageMsg( array( 'noedit' ) );
+                               $this->dieUsageMsg( 'noedit' );
 
                        case EditPage::AS_READ_ONLY_PAGE:
                                $this->dieReadOnly();
 
                        case EditPage::AS_RATE_LIMITED:
-                               $this->dieUsageMsg( array( 'actionthrottledtext' ) );
+                               $this->dieUsageMsg( 'actionthrottledtext' );
 
                        case EditPage::AS_ARTICLE_WAS_DELETED:
-                               $this->dieUsageMsg( array( 'wasdeleted' ) );
+                               $this->dieUsageMsg( 'wasdeleted' );
 
                        case EditPage::AS_NO_CREATE_PERMISSION:
-                               $this->dieUsageMsg( array( 'nocreate-loggedin' ) );
+                               $this->dieUsageMsg( 'nocreate-loggedin' );
 
                        case EditPage::AS_BLANK_ARTICLE:
-                               $this->dieUsageMsg( array( 'blankpage' ) );
+                               $this->dieUsageMsg( 'blankpage' );
 
                        case EditPage::AS_CONFLICT_DETECTED:
-                               $this->dieUsageMsg( array( 'editconflict' ) );
+                               $this->dieUsageMsg( 'editconflict' );
 
                        // case EditPage::AS_SUMMARY_NEEDED: Can't happen since we set wpIgnoreBlankSummary
                        case EditPage::AS_TEXTBOX_EMPTY:
-                               $this->dieUsageMsg( array( 'emptynewsection' ) );
+                               $this->dieUsageMsg( 'emptynewsection' );
 
                        case EditPage::AS_SUCCESS_NEW_ARTICLE:
                                $r['new'] = '';
@@ -330,20 +350,17 @@ class ApiEditPage extends ApiBase {
                                break;
 
                        case EditPage::AS_SUMMARY_NEEDED:
-                               $this->dieUsageMsg( array( 'summaryrequired' ) );
+                               $this->dieUsageMsg( 'summaryrequired' );
 
                        case EditPage::AS_END:
-                               // This usually means some kind of race condition
-                               // 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
+                               // $status came from WikiPage::doEdit()
+                               $errors = $status->getErrorsArray();
+                               $this->dieUsageMsg( $errors[0] ); // TODO: Add new errors to message map
+                               break;
                        default:
-                               $this->dieUsageMsg( array( 'unknownerror', $retval ) );
+                               $this->dieUsageMsg( array( 'unknownerror', $status->value ) );
                }
-               $this->getResult()->addValue( null, $this->getModuleName(), $r );
+               $apiResult->addValue( null, $this->getModuleName(), $r );
        }
 
        public function mustBePosted() {
@@ -354,7 +371,7 @@ class ApiEditPage extends ApiBase {
                return true;
        }
 
-       protected function getDescription() {
+       public function getDescription() {
                return 'Create and edit pages.';
        }
 
@@ -390,10 +407,12 @@ class ApiEditPage extends ApiBase {
                        array( 'unknownerror', 'retval' ),
                        array( 'code' => 'nosuchsection', 'info' => 'There is no section section.' ),
                        array( 'code' => 'invalidsection', 'info' => 'The section parameter must be set to an integer or \'new\'' ),
+                       array( 'customcssprotected' ),
+                       array( 'customjsprotected' ),
                ) );
        }
 
-       protected function getAllowedParams() {
+       public function getAllowedParams() {
                return array(
                        'title' => array(
                                ApiBase::PARAM_TYPE => 'string',
@@ -446,18 +465,20 @@ class ApiEditPage extends ApiBase {
                );
        }
 
-       protected function getParamDescription() {
+       public function getParamDescription() {
                $p = $this->getModulePrefix();
                return array(
                        'title' => 'Page title',
                        'section' => 'Section number. 0 for the top section, \'new\' for a new section',
                        'text' => 'Page content',
-                       'token' => 'Edit token. You can get one of these through prop=info',
+                       'token' => array( 'Edit token. You can get one of these through prop=info.',
+                                               'The token should always be sent as the last parameter, or at least, after the text parameter'
+                       ),
                        'summary' => 'Edit summary. Also section title when section=new',
                        'minor' => 'Minor edit',
                        'notminor' => 'Non-minor edit',
                        'bot' => 'Mark this edit as bot',
-                       'basetimestamp' => array( 'Timestamp of the base revision (gotten through prop=revisions&rvprop=timestamp).',
+                       'basetimestamp' => array( 'Timestamp of the base revision (obtained through prop=revisions&rvprop=timestamp).',
                                                'Used to detect edit conflicts; leave unset to ignore conflicts.'
                        ),
                        'starttimestamp' => array( 'Timestamp when you obtained the edit token.',
@@ -472,7 +493,7 @@ class ApiEditPage extends ApiBase {
                        'captchaid' => 'CAPTCHA ID from previous request',
                        'captchaword' => 'Answer to the CAPTCHA',
                        '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' ),
+                                       'If set, the edit won\'t be done unless the hash is correct' ),
                        'prependtext' => "Add this text to the beginning of the page. Overrides {$p}text",
                        '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",
@@ -489,17 +510,21 @@ class ApiEditPage extends ApiBase {
                return '';
        }
 
-       protected function getExamples() {
+       public function getExamples() {
                return array(
                        'Edit a page (anonymous user):',
                        '    api.php?action=edit&title=Test&summary=test%20summary&text=article%20content&basetimestamp=20070824123454&token=%2B\\',
                        'Prepend __NOTOC__ to a page (anonymous user):',
-                       '    api.php?action=edit&title=Test&summary=NOTOC&minor&prependtext=__NOTOC__%0A&basetimestamp=20070824123454&token=%2B\\',
+                       '    api.php?action=edit&title=Test&summary=NOTOC&minor=&prependtext=__NOTOC__%0A&basetimestamp=20070824123454&token=%2B\\',
                        'Undo r13579 through r13585 with autosummary (anonymous user):',
                        '    api.php?action=edit&title=Test&undo=13585&undoafter=13579&basetimestamp=20070824123454&token=%2B\\',
                );
        }
 
+       public function getHelpUrls() {
+               return 'http://www.mediawiki.org/wiki/API:Edit';
+       }
+
        public function getVersion() {
                return __CLASS__ . ': $Id$';
        }