209 $this->sqlFile =
$path . DIRECTORY_SEPARATOR . $this->component_name .
"_schema.sql";
210 $this->xmlDir =
$path . DIRECTORY_SEPARATOR .
"updates";
234 ComponentManager::scanTrace(
"UpgradeManager:: upgrade: latest version in db for component {$this->component_name} is $latest_version", 3);
236 if(count($this->updateMap) > 0)
238 foreach($this->updateMap as $version_number =>
$handler)
242 if($latest_version < $version_number)
244 list($function, $description) = explode(
":",
$handler);
251 if ($upgrade_to !=
"" && $upgrade_to == $version_number)
break;
273 if($function ==
"sql")
275 else if ($function ==
"xml")
277 elseif($function !=
"done")
279 $updater = array($this, $function);
280 return call_user_func($updater, $version_number);
305 if(!file_exists($this->sqlFile))
311 $fp = fopen($this->sqlFile,
'r');
312 $version_id = str_ireplace(
".",
"\\.",
"version " . $version_number);
313 trace(
"UpgradeManager::version id: $version_id file $this->sqlFile", 3);
321 while (($buffer = fgets($fp)) !==
false && !preg_match(
"/^--.*?\s+$version_id/i", $buffer))
325 if(preg_match(
"/^--\s*START\s*$version_id/i", $buffer))
327 while(($buffer = fgets($fp)) !==
false && !preg_match(
"/^--\s*END\s*$version_id/i", $buffer))
330 if (preg_match(
'/^--\s*Depends\s+On\s+([\w_]+)\s+(.*)/i', $buffer, $depends))
338 if(trim($buffer) ==
"" || preg_match(
"/^--(.*?)\s/", $buffer))
343 trace(
"** Upgrade has ".count($lines).
" lines", 3);
348 if(count($sqlStatements) == 0)
362 throw new FakoliException(
"Failed to upgrade {$this->component_name} to version $version_number - ". $e->getMessage());
372 if(count($lines) == 0)
376 foreach($lines as $line)
381 if (preg_match(
"/^\s*DELIMITER\s+(.*?)$/i", $line,
$d))
383 $delimiter = preg_quote(
$d[1]);
388 if(preg_match(
"/{$delimiter}\s*$/", $line))
390 $sqlStatement .= preg_replace(
"/{$delimiter}\s*$/",
"", $line);
391 $sqlStatements[] = $sqlStatement;
396 $sqlStatement .= $line;
400 trace(print_r($sqlStatements,
true), 3);
401 return $sqlStatements;
408 trace(
"Executing SQL Statements", 2);
410 $db = ConnectionManager::getConnection();
411 $db->beginTransaction();
413 foreach($sqlStatements as $sqlStatement)
415 trace($sqlStatement, 2);
416 $db->exec($sqlStatement);
422 catch(PDOException $e)
434 if(preg_match(
"/(<br>|<br\/>)$/i",
$text))
435 $this->
log .= preg_replace(array(
"/<br>$/i",
"/<br\/>$/i"),
"\r\n",
$text);
446 if (preg_match(
"/\\b{$this->component_name}\\b/",
$config[
"ignore_xml_updates"]))
452 if (!is_dir($this->xmlDir))
454 throw new FakoliException(
"XML update directory does not exists for {$this->component_name}");
457 $file = $this->xmlDir . DIRECTORY_SEPARATOR . $this->component_name .
"_" . $version .
".xml";
459 if (!file_exists(
$file))
461 throw new FakoliException(
"No XML update for version $version of $this->component_name");
464 $xml = file_get_contents(
$file);
466 $mgr->importAll($xml);
482 $fileName = $this->component_name .
"_" . $version .
".txt";
483 $fp = fopen($upgradePath . DIRECTORY_SEPARATOR . $fileName,
'w');
484 fwrite($fp, $this->
log);
521 trace(
"No items found to add to section", 2);
526 ->bind(
":section", $section_name)
531 trace(
"UpgradeManager::addSectionContent - section $section_name not found", 3);
539 if (is_object($item))
541 $ident = $item->identifier;
549 $contents = Query::create(
SectionContent,
"WHERE identifier=:identifier AND section_id=:section_id")
550 ->bind(
":identifier", $ident,
":section_id",
$section->section_id)
553 $content = (count($contents) > 0) ? $contents[0] :
null;
563 $content->filter =
new InclusionFilter(
"template",
"role");
566 if(
$section->default_template != $template)
575 trace(
"Added $item to $section_name", 3);
597 ->bind(
":title", $module_name)
607 if(!is_object($item))
610 if(!
$page)
return false;
619 $found = Query::create(
$xref_class,
"WHERE $pk=:$pk AND module_id=:module_id")
620 ->bind(
":$pk",
$page->$pk,
":module_id",
$module->module_id)
621 ->executeValue(
"COUNT(1)");
625 $xref->$pk =
$page->$pk;
626 $xref->module_id =
$module->module_id;
627 $xref->position = $position;
628 $xref->sort_order = $sort_order;
630 $this->
log .=
$page->format(
"Adding $xref_class record for page {identifier} to module {$module->title}\r\n");
634 $this->
log .=
"Page identifier $identifier already linked to module {$module->title}\r\n";
647 $pages = Query::create(
Page,
"WHERE identifier=:identifier")
660 $this->
log .=
"Page identifier $identifier not found in Page or ComponentPage tables\r\n";
693 function addMenuItem($item, $menu_identifier, $parent_identifier =
"",
$title =
"", $section_name =
"", $sort_order = 0)
696 $menus = Query::create(
Menu,
"WHERE identifier=:identifier")
697 ->bind(
":identifier", $menu_identifier)
700 if(count($menus) == 0)
705 if(!is_object($item))
708 if(!
$page)
return false;
713 $found = Query::create(
MenuItem,
"WHERE (identifier=:identifier OR url like '%{$page->identifier}') AND menu_id=:menu_id")
714 ->bind(
":identifier",
$page->identifier,
":menu_id",
$menu->menu_id)
715 ->executeValue(
"COUNT(1)");
719 $this->
log .=
"Page identifier {$page->identifier} already linked to menu {$menu_identifier}\r\n";
725 if($parent_identifier)
727 $parents = Query::create(
MenuItem,
"WHERE identifier=:identifier")
728 ->bind(
":identifier", $parent_identifier)
731 if(count($parents) > 0)
737 $sort_order = (Query::create(
MenuItem,
"WHERE menu_id=:menu_id AND parent_id=:parent_id")
739 ->executeValue(
"MAX(sort_order)") + 1);
745 ->bind(
":identifier",
$page->identifier)
750 trace(
"UpgradeManager error: page {$page->identifier} is not in section content", 3);
754 $section_name =
$sections[0]->Section()->section;
769 $this->
log .=
$page->format(
"Adding menu item {identifier} to menu {$menu->name}\r\n");
784 $oldPhone = $item->$field;
789 $phone = preg_replace(
"/^1-/",
"", $oldPhone);
791 $phone = preg_replace(
"/\D/",
"", $phone);
793 if($phone != $oldPhone)
795 $item->$field = $phone;
797 $pk = $item->getPrimaryKey();
798 $this->
log($item->format(
"Setting {$item->table} {$pk} $field from {$oldPhone} to {$phone}"));
799 $item->filter =
new InclusionFilter(
$field);
818 trace(
"** Upgrade {$this->component_name} to version $version_number by executing file {$sqlFile}", 3);
822 trace(
"UpgradeManager:: sql file does not exists {$sqlFile}", 2);
830 trace(
"Error: executeVersionSql failed to open file", 3);
835 while(($buffer = fgets($fp)) !==
false)
838 if(trim($buffer) ==
"" || preg_match(
"/^--(.*?)\s/", $buffer))
843 if(count($lines) == 0)
845 trace(
"UpgradeManager:: no lines parsed in file {$sqlFile}", 3);
851 if(count($sqlStatements) == 0)
853 trace(
"UpgradeManager:: no query statements found in sql file {$sqlFile} for version $version_number", 3);
867 trace(
"Firing upgrade event to $component, target version $version", 3);
static scanTrace($message, $level)
static setComponentVersion($component_name)
static findComponentPath($name)
Locate the component path for the named component using the standard precedence rules.
static fireEventTo($event, $component, $parameter=null)
Fire an event to the specificed component.
static recordUpdate($component, $version_number, $description="", $fileName)
static getLatestVersion($component)
FakoliException is the base exception class for all Fakoli errors.
Defines the Module class.
Section DataItem, defining the data model for sections within a site.
SerializationManager handles import/export of DataItems via an XML file.
static getValue($component, $name)
Retrieve the value of the specified Setting.
executeSQLFile($sqlFile, $version_number)
recordUpdate($version, $description)
addMenuItem($item, $menu_identifier, $parent_identifier="", $title="", $section_name="", $sort_order=0)
reformatPhone(&$item, $field)
parseSQLStatements($lines)
executeVersionSql($version_number)
upgradeOneVersion($version_number, $function)
searchByIdentifier($identifier)
static upgradeComponentToVersion($component, $version)
addSectionContent($items, $section_name="/", $role="", $template="", $permissions="")
Utility function for use by calling classes to add an array of items of class Page,...
executeSQLStatements($sqlStatements)
executeVersionXML($version)
dependsOn($component, $version)
addModuleToPages($items, $module_name, $position='right', $sort_order=1)
Given an array of either identifiers that must be either CMS Page or ComponentPage identifiers or ite...
if(!checkRole($library->allow_access) &&! $library->allowAccess()) if(!checkRole($document->allow_access)) $d
if(array_key_exists("HTTP_IF_MODIFIED_SINCE", $_SERVER)) $content