8 if(!defined('XOOPS_ROOT_PATH'))
13 require_once SD3RD_TRUST_PATH . '/class/Sd3rdUtils.class.php';
14 require_once SD3RD_TRUST_PATH . '/class/AssetManager.class.php';
15 require_once SD3RD_TRUST_PATH . '/class/updater/Context.class.php';
16 require_once SD3RD_TRUST_PATH . '/class/updater/AbstractProcess.class.php';
21 class Sd3rd_UpdateUtils
23 protected static /*** Sd3rd_AssetManager ***/ $_mAsset = null;
24 protected static /*** string{} ***/ $_mEncoding = array();
33 public static function setup()
35 $root =& XCube_Root::getSingleton();
36 $dirname = $root->getSiteConfig('Sd3rd','publicName');
37 $root->mLanguageManager->loadModuleMessageCatalog($dirname);
38 self::$_mAsset =& Sd3rd_AssetManager::getInstance($dirname);
39 self::$_mEncoding = $root->getSiteConfig('Sd3rd.Encoding');
42 #region output methods
46 * @param string $message
51 public static function output(/*** string ***/ $message = null,/*** bool ***/ $lf = true)
53 echo self::convertToOutput($message);
54 $lf && self::output("\n",false);
58 * Output error message.
60 * @param Sd3rd_Exception $ex
61 * @param callback $callback
65 public static function outputError(Sd3rd_Exception $ex,/*** callback ***/ $callback = null)
67 self::output($ex->__toString());
68 if(is_callable($callback))
70 call_user_func($callback);
79 #region process methods
81 * Execute filter list.
88 public static function executeFilter(/*** Enum ***/ $mode,/*** string ***/ $type = 'Sd3rd_AbstractFilter')
90 $process = new XCube_Delegate();
91 foreach(self::getFileList($mode) as $file)
93 $filter = self::makeInstance($mode,$file,$type);
94 $filter->prepare(SD3RD_UPDATE_TIME);
95 $process->add(array($filter,'execute'),$filter->getPriority());
102 catch(Sd3rd_Exception $ex)
104 self::outputError($ex);
112 * @param string $name
113 * @param string $type
115 * @return Sd3rd_AbstractProcess
117 public static function getProcess(/*** Enum ***/ $mode,/*** string ***/ $name,/*** string ***/ $type = 'Sd3rd_AbstractProcess')
119 static $cache = array();
120 if(!isset($cache[$mode]))
122 $cache[$mode] = self::getProcessCache($mode,$type);
124 if(!isset($cache[$mode][$name = self::makeClassName($mode,$name)]))
126 throw new Sd3rd_ProcessNotFoundException('Request process is not found.');
128 return $cache[$mode][$name];
132 * Get sub process list.
135 * @param string $type
137 * @return Sd3rd_AbstractProcess{}
139 public static function getProcessCache(/*** Enum ***/ $mode,/*** string ***/ $type)
142 foreach(self::getFileList($mode) as $file)
144 $list[$name = self::makeClassNameFromPath($mode,$file)] = self::makeInstance($mode,$file,$type);
145 $list[$name]->prepare(SD3RD_UPDATE_TIME);
151 * Get sub process file list.
157 public static function getFileList(/*** Enum ***/ $mode)
159 return glob(SD3RD_TRUST_PATH . '/class/updater/' . $mode . '/*.class.php');
163 * Get sub process instance.
166 * @param string $file
167 * @param string $type
171 public static function makeInstance(/*** Enum ***/ $mode,/*** string ***/ $file,/*** string ***/ $type = 'Sd3rd_AbstractProcess')
174 if(!class_exists($class = self::makeClassNameFromPath($mode,$file)))
176 throw new Sd3rd_ClassNotFoundException(sprintf('Class "%s" is not found.',$class));
178 $instance = new $class();
179 if(!$instance instanceof $type)
181 throw new Sd3rd_InvalidClassException(sprintf('"%s" is not implements "%s".',$class,$type));
187 * Get sub process name.
190 * @param string $name
194 public static function makeClassName(/*** Enum ***/ $mode,/*** string ***/ $name)
196 return 'Sd3rd_' . ucfirst($name) . ucfirst(basename($mode));
200 * Get sub process name from file path.
203 * @param string $path
207 public static function makeClassNameFromPath(/*** Enum ***/ $mode,/*** string ***/ $path)
209 return self::makeClassName($mode,substr(basename($path),0,-10));
213 #region convert methods
215 * Convert message from input encoding to internal encoding.
217 * @param string $message
221 public static function convertFromInput(/*** string ***/ $message)
223 return Sd3rd_Utils::normalizeString(mb_convert_encoding($message,mb_internal_encoding(),self::$_mEncoding['input']));
227 * Convert message from internal encoding to output encoding.
229 * @param string $message
233 public static function convertToOutput(/*** string ***/ $message)
235 return mb_convert_encoding($message,self::$_mEncoding['output'],mb_internal_encoding());
239 * Get table name from table type.
245 public static function convertTableName(/*** Enum ***/ $type)
247 return self::getHandler($type)->mTable;
251 #region parse methods
253 * Parse line by regex.
255 * @param string $line
256 * @param string $pattern
260 public static function parseByRegex(/*** string ***/ $line,/*** string ***/ $pattern)
263 if(!preg_match($pattern,$line,$arr))
265 throw new Sd3rd_ParseErrorException('No match at pattern.',array('line' => $line,'pattern' => $pattern));
271 * Parse line by explode.
273 * @param string $line
277 * @param string $splitter
281 public static function parseByExplode(/*** string ***/ $line,/*** int ***/ $cnt,/*** int ***/ $offset,/*** int ***/ $end = null,/*** string ***/ $splitter = ',')
283 $arr = $end != null ? explode($splitter,substr($line,$offset,$end)) : explode($splitter,substr($line,$offset));
284 if(count($arr) != $cnt)
286 throw new Sd3rd_ParseErrorException('Mismatch token length.',array('line' => $line,'count' => $cnt,'offset' => $offset,'end' => $end,'splitter' => $splitter,'result' => $arr));
292 * Parse line by named explode.
294 * @param string $line
295 * @param string[] $nameList
298 * @param string $splitter
302 public static function parseByExplodeWithName(/*** string ***/ $line,/*** string[] ***/ $nameList,/*** int ***/ $offset,/*** int ***/ $end = null,/*** string ***/ $splitter = ',')
306 return array_combine($nameList,self::parseByExplode($line,count($nameList),$offset,$end,$splitter));
308 catch(Sd3rd_ParseErrorException $ex)
310 $ex->set('nameList',$nameList);
316 #region handler methods
318 * Get object handler.
322 * @return XoopsObjectGenericHandler
324 public static function getHandler(/*** Enum ***/ $name)
326 return self::$_mAsset->getObject('handler',$name);
334 * @return XoopsSimpleObject
336 public static function createObject(/*** Enum ***/ $type)
338 return self::getHandler($type)->create();
345 * @param XoopsSimpleObject &$obj
349 public static function insertObject(/*** Enum ***/ $type,XoopsSimpleObject &$obj)
351 if(!self::getHandler($type)->insert($obj,true))
353 throw new Sd3rd_QueryErrorException('Cannot insert object.',array('type' => $type,'object' => $obj));
361 * @param XoopsSimpleObject $obj
365 public static function deleteObject(/*** Enum ***/ $type,XoopsSimpleObject $obj)
367 if(!self::getHandler($type)->delete($obj,true))
369 throw new Sd3rd_QueryErrorException('Cannot delete object.',array('type' => $type,'object' => $obj));
377 * @param CriteriaElement $cri
381 public static function deleteMultiObjects(/*** Enum ***/ $type,CriteriaElement $cri)
383 if(!$handler->deleteAll($cri,true))
385 throw new Sd3rd_QueryErrorException('Cannot delete objects.',array('type' => $type,'criteria' => $cri));