svnno****@sourc*****
svnno****@sourc*****
2010年 1月 18日 (月) 18:46:51 JST
Revision: 793 http://sourceforge.jp/projects/p2-php/svn/view?view=rev&revision=793 Author: rsk Date: 2010-01-18 18:46:50 +0900 (Mon, 18 Jan 2010) Log Message: ----------- expack: - æ°çã¾ã¨ãèªã¿ãã£ãã·ã¥ã®ä½æ¥ãã¼ã¿ãã¡ã¢ãªã§ãªãã ä¸æãã¡ã¤ã«ã«ä¿åããããã«ããã - P2KeyValueStoreã«JSON Codecã追å ã -- $kvs->getRawKVS()->get($key) ã¨ãã¦å¤ããã³ã¼ãããã« åãåºããAjaxã§å©ç¨ãããã¨ãæ³å®ãã¦ããã -- ãã®æä½ã¯P2DataStoreãã¡ããªã¼ã®ã¯ã©ã¹ã§ã©ããããã Modified Paths: -------------- p2ex/trunk/lib/MatomeCache.php p2ex/trunk/lib/MatomeCacheList.php p2ex/trunk/lib/P2Client.php p2ex/trunk/lib/P2DataStore/MatomeCacheDataStore.php p2ex/trunk/lib/P2DataStore/MatomeCacheMetaDataStore.php p2ex/trunk/lib/P2KeyValueStore/Codec/Array.php p2ex/trunk/lib/P2KeyValueStore/Codec/ArrayShiftJIS.php p2ex/trunk/lib/P2KeyValueStore.php Added Paths: ----------- p2ex/trunk/lib/P2KeyValueStore/Codec/JSON.php p2ex/trunk/lib/P2KeyValueStore/Codec/JSONShiftJIS.php -------------- next part -------------- Modified: p2ex/trunk/lib/MatomeCache.php =================================================================== --- p2ex/trunk/lib/MatomeCache.php 2010-01-17 10:34:34 UTC (rev 792) +++ p2ex/trunk/lib/MatomeCache.php 2010-01-18 09:46:50 UTC (rev 793) @@ -24,6 +24,20 @@ private $_metaData; /** + * ÜÆßÇÝð³kÛ¶·ét@C¼ + * + * @var string + */ + private $_tempFile; + + /** + * ÜÆßÇÝð³kÛ¶·éXg[ + * + * @var stream + */ + private $_stream; + + /** * ÜÆßÇÝLbV ðc· * * @var int @@ -43,25 +57,57 @@ /** * RXgN^ * - * àeðú»µAL[ðæ¾·éB + * vpeBðú»µAêt@Cð쬷éB * * @param string $title * @param int $maxNumEntries */ public function __construct($title, $maxNumEntries = -1) { + global $_conf; + + if ($maxNumEntries == 0) { + $this->_enabled = false; + return; + } + + // vpeBÌú» $this->_content = ''; $this->_metaData = array( 'time' => time(), 'title' => $title, 'threads' => array(), - 'size' => null, + 'size' => 0, ); + $this->_tempFile = null; + $this->_stream = null; $this->_maxNumEntries = $maxNumEntries; - if ($maxNumEntries == 0) { - $this->_enabled = false; + $this->_enabled = true; + + // êt@Cð쬷é + /* + * PHPÅ tmpnam() ÖªÄÎêéÆAC¾êxÅÍ + * PHP_FUNCTION(tempnam) -> php_open_temporary_fd() -> + * php_do_open_temporary_file() -> virtual_file_ex() + * Æ¢¤¬êÅêt@CpfBNgÌðªsíêéB + * ±ÌÛAvirtual_file_ex() Ì use_realpath øÉ + * CWD_REALPATH ªwè³êÄ¢éÌÅ tempnam() ÌÊÍ + * realpath() É©¯éKvªÈ¢B + */ + $tempFile = tempnam($_conf['tmp_dir'], 'matome_'); + if (!$tempFile) { + return; + } + + // êt@CðJ«AXg[tB^ðtÁ·é + $fp = fopen($tempFile, 'wb'); + if ($fp) { + stream_filter_append($fp, 'zlib.deflate'); + stream_filter_append($fp, 'convert.base64-encode'); + $this->_tempFile = $tempFile; + $this->_stream = $fp; } else { - $this->_enabled = true; + unlink($tempfile); } } @@ -72,19 +118,39 @@ * fXgN^ * * àeðÛ¶µAâLbV ðí·éB - * XbhîñªóÌêÍV XȵÆÝȵAÛ¶µÈ¢B + * XbhîñªóÌêÍÛ¶µÈ¢B * * @param void */ public function __destruct() { - if ($this->_enabled && count($this->_metaData['threads'])) { - $this->_metaData['size'] = strlen($this->_content); - MatomeCacheList::add($this->_content, $this->_metaData); + if (!$this->_enabled) { + return; + } + + // Xg[ð¶é + if ($this->_stream) { + fclose($this->_stream); + } + + // Xª éÈç + if (count($this->_metaData['threads'])) { + // àeðÛ¶·é + if ($this->_tempFile) { + MatomeCacheList::add($this->_tempFile, $this->_metaData, true); + } else { + MatomeCacheList::add($this->_content, $this->_metaData, false); + } + // âLbV ðí·éB if ($this->_maxNumEntries > 0) { MatomeCacheList::trim($this->_maxNumEntries); } } + + // êt@Cðí·é + if ($this->_tempFile) { + unlink($this->_tempFile); + } } // }}} @@ -99,7 +165,12 @@ public function concat($content) { if ($this->_enabled) { - $this->_content .= $content; + if ($this->_stream) { + fwrite($this->_stream, $content); + } else { + $this->_content .= $content; + } + $this->_metaData['size'] += strlen($content); } } Modified: p2ex/trunk/lib/MatomeCacheList.php =================================================================== --- p2ex/trunk/lib/MatomeCacheList.php 2010-01-17 10:34:34 UTC (rev 792) +++ p2ex/trunk/lib/MatomeCacheList.php 2010-01-18 09:46:50 UTC (rev 793) @@ -14,13 +14,22 @@ * * @param string $content * @param array $metaData - * @return $key + * @param bool $isRawFile trueÈç$contentÍt@C¼ÅA»ÌàeÍ + * deflate³k+Base64GR[h³ê½f[^ + * @return string */ - static public function add($content, array $metaData) + static public function add($content, array $metaData, $isRawFile = false) { $key = sprintf('%s%0.6f', self::getKeyPrefix(), microtime(true)); - MatomeCacheDataStore::set($key, $content); + + if ($isRawFile) { + MatomeCacheDataStore::setRaw($key, file_get_contents($content)); + } else { + MatomeCacheDataStore::set($key, $content); + } + MatomeCacheMetaDataStore::set($key, $metaData); + return $key; } @@ -107,9 +116,12 @@ // $lengthª[ÌêÍSí if ($length == 0) { - MatomeCacheDataStore::clear($prefix); + $numRemoved = MatomeCacheDataStore::clear($prefix); + if ($numRemoved === false) { + return false; + } MatomeCacheMetaDataStore::clear($prefix); - return true; + return $numRemoved; } // XVÉ\[gµÄ$length+1ÔÚÌR[hðæ¾ @@ -159,7 +171,7 @@ // }}} // {{{ optimize() - + /** * ÜÆßÇÝLbV ðÅK»·é * Modified: p2ex/trunk/lib/P2Client.php =================================================================== --- p2ex/trunk/lib/P2Client.php 2010-01-17 10:34:34 UTC (rev 792) +++ p2ex/trunk/lib/P2Client.php 2010-01-18 09:46:50 UTC (rev 793) @@ -16,7 +16,7 @@ const COOKIE_STORE_NAME = 'p2_2ch_net_cookies.sqlite3'; /** - * ö®P2ÌURIÆeGg|Cg + * ö®p2ÌURIÆeGg|Cg */ const P2_ROOT_URI = 'http://p2.2ch.net/p2/'; const SCRIPT_NAME_READ = 'read.php'; @@ -133,7 +133,7 @@ } if ($cookieManager = $cookieStore->get($loginId)) { - if (!$cookieManager instanceof HTTP_Client_CookieManager) { + if (!($cookieManager instanceof HTTP_Client_CookieManager)) { $cookieStore->delete($loginId); throw new Exception('Cannot restore the cookie manager.'); } @@ -289,7 +289,7 @@ $expression = './/a[contains(@href, "' . self::SCRIPT_NAME_READ . '?")' . ' and contains(@href, "&moritapodat=")]'; $result = $dom->query($expression); - if ($result instanceof DOMNodeList && $result->length > 0) { + if (($result instanceof DOMNodeList) && $result->length > 0) { $anchor = $result->item(0); $uri = self::P2_ROOT_URI . strstr($anchor->getAttribute('href'), self::SCRIPT_NAME_READ); @@ -365,7 +365,7 @@ $dom = new P2DOM($html); $expression = './/form[contains(@action, "' . self::SCRIPT_NAME_POST . '")]'; $result = $dom->query($expression); - if ($result instanceof DOMNodeList && $result->length > 0) { + if (($result instanceof DOMNodeList) && $result->length > 0) { $postData = $this->setupPostData($dom, $result->item(0), $name, $mail, $message); $response = $this->httpPost($uri, $postData, true); } else { @@ -437,7 +437,7 @@ protected function getLoginForm(P2DOM $dom) { $result = $dom->query('.//form[@action and @id="login"]'); - if ($result instanceof DOMNodeList && $result->length > 0) { + if (($result instanceof DOMNodeList) && $result->length > 0) { return $result->item(0); } return null; @@ -455,7 +455,7 @@ protected function getPostForm(P2DOM $dom) { $result = $dom->query('.//form[@action and @id="resform"]'); - if ($result instanceof DOMNodeList && $result->length > 0) { + if (($result instanceof DOMNodeList) && $result->length > 0) { return $result->item(0); } return null; Modified: p2ex/trunk/lib/P2DataStore/MatomeCacheDataStore.php =================================================================== --- p2ex/trunk/lib/P2DataStore/MatomeCacheDataStore.php 2010-01-17 10:34:34 UTC (rev 792) +++ p2ex/trunk/lib/P2DataStore/MatomeCacheDataStore.php 2010-01-18 09:46:50 UTC (rev 793) @@ -10,7 +10,7 @@ // {{{ getKVS() /** - * «Ýf[^ðÛ¶·éP2KeyValueStoreIuWFNgðæ¾·é + * ÜÆßÇÝf[^ðÛ¶·éP2KeyValueStoreIuWFNgðæ¾·é * * @param void * @return P2KeyValueStore @@ -96,6 +96,26 @@ // }}} // }}} Rsy±±ÜÅ + // {{{ setRaw() + + /** + * CodecÉæéϷȵÅf[^ðÛ¶·é + * + * @param string $key + * @param string $value + * @return bool + */ + static public function setRaw($key, $value) + { + $kvs = self::getKVS()->getRawKVS(); + if ($kvs->exists($key)) { + return $kvs->update($key, $value); + } else { + return $kvs->set($key, $value); + } + } + + // }}} } // }}} Modified: p2ex/trunk/lib/P2DataStore/MatomeCacheMetaDataStore.php =================================================================== --- p2ex/trunk/lib/P2DataStore/MatomeCacheMetaDataStore.php 2010-01-17 10:34:34 UTC (rev 792) +++ p2ex/trunk/lib/P2DataStore/MatomeCacheMetaDataStore.php 2010-01-18 09:46:50 UTC (rev 793) @@ -10,7 +10,7 @@ // {{{ getKVS() /** - * «Ýf[^ðÛ¶·éP2KeyValueStoreIuWFNgðæ¾·é + * ÜÆßÇÝ^f[^ðÛ¶·éP2KeyValueStoreIuWFNgðæ¾·é * * @param void * @return P2KeyValueStore Modified: p2ex/trunk/lib/P2KeyValueStore/Codec/Array.php =================================================================== --- p2ex/trunk/lib/P2KeyValueStore/Codec/Array.php 2010-01-17 10:34:34 UTC (rev 792) +++ p2ex/trunk/lib/P2KeyValueStore/Codec/Array.php 2010-01-18 09:46:50 UTC (rev 793) @@ -5,6 +5,8 @@ /** * zñðVACYEAVACY·éCodec * + * ÀÛÍñ³kVACYCodecÈÌÅzñÈOÉàεĢéB + * * VACYãÌTCYª³kðKvÆ·éÙÇå«È¢êÉg¤B * zñÌvfɶñðÜÞêAÃÈUTF-8V[PXÅȯêÎÈçÈ¢B */ Modified: p2ex/trunk/lib/P2KeyValueStore/Codec/ArrayShiftJIS.php =================================================================== --- p2ex/trunk/lib/P2KeyValueStore/Codec/ArrayShiftJIS.php 2010-01-17 10:34:34 UTC (rev 792) +++ p2ex/trunk/lib/P2KeyValueStore/Codec/ArrayShiftJIS.php 2010-01-18 09:46:50 UTC (rev 793) @@ -5,6 +5,8 @@ /** * zñðVACYEAVACY·éCodec * + * ÀÛÍñ³kVACYCodecÈÌÅzñÈOÉàεĢéB + * * VACYãÌTCYª³kðKvÆ·éÙÇå«È¢êÉg¤B * zñÌvfÉÜÜêé¶ñÍÃÈShift_JISV[PXÅȯêÎÈç¸A * L[Íl©US-ASCII¶ñÅ é±ÆðúÒ·éB @@ -14,7 +16,7 @@ // {{{ encodeValue() /** - * lðVACY·é + * lðUTF-8ÉÏ·µ½ãAVACY·é * * @param array $array * @return string @@ -29,7 +31,7 @@ // {{{ decodeValue() /** - * lðAVACY·é + * lðAVACYµ½ãAShift_JISÉÏ··é * * @param string $value * @return array Added: p2ex/trunk/lib/P2KeyValueStore/Codec/JSON.php =================================================================== --- p2ex/trunk/lib/P2KeyValueStore/Codec/JSON.php (rev 0) +++ p2ex/trunk/lib/P2KeyValueStore/Codec/JSON.php 2010-01-18 09:46:50 UTC (rev 793) @@ -0,0 +1,84 @@ +<?php + +// {{{ P2KeyValueStore_Codec_JSON + +/** + * lðJSONGR[hEfR[h·éCodec + * + * ¶ñÍÃÈUTF-8V[PXÅȯêÎÈçÈ¢B + */ +class P2KeyValueStore_Codec_JSON implements P2KeyValueStore_Codec_Interface +{ + // {{{ encodeKey() + + /** + * L[Í»ÌÜÜ + * + * @param string $key + * @return string + */ + public function encodeKey($key) + { + return $key; + } + + // }}} + // {{{ decodeKey() + + /** + * L[Í»ÌÜÜ + * + * @param string $key + * @return string + */ + public function decodeKey($key) + { + return $key; + } + + // }}} + // {{{ encodeValue() + + /** + * lðJSONGR[h·é + * + * @param mixed $value + * @return string + */ + public function encodeValue($value) + { + return json_encode($value); + } + + // }}} + // {{{ decodeValue() + + /** + * lðJSONfR[h·é + * + * JSONÌIuWFNgÍstdClassIuWFNgÅÍÈ + * AzzñÉÏ··é + * + * @param string $json + * @return mixed + */ + public function decodeValue($json) + { + return json_decode($json, true); + } + + // }}} +} + +// }}} + +/* + * Local Variables: + * mode: php + * coding: cp932 + * tab-width: 4 + * c-basic-offset: 4 + * indent-tabs-mode: nil + * End: + */ +// vim: set syn=php fenc=cp932 ai et ts=4 sw=4 sts=4 fdm=marker: Added: p2ex/trunk/lib/P2KeyValueStore/Codec/JSONShiftJIS.php =================================================================== --- p2ex/trunk/lib/P2KeyValueStore/Codec/JSONShiftJIS.php (rev 0) +++ p2ex/trunk/lib/P2KeyValueStore/Codec/JSONShiftJIS.php 2010-01-18 09:46:50 UTC (rev 793) @@ -0,0 +1,57 @@ +<?php + +// {{{ P2KeyValueStore_Codec_JSONShiftJIS + +/** + * lðJSONGR[hEfR[h·éCodec + * + * ¶ñÍÃÈShift_JISV[PXÅȯêÎÈç¸A + * zñÌL[Íl©US-ASCII¶ñÅ é±ÆðúÒ·éB + */ +class P2KeyValueStore_Codec_JSONShiftJIS extends P2KeyValueStore_Codec_JSON +{ + // {{{ encodeValue() + + /** + * lðUTF-8ÉÏ·µ½ãAJSONGR[h·é + * + * @param mixed $value + * @return string + */ + public function encodeValue($value) + { + mb_convert_variables('UTF-8', 'Shift_JIS', $value); + return parent::encodeValue($value); + } + + // }}} + // {{{ decodeValue() + + /** + * lðJSONfR[hµ½ãAShift_JISÉÏ··é + * + * @param string $value + * @return mixed + */ + public function decodeValue($json) + { + $value = parent::decodeValue($json); + mb_convert_variables('SJIS-win', 'UTF-8', $value); + return $value; + } + + // }}} +} + +// }}} + +/* + * Local Variables: + * mode: php + * coding: cp932 + * tab-width: 4 + * c-basic-offset: 4 + * indent-tabs-mode: nil + * End: + */ +// vim: set syn=php fenc=cp932 ai et ts=4 sw=4 sts=4 fdm=marker: Modified: p2ex/trunk/lib/P2KeyValueStore.php =================================================================== --- p2ex/trunk/lib/P2KeyValueStore.php 2010-01-17 10:34:34 UTC (rev 792) +++ p2ex/trunk/lib/P2KeyValueStore.php 2010-01-18 09:46:50 UTC (rev 793) @@ -36,13 +36,15 @@ const C_KEY_BEGINS = 'arkey LIKE :pattern ESCAPE :escape'; - const CODEC_DEFAULT = 'P2KeyValueStore_Codec_Default'; - const CODEC_BINARY = 'P2KeyValueStore_Codec_Binary'; - const CODEC_COMPRESSING = 'P2KeyValueStore_Codec_Compressing'; - const CODEC_SHIFTJIS = 'P2KeyValueStore_Codec_ShiftJIS'; - const CODEC_SERIALIZING = 'P2KeyValueStore_Codec_Serializing'; - const CODEC_ARRAY = 'P2KeyValueStore_Codec_Array'; - const CODEC_ARRAYSHIFTJIS = 'P2KeyValueStore_Codec_ArrayShiftJIS'; + const CODEC_DEFAULT = 'P2KeyValueStore_Codec_Default'; + const CODEC_BINARY = 'P2KeyValueStore_Codec_Binary'; + const CODEC_COMPRESSING = 'P2KeyValueStore_Codec_Compressing'; + const CODEC_SHIFTJIS = 'P2KeyValueStore_Codec_ShiftJIS'; + const CODEC_SERIALIZING = 'P2KeyValueStore_Codec_Serializing'; + const CODEC_ARRAY = 'P2KeyValueStore_Codec_Array'; + const CODEC_ARRAYSHIFTJIS = 'P2KeyValueStore_Codec_ArrayShiftJIS'; + const CODEC_JSON = 'P2KeyValueStore_Codec_JSON'; + const CODEC_JSONSHIFTJIS = 'P2KeyValueStore_Codec_JSONShiftJIS'; const MEMORY_DATABASE = ':memory:'; @@ -84,6 +86,13 @@ private $_codec; /** + * e[u¼ + * + * @var string + */ + private $_tableName; + + /** * ¯ÊqƵÄNH[gÏÝÌe[u¼ * * @var string @@ -184,6 +193,7 @@ $this->_pdo = $pdo; $this->_pdoId = spl_object_hash($pdo); $this->_codec = $codec; + $this->_tableName = $tableName; $this->_quotedTableName = '"' . str_replace('"', '""', $tableName) . '"'; $this->_sharedResult = new P2KeyValueStore_Result; @@ -921,17 +931,43 @@ // {{{ getTableName() /** - * NH[gÏÝÌe[u¼ðÔ· + * e[u¼ðÔ· * - * @param void + * @param bool $quoted * @return string */ - public function getTableName() + public function getTableName($quoted = false) { - return $this->_quotedTableName; + if ($quoted) { + return $this->_quotedTableName; + } else { + return $this->_tableName; + } } // }}} + // {{{ getRawKVS() + + /** + * ¯¶e[uðµ¢AL[ElÌÏ·ðµÈ¢Key-Value StoreðÔ· + * + * @param void + * @return new P2KeyValueStore + */ + public function getRawKVS() + { + if (array_key_exists('__raw__', self::$_codecCache)) { + $codec = self::$_codecCache['__raw__']; + } else { + $className = self::CODEC_DEFAULT; + $codec = new $className; + self::$_codecCache['__raw__'] = $codec; + } + + return new P2KeyValueStore($this->_pdo, $codec, $this->_tableName); + } + + // }}} // {{{ loadClass() /**