isWrapsMode()) { $pico->fixCategories(); $pico->fixContents(); } } class FixPicoWraps { private $mDirname; private $mModule; private $mModuleConfig; private $mWrapsPath; public function __construct($dirname) { $this->mDirname = $dirname; $moduleHandler = xoops_gethandler('module'); $this->mModule = $moduleHandler->getByDirname($dirname); $moduleConfigHandler = xoops_gethandler('config'); $this->mModuleConfig = $moduleConfigHandler->getConfigsByDirname($dirname); $this->mWrapsPath = XOOPS_TRUST_PATH.'/wraps/'.$dirname; } public function isWrapsMode() { $key = 'use_wraps_mode'; return isset($this->mModuleConfig[$key]) && '1' === $this->mModuleConfig[$key]; } public function getWrapsFiles() { exec(sprintf('find "%s" -type f -name "*.html" -o -name "*.htm"', $this->mWrapsPath), $fpaths); $ret = []; foreach ($fpaths as $fpath) { $ret[] = preg_replace('/^'.preg_quote($this->mWrapsPath, '/').'/', '', $fpath); } return $ret; } public function getWrapsFilesDirectory() { $files = $this->getWrapsFiles(); $dirs = array_map('dirname', $files); $ret = array_unique($dirs); sort($ret); return $ret; } public function fixCategories() { $catVpaths = $this->getWrapsFilesDirectory(); $registeredCatVpaths = ['/' => 0]; $categories = $this->getAllCategories(); foreach ($categories as $category) { $contents = $category->getContents(true); $data = $category->getData(); if (empty($contents)) { // remove empty category. echo "empty category found. deleting...\n"; $this->deleteCategory($category); continue; } if (null !== $data['cat_vpath']) { if (!in_array($data['cat_vpath'], $catVpaths)) { echo 'category vpath not found. '.$data['cat_vpath'].' need to confirm by manual.'."\n"; } else { $registeredCatVpaths[$data['cat_vpath']] = intval($data['cat_id']); } } } // register not registered wraps directory. $notRegisteredCatVpaths = array_diff($catVpaths, array_keys($registeredCatVpaths)); foreach ($notRegisteredCatVpaths as $catVpath) { $title = basename($catVpath); $this->createCategory($title, '', 0, $catVpath, 0); } // update parent directory $categories = $this->getAllCategories(); $cmap = ['/' => ['cat_id' => '0', 'cat_vpath' => '/']]; foreach ($categories as $category) { $data = $category->getData(); if (null !== $data['cat_vpath']) { $cmap[$data['cat_vpath']] = $data; } } foreach ($cmap as $cvpath => $data) { if ('0' === $data['cat_id']) { continue; } $pcvpath = dirname($cvpath); if (!isset($cmap[$pcvpath])) { echo 'parent category vpath not found: '.$pcvpath.PHP_EOL; } else { $pdata = $cmap[$pcvpath]; if ($data['pid'] !== $pdata['cat_id']) { echo 'parent category id mismatched: '.$data['cat_vpath'].' need to confirm by manual.'."\n"; } } } } public function fixContents() { $wrapFiles = $this->getWrapsFiles(); $foundFiles = []; foreach ($this->getAllCategories() as $category) { $cdata = $category->getData(); $cvpath = null === $cdata['cat_vpath'] ? '/' : $cdata['cat_vpath']; $contents = $category->getContents(true); foreach ($contents as $content) { $data = $content->getData4Html(); $vpath = $data['vpath']; if ($cvpath !== dirname($vpath)) { echo 'vpath and cat_vpath is not matched..:'.$vpath."\n"; } $filters = array_map('trim', explode('|', $data['filters'])); if (in_array('wraps', $filters)) { $fpath = $this->mWrapsPath.$data['vpath']; if (!file_exists($fpath)) { echo 'wraps file not found: '.$data['vpath'].PHP_EOL; } } $html = trim($data['body_cached']); if (0 === strlen($html)) { echo 'Empty file found: '.$data['vpath'].PHP_EOL; } if (in_array($data['vpath'], $wrapFiles)) { $foundFiles[] = $data['vpath']; } else { echo 'Real wraps file not found: '.$data['vpath'].PHP_EOL; } if (preg_match('/