jenkins-bot has submitted this change and it was merged.

Change subject: remove code dublication for "missing page" message.
......................................................................


remove code dublication for "missing page" message.

Change-Id: Ifaac47cc66b49517326c858b25b1d7f02b2f21fe
---
M repo/Wikibase.php
M repo/includes/Actions/ViewEntityAction.php
M repo/includes/Content/EntityHandler.php
M repo/tests/phpunit/includes/Actions/ViewEntityActionTest.php
M repo/tests/phpunit/includes/Content/EntityHandlerTest.php
5 files changed, 80 insertions(+), 95 deletions(-)

Approvals:
  Adrian Heine: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/repo/Wikibase.php b/repo/Wikibase.php
index 4f132da..f1e29d1 100644
--- a/repo/Wikibase.php
+++ b/repo/Wikibase.php
@@ -256,6 +256,7 @@
        $wgHooks['SkinMinervaDefaultModules'][] = 
'Wikibase\RepoHooks::onSkinMinervaDefaultModules';
        $wgHooks['ResourceLoaderRegisterModules'][] = 
'Wikibase\RepoHooks::onResourceLoaderRegisterModules';
        $wgHooks['ContentHandlerForModelID'][] = 
'Wikibase\RepoHooks::onContentHandlerForModelID';
+       $wgHooks['BeforeDisplayNoArticleText'][] = 
'Wikibase\ViewEntityAction::onBeforeDisplayNoArticleText';
 
        // CirrusSearch hooks
        $wgHooks['CirrusSearchMappingConfig'][] = 
'Wikibase\Repo\Hooks\CirrusSearchHookHandlers::onCirrusSearchMappingConfig';
diff --git a/repo/includes/Actions/ViewEntityAction.php 
b/repo/includes/Actions/ViewEntityAction.php
index 29de409..084aaf2 100644
--- a/repo/includes/Actions/ViewEntityAction.php
+++ b/repo/includes/Actions/ViewEntityAction.php
@@ -2,13 +2,12 @@
 
 namespace Wikibase;
 
-use ContentHandler;
-use Hooks;
-use LogEventsList;
+use Article;
+use MWException;
 use OutputPage;
-use SpecialPage;
 use ViewAction;
-use Wikibase\Repo\Content\EntityHandler;
+use Wikibase\Repo\WikibaseRepo;
+use Xml;
 
 /**
  * Handles the view action for Wikibase entities.
@@ -22,19 +21,39 @@
 abstract class ViewEntityAction extends ViewAction {
 
        /**
+        * Handler for the BeforeDisplayNoArticleText called by Article.
+        * We implement this solely to replace the standard message that
+        * is shown when an entity does not exists.
+        *
+        * @param Article $article
+        * @return bool
+        * @throws MWException
+        */
+       public static function onBeforeDisplayNoArticleText( Article $article ) 
{
+               $namespaceLookup = 
WikibaseRepo::getDefaultInstance()->getEntityNamespaceLookup();
+               $contentFactory = 
WikibaseRepo::getDefaultInstance()->getEntityContentFactory();
+
+               $ns = $article->getTitle()->getNamespace();
+               $oldid = $article->getOldID();
+               if ( $namespaceLookup->isEntityNamespace( $ns ) && !$oldid ) {
+                       $typeMap = array_flip( 
$namespaceLookup->getEntityNamespaces() );
+                       $entityType = $typeMap[ $ns ]; // isEntityNamespace() 
guarantees that this is set.
+
+                       $handler = $contentFactory->getContentHandlerForType( 
$entityType );
+                       $handler->showMissingEntity( $article->getTitle(), 
$article->getContext() );
+
+                       return false;
+               }
+
+               return true;
+       }
+
+       /**
         * @see ViewAction::show
         *
         * Parent is doing $this->checkCanExecute( $this->getUser() )
         */
        public function show() {
-               if ( !$this->page->exists() ) {
-                       // @fixme could use ShowMissingArticle hook instead.
-                       // Article checks for missing / deleted revisions and 
either
-                       // shows appropriate error page or deleted revision, if 
permission allows.
-                       $this->displayMissingEntity();
-                       return;
-               }
-
                $this->showEntityPage();
        }
 
@@ -64,11 +83,6 @@
 
                // NOTE: page-wide property, independent of user permissions
                $outputPage->addJsConfigVars( 'wbIsEditView', $editable );
-
-               $user = $this->getUser();
-               $parserOptions = $this->page->makeParserOptions( $user );
-
-               $this->page->setParserOptions( $parserOptions );
                $this->page->view();
 
                $this->overrideTitleText( $outputPage );
@@ -121,83 +135,6 @@
        private function setHTMLTitle( OutputPage $outputPage, $titleText ) {
                // Prevent replacing {{...}} by using rawParams() instead of 
params():
                $outputPage->setHTMLTitle( $this->msg( 'pagetitle' 
)->rawParams( $titleText ) );
-       }
-
-       /**
-        * Displays there is no entity for the current page.
-        */
-       private function displayMissingEntity() {
-               $title = $this->getTitle();
-               $oldid = $this->page->getOldID();
-
-               $out = $this->getOutput();
-
-               $out->setPageTitle( $title->getPrefixedText() );
-
-               // TODO: Factor the "show stuff for missing page" code out from 
Article::showMissingArticle,
-               //       so it can be re-used here. The below code is copied & 
modified from there...
-
-               Hooks::run( 'ShowMissingArticle', array( $this->page ) );
-
-               # Show delete and move logs
-               LogEventsList::showLogExtract( $out, array( 'delete', 'move' ), 
$title, '',
-                       array(
-                               'lim' => 10,
-                               'conds' => array( "log_action != 'revision'" ),
-                               'showIfEmpty' => false,
-                               'msgKey' => array( 'moveddeleted-notice' )
-                       )
-               );
-
-               $this->send404Code();
-
-               $hookResult = Hooks::run( 'BeforeDisplayNoArticleText', array( 
$this ) );
-
-               // XXX: ...end of stuff stolen from Article::showMissingArticle
-
-               if ( $hookResult ) {
-                       // Show error message
-                       if ( $oldid ) {
-                               $text = wfMessage( 'missing-article',
-                                       $this->getTitle()->getPrefixedText(),
-                                       wfMessage( 'missingarticle-rev', $oldid 
)->plain() )->plain();
-                       } else {
-                               /** @var EntityHandler $entityHandler */
-                               $entityHandler = ContentHandler::getForTitle( 
$this->getTitle() );
-                               $entityCreationPage = 
$entityHandler->getSpecialPageForCreation();
-
-                               $text = wfMessage( 'wikibase-noentity' 
)->plain();
-
-                               if ( $entityCreationPage !== null
-                                       && $this->getTitle()->quickUserCan( 
'create', $this->getUser() )
-                                       && $this->getTitle()->quickUserCan( 
'edit', $this->getUser() )
-                               ) {
-                                       /*
-                                        * add text with link to special page 
for creating an entity of that type if possible and
-                                        * if user has the rights for it
-                                        */
-                                       $createEntityPage = 
SpecialPage::getTitleFor( $entityCreationPage );
-                                       $text .= ' ' . wfMessage(
-                                               'wikibase-noentity-createone',
-                                               
$createEntityPage->getPrefixedText() // TODO: might be nicer to use an 
'action=create' instead
-                                       )->plain();
-                               }
-                       }
-
-                       $text = "<div class='noarticletext'>\n$text\n</div>";
-
-                       $out->addWikiText( $text );
-               }
-       }
-
-       private function send404Code() {
-               global $wgSend404Code;
-
-               if ( $wgSend404Code ) {
-                       // If there's no backing content, send a 404 Not Found
-                       // for better machine handling of broken links.
-                       $this->getRequest()->response()->header( 'HTTP/1.1 404 
Not Found' );
-               }
        }
 
        /**
diff --git a/repo/includes/Content/EntityHandler.php 
b/repo/includes/Content/EntityHandler.php
index c676d0b..9d0a8b4 100644
--- a/repo/includes/Content/EntityHandler.php
+++ b/repo/includes/Content/EntityHandler.php
@@ -6,6 +6,7 @@
 use ContentHandler;
 use DataUpdate;
 use Diff\Patcher\PatcherException;
+use Html;
 use IContextSource;
 use InvalidArgumentException;
 use Language;
@@ -150,6 +151,32 @@
        abstract protected function getContentClass();
 
        /**
+        * Handle the fact that a given page does not contain an Entity, even 
though it could.
+        * Per default, this behaves similarly to Article::showMissingArticle: 
it shows
+        * a message to the user.
+        *
+        * @see Article::showMissingArticle
+        *
+        * @param Title $title The title of the page that potentially could, 
but does not,
+        *        contain an entity.
+        * @param IContextSource $context Context to use for reporting. In 
particular, output
+        *        will be written to $context->getOutput().
+        */
+       public function showMissingEntity( Title $title, IContextSource 
$context ) {
+               $text = wfMessage( 'wikibase-noentity' )->setContext( $context 
)->plain();
+
+               $dir = $context->getLanguage()->getDir();
+               $lang = $context->getLanguage()->getCode();
+
+               $outputPage = $context->getOutput();
+               $outputPage->addWikiText( Html::openElement( 'div', [
+                               'class' => "noarticletext mw-content-$dir",
+                               'dir' => $dir,
+                               'lang' => $lang,
+                       ] ) . "\n$text\n</div>" );
+       }
+
+       /**
         * @see ContentHandler::getDiffEngineClass
         *
         * @return string
diff --git a/repo/tests/phpunit/includes/Actions/ViewEntityActionTest.php 
b/repo/tests/phpunit/includes/Actions/ViewEntityActionTest.php
index 02495ce..b3692b1 100644
--- a/repo/tests/phpunit/includes/Actions/ViewEntityActionTest.php
+++ b/repo/tests/phpunit/includes/Actions/ViewEntityActionTest.php
@@ -110,6 +110,9 @@
                return $action->getOutput()->getHTML();
        }
 
+       /**
+        * @covers ViewEntityAction::onBeforeDisplayNoArticleText
+        */
        public function testShow404() {
                $id = new ItemId( 'q1122334455' );
                $title = 
WikibaseRepo::getDefaultInstance()->getEntityContentFactory()->getTitleForId( 
$id );
@@ -125,6 +128,7 @@
 
                $action->show();
                $this->assertEquals( 200, $response->getStatusCode(), "response 
code" );
+               $this->assertContains( 'noarticletext', 
$action->getOutput()->getHTML(), "response HTML" );
 
                // $wgSend404Code enabled -----
                $this->setMwGlobals( 'wgSend404Code', true );
diff --git a/repo/tests/phpunit/includes/Content/EntityHandlerTest.php 
b/repo/tests/phpunit/includes/Content/EntityHandlerTest.php
index c8badf8..bffad59 100644
--- a/repo/tests/phpunit/includes/Content/EntityHandlerTest.php
+++ b/repo/tests/phpunit/includes/Content/EntityHandlerTest.php
@@ -4,9 +4,11 @@
 
 use ContentHandler;
 use DataValues\Serializers\DataValueSerializer;
+use FauxRequest;
 use InvalidArgumentException;
 use Language;
 use MWException;
+use RequestContext;
 use Revision;
 use RuntimeException;
 use Title;
@@ -486,4 +488,18 @@
                $this->assertRegExp( '/wb\d+/', $hash, 'contains Wikibase 
version' );
        }
 
+       public function testShowMissingEntity() {
+               $handler = $this->getHandler();
+
+               $title = Title::makeTitle( $handler->getEntityNamespace(), 
'MISSING' );
+
+               $context = new RequestContext( new FauxRequest() );
+               $context->setLanguage( 'qqx' );
+               $context->setTitle( $title );
+
+               $handler->showMissingEntity( $title, $context );
+
+               $this->assertContains( '(wikibase-noentity)', 
$context->getOutput()->getHTML() );
+       }
+
 }

-- 
To view, visit https://gerrit.wikimedia.org/r/292753
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: Ifaac47cc66b49517326c858b25b1d7f02b2f21fe
Gerrit-PatchSet: 8
Gerrit-Project: mediawiki/extensions/Wikibase
Gerrit-Branch: master
Gerrit-Owner: Daniel Kinzler <daniel.kinz...@wikimedia.de>
Gerrit-Reviewer: Addshore <addshorew...@gmail.com>
Gerrit-Reviewer: Adrian Heine <adrian.l...@wikimedia.de>
Gerrit-Reviewer: Aude <aude.w...@gmail.com>
Gerrit-Reviewer: Daniel Kinzler <daniel.kinz...@wikimedia.de>
Gerrit-Reviewer: Hoo man <h...@online.de>
Gerrit-Reviewer: Jonas Kress (WMDE) <jonas.kr...@wikimedia.de>
Gerrit-Reviewer: jenkins-bot <>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to