Merge "Move section ID fallbacks into headers themselves"
[lhc/web/wiklou.git] / includes / page / WikiPage.php
index 0e23a88..790845e 100644 (file)
@@ -20,6 +20,7 @@
  * @file
  */
 
+use MediaWiki\Edit\PreparedEdit;
 use \MediaWiki\Logger\LoggerFactory;
 use \MediaWiki\MediaWikiServices;
 use Wikimedia\Rdbms\FakeResultWrapper;
@@ -49,7 +50,7 @@ class WikiPage implements Page, IDBAccessObject {
        public $mLatest = false;             // !< Integer (false means "not loaded")
        /**@}}*/
 
-       /** @var stdClass Map of cache fields (text, parser output, ect) for a proposed/new edit */
+       /** @var PreparedEdit Map of cache fields (text, parser output, ect) for a proposed/new edit */
        public $mPreparedEdit = false;
 
        /**
@@ -382,11 +383,12 @@ class WikiPage implements Page, IDBAccessObject {
                if ( is_int( $from ) ) {
                        list( $index, $opts ) = DBAccessObjectUtils::getDBOptions( $from );
                        $data = $this->pageDataFromTitle( wfGetDB( $index ), $this->mTitle, $opts );
+                       $loadBalancer = MediaWikiServices::getInstance()->getDBLoadBalancer();
 
                        if ( !$data
                                && $index == DB_REPLICA
-                               && wfGetLB()->getServerCount() > 1
-                               && wfGetLB()->hasOrMadeRecentMasterChanges()
+                               && $loadBalancer->getServerCount() > 1
+                               && $loadBalancer->hasOrMadeRecentMasterChanges()
                        ) {
                                $from = self::READ_LATEST;
                                list( $index, $opts ) = DBAccessObjectUtils::getDBOptions( $from );
@@ -781,7 +783,7 @@ class WikiPage implements Page, IDBAccessObject {
         * Determine whether a page would be suitable for being counted as an
         * article in the site_stats table based on the title & its content
         *
-        * @param object|bool $editInfo (false): object returned by prepareTextForEdit(),
+        * @param PreparedEdit|bool $editInfo (false): object returned by prepareTextForEdit(),
         *   if false, the current database state will be used
         * @return bool
         */
@@ -1045,9 +1047,9 @@ class WikiPage implements Page, IDBAccessObject {
         *
         * @since 1.19
         * @param ParserOptions $parserOptions ParserOptions to use for the parse operation
-        * @param null|int      $oldid Revision ID to get the text from, passing null or 0 will
-        *                             get the current revision (default value)
-        * @param bool          $forceParse Force reindexing, regardless of cache settings
+        * @param null|int $oldid Revision ID to get the text from, passing null or 0 will
+        *   get the current revision (default value)
+        * @param bool $forceParse Force reindexing, regardless of cache settings
         * @return bool|ParserOutput ParserOutput or false if the revision was not found
         */
        public function getParserOutput(
@@ -1314,7 +1316,6 @@ class WikiPage implements Page, IDBAccessObject {
         * @return bool
         */
        public function updateIfNewerOn( $dbw, $revision ) {
-
                $row = $dbw->selectRow(
                        [ 'revision', 'page' ],
                        [ 'rev_id', 'rev_timestamp', 'page_is_redirect' ],
@@ -1386,7 +1387,6 @@ class WikiPage implements Page, IDBAccessObject {
        public function replaceSectionContent(
                $sectionId, Content $sectionContent, $sectionTitle = '', $edittime = null
        ) {
-
                $baseRevId = null;
                if ( $edittime && $sectionId !== 'new' ) {
                        $dbr = wfGetDB( DB_REPLICA );
@@ -1425,7 +1425,6 @@ class WikiPage implements Page, IDBAccessObject {
        public function replaceSectionAtRev( $sectionId, Content $sectionContent,
                $sectionTitle = '', $baseRevId = null
        ) {
-
                if ( strval( $sectionId ) === '' ) {
                        // Whole-page edit; let the whole text through
                        $newContent = $sectionContent;
@@ -1609,7 +1608,7 @@ class WikiPage implements Page, IDBAccessObject {
                $meta = [
                        'bot' => ( $flags & EDIT_FORCE_BOT ),
                        'minor' => ( $flags & EDIT_MINOR ) && $user->isAllowed( 'minoredit' ),
-                       'serialized' => $editInfo->pst,
+                       'serialized' => $pstContent->serialize( $serialFormat ),
                        'serialFormat' => $serialFormat,
                        'baseRevId' => $baseRevId,
                        'oldRevision' => $old_revision,
@@ -1639,7 +1638,7 @@ class WikiPage implements Page, IDBAccessObject {
 
        /**
         * @param Content $content Pre-save transform content
-        * @param integer $flags
+        * @param int $flags
         * @param User $user
         * @param string $summary
         * @param array $meta
@@ -1813,7 +1812,7 @@ class WikiPage implements Page, IDBAccessObject {
 
        /**
         * @param Content $content Pre-save transform content
-        * @param integer $flags
+        * @param int $flags
         * @param User $user
         * @param string $summary
         * @param array $meta
@@ -1963,7 +1962,9 @@ class WikiPage implements Page, IDBAccessObject {
 
        /**
         * Prepare content which is about to be saved.
-        * Returns a stdClass with source, pst and output members
+        *
+        * Prior to 1.30, this returned a stdClass object with the same class
+        * members.
         *
         * @param Content $content
         * @param Revision|int|null $revision Revision object. For backwards compatibility, a
@@ -1972,7 +1973,7 @@ class WikiPage implements Page, IDBAccessObject {
         * @param string|null $serialFormat
         * @param bool $useCache Check shared prepared edit cache
         *
-        * @return object
+        * @return PreparedEdit
         *
         * @since 1.21
         */
@@ -2022,7 +2023,7 @@ class WikiPage implements Page, IDBAccessObject {
                $popts = ParserOptions::newFromUserAndLang( $user, $wgContLang );
                Hooks::run( 'ArticlePrepareTextForEdit', [ $this, $popts ] );
 
-               $edit = (object)[];
+               $edit = new PreparedEdit();
                if ( $cachedEdit ) {
                        $edit->timestamp = $cachedEdit->timestamp;
                } else {
@@ -2110,13 +2111,13 @@ class WikiPage implements Page, IDBAccessObject {
         * @param Revision $revision
         * @param User $user User object that did the revision
         * @param array $options Array of options, following indexes are used:
-        * - changed: boolean, whether the revision changed the content (default true)
-        * - created: boolean, whether the revision created the page (default false)
-        * - moved: boolean, whether the page was moved (default false)
-        * - restored: boolean, whether the page was undeleted (default false)
+        * - changed: bool, whether the revision changed the content (default true)
+        * - created: bool, whether the revision created the page (default false)
+        * - moved: bool, whether the page was moved (default false)
+        * - restored: bool, whether the page was undeleted (default false)
         * - oldrevision: Revision object for the pre-update revision (default null)
-        * - oldcountable: boolean, null, or string 'no-change' (default null):
-        *   - boolean: whether the page was counted as an article before that
+        * - oldcountable: bool, null, or string 'no-change' (default null):
+        *   - bool: whether the page was counted as an article before that
         *     revision, only used in changed is true and created is false
         *   - null: if created is false, don't update the article count; if created
         *     is true, do update the article count
@@ -2301,7 +2302,7 @@ class WikiPage implements Page, IDBAccessObject {
                global $wgCascadingRestrictionLevels, $wgContLang;
 
                if ( wfReadOnly() ) {
-                       return Status::newFatal( 'readonlytext', wfReadOnlyReason() );
+                       return Status::newFatal( wfMessage( 'readonlytext', wfReadOnlyReason() ) );
                }
 
                $this->loadPageData( 'fromdbmaster' );
@@ -2483,6 +2484,7 @@ class WikiPage implements Page, IDBAccessObject {
                        $cascade = false;
 
                        if ( $limit['create'] != '' ) {
+                               $commentFields = CommentStore::newKey( 'pt_reason' )->insert( $dbw, $reason );
                                $dbw->replace( 'protected_titles',
                                        [ [ 'pt_namespace', 'pt_title' ] ],
                                        [
@@ -2492,8 +2494,7 @@ class WikiPage implements Page, IDBAccessObject {
                                                'pt_timestamp' => $dbw->timestamp(),
                                                'pt_expiry' => $dbw->encodeExpiry( $expiry['create'] ),
                                                'pt_user' => $user->getId(),
-                                               'pt_reason' => $reason,
-                                       ], __METHOD__
+                                       ] + $commentFields, __METHOD__
                                );
                                $logParamsDetails[] = [
                                        'type' => 'create',
@@ -2745,7 +2746,7 @@ class WikiPage implements Page, IDBAccessObject {
                $reason, $suppress = false, $u1 = null, $u2 = null, &$error = '', User $user = null,
                $tags = [], $logsubtype = 'delete'
        ) {
-               global $wgUser, $wgContentHandlerUseDB;
+               global $wgUser, $wgContentHandlerUseDB, $wgCommentTableSchemaMigrationStage;
 
                wfDebug( __METHOD__ . "\n" );
 
@@ -2809,6 +2810,9 @@ class WikiPage implements Page, IDBAccessObject {
                        $content = null;
                }
 
+               $revCommentStore = new CommentStore( 'rev_comment' );
+               $arCommentStore = new CommentStore( 'ar_comment' );
+
                $fields = Revision::selectFields();
                $bitfield = false;
 
@@ -2826,20 +2830,23 @@ class WikiPage implements Page, IDBAccessObject {
                // the rev_deleted field, which is reserved for this purpose.
 
                // Get all of the page revisions
+               $commentQuery = $revCommentStore->getJoin();
                $res = $dbw->select(
-                       'revision',
-                       $fields,
+                       [ 'revision' ] + $commentQuery['tables'],
+                       $fields + $commentQuery['fields'],
                        [ 'rev_page' => $id ],
                        __METHOD__,
-                       'FOR UPDATE'
+                       'FOR UPDATE',
+                       $commentQuery['joins']
                );
                // Build their equivalent archive rows
                $rowsInsert = [];
+               $revids = [];
                foreach ( $res as $row ) {
+                       $comment = $revCommentStore->getComment( $row );
                        $rowInsert = [
                                'ar_namespace'  => $namespace,
                                'ar_title'      => $dbKey,
-                               'ar_comment'    => $row->rev_comment,
                                'ar_user'       => $row->rev_user,
                                'ar_user_text'  => $row->rev_user_text,
                                'ar_timestamp'  => $row->rev_timestamp,
@@ -2853,12 +2860,13 @@ class WikiPage implements Page, IDBAccessObject {
                                'ar_page_id'    => $id,
                                'ar_deleted'    => $suppress ? $bitfield : $row->rev_deleted,
                                'ar_sha1'       => $row->rev_sha1,
-                       ];
+                       ] + $arCommentStore->insert( $dbw, $comment );
                        if ( $wgContentHandlerUseDB ) {
                                $rowInsert['ar_content_model'] = $row->rev_content_model;
                                $rowInsert['ar_content_format'] = $row->rev_content_format;
                        }
                        $rowsInsert[] = $rowInsert;
+                       $revids[] = $row->rev_id;
                }
                // Copy them into the archive table
                $dbw->insert( 'archive', $rowsInsert, __METHOD__ );
@@ -2873,6 +2881,9 @@ class WikiPage implements Page, IDBAccessObject {
                // Now that it's safely backed up, delete it
                $dbw->delete( 'page', [ 'page_id' => $id ], __METHOD__ );
                $dbw->delete( 'revision', [ 'rev_page' => $id ], __METHOD__ );
+               if ( $wgCommentTableSchemaMigrationStage > MIGRATION_OLD ) {
+                       $dbw->delete( 'revision_comment_temp', [ 'revcomment_rev' => $revids ], __METHOD__ );
+               }
 
                // Log the deletion, if the page was suppressed, put it in the suppression log instead
                $logtype = $suppress ? 'suppress' : 'delete';
@@ -2918,7 +2929,7 @@ class WikiPage implements Page, IDBAccessObject {
        /**
         * Lock the page row for this title+id and return page_latest (or 0)
         *
-        * @return integer Returns 0 if no row was found with this title+id
+        * @return int Returns 0 if no row was found with this title+id
         * @since 1.27
         */
        public function lockAndGetLatest() {
@@ -2973,7 +2984,7 @@ class WikiPage implements Page, IDBAccessObject {
                }
 
                // Clear caches
-               WikiPage::onArticleDelete( $this->mTitle );
+               self::onArticleDelete( $this->mTitle );
                ResourceLoaderWikiModule::invalidateModuleCache(
                        $this->mTitle, $revision, null, wfWikiID()
                );
@@ -3000,7 +3011,7 @@ class WikiPage implements Page, IDBAccessObject {
         * @param string $token Rollback token.
         * @param bool $bot If true, mark all reverted edits as bot.
         *
-        * @param array $resultDetails Array contains result-specific array of additional values
+        * @param array &$resultDetails Array contains result-specific array of additional values
         *    'alreadyrolled' : 'current' (rev)
         *    success        : 'summary' (str), 'current' (rev), 'target' (rev)
         *
@@ -3052,7 +3063,7 @@ class WikiPage implements Page, IDBAccessObject {
         * @param string $summary Custom summary. Set to default summary if empty.
         * @param bool $bot If true, mark all reverted edits as bot.
         *
-        * @param array $resultDetails Contains result-specific array of additional values
+        * @param array &$resultDetails Contains result-specific array of additional values
         * @param User $guser The user performing the rollback
         * @param array|null $tags Change tags to apply to the rollback
         * Callers are responsible for permission checks
@@ -3334,7 +3345,7 @@ class WikiPage implements Page, IDBAccessObject {
                HTMLFileCache::clearFileCache( $title );
 
                $revid = $revision ? $revision->getId() : null;
-               DeferredUpdates::addCallableUpdate( function() use ( $title, $revid ) {
+               DeferredUpdates::addCallableUpdate( function () use ( $title, $revid ) {
                        InfoAction::invalidateCache( $title, $revid );
                } );
        }
@@ -3413,7 +3424,7 @@ class WikiPage implements Page, IDBAccessObject {
         *
         * @param array $added The names of categories that were added
         * @param array $deleted The names of categories that were deleted
-        * @param integer $id Page ID (this should be the original deleted page ID)
+        * @param int $id Page ID (this should be the original deleted page ID)
         */
        public function updateCategoryCounts( array $added, array $deleted, $id = 0 ) {
                $id = $id ?: $this->getId();
@@ -3632,7 +3643,7 @@ class WikiPage implements Page, IDBAccessObject {
                return $this->getTitle()->getCanonicalURL();
        }
 
-       /*
+       /**
         * @param WANObjectCache $cache
         * @return string[]
         * @since 1.28