CMS  Version 3.9
serialization_manager.inc
Go to the documentation of this file.
1 <?php
7 Fakoli::using("component");
8 
15 {
20  function export();
21 
27  function import($doc, $tx);
28 }
29 
48 {
49  static $map;
50 
55  function __construct()
56  {
58  {
60  ComponentManager::fireEvent("RegisterSerializationHandler");
62  }
63  }
64 
72  {
73  SerializationManager::$map[$component] = array('title' => $title, 'handler' => $handler);
74  }
75 
81  function export($components)
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  }
102 
107  function exportAll()
108  {
109  $components = implode(",", array_keys(SerializationManager::$map));
110  return $this->export($components);
111  }
112 
121  function import($xml, $components)
122  {
123  $components = explode(",", $components);
124  $this->processImport($xml, $components);
125  }
126 
127  function importBackground()
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  }
144 
145  private function processImport($xml, $components)
146  {
147  $doc = new DOMDocument();
148  if (!$doc->loadXML($xml))
149  {
150  throw new FakoliException("Failed to load XML data file");
151  };
152 
153 
154  $tx = new DataTransaction();
155 
156  try
157  {
158  foreach($components as $component)
159  {
161  $xml .= $handler->import($doc, $tx);
162  }
163 
164  $tx->commit();
165  }
166  catch(Exception $e)
167  {
168  $tx->rollback();
169  throw $e;
170  }
171  }
172 
177  function importAll($xml)
178  {
179  $components = implode(",", array_keys(SerializationManager::$map));
180  return $this->import($xml, $components);
181  }
182 
192  static function unserialize($class, $doc, $tx, $save = true)
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  }
257 
258 
259  static function store($class, $doc, $tx)
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  }
301 
309  static function serialize($class, $constraint = "")
310  {
311  $objects = IteratedQuery::create($class, $constraint)->execute();
312  return toXML($class."List", $objects, "");
313  }
314 
315 }
316 
322 {
323  var $mgr;
324  var $mode;
325  var $action;
326 
328  {
329  $this->mode = $mode;
330  $this->action = $action;
331  $this->mgr = new SerializationManager();
332  }
333 
334  function getSelected()
335  {
336  global $_POST;
337  return implode(",", $_POST["content_type"]);
338  }
339 
340  function writeScript()
341  {
342  }
343 
344  function drawForm()
345  {
346  global $config;
347 
348  if ($this->mode == "Import")
349  {
350  $onsubmit = " onsubmit=\"return confirm('WARNING! Importing site content WILL overwrite existing content in your database.\\nThis CANNOT be undone.\\nDo you wish to continue?');\"";
351  }
352 
353 ?>
354  <form method="POST" action="<?echo $this->action?>" enctype="multipart/form-data"<?echo $onsubmit?>>
355 <?
357  {
358 ?>
359  <input type="checkbox" value="<?echo $component?>" name="content_type[]"/>&nbsp;<?echo $handler['title'];?><br/>
360 <?
361  }
362 ?>
363  <br/>
364 <?
365  if ($this->mode == "Import")
366  {
367 ?>
368  <label for="import_file">Import File </label><input type="file" name="import_file"/><br/><br/>
369 <?
370  }
371  else
372  {
373  $file = codify($config["sitename"]."_".date("Ymd")).".xml";
374 ?>
375  <label for="export_file">Export File Name </label><input type="text" size="50" name="export_file" value="<?echo $file?>"/><br/><br/>
376 <?
377  }
378 ?>
379  <input type="submit" class="button" value="<?echo $this->mode?> Configuration"/>
380  </form>
381 <?
382  }
383 }
384 
392 {
393  var $class;
395 
402  {
403  $this->class = $class;
404 
405  if ($constraint)
406  {
407  $this->constraint = $constraint;
408  }
409  else
410  {
411  $obj = new $class;
412  $this->constraint = "ORDER BY ".$obj->getPrimaryKey();
413  }
414  }
415 
420  function export()
421  {
422  return SerializationManager::serialize($this->class, $this->constraint);
423  }
424 
430  function import($doc, $tx)
431  {
432  global $process;
433  SerializationManager::unserialize($this->class, $doc, $tx);
434  if ($process)
435  {
436  $process->setProgress("Completed", "Import successful", 100);
437  }
438  }
439 }
440 
457 {
459  var $classes = array();
460  var $constraints = array();
461 
468  {
469  if (!$groupTag)
470  {
471  throw new FakoliException("No group tag specified for compound serialization handler");
472  }
473 
474  $this->groupTag = $groupTag;
475  for($i = 1; $i < func_num_args(); ++$i)
476  {
477  $arg = func_get_arg($i);
478  if (is_array($arg))
479  {
480  $this->classes[] = $arg;
481  }
482  else
483  {
484  $this->classes[] = array($arg, "");
485  }
486  }
487  }
488 
494  function addTarget($class, $constraint = "")
495  {
496  $this->classes[] = array($class, $constraint);
497  }
498 
503  function export()
504  {
505  $xml = "\n<{$this->groupTag}>";
506  foreach($this->classes as $class)
507  {
509  }
510 
511  $xml .= "\n</{$this->groupTag}>";
512  return $xml;
513  }
514 
520  function import($doc, $tx)
521  {
522  global $process;
523 
524  foreach($this->classes as $class)
525  {
527  }
528 
529  if ($process)
530  {
531  $process->setProgress("Completed", "Import successful", 100);
532  }
533  }
534 
542  function retrieve($doc, $class)
543  {
544  return SerializationManager::unserialize($class, $doc, null, false);
545  }
546 }?>
$constraint
& nbsp
Definition: index.inc:49
$_POST["owner_id"]
Definition: blog_form.inc:54
$handler
Definition: event_form.inc:62
$component
Definition: help.inc:38
$components
Definition: tree.inc:41
$imageUpload size
Definition: image_form.inc:68
$form action
Definition: edit.inc:67
$file
Definition: delete.inc:47
static fireEvent($event, $parameter=null, $mustBeConsumed=false)
Fire an event to all subscribers as detailed in their manifests.
Provides serialization for a number of DataItem classes, grouped under a common tag in the serialized...
addTarget($class, $constraint="")
Add a target DataItem with optional constraint.
retrieve($doc, $class)
Retrieves serialized objects from the provided XML document, but does not store them in the database.
__construct($groupTag)
Creates the CompoundSerializationHandler.
export()
Exports the target DataItems to XML.
FakoliException is the base exception class for all Fakoli errors.
Definition: core.inc:53
static using()
Import the datamodels, views and manifest for the specified component(s).
Definition: core.inc:116
SerializationForm implements the custom form used in the Fakoli admin section import and export pages...
SerializationForm($mode, $action="")
SerializationManager handles import/export of DataItems via an XML file.
export($components)
Exports data for the specified components.
exportAll()
Export data for all registered components.
__construct()
Creates a SerializationManager, building the serialization map from the registered components via the...
static serialize($class, $constraint="")
Serializes the specified DataItems to XML.
importAll($xml)
Import data for all registered components.
registerHandler($component, $title, $handler)
Registers a serialization handler for a component.
static unserialize($class, $doc, $tx, $save=true)
Instantiates DataItems from the supplied XML document and stores them in the database.
static store($class, $doc, $tx)
Provides a simple implementation of a SerializationHandler that can serialize a single DataItem class...
export()
Exports the DataItems to XML.
__construct($class, $constraint="")
Creates a new SimpleSerializationHandler.
$table mode
if(!checkRole("admin")) $c
global $config
Definition: import.inc:4
Defines the basic interface contract for a SerializationHandler.
export()
Export the serialized objects.
$process
Definition: run.php:54