Ori.livneh has uploaded a new change for review.
https://gerrit.wikimedia.org/r/76327
Change subject: Declare NS_BOOK namespace unconditionally
......................................................................
Declare NS_BOOK namespace unconditionally
EventLogging deferred namespace registration to a hook handler as a way of
allowing a configuration variable to specify whether the namespace should be
activated or not on a particular wiki. (MediaWiki needs to run the extension's
main file for the configuration variables to be initialized, so the code at the
top level of EventLogging.php doesn't know yet if the namespace should be
activated, because the site-specific configuration data hasn't loaded yet.)
I hadn't realized it at the time, but waiting for registerHandlers to be called
before registering the namespace means the namespace is registered too late,
leading to bug 45031.
Fortunately, BookManagerv2 needn't go through the trouble. Any wiki that
enables the extension will also want the extension's namespace; there is no
case in which you'd want one but not the other. So it's OK to declare the
namespace unconditionally at the file level in BookManagerv2.php.
This patch moves the registration of the NS_BOOK namespace and its content
model out of the hook handler, to BookManagerv2.php.
With the namespace logic relocated, the only remaining method in JsonHooks.php
was 'onEditFilterMerged'. I figured it was better to move it to
BookManagerv2.hooks.php and thus have the extension's two hooks consolidated in
a single file, so that's what I did.
Bug: 45031
Change-Id: I76cfa1d5a15d2267e44b794164e8b1bc41d50229
---
M BookManagerv2.hooks.php
M BookManagerv2.php
D includes/JsonHooks.php
3 files changed, 44 insertions(+), 105 deletions(-)
git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/BookManagerv2
refs/changes/27/76327/1
diff --git a/BookManagerv2.hooks.php b/BookManagerv2.hooks.php
index bed3c27..ce39a27 100644
--- a/BookManagerv2.hooks.php
+++ b/BookManagerv2.hooks.php
@@ -26,6 +26,42 @@
class BookManagerv2Hooks {
/**
+ * Validates that the revised contents of an NS_BOOK page are valid
JSON.
+ * If not valid, rejects edit with error message.
+ * @param EditPage $editor
+ * @param string $text Content of the revised article
+ * @param string &$error Error message to return
+ * @param string $summary Edit summary provided for edit.
+ * @return True
+ */
+ static function onEditFilterMerged( $editor, $text, &$error, $summary )
{
+ $pageTitle = $editor->getTitle();
+ if ( $pageTitle->getNamespace() !== NS_BOOK ) {
+ return true;
+ }
+
+ global $wgMemc;
+ $content = new JsonBookContent( $text );
+
+ try {
+ $content->validate();
+ $blockIsValid = true;
+ } catch ( JsonSchemaException $e ) {
+ $error = $e->getMessage();
+ $blockIsValid = false;
+ }
+
+ if ( $blockIsValid ) {
+ //Add to cache
+ $cacheKey = wfMemcKey( 'BookManagerv2',
$pageTitle->getArticleID(), 'json' );
+ $wgMemc->set( $cacheKey,
+ FormatJson::decode( $content->getNativeData(),
false ));
+ }
+
+ return true;
+ }
+
+ /**
* Adds a navigation bar to the page
*
* @param object $prev Object representing the preceding page; contains
diff --git a/BookManagerv2.php b/BookManagerv2.php
index 8f56bb9..1c91151 100644
--- a/BookManagerv2.php
+++ b/BookManagerv2.php
@@ -35,10 +35,14 @@
'descriptionmsg' => 'bookmanagerv2-desc',
);
-// Define namespaces
+// Declare namespace & associated content handler.
define( 'NS_BOOK', 240 );
define( 'NS_BOOK_TALK', 241 );
+
+$wgExtraNamespaces[ NS_BOOK ] = 'Book';
+$wgExtraNamespaces[ NS_BOOK_TALK ] = 'Book_talk';
+$wgContentHandlers[ 'JsonBook' ] = 'JsonBookContentHandler';
// Configuration
@@ -89,7 +93,6 @@
$wgAutoloadClasses += array(
// Hooks
'BookManagerv2Hooks' => __DIR__ . '/BookManagerv2.hooks.php',
- 'JsonHooks' => __DIR__ . '/includes/JsonHooks.php',
// ContentHandler
'JsonBookContent' => __DIR__ . '/includes/JsonContent.php',
@@ -118,23 +121,20 @@
// Register hooks
$wgHooks['BeforePageDisplay'][] = 'BookManagerv2Hooks::onBeforePageDisplay';
+$wgHooks['EditFilterMerged'][] = 'BookManagerv2Hooks::onEditFilterMerged';
// Load resources
$wgResourceModules['ext.BookManagerv2'] = array(
'styles' => 'ext.BookManagerv2.css',
- 'localBasePath' => dirname( __FILE__ ) . '/modules',
+ 'localBasePath' => __DIR__ . '/modules',
'remoteExtPath' => 'BookManagerv2/modules'
);
$wgResourceModules['ext.BookManagerv2js'] = array(
'scripts' => 'ext.BookManagerv2.js',
'styles' => 'ext.BookManagerv2js.css',
- 'localBasePath' => dirname( __FILE__ ) . '/modules',
+ 'localBasePath' => __DIR__ . '/modules',
'remoteExtPath' => 'BookManagerv2/modules'
);
-
-// Register hook and content handlers for the JSON schema content iff
-// running on the MediaWiki instance housing the schemas.
-$wgExtensionFunctions[] = 'JsonHooks::registerHandlers';
// User configuration
diff --git a/includes/JsonHooks.php b/includes/JsonHooks.php
deleted file mode 100644
index 308016b..0000000
--- a/includes/JsonHooks.php
+++ /dev/null
@@ -1,97 +0,0 @@
-<?php
-/**
- * Hooks for managing JSON Schema namespace and content model.
- *
- * @file
- * @ingroup Extensions
- * @ingroup BookManagerv2
- *
- * @author Molly White
- * @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 JsonHooks {
-
- /**
- * Registers hook and content handlers.
- * @return bool: Whether hooks and handler were registered.
- */
- static function registerHandlers() {
- global $wgHooks, $wgContentHandlers;
-
- $wgContentHandlers[ 'JsonBook' ] =
- 'JsonBookContentHandler';
-
- $wgHooks[ 'CanonicalNamespaces' ][] =
- 'JsonHooks::onCanonicalNamespaces';
- $wgHooks[ 'EditFilterMerged' ][] =
- 'JsonHooks::onEditFilterMerged';
-
- return true;
- }
-
- /**
- * Registers the Book namespaces and assigns edit rights.
- * @param array &$namespaces Mapping of numbers to namespace names.
- * @return bool
- */
- static function onCanonicalNamespaces( array &$namespaces ) {
- global $wgNamespaceContentModels;
-
- $namespaces[ NS_BOOK ] = 'Book';
- $namespaces[ NS_BOOK_TALK ] = 'Book_talk';
-
- $wgNamespaceContentModels[ NS_BOOK ] = 'javascript';
-
- return true;
- }
-
- /**
- * Validates that the revised contents are valid JSON.
- * If not valid, rejects edit with error message.
- * @param EditPage $editor
- * @param string $text Content of the revised article
- * @param string &$error Error message to return
- * @param string $summary Edit summary provided for edit.
- * @return True
- */
- static function onEditFilterMerged( $editor, $text, &$error, $summary )
{
- $pageTitle = $editor->getTitle();
- if ( $pageTitle->getNamespace() !== NS_BOOK ) {
- return true;
- }
-
- global $wgMemc;
- $content = new JsonBookContent( $text );
-
- try {
- $content->validate();
- $blockIsValid = true;
- } catch ( JsonSchemaException $e ) {
- $error = $e->getMessage();
- $blockIsValid = false;
- }
-
- if ( $blockIsValid ) {
- //Add to cache
- $cacheKey = wfMemcKey( 'BookManagerv2',
$pageTitle->getArticleID(), 'json' );
- $wgMemc->set( $cacheKey,
- FormatJson::decode( $content->getNativeData(),
false ));
- }
-
- return true;
- }
-}
--
To view, visit https://gerrit.wikimedia.org/r/76327
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: I76cfa1d5a15d2267e44b794164e8b1bc41d50229
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/BookManagerv2
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