Using a custom static cache handler for offline wiki sync in eZ Publish

Mugo has an internal wiki to share and store various information for all team members. Previously we used mostly text files saved in Dropbox, which actually worked quite well. However, we are now using a more powerful solution that combines eZ Publish, a custom static cache handler, and Dropbox.

The Dropbox-only approach was already a good solution because it provided:

  • Immediate sync to all team members
  • Offline access
  • Ease of use
  • Several backup sources

We wanted to add better searchability of content, wiki content formatting, and better content organization without sacrificing any of the above points. After deciding that a password-protected eZ Publish install, along with its enterprise search extension eZ Find, would be able to achieve the new desired functionality, the key was to write a custom static cache handler to bring the eZ Publish content (stored on our server) to Dropbox (on each team member's computer). eZ Publish already comes with a static cache handler in order to store the full HTML output of pages, but it didn't fully meet our needs.

Thankfully, a recent Mugo pull request to support custom static cache handlers had just been merged into the eZ Publish kernel, making this possible :) The pull request was originally intended to more easily purge the cache of a reversy proxy such as Varnish but we were of course very happy to find this new, unforeseen use case!

The first step is to activate the static cache in eZ Publish and create a custom handler; to do so, we use the following settings in site.ini.append.php:

 [ContentSettings]
StaticCache=enabled
#this is if you want to re-generate the cache as soon as an object is published
PreViewCache=enabled
#set your own cache handler by giving its class name 
#(make sure you regenerate the autoload after you add your class)
StaticCacheHandler=MugoStaticCache

Then, we have some standard settings in staticcache.ini.append.php; note that we're storing the static cache files in a static/ folder:

<?php /* #?ini charset="utf-8"?
 
[CacheSettings]
HostName=<myhost>
#folder in which the cache will be generated (the folder must already 
#exist with the proper permissions or the cache may not be written)
StaticStorageDir=static
MaxCacheDepth=10
# A list of url's to cache
CachedURLArray[]
CachedURLArray[]=/*
 
AlwaysUpdateArray[]
AlwaysUpdateArray[]=/*
 
CachedSiteAccesses[]
CachedSiteAccesses[]=mysiteaccess
 
*/ ?>

As a base for our custom static cache handler, we copied the default class (kernel/classes/ezstaticcache.php). Our only modifications were to the storeCache() method, in order to accomplish the following whenever new content was added or existing content was updated:

  1. Bypass the login requirement on our internal wiki in order to store the content. The default static cache handler does a cURL request to save the HTML output of each page, which would not suffice in our case.
  2. Since our local copies would reside outside of the normal eZ Publish environment, we implemented some rewrite rules for design elements such as stylesheets and images
  3. Save binary files locally

The code for the first two goals is shown below. We need to fetch the full view module result of the content in question, inserted into the correct pagelayout for the proper siteaccess

//remember the current siteaccess to put it back after
$current_access = eZSiteAccess::current();
//set the siteaccess
$access = array( 'name' => 'mysiteaccess', 'type' => eZSiteAccess::TYPE_DEFAULT );
eZSiteAccess::change( $access );
 
//then run the content/view with the current node
$moduleName = "content";
$Module = eZModule::findModule( $moduleName );
$result = $Module->run(
    'view',
    array ( 'full', $nodeId ),
    array(
        'ViewCache' => false,
        'AttributeValidation' => array( 'processed' => true, 'attributes' => false ),
        'CollectionAttributes' => false
    ),
    //in this parameter, you can set any variable in the array and each of them will be 
    //available in the view_parameters variable which allows you in templates to make the 
    //difference between a live and cached version (if you want to remove things from the
    //static view for example). You can also get the content of pages by setting the limit 
    //and offset variables here.
    array( 'viewtype' => 'static' )
);
 
//once we have the content back from the module, we can fetch the pagelayout
$moduleResult=array();
$moduleResult[ 'content' ] = $result[ 'content' ];
$tpl->setVariable( "module_result", $moduleResult );
$content = $tpl->fetch( "design:pagelayout.tpl" );
 
//In this case I have to rewrite some css / js / image links to point them to the proper folder in the dropbox
$content = preg_replace( '/<link ([^>]*)href="\/extension\/myextension\/design\/myextension([^"]*)"([^>]*)>/sm', '<link $1href="$2"$3 />', $content );
$content = preg_replace( '/<script ([^>]*)src="\/extension\/myextension\/design\/myextension([^"]*)"([^>]*)><\/script>/sm', '<script $1src="$2"$3></script>', $content );
$content = preg_replace( '/"\/extension\/myextension\/design\/myextension\/images\/([^"]*)"/', '"/images/$1"', $content );
 
//set the access back to what it was
eZSiteAccess::change( $current_access );

For simplicity purposes, not shown is a custom method to scan the content in question and see whether it references any binary files that we need to store.

On the Dropbox side, we did a bit of manual setup to copy the design elements of our new wiki into images/, javascript/ and stylesheets/ folders. The mapping of the such files to the Dropbox folders is made by the preg_replace() calls in the code above. A Dropbox instance sits on the eZ Publish server in order to sync the contents of the static/ folder (as configured above in staticcache.ini.append.php) via a symbolic link. This folder is shared with all of the team members' Dropbox accounts.

(In order to install Dropbox via the command line, follow these instructions.)

And that's it! Do you have a similar wiki need that you handled in a different way? Or do you have a new usage for a custom static cache handler, now that it's possible to have one in eZ Publish? Let us know!

Comments

blog comments powered by Disqus

Contact

604-637-6396
hiATmugo.ca

#414-207 W. Hastings St
Vancouver, BC
Canada


RSS icon Twitter icon

We have been using this team for editing, consulting, support and training services in North America for the last 3 years and we still do. The team has shown their expert knowledge of our systems, and is responsive and dedicated. The services they provide include expert consulting on eZ Publish towards large, high-traffic enterprise customers.

CEO Aleksander Farstad , eZ Systems

I relied on Mugo for the development of two complex web applications, and I was extremely impressed with their accurate estimates, consistent and timely delivery of production code, and commitment. They have the rare blend of business knowledge and deep technical expertise, and they excel at listening to business requirements and translating those requirements into intelligent and reliable software products. They are able to work on all aspects of software solutions -- front end/UI, core code, modules and extensions, application integration, and infrastructure -- and they are extremely capable in all of those areas. I highly recommend Mugo to anyone in need of a competent and dependable technology partner.

Co-Founder Graham Tillotson, Tandem, Chicago

Mugo maintains and develops new features for the Rasmussen Reports website, www.rasmussenreports.com. We are impressed with how efficiently they deliver high quality solutions that exactly fit our needs. The Mugo team understands the complexity of our high traffic website and was able to improve our site's overall performance, as well as its search engine rating. For our site's subscribers, Mugo quickly and efficiently developed many new features for our Premium Section.

The Mugo team is great to work with, and I look forward to a long and productive relationship.

Stephen W Smith, Interim CEO, Rasmussen Reports

If you ask me what is the first thing you think of about the Mugo team, I would say that they listen.

For a customer, the worst thing is to work with a partner that doesn't listen to you. That doesn't understand your points. That simply doesn't listen and try to help you.

Daniel Iribarren, ClearCap

Mugo Web is not another service provider. They are your partners.
One of the things I really like about the Mugo team is that they care about my project and myself. That is why I call them my partners.

Daniel Iribarren, ClearCap

Mugo Web is not just a group of geeks. They are a team of highly knowledgeable technology and business people.
They understand that they are helping you to design solutions to satisfy or solve business problems. And not every developer is able to deliver that. I have worked with more than 10 different let's says production centers and what they delivered to me was geek stuff. Not business solutions.
Mugo Web will help you to use geek stuff to solve business problems.

Daniel Iribarren, ClearCap

It's been a pleasure working with Mugo Web. They are fast, efficient and always helpful. They respond positively to questions and offer great solutions to technical problems.

Susan Wright, Director of Operations, Toronto Arts Council and Foundation

We've been extremely pleased with the work Mugo has done for us. They have been responsive and understanding in dealing with the complexities of our needs.

Wendy Prugh, Program Manager, The Christian Science Monitor

The team members we work with at Mugo have demonstrated a high caliber of competency in the work they do, and it instills great confidence in us to know that we have such quality professionals in our corner. Their consistent delivery has helped us develop a fluid system for enacting change and improvements on our website. We certainly look forward to continuing this relationship!

Wendy Prugh, Program Manager, The Christian Science Monitor

Thank you very much Mugo Web for the website work you've done for our company. You valued & respected our time. You produced quality work. You paid attention to detail and understood the JPLradio concept. Most importantly, we appreciate the good business ethic of our contact person your company provided. For that, I/we thank you.

Troy B. Williams, Founder & Program Director, Just Positive Lyrics Radio.com

We thoroughly enjoy our working relationship with Mugo Web. They are customer-focused, take the time to listen, give good solid advice with our best interests in mind, and have a quick turnaround time. We've had a number of compliments on the new website, which looks goods, functions well, and performs solidly.

Paul O'Sullivan, IT Specialist, New Society Publishers

From our first contact, the team at Mugo has shown an unprecedented level of commitment to our web projects... ending almost 4 years of struggling! In the three months Mugo worked on our new version -- which had stalled with our previous developers -- we have made more progress than ever before. They are fast, precise and thorough, and definitely stand out in the industry!

Sébastien Michel, Director, Frogs-in-NZ

Tweets

  • Looking forward to the next eZ Publish Americas conference call, Friday at 11am PT http://t.co/xJPXRlBX #ezpublish #ezcommunity
    Posted at Wed Feb 22 16:13:12 +0000 2012
  • Celebrating the launch of Canadian book site The 49th Shelf with lead sponsor Amazon.ca; see press release http://t.co/CYUIKUdI #ezpublish
    Posted at Tue Feb 21 15:31:43 +0000 2012
  • eZ Flow edit template more user-friendly on node relation custom attributes; merged pulled request: https://t.co/drgtL7BH #ezpublish
    Posted at Mon Feb 20 15:26:24 +0000 2012
  • @gauthiergarnier Nice! After California we can finally host an eZ event in Canada. It's been lonely. cc @ilukac
    Posted at Fri Feb 17 15:59:13 +0000 2012

Follow Mugo on Twitter