Add audience parameter to PoolWorkArticleView
authorGergő Tisza <tgr.huwiki@gmail.com>
Tue, 2 Oct 2018 01:33:28 +0000 (18:33 -0700)
committerGergő Tisza <tgr.huwiki@gmail.com>
Tue, 2 Oct 2018 10:53:28 +0000 (03:53 -0700)
The old behavior was that the audience was RAW if the revision object
parameter got passed in, otherwise PUBLIC. This was undocumented and
not used outside core; this patch gets rid of it in favor of an
explicit argument.

Bug: T205578
Change-Id: Ic7cdb38f658f6d85c48ff13c7f84c64a45c9b1ee

includes/page/Article.php
includes/poolcounter/PoolWorkArticleView.php
tests/phpunit/includes/page/ArticleViewTest.php
tests/phpunit/includes/poolcounter/PoolWorkArticleViewTest.php

index b6f5dce..4a689d3 100644 (file)
@@ -767,7 +767,9 @@ class Article implements Page {
                                                        $parserOptions,
                                                        $this->getRevIdFetched(),
                                                        $useParserCache,
-                                                       $rev
+                                                       $rev,
+                                                       // permission checking was done earlier via showDeletedRevisionHeader()
+                                                       RevisionRecord::RAW
                                                );
                                                $ok = $poolArticleView->execute();
                                                $error = $poolArticleView->getError();
index 157b508..6e6a574 100644 (file)
@@ -44,6 +44,9 @@ class PoolWorkArticleView extends PoolCounterWork {
        /** @var RevisionRecord|null */
        private $revision = null;
 
+       /** @var int */
+       private $audience;
+
        /** @var RevisionStore */
        private $revisionStore = null;
 
@@ -67,9 +70,10 @@ class PoolWorkArticleView extends PoolCounterWork {
         *   operation.
         * @param RevisionRecord|Content|string|null $revision Revision to render, or null to load it;
         *        may also be given as a wikitext string, or a Content object, for BC.
+        * @param int $audience One of the RevisionRecord audience constants
         */
        public function __construct( WikiPage $page, ParserOptions $parserOptions,
-               $revid, $useParserCache, $revision = null
+               $revid, $useParserCache, $revision = null, $audience = RevisionRecord::FOR_PUBLIC
        ) {
                if ( is_string( $revision ) ) { // BC: very old style call
                        $modelId = $page->getRevision()->getContentModel();
@@ -108,6 +112,7 @@ class PoolWorkArticleView extends PoolCounterWork {
                $this->cacheable = $useParserCache;
                $this->parserOptions = $parserOptions;
                $this->revision = $revision;
+               $this->audience = $audience;
                $this->cacheKey = $this->parserCache->getKey( $page, $parserOptions );
                $keyPrefix = $this->cacheKey ?: wfMemcKey( 'articleview', 'missingcachekey' );
 
@@ -152,8 +157,8 @@ class PoolWorkArticleView extends PoolCounterWork {
 
                $isCurrent = $this->revid === $this->page->getLatest();
 
-               // Bypass audience check for current revision
-               $audience = $isCurrent ? RevisionRecord::RAW : RevisionRecord::FOR_PUBLIC;
+               // The current revision cannot be hidden so we can skip some checks.
+               $audience = $isCurrent ? RevisionRecord::RAW : $this->audience;
 
                if ( $this->revision !== null ) {
                        $rev = $this->revision;
index d07a9e1..629621e 100644 (file)
@@ -298,6 +298,34 @@ class ArticleViewTest extends MediaWikiTestCase {
                $this->assertNotContains( 'Test B', $this->getHtml( $output ) );
        }
 
+       public function testUnhiddenViewOfDeletedRevision() {
+               $revisions = [];
+               $page = $this->getPage( __METHOD__, [ 1 => 'Test A', 2 => 'Test B' ], $revisions );
+               $idA = $revisions[1]->getId();
+
+               $revDelList = new RevDelRevisionList(
+                       RequestContext::getMain(), $page->getTitle(), [ $idA ]
+               );
+               $revDelList->setVisibility( [
+                       'value' => [ RevisionRecord::DELETED_TEXT => 1 ],
+                       'comment' => "Testing",
+               ] );
+
+               $article = new Article( $page->getTitle(), $idA );
+               $context = new DerivativeContext( $article->getContext() );
+               $article->setContext( $context );
+               $context->getOutput()->setTitle( $page->getTitle() );
+               $context->getRequest()->setVal( 'unhide', 1 );
+               $context->setUser( $this->getTestUser( [ 'sysop' ] )->getUser() );
+               $article->view();
+
+               $output = $article->getContext()->getOutput();
+               $this->assertContains( '(rev-deleted-text-view)', $this->getHtml( $output ) );
+
+               $this->assertContains( 'Test A', $this->getHtml( $output ) );
+               $this->assertNotContains( 'Test B', $this->getHtml( $output ) );
+       }
+
        public function testViewMissingPage() {
                $page = $this->getPage( __METHOD__ );
 
index a0beb45..47adfc0 100644 (file)
@@ -164,6 +164,10 @@ class PoolWorkArticleViewTest extends MediaWikiTestCase {
                $work = new PoolWorkArticleView( $page, $options, $rev1->getId(), false, $fakeRev );
                $this->assertFalse( $work->execute() );
 
+               $work = new PoolWorkArticleView( $page, $options, $rev1->getId(), false, $fakeRev,
+                       RevisionRecord::RAW );
+               $this->assertNotFalse( $work->execute() );
+
                // a deleted current revision should still be show
                $fakeRev->setId( $rev2->getId() );
                $work = new PoolWorkArticleView( $page, $options, $rev2->getId(), false, $fakeRev );