<?php
 class vB_PageRestore { use vB_Trait_NoSerialize; protected $xmldir = ''; protected $fileVersions = []; protected $xml = []; protected $assertor = null; public function __construct($xmldir) { $this->xmldir = (string) $xmldir; $this->assertor = vB::getDbAssertor(); $items = ['page', 'route', 'pagetemplate']; foreach ($items AS $item) { $this->xml[$item] = $this->loadXmlFile($item); } } protected function loadXmlFile($itemname) { $filename = "vbulletin-{$itemname}s.xml"; $xml = vB_Xml_Import::parseFile("$this->xmldir/$filename"); $items = []; foreach ($xml[$itemname] AS $item) { $items[$item['guid']] = $item; } $this->fileVersions[$filename] = $xml['vbversion']; return $items; } public function getPageTitleByGuid($guid) { $phraseLib = vB_Library::instance('phrase'); $phraseVarname = 'page_' . $phraseLib->cleanGuidForPhrase($guid) . '_title'; $phrases = vB_Api::instanceInternal('phrase')->fetch([$phraseVarname]); return $phrases[$phraseVarname]; } public function getFileVersions() { $output = []; foreach ($this->fileVersions AS $filename => $version) { $output[] = "$filename: $version"; } return $output; } public function getPageFromXmlByGuid($guid) { return $this->xml['page'][$guid]; } public function getPagesFromXml() { return $this->xml['page']; } public function getMatchingPageFromDbByXmlGuid($guid) { $routeGuid = $this->xml['page'][$guid]['routeGuid']; if (empty($this->xml['route'][$routeGuid]['name'])) { $page = $this->assertor->getRow('page', ['guid' => $guid]); } else { $route = $this->assertor->getRow('routenew', ['name' => $this->xml['route'][$routeGuid]['name']]); $args = vb_unserialize_array($route['arguments']); $page = $this->assertor->getRow('page', ['pageid' => $args['pageid']]); } return $page; } public function getXmlRouteByPageGuid($guid) { $page = $this->xml['page'][$guid]; return $this->xml['route'][$page['routeGuid']]; } public function getDbRouteByRouteId($routeid) { return $this->assertor->getRow('routenew', ['routeid' => $routeid]); } public function getXmlPageTemplateByPageGuid($guid) { $page = $this->xml['page'][$guid]; return $this->xml['pagetemplate'][$page['pageTemplateGuid']]; } public function getDbPageTemplateByPageTemplateId($pagetemplateid) { return $this->assertor->getRow('pagetemplate', ['pagetemplateid' => $pagetemplateid]); } public function restorePage($guid, $printMessage = true) { $xmlpage = $this->xml['page'][$guid]; $xmlroute = $this->getXmlRouteByPageGuid($xmlpage['guid']); $xmlpagetemplate = $this->getXmlPageTemplateByPageGuid($xmlpage['guid']); $dbpage = $this->getMatchingPageFromDbByXmlGuid($xmlpage['guid']); $dbroute = $this->getDbRouteByRouteId($dbpage['routeid']); $dbpagetemplate = $this->getDbPageTemplateByPageTemplateId($dbpage['pagetemplateid']); if ($printMessage) { echo $xmlpage['title']; } $this->assertor->delete('page', ['guid' => $xmlpage['guid']]); $this->assertor->delete('pagetemplate', ['guid' => $xmlpagetemplate['guid']]); $this->assertor->delete('routenew', ['guid' => $xmlroute['guid']]); if ($dbroute['guid'] != $xmlroute['guid']) { $this->assertor->update('routenew', ['name' => vB_dB_Query::VALUE_ISNULL], ['routeid' => $dbroute['routeid']]); } $options = vB_Xml_Import::OPTION_OVERWRITE; $xml_importer = new vB_Xml_Import_PageTemplate('vbulletin', $options); $xml_importer->importFromFile("$this->xmldir/vbulletin-pagetemplates.xml", $xmlpagetemplate['guid']); $xml_importer->replacePhrasePlaceholdersInWidgetConfigs(); $options = vB_Xml_Import::OPTION_OVERWRITE; $xml_importer = new vB_Xml_Import_Page('vbulletin', $options); $xml_importer->importFromFile("$this->xmldir/vbulletin-pages.xml", $xmlpage['guid']); $options = vB_Xml_Import::OPTION_OVERWRITE; $xml_importer = new vB_Xml_Import_Route('vbulletin', $options); $xml_importer->importFromFile("$this->xmldir/vbulletin-routes.xml", $xmlroute['guid']); $xml_importer = new vB_Xml_Import_Page('vbulletin', 0); $parsedXML = $xml_importer->parseFile("$this->xmldir/vbulletin-pages.xml"); $xml_importer->updatePageRoutes($parsedXML); $newRoute = $this->assertor->getRow('routenew', ['guid' => $xmlroute['guid']]); if ($dbroute['guid'] != $xmlroute['guid']) { $this->assertor->update('routenew', ['redirect301' => $newRoute['routeid']], ['routeid' => $dbroute['routeid']]); } $this->assertor->update('vbForum:node', ['routeid' => $newRoute['routeid']], ['routeid' => $dbroute['routeid']]); vB_Cache::resetAllCache(); } } 