Fix example per r78832 CR
[lhc/web/wiklou.git] / includes / api / ApiEditPage.php
index ea3bcd6..62954ec 100644 (file)
@@ -1,9 +1,8 @@
 <?php
-
 /**
- * Created on August 16, 2007
  *
- * API for MediaWiki 1.8+
+ *
+ * Created on August 16, 2007
  *
  * Copyright © 2007 Iker Labarga <Firstname><Lastname>@gmail.com
  *
  *
  * 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 )
@@ -60,6 +57,37 @@ class ApiEditPage extends ApiBase {
                if ( !$titleObj || $titleObj->isExternal() ) {
                        $this->dieUsageMsg( array( 'invalidtitle', $params['title'] ) );
                }
+               
+               if( $params['redirect'] ) {
+                       if( $titleObj->isRedirect() ) {
+                               $oldTitle = $titleObj;
+                               
+                               $titles = Title::newFromRedirectArray( Revision::newFromTitle( $oldTitle )->getText( Revision::FOR_THIS_USER ) );
+                               //array_shift( $titles );
+                               
+                               $this->getResult()->addValue( null, 'foo', $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 );
+
+                       }
+               }
 
                // Some functions depend on $wgTitle == $ep->mTitle
                global $wgTitle;
@@ -117,16 +145,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 +170,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 +195,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,7 +204,7 @@ 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' ) ) )     {
@@ -193,8 +217,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'];
@@ -202,7 +225,7 @@ class ApiEditPage extends ApiBase {
                        $reqArr['wpSection'] = '';
                }
 
-               $watch = $this->getWatchlistValue( $params['watchlist'], $titleObj ) || $wgUser->getOption( 'watchcreations' );
+               $watch = $this->getWatchlistValue( $params['watchlist'], $titleObj );
 
                // Deprecated parameters
                if ( $params['watch'] ) {
@@ -229,8 +252,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 +272,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 +296,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 +328,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,6 +349,9 @@ 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. Fall through to throw an unknown
@@ -355,7 +382,6 @@ class ApiEditPage extends ApiBase {
                global $wgMaxArticleSize;
 
                return array_merge( parent::getPossibleErrors(), array(
-                       array( 'missingparam', 'title' ),
                        array( 'missingtext' ),
                        array( 'invalidtitle', 'title' ),
                        array( 'createonly-exists' ),
@@ -369,6 +395,7 @@ class ApiEditPage extends ApiBase {
                        array( 'noimageredirect-anon' ),
                        array( 'noimageredirect-logged' ),
                        array( 'spamdetected', 'spam' ),
+                       array( 'summaryrequired' ),
                        array( 'filtered' ),
                        array( 'blockedtext' ),
                        array( 'contenttoobig', $wgMaxArticleSize ),
@@ -388,7 +415,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,
@@ -429,10 +459,15 @@ class ApiEditPage extends ApiBase {
                        'undoafter' => array(
                                ApiBase::PARAM_TYPE => 'integer'
                        ),
+                       'redirect' => array(
+                               ApiBase::PARAM_TYPE => 'boolean',
+                               ApiBase::PARAM_DFLT => false,
+                       ),
                );
        }
 
        protected function getParamDescription() {
+               $p = $this->getModulePrefix();
                return array(
                        'title' => 'Page title',
                        'section' => 'Section number. 0 for the top section, \'new\' for a new section',
@@ -446,7 +481,7 @@ class ApiEditPage extends ApiBase {
                                                'Used to detect edit conflicts; leave unset to ignore conflicts.'
                        ),
                        'starttimestamp' => array( 'Timestamp when you obtained the edit token.',
-                                               'Used to detect edit conflicts; leave unset to ignore conflicts.'
+                                               'Used to detect edit conflicts; leave unset to ignore conflicts'
                        ),
                        'recreate' => 'Override any errors about the article having been deleted in the meantime',
                        'createonly' => 'Don\'t edit the page if it exists already',
@@ -456,15 +491,20 @@ class ApiEditPage extends ApiBase {
                        'watchlist' => 'Unconditionally add or remove the page from your watchlist, use preferences or do not change watch',
                        'captchaid' => 'CAPTCHA ID from previous request',
                        'captchaword' => 'Answer to the CAPTCHA',
-                       'md5' => array( 'The MD5 hash of the text parameter, or the prependtext and appendtext parameters concatenated.',
+                       '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' ),
-                       'prependtext' => 'Add this text to the beginning of the page. Overrides text.',
-                       'appendtext' => 'Add this text to the end of the page. Overrides text',
-                       'undo' => 'Undo this revision. Overrides text, prependtext and appendtext',
+                       '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",
                        'undoafter' => 'Undo all revisions from undo to this one. If not set, just undo one revision',
+                       'redirect' => 'Automatically resolve redirects',
                );
        }
 
+       public function needsToken() {
+               return true;
+       }
+
        public function getTokenSalt() {
                return '';
        }
@@ -474,7 +514,7 @@ class ApiEditPage extends ApiBase {
                        '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\\',
                );