Exporting CMS Content

Exporting CMS Content

Retrieving all CMS data

The code below represents a generic retrieval script able to handle any kind of data stored in a Scrivito CMS. Don't be intimidated by the sheer amount of code, mind you, it's a fully fledged exporter.

To run the script, first save it to a file at the root of a Scrivito project, export.rb, then execute rails runner export.rb.

Most of the code above structures the exported data. However, the export data format produced by this script probably won't be understood by the target system, so the script needs to be adapted to make it generate export data that accomodates the new system.

Adapting the output format

The script doesn't make any assumptions with respect to the data contained in a Scrivito CMS. However, if you know how your content is structured, you can optimize the extracted data according to your needs. For example, it is common to have widgets that embed images. Such widgets usually utilize a reference attribute for storing the binary CMS object representing the image. The script, as given above, exports an ImageWidget as:

The referenced CMS image object whose ID is foo is exported as:

For importing the output later on, it might be useful to include the path of the downloaded image directly in the ImageWidget. To achieve this, you could adapt the object_to_json method so that it processes image widgets separately, differentiating them from CMS objects of other types. For example:

This method calls download_binary for every widget that references an image. Thus, if an image is referenced more than once, the method is called for each reference, downloading the binary data more often than required. Therefore, it's a good idea to check whether the file already exists and download it only once:

After applying these changes, the exported data will look like this:

Further adjustments to the script might be required to store the retrieved content in a format the target system is able to process.

Scrivito's extensive API lets you make full of use of Ruby to extract exactly the data you need and format it according to the requirements. You could even render all widgetlist attributes of a CMS object into an HTML string, if that's what you're aiming at.

Processing large amounts of content

Although the script above has not been optimized for speed, it still exports larger amounts of content in an acceptable period of time. Exporting a medium-sized website with 800 CMS objects of which 500 are binaries takes approximately seven minutes. So, for most CMS instances, it should be a reasonable approach to load all data into memory and store it locally in one go. The error handling of the script is also minimal, as it just retries once when fetching a binary piece of content. It is, for most use cases, good enough to simply restart the script if items weren't available in time.

However, if the amount of content stored in a CMS instance gets very large, this approach is not advisable. The formatted export data may not fit into memory, and just restarting the script on an error is not an option if the export takes several hours. In a special case like this, the script needs to be adapted to include some error handling.

The first step in adapting the script is to ensure that after exporting an individual CMS object, the current state is stored. For this, the script could generate an individual file for each CMS object, meaning that the exported data doesn't have to be kept in memory, which solves the first problem. For very large amounts of content, it may even be suitable to store the data in a database or export the content directly to the target system.

The next step is to ensure that the data is in a stable order. This is easy, just replace export(Obj.all) with export(Obj.all.order('id')). This allows the script to continue the export starting at the last exported CMS object in case of an error. If the most recently exported ID is saved to last_exported_id, the code to start the export could be adapted like so:

That's it! Now the script is able to handle even the largest Scrivito CMS instances.

If questions arise while further adapting the export script or writing your own one, don't hesitate to contact the Scrivito support team. They're just one click away.