Ori.livneh has uploaded a new change for review.

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


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. The module returns a JSON object with a "jsonschema" key that maps
to the JSON schema itself.

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.php
A includes/ApiJsonSchema.php
M includes/JsonSchemaHooks.php
3 files changed, 86 insertions(+), 1 deletion(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/EventLogging 
refs/changes/17/68617/1

diff --git a/EventLogging.php b/EventLogging.php
index 536416e..45343e2 100644
--- a/EventLogging.php
+++ b/EventLogging.php
@@ -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..6a4ad18
--- /dev/null
+++ b/includes/ApiJsonSchema.php
@@ -0,0 +1,79 @@
+<?php
+/**
+ * API module for retrieving JSON Schema.
+ *
+ * @file
+ * @ingroup API
+ * @ingroup EventLogging
+ * @ingroup Extensions
+ *
+ * @author Ori Livneh <[email protected]>
+ */
+
+class ApiJsonSchema extends ApiBase {
+
+       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 markCacheable() {
+               $main = $this->getMain();
+               $main->setCacheMode( 'public' );
+               $main->setCacheMaxAge( 300 );
+       }
+
+       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 = FormatJson::decode( $content->getNativeData() );
+               $schema->title = $title->getText();
+               $this->getResult()->addValue( null, $this->getModuleName(), 
$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;
                }
 

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I4f17633726d0ac393bfe3dc2937738f50be38291
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/EventLogging
Gerrit-Branch: master
Gerrit-Owner: Ori.livneh <[email protected]>
Gerrit-Reviewer: jenkins-bot

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

Reply via email to