Lucie Kaffee has uploaded a new change for review.
https://gerrit.wikimedia.org/r/232266
Change subject: [WIP] Add the content to the generated page
......................................................................
[WIP] Add the content to the generated page
Add label, description, aliases, statements and sitelinks to page that is
generated by the special page.
Not formatted yet.
Work in progress, do not merge
Change-Id: I8f42acb1ac915dd50f8be2cdc9d42e0a0879bcc7
---
M Specials/SpecialFancyUnicorn.php
1 file changed, 225 insertions(+), 124 deletions(-)
git pull
ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/ArticlePlaceholder
refs/changes/66/232266/1
diff --git a/Specials/SpecialFancyUnicorn.php b/Specials/SpecialFancyUnicorn.php
index a3c64b6..88b2ddb 100644
--- a/Specials/SpecialFancyUnicorn.php
+++ b/Specials/SpecialFancyUnicorn.php
@@ -5,7 +5,7 @@
* @ingroup Extensions
* @author Lucie-Aimée Kaffee
* @license GNU General Public Licence 2.0 or later
- *
+ *
*/
namespace ArticlePlaceholder\Specials;
@@ -15,147 +15,248 @@
use Wikibase\DataModel\Entity\EntityIdParser;
use Wikibase\DataModel\Entity\EntityIdParsingException;
use Wikibase\DataModel\Entity\ItemId;
+use Wikibase\Lib\Store\EntityLookup;
+use Wikibase\Lib\SnakFormatter;
+use Wikibase\Lib\Store\TermLookup;
+use Wikibase\DataModel\Entity\EntityDocument;
+use Wikibase\DataModel\Entity\Item;
+use Wikibase\DataModel\Entity\Entity;
+use Wikibase\DataModel\Term\Fingerprint;
use SpecialPage;
class SpecialFancyUnicorn extends SpecialPage {
- public static function newFromGlobalState() {
- $wikibaseClient = WikibaseClient::getDefaultInstance();
+ public static function newFromGlobalState() {
+ $wikibaseClient = WikibaseClient::getDefaultInstance();
- return new self(
- $wikibaseClient->getEntityIdParser()
- );
- }
+ return new self(
+ $wikibaseClient->getEntityIdParser(),
$wikibaseClient->getStore()->getEntityLookup()
+ );
+ }
- /**
- * @var EntityIdParser
- */
- private $idParser;
+ /**
+ * @return TermLookup
+ */
+ #private function getTermLookup() {
+ # return new EntityRetrievingTermLookup( $this->getEntityLookup() );
+ #}
- /**
- * Initialize the special page.
- */
- public function __construct( EntityIdParser $idParser ) {
- $this->idParser = $idParser;
- parent::__construct( 'FancyUnicorn' );
- }
+ /**
+ * @var EntityIdParser
+ */
+ private $idParser;
- /**
- * @param string $sub
- *
- */
- public function execute( $sub ) {
- $out = $this->getOutput();
+ /**
+ * @var EntityLookup
+ */
+ private $entityLookup;
- $out->setPageTitle( $this->msg(
'articleplaceholder-fancyunicorn' ) );
- if ( $this->getItemIdParam( 'entityid', $sub ) != null ) {
- $out->addWikiText( 'Testing test test' );
- } else {
- //create the html elements
- $this->createForm();
- }
- }
+ /**
+ * @var SnakFormatter
+ */
+ private $snakFormatter;
- /**
- *
- * @todo add wikibase group?
- */
- protected function getGroupName() {
- return 'other';
- }
+ /**
+ *
+ * @var TermLookup
+ */
+ #private $termLookup;
- /**
- * first function to add a form to enter an entity id and a submit
button
- */
- protected function createForm() {
- //add css style thing (in Wikibase OutputPage#addModuleStyles
+ /**
+ * Initialize the special page.
+ */
+ public function __construct( EntityIdParser $idParser, EntityLookup
$entityLookup ) {
+ $this->idParser = $idParser;
+ $this->entityLookup = $entityLookup;
+ #$this->termLookup = $termLookup;
+ parent::__construct( 'FancyUnicorn' );
+ }
- // Form header
- $this->getOutput()->addHTML(
- Html::openElement(
- 'form', array(
- 'method' => 'get',
- 'action' =>
$this->getPageTitle()->getFullUrl(),
- 'name' => 'ap-fancyunicorn',
- 'id' => 'ap-fancyunicorn-form1',
- 'class' => 'ap-form'
- )
- )
- );
+ /**
+ * @param string $sub
+ *
+ */
+ public function execute( $sub ) {
+ $out = $this->getOutput();
- // Form elements
- $this->getOutput()->addHTML( $this->getFormElements() );
+ $out->setPageTitle( $this->msg( 'articleplaceholder-fancyunicorn' ) );
+ if ( $this->getItemIdParam( 'entityid', $sub ) != null ) {
+ $entityId = $this->getItemIdParam( 'entityid', $sub );
+ $this->showPlaceholder( $entityId );
+ } else {
+ //create the html elements
+ $this->createForm();
+ }
+ }
- // Form body
- $this->getOutput()->addHTML(
- Html::input(
- 'submit', $this->msg(
'articleplaceholder-fancyunicorn-submit' )->text(), 'submit', array(
- 'id' => 'submit'
- )
- )
- . Html::closeElement( 'fieldset' )
- . Html::closeElement( 'form' )
- );
- }
+ /**
+ *
+ * @todo add wikibase group?
+ */
+ protected function getGroupName() {
+ return 'other';
+ }
- /**
- * Returns the form elements.
- *
- * @return string
- * @todo exchange all those . Html::element( 'br' ) with something
pretty
- */
- protected function getFormElements() {
- return Html::rawElement(
- 'p', array(),
- $this->msg(
'articleplaceholder-fancyunicorn-intro' )->parse()
- )
- . Html::element( 'br' )
- . Html::element(
- 'label', array(
- 'for' => 'ap-fancyunicorn-entityid',
- 'class' => 'ap-label'
- ), $this->msg(
'articleplaceholder-fancyunicorn-entityid' )->text()
- )
- . Html::element( 'br' )
- . Html::input(
- 'entityid',
$this->getRequest()->getVal( 'entityid' ), 'text', array(
- 'class' => 'ap-input',
- 'id' => 'ap-fancyunicorn-entityid'
- )
- )
- . Html::element( 'br' );
- }
+ /**
+ * first function to add a form to enter an entity id and a submit button
+ */
+ protected function createForm() {
+ //add css style thing (in Wikibase OutputPage#addModuleStyles
- private function getTextParam( $name, $fallback ) {
- $value = $this->getRequest()->getText( $name, $fallback );
- return trim( $value );
- }
+ // Form header
+ $this->getOutput()->addHTML(
+ Html::openElement(
+ 'form', array(
+ 'method' => 'get',
+ 'action' => $this->getPageTitle()->getFullUrl(),
+ 'name' => 'ap-fancyunicorn',
+ 'id' => 'ap-fancyunicorn-form1',
+ 'class' => 'ap-form'
+ )
+ )
+ );
- /**
- * @param string $name
- * @param string $fallback
- *
- * @return ItemId|null
- * @throws @todo UserInputException
- */
- private function getItemIdParam( $name, $fallback ) {
- $rawId = $this->getTextParam( $name, $fallback );
+ // Form elements
+ $this->getOutput()->addHTML( $this->getFormElements() );
- if ( $rawId === '' ) {
- return null;
- }
+ // Form body
+ $this->getOutput()->addHTML(
+ Html::input(
+ 'submit', $this->msg(
'articleplaceholder-fancyunicorn-submit' )->text(), 'submit', array(
+ 'id' => 'submit'
+ )
+ )
+ . Html::closeElement( 'fieldset' )
+ . Html::closeElement( 'form' )
+ );
+ }
- try {
- $id = $this->idParser->parse( $rawId );
- if ( !( $id instanceof ItemId ) ) {
- throw new Exception();
- }
+ /**
+ * Returns the form elements.
+ *
+ * @return string
+ * @todo exchange all those . Html::element( 'br' ) with something pretty
+ */
+ protected function getFormElements() {
+ return Html::rawElement(
+ 'p', array(),
+ $this->msg( 'articleplaceholder-fancyunicorn-intro'
)->parse()
+ )
+ . Html::element( 'br' )
+ . Html::element(
+ 'label', array(
+ 'for' => 'ap-fancyunicorn-entityid',
+ 'class' => 'ap-label'
+ ), $this->msg( 'articleplaceholder-fancyunicorn-entityid'
)->text()
+ )
+ . Html::element( 'br' )
+ . Html::input(
+ 'entityid', $this->getRequest()->getVal( 'entityid' ),
'text', array(
+ 'class' => 'ap-input',
+ 'id' => 'ap-fancyunicorn-entityid'
+ )
+ )
+ . Html::element( 'br' );
+ }
- return $id;
- } catch ( Exception $ex ) {
- // @todo proper Exception Handling
- $this->getOutput()->addWikiText( $ex->getMessage() );
- }
- }
+ private function getTextParam( $name, $fallback ) {
+ $value = $this->getRequest()->getText( $name, $fallback );
+ return trim( $value );
+ }
-}
+ /**
+ * @param string $name
+ * @param string $fallback
+ *
+ * @return ItemId|null
+ * @throws @todo UserInputException
+ */
+ private function getItemIdParam( $name, $fallback ) {
+ $rawId = $this->getTextParam( $name, $fallback );
+
+ if ( $rawId === '' ) {
+ return null;
+ }
+
+ try {
+ $id = $this->idParser->parse( $rawId );
+ if ( !( $id instanceof ItemId ) ) {
+ throw new Exception();
+ }
+
+ return $id;
+ } catch ( Exception $ex ) {
+ // @todo proper Exception Handling
+ $this->getOutput()->addWikiText( $ex->getMessage() );
+ }
+ }
+
+ private function showPlaceholder( ItemId $entityId ) {
+ if ( !$this->entityLookup->hasEntity( $entityId ) ) {
+ return;
+ }
+ $entity = $this->entityLookup->getEntity( $entityId );
+ if ( !$entity instanceof Item ) {
+ return;
+ }
+ $fingerprint = $entity->getFingerprint();
+ $this->showTitle( $fingerprint, $entityId );
+ $this->showAliases( $fingerprint, $entityId );
+ $this->showDescription( $fingerprint, $entityId );
+ #$this->showStatements( $entity );
+ }
+
+ /**
+ *
+ * @param EntityDocument $entity
+ * @todo LanguageFallbacks
+ */
+ private function showTitle( Fingerprint $fingerprint, ItemId $entityId ) {
+ $label = "";
+ if ( $fingerprint->hasLabel( $this->getLanguage()->getCode() ) ) {
+ $label = $fingerprint->getLabel( $this->getLanguage()->getCode()
)->getText();
+ } else {
+ // @todo Language Fallback
+ $label = $entityId->getSerialization();
+ }
+ $this->getOutput()->setPageTitle( $label );
+ }
+
+ private function showAliases( Fingerprint $fingerprint, ItemId $entityId )
{
+ $aliases = "";
+ if ( $fingerprint->hasAliasGroup( $this->getLanguage()->getCode() ) ) {
+ $aliasesArray = $fingerprint->getAliasGroup(
$this->getLanguage()->getCode() )->getAliases();
+ $aliases = implode( ' | ', $aliasesArray );
+ } // @todo else Language Fallback
+ $this->getOutput()->addWikiText( $aliases );
+ }
+
+ private function showDescription( Fingerprint $fingerprint, ItemId
$entityId ) {
+ $description = "";
+ if ( $fingerprint->hasDescription( $this->getLanguage()->getCode() ) )
{
+ $description = $fingerprint->getLabel(
$this->getLanguage()->getCode() )->getText();
+ } else {
+ // @todo Language Fallback
+ $description = "";
+ }
+ $this->getOutput()->addWikiText( $description );
+ }
+
+ private function showStatements( Item $entity ) {
+ $statementStr = "";
+ if ( $entity->getStatements()->isEmpty() ) {
+ return;
+ }
+ $statementsList =
$entity->getStatements()->getBestStatementPerProperty()->toArray();
+ foreach ( $statementsList as $statement ) {
+ $this->getPropertyLabel( $statement );
+ $statementStr .= $this->getPropertyLabel( $statement );
+ }
+ $this->getOutput()->addWikiText( $statementStr );
+ }
+
+ private function getPropertyLabel( $statement ) {
+ $statement->getPropertyId()->getSerialization();
+ }
+
+}
\ No newline at end of file
--
To view, visit https://gerrit.wikimedia.org/r/232266
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: I8f42acb1ac915dd50f8be2cdc9d42e0a0879bcc7
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/ArticlePlaceholder
Gerrit-Branch: master
Gerrit-Owner: Lucie Kaffee <[email protected]>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits