CMS  Version 3.9
SerializationManager Class Reference

SerializationManager handles import/export of DataItems via an XML file. More...

Public Member Functions

 __construct ()
 Creates a SerializationManager, building the serialization map from the registered components via the RegisterSerializationHandler event. More...
 
 registerHandler ($component, $title, $handler)
 Registers a serialization handler for a component. More...
 
 export ($components)
 Exports data for the specified components. More...
 
 exportAll ()
 Export data for all registered components. More...
 
 import ($xml, $components)
 Imports data from the specified XML document for the specified components. More...
 
 importBackground ()
 
 importAll ($xml)
 Import data for all registered components. More...
 

Static Public Member Functions

static unserialize ($class, $doc, $tx, $save=true)
 Instantiates DataItems from the supplied XML document and stores them in the database. More...
 
static store ($class, $doc, $tx)
 
static serialize ($class, $constraint="")
 Serializes the specified DataItems to XML. More...
 

Static Public Attributes

static $map
 

Detailed Description

SerializationManager handles import/export of DataItems via an XML file.

This allows data to be transferred between Fakoli instances that provide the same data model (for instance, between development and production versions of the same application).

To implement serialization for your Fakoli component, add a line to your manifest specifying that you can recieve the RegisterSerializationHandler event. Your callback for this event you then use the SerializationManager::registerHandler method to register the serialization handler in charge of serializing your data model. Two standard serializers are provided - SimpleSerializationHandler will serialize a single DataItem class. CompoundSerializationHandler serializes a set of classes grouped together in the XML format under a common tag. You can provide custom serialization handlers for more complex needs (should they arise) by implementing the SerializationHandler interface and providing your own implementation.

Author
Andy Green

Definition at line 47 of file serialization_manager.inc.

Constructor & Destructor Documentation

◆ __construct()

SerializationManager::__construct ( )

Creates a SerializationManager, building the serialization map from the registered components via the RegisterSerializationHandler event.

Definition at line 55 of file serialization_manager.inc.

56  {
58  {
60  ComponentManager::fireEvent("RegisterSerializationHandler");
62  }
63  }
static fireEvent($event, $parameter=null, $mustBeConsumed=false)
Fire an event to all subscribers as detailed in their manifests.

Member Function Documentation

◆ export()

SerializationManager::export (   $components)

Exports data for the specified components.

Parameters
string$componentsa comma-separated list of the components for which data is to be exported
Returns
string the serialized XML data representation

Definition at line 81 of file serialization_manager.inc.

82  {
83  global $config;
84 
85  $components = explode(",", $components);
86 
87  $default_charset = $config["default_charset"];
88  if (!$default_charset) $default_charset = "iso-8859-1";
89 
90  $xml = "<?xml version=\"1.0\" encoding=\"{$default_charset}\"?>";
91  $xml .= "\n<Fakoli>";
92 
93  foreach($components as $component)
94  {
96  $xml .= $handler->export();
97  }
98 
99  $xml .= "\n</Fakoli>";
100  return $xml;
101  }
$handler
Definition: event_form.inc:62
$component
Definition: help.inc:38
$components
Definition: tree.inc:41
global $config
Definition: import.inc:4

◆ exportAll()

SerializationManager::exportAll ( )

Export data for all registered components.

Returns
string the serialized XML data representation

Definition at line 107 of file serialization_manager.inc.

108  {
109  $components = implode(",", array_keys(SerializationManager::$map));
110  return $this->export($components);
111  }
export($components)
Exports data for the specified components.

◆ import()

SerializationManager::import (   $xml,
  $components 
)

Imports data from the specified XML document for the specified components.

Any other data in the XML document is ignored.

Parameters
string$xmlthe file path of the XML document containing the serialized data
string$componentsa comma-separated list of the components for which data is to be exported
Exceptions
FakoliException
Exception

Definition at line 121 of file serialization_manager.inc.

122  {
123  $components = explode(",", $components);
124  $this->processImport($xml, $components);
125  }

◆ importAll()

SerializationManager::importAll (   $xml)

Import data for all registered components.

Parameters
string$xmlthe file path of the XML document containing the serialized data

Definition at line 177 of file serialization_manager.inc.

178  {
179  $components = implode(",", array_keys(SerializationManager::$map));
180  return $this->import($xml, $components);
181  }

◆ importBackground()

SerializationManager::importBackground ( )

Definition at line 127 of file serialization_manager.inc.

128  {
129  global $process;
130  if (!$process)
131  {
132  throw new FakoliException("Not running as a background process");
133  }
134 
135  $xml = file_get_contents($process->get("xml_import_file_path"));
136  $components = $process->get("xml_import_components");
137 
138  trace("Importing $components from $xml", 3);
139  $process->setProgress("Running", "Starting import", 0);
140 
141  $components = explode(",", $components);
142  $this->processImport($xml, $components);
143  }
FakoliException is the base exception class for all Fakoli errors.
Definition: core.inc:53
$process
Definition: run.php:54

◆ registerHandler()

SerializationManager::registerHandler (   $component,
  $title,
  $handler 
)

Registers a serialization handler for a component.

Parameters
string$componentthe component for which the handler is being registered (i.e. "blog")
string$titlethe display title describing the data that is serialized for this component (i.e. "Blogs and Articles")
SerializationHandler$handlerthe serialization handler object providing the implementation

Definition at line 71 of file serialization_manager.inc.

72  {
73  SerializationManager::$map[$component] = array('title' => $title, 'handler' => $handler);
74  }

◆ serialize()

static SerializationManager::serialize (   $class,
  $constraint = "" 
)
static

Serializes the specified DataItems to XML.

This utility method is provided to simplify the implementation of SerializationHandlers.

Parameters
string$classthe target DataItem class
string$constraintoptional database constraint for the objects to be serialized
Returns
string serialized XML representation of the DataItems

Definition at line 309 of file serialization_manager.inc.

310  {
311  $objects = IteratedQuery::create($class, $constraint)->execute();
312  return toXML($class."List", $objects, "");
313  }
$constraint

◆ store()

static SerializationManager::store (   $class,
  $doc,
  $tx 
)
static

Definition at line 259 of file serialization_manager.inc.

260  {
261  global $process;
262  trace("== Instantiating $class Records", 3);
263 
264  // Check to see if there are any list tags for the class present.
265  // If not, the class was not serialized in this document, so return
266  // without processing. If there is a list tag (even if it is empty)
267  // we continue processing. This allows for correct deletion of items
268  // as part of the import process.
269 
270  $lists = $doc->getElementsByTagName($class . "List");
271  if ($lists->length == 0) return;
272 
273  $nodes = $doc->getElementsByTagName($class);
274 
275  $count = count($nodes);
276 
277  if ($process)
278  {
279  $process->setProgress("Running", pluralize("Reading $count {$class}", $count), 0);
280  }
281 
282  $objects = array();
283 
284  $c = 0;
285 
286  $object = new $class;
287 
288  foreach($nodes as $node)
289  {
290  $object->fromXML($node);
291  $object->_disablePK = true;
292  $object->joinTransaction($tx);
293  $object->save();
294  ++$c;
295  if ($process)
296  {
297  $process->setProgress("Running", pluralize("Imported $c of $count {$class}", $count), intval((100*$c)/$count));
298  }
299  }
300  }
if(!checkRole("admin")) $c

◆ unserialize()

static SerializationManager::unserialize (   $class,
  $doc,
  $tx,
  $save = true 
)
static

Instantiates DataItems from the supplied XML document and stores them in the database.

This utility method is provided to simplify the implementation of SerializationHandlers.

Parameters
string$classthe target DataItem class
DOMDocument$docthe source XML document
DataTransaction$txThe database transaction to join when creating imported objects
bool$savetrue to save to the database, false to just return the reconstituted objects

Definition at line 192 of file serialization_manager.inc.

193  {
194  global $process;
195  trace("== Instantiating $class Records", 3);
196 
197  // Check to see if there are any list tags for the class present.
198  // If not, the class was not serialized in this document, so return
199  // without processing. If there is a list tag (even if it is empty)
200  // we continue processing. This allows for correct deletion of items
201  // as part of the import process.
202 
203  $lists = $doc->getElementsByTagName($class . "List");
204  if ($lists->length == 0) return;
205 
206  $nodes = $doc->getElementsByTagName($class);
207 
208  $count = count($nodes);
209 
210 
211  if ($process)
212  {
213  $process->setProgress("Running", pluralize("Reading $count {$class}", $count), 0);
214  }
215 
216  $objects = array();
217 
218  $c = 0;
219 
220  foreach($nodes as $node)
221  {
222  $object = new $class;
223  $object->fromXML($node);
224  $objects[] = $object;
225  ++$c;
226 
227  if ($process)
228  {
229  $process->setProgress("Running", pluralize("Reading $c of $count {$class}", $count), intval((100*$c)/$count));
230  }
231 
232  }
233 
234  if ($save)
235  {
236  $o = new $class;
237  $o->joinTransaction($tx);
238  $o->deleteAll();
239 
240  $c = 0;
241 
242  foreach($objects as $object)
243  {
244  $object->_disablePK = true;
245  $object->joinTransaction($tx);
246  $object->save();
247  ++$c;
248  if ($process)
249  {
250  $process->setProgress("Running", pluralize("Imported $c of $count {$class}", $count), intval((100*$c)/$count));
251  }
252  }
253  }
254 
255  return $objects;
256  }

Member Data Documentation

◆ $map

SerializationManager::$map
static

Definition at line 49 of file serialization_manager.inc.


The documentation for this class was generated from the following file: