Daniel Werner has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/82874


Change subject: (bug 52387) Special page for querying by property value
......................................................................

(bug 52387) Special page for querying by property value

Special:SimpleQuery allows to query for a value used in a property-value Snak.

TODO: This will fail because of the current unstable state of the extension.
 Tests have been added but were not able to execute because of this.
 After these issues get fixed, queries will still not work via the special page 
since the backend
 is not yet fit to handle the queries.

Currently, the value field requires input of the data value as JSON, e.g.:
  "type":"time"
  {
    "value":{
      "time":"+00000000002-01-21T00:00:00Z",
      "timezone":0,
      "before":0,
      "after":0,
      "precision":11,
      "calendarmodel":"http://www.wikidata.org/entity/Q1985786";
    },
}
This is currently necessary because there is no parser for Time values in the 
backend.
In the future there could also be a field for the "type" and one for the 
"value", for now
allowing to copy/paste into one single field might be more convenient.

In a follow up, the JavaScript UI should be used to allow entering values in a 
more convenient
way.

Change-Id: If089492726d2745158c646e3ca568d14754fb692
---
A Tests/Specials/SimpleQueryTest.php
A WikibaseQuery.i18n.aliases.php
M WikibaseQuery.i18n.php
M WikibaseQuery.php
M src/Wikibase/Query/ByPropertyValueEntityFinder.php
A src/Wikibase/Query/Specials/SimpleQuery.php
6 files changed, 242 insertions(+), 2 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/WikibaseQuery 
refs/changes/74/82874/1

diff --git a/Tests/Specials/SimpleQueryTest.php 
b/Tests/Specials/SimpleQueryTest.php
new file mode 100644
index 0000000..22759ba
--- /dev/null
+++ b/Tests/Specials/SimpleQueryTest.php
@@ -0,0 +1,52 @@
+<?php
+
+namespace Tests\Phpunit\Wikibase\Query\Special;
+use \Wikibase\Test\SpecialPageTestBase;
+use \Wikibase\Query\Specials\SimpleQuery;
+
+/**
+ * @group Wikibase
+ * @group SpecialPage
+ * @group WikibaseSpecialPage
+ *
+ * @since 0.1
+ * @licence GNU GPL v2+
+ * @author Daniel Werner < [email protected] >
+ */
+class SimpleQueryTest extends SpecialPageTestBase {
+
+       protected function newSpecialPage() {
+               return new SimpleQuery();
+       }
+
+       public function testExecute() {
+               $matchers['property'] = array(
+                       'tag' => 'input',
+                       'attributes' => array(
+                               'id' => 'wb-specialsimplequery-property',
+                               'name' => 'property',
+                       )
+               );
+               $matchers['valuejson'] = array(
+                       'tag' => 'input',
+                       'attributes' => array(
+                               'id' => 'wb-specialsimplequery-valuejson',
+                               'name' => 'valuejson',
+                       )
+               );
+               $matchers['submit'] = array(
+                       'tag' => 'input',
+                       'attributes' => array(
+                               'id' => 'wikibase-entitieswithoutlabel-submit',
+                               'class' => 'wb-input-button',
+                               'type' => 'submit',
+                               'name' => 'submit',
+                       ) );
+
+               list( $output, ) = $this->executeSpecialPage( '' );
+               foreach( $matchers as $key => $matcher ){
+                       $this->assertTag( $matcher, $output, "Failed to match 
html output '$key'" );
+               }
+       }
+
+}
\ No newline at end of file
diff --git a/WikibaseQuery.i18n.aliases.php b/WikibaseQuery.i18n.aliases.php
new file mode 100644
index 0000000..76c7c3e
--- /dev/null
+++ b/WikibaseQuery.i18n.aliases.php
@@ -0,0 +1,20 @@
+<?php
+
+/**
+ * Aliases for the special pages of the WikibaseQuery extension.
+ *
+ * @since 0.1
+ *
+ * @file
+ * @ingroup WikibaseQuery
+ *
+ * @licence GNU GPL v2+
+ * @author Daniel Werner < [email protected] >
+ */
+
+$specialPageAliases = array();
+
+/** English (English) */
+$specialPageAliases['en'] = array(
+       'SimpleQuery' => array( 'SimpleQuery' ),
+);
diff --git a/WikibaseQuery.i18n.php b/WikibaseQuery.i18n.php
index 9e2cd6e..1c9a059 100644
--- a/WikibaseQuery.i18n.php
+++ b/WikibaseQuery.i18n.php
@@ -16,16 +16,25 @@
 
 /** English
  * @author Jeroen De Dauw
+ * @author Daniel Werner
  */
 $messages['en'] = array(
        'wikibasequery-desc' => 'Adds query capabilities to Wikibase',
+       'special-simplequery' => 'Simple Query',
+       'wikibase-specialsimplequery-legend' => 'Find entities with 
property-value pair',
+       'wikibase-specialsimplequery-label-property' => 'Property:',
+       'wikibase-specialsimplequery-label-valuejson' => 'Value (as JSON):',
 );
 
 /** Message documentation (Message documentation)
  * @author Shirayuki
+ * @author Daniel Werner
  */
 $messages['qqq'] = array(
        'wikibasequery-desc' => '{{desc|name=Wikibase 
Query|url=http://www.mediawiki.org/wiki/Extension:Wikibase}}',
+       'wikibase-specialsimplequery-legend' => 'Describes what the input form 
is good for.',
+       'wikibase-specialsimplequery-label-property' => 'Label for the property 
input field.',
+       'wikibase-specialsimplequery-label-valuejson' => 'Label for the value 
JSON input field. The field allows the user to specify a property value to 
search for. The value has to be given in its serialized form.',
 );
 
 /** Afrikaans (Afrikaans)
diff --git a/WikibaseQuery.php b/WikibaseQuery.php
index 9197f1e..b7fd3b1 100644
--- a/WikibaseQuery.php
+++ b/WikibaseQuery.php
@@ -73,7 +73,7 @@
 
 call_user_func( function() {
        global $wgExtensionCredits, $wgExtensionMessagesFiles, $wgHooks;
-       global $wgAPIModules;
+       global $wgAPIModules, $wgSpecialPages, $wgSpecialPageGroups, 
$wgResourceModules;
 
        $wgExtensionCredits['wikibase'][] = array(
                'path' => __DIR__,
@@ -86,10 +86,18 @@
                'descriptionmsg' => 'wikibasequery-desc'
        );
 
-       $wgExtensionMessagesFiles['WikibaseQuery'] = __DIR__ . 
'/WikibaseQuery.i18n.php';
+       $wgExtensionMessagesFiles['WikibaseQuery']        = __DIR__ . 
'/WikibaseQuery.i18n.php';
+       $wgExtensionMessagesFiles['WikibaseQueryAliases'] = __DIR__ . 
'/WikibaseQuery.i18n.aliases.php';
 
+       // API modules registration
        $wgAPIModules['entitiesByPropertyValue'] = 
'Wikibase\Query\Api\EntitiesByPropertyValue';
 
+       // Special page registration
+       $wgSpecialPages['SimpleQuery'] = 'Wikibase\Query\Specials\SimpleQuery';
+
+       // Special page groups
+       $wgSpecialPageGroups['SimpleQuery'] = 'wikibaserepo';
+
        /**
         * Hook to add PHPUnit test cases.
         * @see https://www.mediawiki.org/wiki/Manual:Hooks/UnitTestsList
diff --git a/src/Wikibase/Query/ByPropertyValueEntityFinder.php 
b/src/Wikibase/Query/ByPropertyValueEntityFinder.php
index beb33bb..614c2d2 100644
--- a/src/Wikibase/Query/ByPropertyValueEntityFinder.php
+++ b/src/Wikibase/Query/ByPropertyValueEntityFinder.php
@@ -64,6 +64,8 @@
         *
         * @return EntityId[]
         * @throws InvalidArgumentException
+        *
+        * TODO: Throw more specific exceptions.
         */
        protected function findEntitiesGivenRawArguments( $propertyIdString, 
$valueString, $limit, $offset ) {
                if ( !is_string( $limit ) || !ctype_digit( $limit ) || 
(int)$limit < 1 ) {
diff --git a/src/Wikibase/Query/Specials/SimpleQuery.php 
b/src/Wikibase/Query/Specials/SimpleQuery.php
new file mode 100644
index 0000000..9f47bac
--- /dev/null
+++ b/src/Wikibase/Query/Specials/SimpleQuery.php
@@ -0,0 +1,149 @@
+<?php
+
+namespace Wikibase\Query\Specials;
+use Wikibase\Query\DIC\ExtensionAccess;
+use \SpecialWikibaseQueryPage;
+use \Html;
+
+/**
+ * Special page that allows for querying for all entities with at least one 
PropertySnak using a
+ * certain property and a certain value.
+ *
+ * @since 0.1
+ * @licence GNU GPL v2+
+ * @author Daniel Werner < [email protected] >
+ */
+class SimpleQuery extends SpecialWikibaseQueryPage {
+
+       protected $propertyId;
+       protected $valueJson;
+
+       public function __construct() {
+               parent::__construct( 'SimpleQuery' );
+       }
+
+       /**
+        * @see SpecialWikibasePage::execute
+        *
+        * @since 0.1
+        */
+       public function execute( $subPage ) {
+               if( !parent::execute( $subPage ) ) {
+                       return false;
+               }
+
+               $output = $this->getOutput();
+               $request = $this->getRequest();
+
+               $this->propertyId = $request->getText( 'property' );
+               $this->valueJson = $request->getText( 'valuejson' );
+
+               $output->addHTML(
+                       $this->buildSearchForm( array(
+                               'property' => $this->propertyId,
+                               'valuejson' => $this->valueJson
+                       ) )
+               );
+
+               $this->showQuery();
+
+               // 10 seconds server-side caching max
+               $output->setSquidMaxage( 10 );
+       }
+
+       /**
+        * @see SpecialWikibaseQueryPage::getResult
+        *
+        * @since 0.1
+        */
+       protected function getResult( $offset = 0, $limit = 0 ) {
+               try {
+                       $entityFinder = 
ExtensionAccess::getWikibaseQuery()->getByPropertyValueEntityFinder();
+                       $entityIds = $entityFinder->findEntities( array(
+                               'property' => $this->propertyId,
+                               'value' => $this->valueJson,
+                               'limit' => (string)$this->limit,
+                               'offset' => (string)$this->offset
+                       ) );
+               } catch( \InvalidArgumentException $e ) {
+                       // TODO: Display some useful error to the user. For 
this to be implemented, the base
+                       //  class had to be refactored and more specific 
exception implementations would be
+                       //  useful as well.
+                       return array();
+               }
+
+               return $entityIds;
+       }
+
+       /**
+        * Creates HTML for a search form suitable for the special page's 
purpose.
+        *
+        * @since 0.1
+        *
+        * @param array $formFieldValues
+        * @return string
+        */
+       protected function buildSearchForm( array $formFieldValues ) {
+               return
+                       Html::openElement(
+                               'form',
+                               array(
+                                       'action' => 
$this->getTitle()->getLocalURL(),
+                                       'name' => 'simplequery',
+                                       'id' => 'wb-SimpleQuery-form'
+                               )
+                       ) .
+                       Html::openElement( 'fieldset' ) .
+                       Html::element(
+                               'legend',
+                               array(),
+                               $this->msg( 
'wikibase-specialsimplequery-legend' )->text()
+                       ) .
+
+                       $this->buildSearchFormInput( 'property', 
$formFieldValues['property'] ) .
+                       $this->buildSearchFormInput( 'valuejson', 
$formFieldValues['valuejson'] ) .
+
+                       Html::input(
+                               null,
+                               $this->msg( 
'wikibase-entitieswithoutlabel-submit' )->text(),
+                               'submit',
+                               array(
+                                       'id' => 
'wikibase-entitieswithoutlabel-submit',
+                                       'class' => 'wb-input-button'
+                               )
+                       ) .
+
+                       Html::closeElement( 'fieldset' ) .
+                       Html::closeElement( 'form' );
+       }
+
+       /**
+        * Creates HTML for an input field.
+        *
+        * @since 0.1
+        *
+        * @param string $purpose
+        * @param string $value
+        * @return string
+        */
+       protected function buildSearchFormInput( $purpose, $value ) {
+               return
+                       Html::openElement( 'p' ) .
+                       Html::element(
+                               'label',
+                               array(
+                                       'for' => 
"wb-specialsimplequery-$purpose"
+                               ),
+                               $this->msg( 
"wikibase-specialsimplequery-label-$purpose" )->text()
+                       ) .
+                       Html::input(
+                               $purpose,
+                               $value,
+                               'text',
+                               array(
+                                       'id' => "wb-specialsimplequery-$purpose"
+                               )
+                       ) .
+                       Html::closeElement( 'p' );
+       }
+}

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: If089492726d2745158c646e3ca568d14754fb692
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/WikibaseQuery
Gerrit-Branch: master
Gerrit-Owner: Daniel Werner <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to