Merge "Bug 35671 - PHP Notice: Undefined index: gettoken in includes/api/ApiMain...
[lhc/web/wiklou.git] / includes / WikiPage.php
index 68fd05c..6cad466 100644 (file)
@@ -13,6 +13,30 @@ abstract class Page {}
  * @internal documentation reviewed 15 Mar 2010
  */
 class WikiPage extends Page {
+       // doDeleteArticleReal() return values. Values less than zero indicate fatal errors,
+       // values greater than zero indicate that there were problems not resulting in page
+       // not being deleted
+
+       /**
+        * Delete operation aborted by hook
+        */
+       const DELETE_HOOK_ABORTED = -1;
+
+       /**
+        * Deletion successful
+        */
+       const DELETE_SUCCESS = 0;
+
+       /**
+        * Page not found
+        */
+       const DELETE_NO_PAGE = 1;
+
+       /**
+        * No revisions found to delete
+        */
+       const DELETE_NO_REVISIONS = 2;
+
        /**
         * @var Title
         */
@@ -94,7 +118,7 @@ class WikiPage extends Page {
         *
         * @param $id Int article ID to load
         *
-        * @return WikiPage
+        * @return WikiPage|null
         */
        public static function newFromID( $id ) {
                $t = Title::newFromID( $id );
@@ -390,7 +414,7 @@ class WikiPage extends Page {
         *      Revision::FOR_PUBLIC       to be displayed to all users
         *      Revision::FOR_THIS_USER    to be displayed to $wgUser
         *      Revision::RAW              get the text regardless of permissions
-        * @return String|false The text of the current revision
+        * @return String|bool The text of the current revision. False on failure
         */
        public function getText( $audience = Revision::FOR_PUBLIC ) {
                $this->loadLastEdit();
@@ -403,7 +427,7 @@ class WikiPage extends Page {
        /**
         * Get the text of the current revision. No side-effects...
         *
-        * @return String|false The text of the current revision
+        * @return String|bool The text of the current revision. False on failure
         */
        public function getRawText() {
                $this->loadLastEdit();
@@ -421,6 +445,7 @@ class WikiPage extends Page {
                if ( !$this->mTimestamp ) {
                        $this->loadLastEdit();
                }
+               
                return wfTimestamp( TS_MW, $this->mTimestamp );
        }
 
@@ -864,6 +889,7 @@ class WikiPage extends Page {
 
        /**
         * Perform the actions of a page purging
+        * @return bool
         */
        public function doPurge() {
                global $wgUseSquid;
@@ -879,7 +905,7 @@ class WikiPage extends Page {
                if ( $wgUseSquid ) {
                        // Commit the transaction before the purge is sent
                        $dbw = wfGetDB( DB_MASTER );
-                       $dbw->commit();
+                       $dbw->commit( __METHOD__ );
 
                        // Send purge
                        $update = SquidUpdate::newSimplePurge( $this->mTitle );
@@ -999,7 +1025,7 @@ class WikiPage extends Page {
         * @param $dbw DatabaseBase
         * @param $redirectTitle Title object pointing to the redirect target,
         *                       or NULL if this is not a redirect
-        * @param $lastRevIsRedirect If given, will optimize adding and
+        * @param $lastRevIsRedirect null|bool If given, will optimize adding and
         *                           removing rows in redirect table.
         * @return bool true on success, false on failure
         * @private
@@ -1035,7 +1061,7 @@ class WikiPage extends Page {
         * If the given revision is newer than the currently set page_latest,
         * update the page record. Otherwise, do nothing.
         *
-        * @param $dbw Database object
+        * @param $dbw DatabaseBase object
         * @param $revision Revision object
         * @return mixed
         */
@@ -1100,7 +1126,7 @@ class WikiPage extends Page {
        }
 
        /**
-        * @param $section empty/null/false or a section number (0, 1, 2, T1, T2...)
+        * @param $section null|bool|int or a section number (0, 1, 2, T1, T2...)
         * @param $text String: new text of the section
         * @param $sectionTitle String: new section's subject, only if $section is 'new'
         * @param $edittime String: revision timestamp or null to use the current revision
@@ -1199,7 +1225,7 @@ class WikiPage extends Page {
         * edit-already-exists error will be returned. These two conditions are also possible with
         * auto-detection due to MediaWiki's performance-optimised locking strategy.
         *
-        * @param $baseRevId the revision ID this edit was based off, if any
+        * @param $baseRevId int the revision ID this edit was based off, if any
         * @param $user User the user doing the edit
         *
         * @return Status object. Possible errors:
@@ -1303,7 +1329,7 @@ class WikiPage extends Page {
                        $changed = ( strcmp( $text, $oldtext ) != 0 );
 
                        if ( $changed ) {
-                               $dbw->begin();
+                               $dbw->begin( __METHOD__ );
                                $revisionId = $revision->insertOn( $dbw );
 
                                # Update page
@@ -1325,7 +1351,7 @@ class WikiPage extends Page {
                                        }
 
                                        $revisionId = 0;
-                                       $dbw->rollback();
+                                       $dbw->rollback( __METHOD__ );
                                } else {
                                        global $wgUseRCPatrol;
                                        wfRunHooks( 'NewRevisionFromEditComplete', array( $this, $revision, $baseRevId, $user ) );
@@ -1342,11 +1368,11 @@ class WikiPage extends Page {
 
                                                # Log auto-patrolled edits
                                                if ( $patrolled ) {
-                                                       PatrolLog::record( $rc, true );
+                                                       PatrolLog::record( $rc, true, $user );
                                                }
                                        }
                                        $user->incEditCount();
-                                       $dbw->commit();
+                                       $dbw->commit( __METHOD__ );
                                }
                        } else {
                                // Bug 32948: revision ID must be set to page {{REVISIONID}} and
@@ -1379,14 +1405,14 @@ class WikiPage extends Page {
                        # Create new article
                        $status->value['new'] = true;
 
-                       $dbw->begin();
+                       $dbw->begin( __METHOD__ );
 
                        # Add the page record; stake our claim on this title!
                        # This will return false if the article already exists
                        $newid = $this->insertOn( $dbw );
 
                        if ( $newid === false ) {
-                               $dbw->rollback();
+                               $dbw->rollback( __METHOD__ );
                                $status->fatal( 'edit-already-exists' );
 
                                wfProfileOut( __METHOD__ );
@@ -1423,11 +1449,11 @@ class WikiPage extends Page {
 
                                # Log auto-patrolled edits
                                if ( $patrolled ) {
-                                       PatrolLog::record( $rc, true );
+                                       PatrolLog::record( $rc, true, $user );
                                }
                        }
                        $user->incEditCount();
-                       $dbw->commit();
+                       $dbw->commit( __METHOD__ );
 
                        # Update links, etc.
                        $this->doEditUpdates( $revision, $user, array( 'created' => true ) );
@@ -1474,6 +1500,7 @@ class WikiPage extends Page {
        /**
         * Prepare text which is about to be saved.
         * Returns a stdclass with source, pst and output members
+        * @return bool|object
         */
        public function prepareTextForEdit( $text, $revid = null, User $user = null ) {
                global $wgParser, $wgContLang, $wgUser;
@@ -1889,11 +1916,11 @@ class WikiPage extends Page {
        }
 
        /**
-        * Back-end article deletion
+        * Same as doDeleteArticleReal(), but returns more detailed success/failure status
         * Deletes the article with database consistency, writes logs, purges caches
         *
         * @param $reason string delete reason for deletion log
-        * @param $suppress bitfield
+        * @param $suppress int bitfield
         *      Revision::DELETED_TEXT
         *      Revision::DELETED_COMMENT
         *      Revision::DELETED_USER
@@ -1901,11 +1928,34 @@ class WikiPage extends Page {
         * @param $id int article ID
         * @param $commit boolean defaults to true, triggers transaction end
         * @param &$error Array of errors to append to
-        * @param $user User The relevant user
+        * @param $user User The deleting user
         * @return boolean true if successful
         */
        public function doDeleteArticle(
                $reason, $suppress = false, $id = 0, $commit = true, &$error = '', User $user = null
+       ) {
+               return $this->doDeleteArticleReal( $reason, $suppress, $id, $commit, $error, $user )
+                       == WikiPage::DELETE_SUCCESS;
+       }
+
+       /**
+        * Back-end article deletion
+        * Deletes the article with database consistency, writes logs, purges caches
+        *
+        * @param $reason string delete reason for deletion log
+        * @param $suppress int bitfield
+        *      Revision::DELETED_TEXT
+        *      Revision::DELETED_COMMENT
+        *      Revision::DELETED_USER
+        *      Revision::DELETED_RESTRICTED
+        * @param $id int article ID
+        * @param $commit boolean defaults to true, triggers transaction end
+        * @param &$error Array of errors to append to
+        * @param $user User The deleting user
+        * @return int: One of WikiPage::DELETE_* constants
+        */
+       public function doDeleteArticleReal(
+               $reason, $suppress = false, $id = 0, $commit = true, &$error = '', User $user = null
        ) {
                global $wgUser;
                $user = is_null( $user ) ? $wgUser : $user;
@@ -1913,14 +1963,14 @@ class WikiPage extends Page {
                wfDebug( __METHOD__ . "\n" );
 
                if ( ! wfRunHooks( 'ArticleDelete', array( &$this, &$user, &$reason, &$error ) ) ) {
-                       return false;
+                       return WikiPage::DELETE_HOOK_ABORTED;
                }
                $dbw = wfGetDB( DB_MASTER );
                $t = $this->mTitle->getDBkey();
                $id = $id ? $id : $this->mTitle->getArticleID( Title::GAID_FOR_UPDATE );
 
                if ( $t === '' || $id == 0 ) {
-                       return false;
+                       return WikiPage::DELETE_NO_PAGE;
                }
 
                // Bitfields to further suppress the content
@@ -1935,7 +1985,7 @@ class WikiPage extends Page {
                        $bitfield = 'rev_deleted';
                }
 
-               $dbw->begin();
+               $dbw->begin( __METHOD__ );
                // For now, shunt the revision data into the archive table.
                // Text is *not* removed from the text table; bulk storage
                // is left intact to avoid breaking block-compression or
@@ -1972,11 +2022,11 @@ class WikiPage extends Page {
 
                # Now that it's safely backed up, delete it
                $dbw->delete( 'page', array( 'page_id' => $id ), __METHOD__ );
-               $ok = ( $dbw->affectedRows() > 0 ); // getArticleId() uses slave, could be laggy
+               $ok = ( $dbw->affectedRows() > 0 ); // getArticleID() uses slave, could be laggy
 
                if ( !$ok ) {
-                       $dbw->rollback();
-                       return false;
+                       $dbw->rollback( __METHOD__ );
+                       return WikiPage::DELETE_NO_REVISIONS;
                }
 
                $this->doDeleteUpdates( $id );
@@ -1992,11 +2042,11 @@ class WikiPage extends Page {
                $logEntry->publish( $logid );
 
                if ( $commit ) {
-                       $dbw->commit();
+                       $dbw->commit( __METHOD__ );
                }
 
                wfRunHooks( 'ArticleDeleteComplete', array( &$this, &$user, $reason, $id ) );
-               return true;
+               return WikiPage::DELETE_SUCCESS;
        }
 
        /**
@@ -2054,6 +2104,9 @@ class WikiPage extends Page {
                # Clear caches
                self::onArticleDelete( $this->mTitle );
 
+               # Reset this object
+               $this->clear();
+
                # Clear the cached article id so the interface doesn't act like we exist
                $this->mTitle->resetArticleID( 0 );
        }
@@ -2122,6 +2175,7 @@ class WikiPage extends Page {
         *
         * @param $resultDetails Array: contains result-specific array of additional values
         * @param $guser User The user performing the rollback
+        * @return array
         */
        public function commitRollback( $fromP, $summary, $bot, &$resultDetails, User $guser ) {
                global $wgUseRCPatrol, $wgContLang;
@@ -2133,7 +2187,7 @@ class WikiPage extends Page {
                }
 
                # Get the last editor
-               $current = Revision::newFromTitle( $this->mTitle );
+               $current = $this->getRevision();
                if ( is_null( $current ) ) {
                        # Something wrong... no page?
                        return array( array( 'notanarticle' ) );
@@ -2431,9 +2485,8 @@ class WikiPage extends Page {
        public function getAutoDeleteReason( &$hasHistory ) {
                global $wgContLang;
 
-               $dbw = wfGetDB( DB_MASTER );
                // Get the last revision
-               $rev = Revision::newFromTitle( $this->getTitle() );
+               $rev = $this->getRevision();
 
                if ( is_null( $rev ) ) {
                        return false;
@@ -2454,6 +2507,8 @@ class WikiPage extends Page {
                        }
                }
 
+               $dbw = wfGetDB( DB_MASTER );
+
                // Find out if there was only one contributor
                // Only scan the last 20 revisions
                $res = $dbw->select( 'revision', 'rev_user_text',
@@ -2745,6 +2800,7 @@ class WikiPage extends Page {
 
        /**
         * @deprecated since 1.18
+        * @return bool
         */
        public function useParserCache( $oldid ) {
                wfDeprecated( __METHOD__, '1.18' );
@@ -2781,7 +2837,7 @@ class PoolWorkArticleView extends PoolCounterWork {
        private $text;
 
        /**
-        * @var ParserOutput|false
+        * @var ParserOutput|bool
         */
        private $parserOutput = false;
 
@@ -2791,7 +2847,7 @@ class PoolWorkArticleView extends PoolCounterWork {
        private $isDirty = false;
 
        /**
-        * @var Status|false
+        * @var Status|bool
         */
        private $error = false;
 
@@ -2835,7 +2891,7 @@ class PoolWorkArticleView extends PoolCounterWork {
        /**
         * Get a Status object in case of error or false otherwise
         *
-        * @return Status|false
+        * @return Status|bool
         */
        public function getError() {
                return $this->error;
@@ -2861,10 +2917,10 @@ class PoolWorkArticleView extends PoolCounterWork {
                        $text = $rev->getText();
                }
 
-               $time = - wfTime();
+               $time = - microtime( true );
                $this->parserOutput = $wgParser->parse( $text, $this->page->getTitle(),
                        $this->parserOptions, true, true, $this->revid );
-               $time += wfTime();
+               $time += microtime( true );
 
                # Timing hack
                if ( $time > 3 ) {
@@ -2925,6 +2981,7 @@ class PoolWorkArticleView extends PoolCounterWork {
 
        /**
         * @param $status Status
+        * @return bool
         */
        function error( $status ) {
                $this->error = $status;