Mollywhite has uploaded a new change for review. https://gerrit.wikimedia.org/r/81560
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, 84 insertions(+), 43 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/BookManagerv2 refs/changes/60/81560/1 diff --git a/BookManagerv2.php b/BookManagerv2.php index 6f9acc7..778f435 100644 --- a/BookManagerv2.php +++ b/BookManagerv2.php @@ -218,3 +218,23 @@ * If set to false, this hides the previous/next links from the navigation bar. */ $wgBookManagerv2PrevNext = true; + +/** + * @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..c3b31f1 100644 --- a/JsonEditor.php +++ b/JsonEditor.php @@ -33,6 +33,19 @@ * Pulls the schema from the local schema file and decodes it. */ static function getSchema() { + global $wgBookManagerv2SchemaTitle, $wgBookManagerv2SchemaDBname; + 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' ) ); } diff --git a/includes/RemoteSchema.php b/includes/RemoteSchema.php index 385afb8..82916c9 100644 --- a/includes/RemoteSchema.php +++ b/includes/RemoteSchema.php @@ -2,31 +2,8 @@ /** * Represents a schema revision on a remote wiki. * Handles retrieval (via HTTP) and local caching. - * * @note When we switch to PHP 5.4, add 'implements JsonSerializable' - * - * @file - * @ingroup Extensions - * @ingroup BookManagerv2 - * - * @author Molly White - * @author Ori Livneh <[email protected]> - * @section LICENSE - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License - * as published by the Free Software Foundation; either version 2 - * of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ - class RemoteSchema { const LOCK_TIMEOUT = 20; @@ -38,22 +15,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->cache = $cache ?: wfGetCache( CACHE_ANYTHING ); + $this->http = $http ?: new Http(); + $this->revision = self::getLatestRevId(); + if ( $this->revision === false ) { + return false; + } + $this->key = "schema:{$wgBookManagerv2SchemaDBname}:{$title}:{$revision}"; } + /** * Retrieves schema content. @@ -77,13 +59,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 +75,7 @@ function memcSet() { return $this->cache->set( $this->key, $this->content ); } + /** * Acquire a mutex lock for HTTP retrieval. @@ -100,9 +85,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 +117,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. @@ -145,7 +154,6 @@ return false; } $raw = $this->http->get( $this->getUri(), self::LOCK_TIMEOUT * 0.8 ); - return FormatJson::decode( $raw, true ) ? : false; + 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: newchange Gerrit-Change-Id: Ia9a9447ee61d3fe254128ea392eb1a7168c763d7 Gerrit-PatchSet: 1 Gerrit-Project: mediawiki/extensions/BookManagerv2 Gerrit-Branch: master Gerrit-Owner: Mollywhite <[email protected]> _______________________________________________ MediaWiki-commits mailing list [email protected] https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits
