jenkins-bot has submitted this change and it was merged. Change subject: Bug 50959: Pull the JSON schema from the Meta page. ......................................................................
Bug 50959: Pull the JSON schema from the Meta page. This allows the schema to be pulled from a remote wiki. It also allows the user to set three new configuration variables: the title of the schema page, the API URL for the wiki, and the DB name for the wiki. Bug: 50959 Change-Id: Ia9a9447ee61d3fe254128ea392eb1a7168c763d7 --- M BookManagerv2.php M JsonEditor.php M includes/RemoteSchema.php 3 files changed, 89 insertions(+), 18 deletions(-) Approvals: Mwalker: Looks good to me, approved jenkins-bot: Verified diff --git a/BookManagerv2.php b/BookManagerv2.php index 6f9acc7..9f72af2 100644 --- a/BookManagerv2.php +++ b/BookManagerv2.php @@ -218,3 +218,29 @@ * If set to false, this hides the previous/next links from the navigation bar. */ $wgBookManagerv2PrevNext = true; + +/** + * @var string + * Location of the local JSON schema, relative to the extension directory. + */ +$wgBookManagerv2LocalSchema = '/schemas/bookschema.json'; + +/** + * @var string|bool + * Title of the page containing the remote schema, or false if not set. + */ +$wgBookManagerv2SchemaTitle = false; + +/** + * @var string|bool + * URI of API.php on the schema wiki, or false if not set. + * @example string: 'http://meta.wikimedia.org/w/api.php' + */ +$wgBookManagerv2ApiUri = false; + +/** + * @var string|bool + * Value of $wgDBname for the MediaWiki instance housing schemas, or false if + * not set. + */ +$wgBookManagerv2SchemaDBname = false; diff --git a/JsonEditor.php b/JsonEditor.php index 193aefd..f8e1d20 100644 --- a/JsonEditor.php +++ b/JsonEditor.php @@ -33,8 +33,22 @@ * Pulls the schema from the local schema file and decodes it. */ static function getSchema() { + global $wgBookManagerv2SchemaTitle, $wgBookManagerv2SchemaDBname, + $wgBookManagerv2LocalSchema; + if ( $wgBookManagerv2SchemaTitle && $wgBookManagerv2SchemaDBname ) { + $remoteSchema = new RemoteSchema( $wgBookManagerv2SchemaTitle ); + if ( $remoteSchema !== false ) { + $schema = $remoteSchema->get(); + if ( $schema !== false ) { + return FormatJson::decode( + FormatJson::encode( $schema ) ); + } + } + } + // If we couldn't get the remote schema, fall back to the local + // schema. return FormatJson::decode( - file_get_contents( __DIR__ . '/schemas/bookschema.json' ) ); + file_get_contents( __DIR__ . $wgBookManagerv2LocalSchema ) ); } /** diff --git a/includes/RemoteSchema.php b/includes/RemoteSchema.php index 385afb8..5bf302c 100644 --- a/includes/RemoteSchema.php +++ b/includes/RemoteSchema.php @@ -38,22 +38,27 @@ var $title; var $content = false; + /** - * @param string title + * Constructor. + * @param string $title * @param integer $revision - * @param ObjectCache $cache (optional) cache client - * @param Http $http (optional) HTTP client + * @param ObjectCache $cache (optional) cache client. + * @param Http $http (optional) HTTP client. */ - function __construct( $title, $revision, $cache = NULL, $http = NULL ) { - $dbr = wfGetDB( DB_SLAVE ); - $BookManagerv2DBName = $dbr->getDBname(); + function __construct( $title, $revision = NULL, $cache = NULL, $http = NULL ) { + global $wgBookManagerv2SchemaDBname; $this->title = $title; - $this->revision = $revision; $this->cache = $cache ? : wfGetCache( CACHE_ANYTHING ); $this->http = $http ? : new Http(); - $this->key = "schema:{$BookManagerv2DBname}:{$title}:{$revision}"; + $this->revision = self::getLatestRevId(); + if ( $this->revision === false ) { + return false; + } + $this->key = "schema:{$wgBookManagerv2SchemaDBname}:{$title}:{$revision}"; } + /** * Retrieves schema content. @@ -77,13 +82,15 @@ return $this->content; } + /** * Retrieves content from memcached. - * @return array|bool: Schema or false if not in cache. + * @return array:bool: Schema or false if not in cache. */ function memcGet() { return $this->cache->get( $this->key ); } + /** * Store content in memcached. @@ -91,6 +98,7 @@ function memcSet() { return $this->cache->set( $this->key, $this->content ); } + /** * Acquire a mutex lock for HTTP retrieval. @@ -100,9 +108,31 @@ return $this->cache->add( $this->key . ':lock', 1, self::LOCK_TIMEOUT ); } + + /** + * Gets the latest revision ID of the schema on the remote wiki. + * @return int: Revision ID. + */ + function getLatestRevId() { + global $wgBookManagerv2SchemaApiUri, $wgBookManagerv2SchemaTitle; + + $q = array( + 'action' => 'query', + 'format' => 'json', + 'prop' => 'info', + 'titles' => $wgBookManagerv2SchemaTitle, + ); + + $queryString = wfAppendQuery( $wgBookManagerv2SchemaApiUri, $q ); + $result = FormatJson::decode( $this->http->get( + $queryString, self::LOCK_TIMEOUT * 0.8 ) ); + $pageId = key( $result->query->pages ); + return $result->query->pages->$pageId->lastrevid; + } + /** * Constructs URI for retrieving schema from remote wiki. - * @return string: URI + * @return string: URI. */ function getUri() { global $wgBookManagerv2SchemaApiUri; @@ -110,30 +140,32 @@ if ( substr( $wgBookManagerv2SchemaApiUri, -10 ) === '/index.php' ) { // Old-style request (index.php) $q = array( - 'action' => 'raw', - 'oldid' => $this->revision, + 'action' => 'raw', + 'oldid' => $this->revision, ); } else { // New-style request (api.php) $q = array( - 'action' => 'jsonschema', - 'revid' => $this->revision, + 'action' => 'jsonschema', + 'revid' => $this->revision ); } return wfAppendQuery( $wgBookManagerv2SchemaApiUri, $q ); } + /** * Returns an object containing serializable properties. - * @implements JsonSerializeable + * @implements JsonSerializable */ function jsonSerialize() { return array( - 'schema' => $this->get() ? : new StdClass(), + 'schema' => $this->get() ? : new StdClass(), 'revision' => $this->revision ); } + /** * Retrieves the schema using HTTP. @@ -148,4 +180,3 @@ return FormatJson::decode( $raw, true ) ? : false; } } - -- To view, visit https://gerrit.wikimedia.org/r/81560 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: merged Gerrit-Change-Id: Ia9a9447ee61d3fe254128ea392eb1a7168c763d7 Gerrit-PatchSet: 3 Gerrit-Project: mediawiki/extensions/BookManagerv2 Gerrit-Branch: master Gerrit-Owner: Mollywhite <[email protected]> Gerrit-Reviewer: Mollywhite <[email protected]> Gerrit-Reviewer: Mwalker <[email protected]> Gerrit-Reviewer: jenkins-bot _______________________________________________ MediaWiki-commits mailing list [email protected] https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits
