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

Change subject: Add API module for retrieving JSON Schema
......................................................................


Add API module for retrieving JSON Schema

This change adds a 'jsonschema' API module on wikis which enable the Schema
namespace. Like index.php?action=raw, the module returns the raw JSON content
of the schema. The rationale for having an API module is twofold:

 * It provides a way for looking up a schema by name *and* revision. action=raw
   ignores the title parameter when oldid is set, leading to bug 46174.

 * It is a step forward toward implementing JSON ref. The API module can be
   extended to accept an optional 'resolve' parameter which will return a fully
   dereferenced schema object. See bug 45886.

Bug: 46174
Bug: 45886
Change-Id: I4f17633726d0ac393bfe3dc2937738f50be38291
---
M EventLogging.hooks.php
M EventLogging.php
A includes/ApiJsonSchema.php
M includes/JsonSchemaContent.php
M includes/JsonSchemaHooks.php
M includes/RemoteSchema.php
M server/bin/eventlogging-devserver
7 files changed, 147 insertions(+), 15 deletions(-)

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



diff --git a/EventLogging.hooks.php b/EventLogging.hooks.php
index 0ea5090..da5bcb6 100644
--- a/EventLogging.hooks.php
+++ b/EventLogging.hooks.php
@@ -27,7 +27,7 @@
                        'wgEventLoggingBaseUri',
                        'wgEventLoggingDBname',
                        'wgEventLoggingFile',
-                       'wgEventLoggingSchemaIndexUri'
+                       'wgEventLoggingSchemaApiUri'
                ) as $configVar ) {
                        if ( $GLOBALS[ $configVar ] === false ) {
                                wfDebugLog( 'EventLogging', "$configVar has not 
been configured." );
diff --git a/EventLogging.php b/EventLogging.php
index 536416e..3400422 100644
--- a/EventLogging.php
+++ b/EventLogging.php
@@ -66,11 +66,11 @@
 
 /**
  * @var bool|string: URI or false if not set.
- * URI of index.php on schema wiki.
+ * URI of api.php on schema wiki.
  *
- * @example string: 'http://localhost/wiki/index.php'
+ * @example string: 'http://meta.wikimedia.org/w/api.php'
  */
-$wgEventLoggingSchemaIndexUri = 'http://meta.wikimedia.org/w/index.php';
+$wgEventLoggingSchemaApiUri = 'http://meta.wikimedia.org/w/api.php';
 
 /**
  * @var bool|string: Value of $wgDBname for the MediaWiki instance
@@ -202,6 +202,9 @@
        'TreeRef'             => __DIR__ . '/includes/JsonSchema.php',
        'JsonTreeRef'         => __DIR__ . '/includes/JsonSchema.php',
        'JsonSchemaIndex'     => __DIR__ . '/includes/JsonSchema.php',
+
+       // API
+       'ApiJsonSchema' => __DIR__ . '/includes/ApiJsonSchema.php',
 );
 
 
diff --git a/includes/ApiJsonSchema.php b/includes/ApiJsonSchema.php
new file mode 100644
index 0000000..7262aee
--- /dev/null
+++ b/includes/ApiJsonSchema.php
@@ -0,0 +1,110 @@
+<?php
+/**
+ * API module for retrieving JSON Schema.
+ *
+ * @file
+ * @ingroup EventLogging
+ * @ingroup Extensions
+ *
+ * @author Ori Livneh <[email protected]>
+ */
+
+/**
+ * API module for retrieving JSON Schema.
+ * This avoids API result paths and returns HTTP error codes in order to
+ * act like a request for the raw page content.
+ * @ingroup API
+ */
+class ApiJsonSchema extends ApiBase {
+
+       /**
+        * Restrict the set of valid formatters to just 'json'.
+        * @return ApiFormatJson
+        */
+       public function getCustomPrinter() {
+               return $this->getMain()->createPrinterByName( 'json' );
+       }
+
+       public function getAllowedParams() {
+               return array(
+                       'revid' => array(
+                               ApiBase::PARAM_TYPE => 'integer',
+                               ApiBase::PARAM_REQUIRED => true,
+                       ),
+                       'title' => array(
+                               ApiBase::PARAM_TYPE => 'string',
+                       ),
+               );
+       }
+
+       public function getParamDescription() {
+               return array(
+                       'revid' => 'Schema revision ID',
+                       'title' => 'Schema name',
+               );
+       }
+
+       public function getDescription() {
+               return 'Retrieve a JSON Schema page';
+       }
+
+       public function getExamples() {
+               return array( 'api.php?action=jsonschema&revid=1234'  => 
'Retrieve schema for revision 1234' );
+       }
+
+       /**
+        * Set future expires and public cache-control headers on the
+        * pending HTTP response.
+        */
+       public function markCacheable() {
+               $main = $this->getMain();
+               $main->setCacheMode( 'public' );
+               $main->setCacheMaxAge( 300 );
+       }
+
+       /**
+        * Emit an error response. Like ApiBase::dieUsageMsg, but sets
+        * HTTP 400 ('Bad Request') status code.
+        * @param array|string: user error array
+        */
+       public function dieUsageMsg(  $error ) {
+               $parsed = $this->parseMsg( (array)$error );
+               $this->dieUsage( $parsed['info'], $parsed['code'], 400 );
+       }
+
+       public function execute() {
+               $params = $this->extractRequestParams();
+               $rev = Revision::newFromID( $params['revid'] );
+
+               if ( !$rev ) {
+                       $this->dieUsageMsg( array( 'nosuchrevid', 
$params['revid'] ) );
+               }
+
+               $title = $rev->getTitle();
+               if ( !$title || !$title->inNamespace( NS_SCHEMA ) ) {
+                       $this->dieUsageMsg( array( 'invalidtitle', $title ) );
+               }
+
+               $content = $rev->getContent();
+               if ( !$content ) {
+                       $this->dieUsageMsg( array( 'nosuchrevid', 
$params['revid'] ) );
+               }
+
+               // We use the revision ID for lookup; the 'title' parameter is
+               // optional. If present, it is used to assert that the specified
+               // revision ID is indeed a revision of a page with the specified
+               // title. (Bug 46174)
+               if ( $params['title']  && !$title->equals( Title::newFromText( 
$params['title'], NS_SCHEMA ) ) ) {
+                       $this->dieUsageMsg( array( 'revwrongpage', 
$params['revid'], $params['title'] ) );
+               }
+
+               $this->markCacheable();
+               $schema = $content->getJsonData( true );
+
+               $result = $this->getResult();
+               $result->addValue( null, 'title', $title->getText() );
+               foreach( $schema as $k => &$v ) {
+                       $result->addValue( null, $k, $v );
+               }
+       }
+}
diff --git a/includes/JsonSchemaContent.php b/includes/JsonSchemaContent.php
index 053d131..1ba47d5 100644
--- a/includes/JsonSchemaContent.php
+++ b/includes/JsonSchemaContent.php
@@ -20,11 +20,19 @@
        }
 
        /**
+        * Decodes the JSON schema into a PHP associative array.
+        * @return array: Schema array.
+        */
+       function getJsonData() {
+               return FormatJson::decode( $this->getNativeData(), true );
+       }
+
+       /**
         * @throws JsonSchemaException: If invalid.
         * @return bool: True if valid.
         */
        function validate() {
-               $schema = FormatJson::decode( $this->getNativeData(), true );
+               $schema = $this->getJsonData();
                if ( !is_array( $schema ) ) {
                        throw new JsonSchemaException( wfMessage( 
'eventlogging-invalid-json' )->parse() );
                }
@@ -170,7 +178,7 @@
         * @return string: HTML representation.
         */
        function getHighlightHtml() {
-               $schema = FormatJson::decode( $this->getNativeData(), true );
+               $schema = $this->getJsonData();
                return is_array( $schema ) ? self::objectTable( $schema ) : '';
        }
 }
diff --git a/includes/JsonSchemaHooks.php b/includes/JsonSchemaHooks.php
index 7d64e1b..89ce301 100644
--- a/includes/JsonSchemaHooks.php
+++ b/includes/JsonSchemaHooks.php
@@ -17,7 +17,8 @@
         * @return bool: Whether hooks and handler were registered.
         */
        static function registerHandlers() {
-               global $wgHooks, $wgContentHandlers, $wgEventLoggingDBname, 
$wgDBname;
+               global $wgAPIModules, $wgHooks, $wgContentHandlers,
+                       $wgEventLoggingDBname, $wgDBname;
 
                if ( $wgEventLoggingDBname === $wgDBname ) {
                        $wgContentHandlers[ 'JsonSchema' ] = 
'JsonSchemaContentHandler';
@@ -27,6 +28,8 @@
                        $wgHooks[ 'EditFilterMerged' ][] = 
'JsonSchemaHooks::onEditFilterMerged';
                        $wgHooks[ 'CodeEditorGetPageLanguage' ][] = 
'JsonSchemaHooks::onCodeEditorGetPageLanguage';
 
+                       $wgAPIModules[ 'jsonschema' ] = 'ApiJsonSchema';
+
                        return true;
                }
 
diff --git a/includes/RemoteSchema.php b/includes/RemoteSchema.php
index 9bb029a..45fea20 100644
--- a/includes/RemoteSchema.php
+++ b/includes/RemoteSchema.php
@@ -88,15 +88,23 @@
         * @return string: URI.
         */
        function getUri() {
-               global $wgEventLoggingSchemaIndexUri;
+               global $wgEventLoggingSchemaApiUri;
 
-               $q = array(
-                       'title'  =>  'Schema:' . $this->title,
-                       'action' =>  'raw',
-                       'oldid'  =>  $this->revision
-               );
+               if ( substr( $wgEventLoggingSchemaApiUri, -10 ) === 
'/index.php' ) {
+                       // Old-style request (index.php)
+                       $q = array(
+                               'action' =>  'raw',
+                               'oldid'  =>  $this->revision,
+                       );
+               } else {
+                       // New-style request (api.php)
+                       $q = array(
+                               'action' =>  'jsonschema',
+                               'revid'  =>  $this->revision
+                       );
+               }
 
-               return wfAppendQuery( $wgEventLoggingSchemaIndexUri, $q );
+               return wfAppendQuery( $wgEventLoggingSchemaApiUri, $q );
        }
 
 
diff --git a/server/bin/eventlogging-devserver 
b/server/bin/eventlogging-devserver
index 9f022dd..d2dfe16 100755
--- a/server/bin/eventlogging-devserver
+++ b/server/bin/eventlogging-devserver
@@ -174,7 +174,7 @@
 # Ensure the following values are set in LocalSettings.php:
 require_once( "$IP/extensions/EventLogging/EventLogging.php" );
 $wgEventLoggingBaseUri = 'http://%s:%s/event.gif';
-$wgEventLoggingSchemaIndexUri = 'http://meta.wikimedia.org/w/index.php';
+$wgEventLoggingSchemaApiUri = 'http://meta.wikimedia.org/w/api.php';
 
 # Listening to events.''' % (args.host, args.port), php_lexer, formatter))
 

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

Gerrit-MessageType: merged
Gerrit-Change-Id: I4f17633726d0ac393bfe3dc2937738f50be38291
Gerrit-PatchSet: 7
Gerrit-Project: mediawiki/extensions/EventLogging
Gerrit-Branch: master
Gerrit-Owner: Ori.livneh <[email protected]>
Gerrit-Reviewer: DarTar <[email protected]>
Gerrit-Reviewer: Mattflaschen <[email protected]>
Gerrit-Reviewer: Ori.livneh <[email protected]>
Gerrit-Reviewer: Spage <[email protected]>
Gerrit-Reviewer: Swalling <[email protected]>
Gerrit-Reviewer: jenkins-bot

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

Reply via email to