Mollywhite has uploaded a new change for review.

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


Change subject: Cache the JSON blocks using memcached
......................................................................

Cache the JSON blocks using memcached

JSON blocks are now saved to the cache when the JSON block is edited
or when the JSON block is loaded and doesn't already exist in the
cache.

Change-Id: Ia7c34bf295aab4ef4965a7331f5369a19492b69c
---
M BookManagerv2.hooks.php
M includes/JsonHooks.php
2 files changed, 30 insertions(+), 4 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/BookManagerv2 
refs/changes/03/74803/1

diff --git a/BookManagerv2.hooks.php b/BookManagerv2.hooks.php
index 69a32be..2a38306 100644
--- a/BookManagerv2.hooks.php
+++ b/BookManagerv2.hooks.php
@@ -100,13 +100,18 @@
                                ),
                                $metadata
                        );
-               $html[] = Html::rawElement( 'div', array(
+               $html[] = Html::openElement( 'div', array(
                                'class' => array(
                                        'mw-bookmanagerv2-nav-dropdown',
                                        'mw-bookmanagerv2-nav-toc' )
+                               )
+                       );
+               $html[] = Html::rawElement( 'div', array(
+                       'class' => 'mw-bookmanagerv2-nav-scrollable'
                                ),
                                $chapterList
                        );
+               $html[] = Html::closeElement( 'div' );
                $html[] = Html::closeElement( 'div' );
                $html[] = Html::closeElement( 'div' );
                return implode( $html );
@@ -309,7 +314,7 @@
                global $wgContentNamespaces;
                if ( in_array( $out->getTitle()->getNamespace(), 
$wgContentNamespaces ) ) {
                        if ( $out->getRevisionId() !== null ) {
-                               global $wgContLang, 
$wgBookManagerv2ExampleNavigation;
+                               global $wgContLang, 
$wgBookManagerv2ExampleNavigation, $wgMemc;
                                $categories = $out->getCategories();
                                $namespace = $wgContLang->convertNamespace( 
NS_BOOK ) . ":";
                                $out->addModuleStyles( "ext.BookManagerv2" );
@@ -319,8 +324,18 @@
                                foreach ( $categories as $cat ) {
                                        if ( substr( $cat, 0, strlen( 
$namespace ) ) === $namespace ) {
                                                $jsonPageTitle = 
Title::newFromText( $cat );
+                                               $jsonPageTitleText = 
$jsonPageTitle->getText();
                                                if ( $jsonPageTitle->exists() ) 
{
-                                                       $jsonBook = 
self::getJson( $jsonPageTitle );
+                                                       //Check for cached 
version of the JSON block, otherwise
+                                                       //get it from the DB 
and set the value in the cache.
+                                                       $cacheKey = wfMemcKey( 
$jsonPageTitleText );
+                                                       $jsonBook = 
$wgMemc->get( $cacheKey );
+                                                       if ( $jsonBook === '' ) 
{
+                                                               $jsonBook = 
self::getJson( $jsonPageTitle );
+                                                               if ( $jsonBook 
) {
+                                                                       
$wgMemc->set( $cacheKey, $jsonBook );
+                                                               }
+                                                       } 
                                                        break;
                                                }
                                        }
diff --git a/includes/JsonHooks.php b/includes/JsonHooks.php
index 3322f88..649f85f 100644
--- a/includes/JsonHooks.php
+++ b/includes/JsonHooks.php
@@ -69,17 +69,28 @@
         * @return True
         */
        static function onEditFilterMerged( $editor, $text, &$error, $summary ) 
{
-               if ( $editor->getTitle()->getNamespace() !== NS_BOOK ) {
+               $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
+                       $wgMemc->set( wfMemcKey( $pageTitle->getText() ),
+                               FormatJson::decode( $content->getNativeData(), 
false ));
+               }
+
                return true;
        }
 }

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ia7c34bf295aab4ef4965a7331f5369a19492b69c
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

Reply via email to