diff options
| author | Nick Palmer <nick@sluggardy.net> | 2007-05-18 10:00:00 +0000 |
|---|---|---|
| committer | Nick Palmer <nick@sluggardy.net> | 2007-05-18 10:00:00 +0000 |
| commit | b374dbbfaa2ad1d94d87d5500bcec1c6f0ec56cc (patch) | |
| tree | dfa93b5ad95801a8827e5f3eb1b4b8a15530c7b5 | |
| parent | b8f9fd7ccefe05a22dedc6497d7563ecc78fb2bc (diff) | |
| download | liberty-b374dbbfaa2ad1d94d87d5500bcec1c6f0ec56cc.tar.gz liberty-b374dbbfaa2ad1d94d87d5500bcec1c6f0ec56cc.tar.bz2 liberty-b374dbbfaa2ad1d94d87d5500bcec1c6f0ec56cc.zip | |
Move caching out of tikiwiki format and into LibertyContent so all formats
get cached, fixed caching bugs, added readCache and writeCache functions.
Add 'cleanup' option to parseData to remove trailing <br /> tags and run
the result through HMTMLPurifier.
Add parseSplit call to handle split for descriptions and ensure that
the result is cached properly.
Need to update all contents to use parseSplit to generate descriptions in
lists and previews instead of just truncating.
| -rw-r--r-- | LibertyContent.php | 148 | ||||
| -rwxr-xr-x | LibertySystem.php | 22 | ||||
| -rw-r--r-- | plugins/format.tikiwiki.php | 38 |
3 files changed, 159 insertions, 49 deletions
diff --git a/LibertyContent.php b/LibertyContent.php index 8ec12f4..030349c 100644 --- a/LibertyContent.php +++ b/LibertyContent.php @@ -3,7 +3,7 @@ * Management of Liberty content * * @package liberty -* @version $Header: /cvsroot/bitweaver/_bit_liberty/LibertyContent.php,v 1.210 2007/05/17 18:50:27 spiderr Exp $ +* @version $Header: /cvsroot/bitweaver/_bit_liberty/LibertyContent.php,v 1.211 2007/05/18 10:00:00 nickpalmer Exp $ * @author spider <spider@steelsun.com> */ @@ -46,6 +46,8 @@ if( !defined( 'BIT_CONTENT_DEFAULT_STATUS' ) ) { */ require_once( LIBERTY_PKG_PATH.'LibertyBase.php' ); +define( 'LIBERTY_SPLIT_REGEX', "/\.{3}split\.{3}\s*/i" ); + /** * Virtual base class (as much as one can have such things in PHP) for all * derived tikiwiki classes that require database access. @@ -337,6 +339,7 @@ class LibertyContent extends LibertyBase { $ret = $func( $pParamHash ); } } + LibertyContent::expungeCacheFile( $pParamHash['content_id'] ); // If we renamed the page, we need to update the backlinks if( !empty( $renamed ) && $func = $gLibertySystem->getPluginFunction( $pParamHash['format_guid'], 'rename_function' ) ) { @@ -344,6 +347,7 @@ class LibertyContent extends LibertyBase { $this->mLogs['rename_page'] = "Renamed from {$this->mInfo['title']} to {$pParamHash['content_store']['title']}."; } + // store content preferences if( @is_array( $pParamHash['preferences_store'] ) ) { foreach( $pParamHash['preferences_store'] as $pref => $value ) { @@ -394,6 +398,8 @@ class LibertyContent extends LibertyBase { if( $func = $gLibertySystem->getPluginFunction( $this->getField( 'format_guid' ), 'expunge_function' ) ) { $func( $this->mContentId ); } + LibertyContent::expungeCacheFile( $this->mContentId ); + // remove entries in the history $this->expungeVersion(); @@ -536,7 +542,7 @@ class LibertyContent extends LibertyBase { function removeLastVersion( $comment = '' ) { if( $this->isValid() ) { global $gBitSystem; - $this->invalidateCache(); + $this->expungeCacheFile($this->mContentId); $query = "select * from `".BIT_DB_PREFIX."liberty_content_history` where `content_id`=? order by ".$this->mDb->convertSortmode("last_modified_desc"); $result = $this->mDb->query($query, array( $this->mContentId ) ); if ($result->numRows()) { @@ -1961,6 +1967,59 @@ class LibertyContent extends LibertyBase { return $ret; } + + /* + * Splits content either at the ...split... or at the + * length specified if no manual split is in the content. + * + * @param pParseHash a hash with 'data' in it and any + * arguments to the parser as required + * @param pLength the length to split at if no ...split... is present + * @param pForceLength force split at length (default false) + */ + function parseSplit($pParseHash, $pLength, $pForceLength = false) { + global $gLibertySystem, $gBitSystem; + $res = NULL; + // Force cache extension + $pParseHash['cache_extension'] = 'desc'; + // Strip trailing breaks and fixup tags. + $pParseHash['cleanup'] = true; + + $res['data'] = $pParseHash['data']; + if( $pForceLength ) { + $res['data'] = preg_replace( LIBERTY_SPLIT_REGEX, '', $res['data'] ); + } + if( preg_match( LIBERTY_SPLIT_REGEX, $res['data'] ) ) { + $res['man_split'] = TRUE; + $parts = preg_split( LIBERTY_SPLIT_REGEX, $res['data'] ); + if( empty( $parts[1] ) ) { + $res['has_more'] = FALSE; + } + $pParseHash['data'] = $parts[0]; + } else { + // Include length in cache file + $pParseHash['cache_extension'] .= '.'.$pLength; + $pParseHash['data'] = substr( $res['data'], 0, $pLength ); + } + + // description shouldn't contain {maketoc} + $pParseHash['data'] = preg_replace( "/\{maketoc[^\}]*\}/i", "", $pParseHash['data'] ); + + // Do the actual parsing. + $res['parsed'] = $res['parsed_description'] = $this->parseData($pParseHash); + + // Setup the has_more properly and add ... if required. + if( preg_replace( "/\{maketoc[^\}]*\}/i", "", $res['data'] ) != $pParseHash['data'] && empty( $res['man_split'] )) { + // we append ... when the split was generated automagically + $res['parsed_description'] .= '…'; + $res['has_more'] = TRUE; + } elseif( preg_replace( "/\{maketoc[^\}]*\}/i", "", $res['data'] ) != $pParseHash['data'] ) { + $res['has_more'] = TRUE; + } + + return $res; + } + /** * Process the raw content blob using the speified content GUID processor * @@ -1975,6 +2034,8 @@ class LibertyContent extends LibertyBase { * @return string Formated data string */ function parseData( $pMixed=NULL, $pFormatGuid=NULL ) { + global $gLibertySystem, $gBitSystem; + // get the data into place if( empty( $pMixed ) && !empty( $this->mInfo['data'] ) ) { $parseHash = $this->mInfo; @@ -2003,13 +2064,44 @@ class LibertyContent extends LibertyBase { $formatGuid = $pFormatGuid; } - $ret = $parseHash['data']; - if( !empty( $parseHash['data'] ) && $formatGuid ) { - global $gLibertySystem; - if( $func = $gLibertySystem->getPluginFunction( $formatGuid, 'load_function' ) ) { - $ret = $func( $parseHash, $this ); + $ret = NULL; + + // Handle caching if it is enabled. + if( $gBitSystem->isFeatureActive( 'liberty_cache' ) && !empty( $parseHash['content_id'] ) && empty( $parseHash['no_cache'] ) ) { + if( $cacheFile = LibertyContent::getCacheFile( $parseHash['content_id'], $parseHash['cache_extension'] ) ) { + // Attempt to read cache file + if (! ($ret = LibertyContent::readCacheFile($cacheFile)) ) { + // Read failed. Parse and store. + if( !empty( $parseHash['data'] ) && $formatGuid ) { + if( $func = $gLibertySystem->getPluginFunction( $formatGuid, 'load_function' ) ) { + $ret = $func( $parseHash, $this ); + if (!empty($ret) && !empty($parseHash['cleanup']) && $parseHash['cleanup'] ) { + $ret = preg_replace( '/(<br *\/? *>)*$/i', '', $ret); + $ret = $gLibertySystem->purifyHtml($ret, true); + } + } + } + LibertyContent::writeCacheFile($cacheFile, $ret); + } + else { + // Note that we read from cache. + $pCommonObject->mInfo['is_cached'] = TRUE; + } + } + } + + if (!$ret) { + if( !empty( $parseHash['data'] ) && $formatGuid ) { + if( $func = $gLibertySystem->getPluginFunction( $formatGuid, 'load_function' ) ) { + $ret = $func( $parseHash, $this ); + if (!empty($ret) && !empty($parseHash['cleanup']) && $parseHash['cleanup'] ) { + $ret = preg_replace( '/(<br *\/? *>)*$/i', '', $ret); + $ret = $gLibertySystem->purifyHtml($ret, true); + } + } } } + return $ret; } @@ -2239,7 +2331,8 @@ class LibertyContent extends LibertyBase { $ret = FALSE; if( @BitBase::verifyId( $pContentId ) ) { - $path = LibertyContent::getCacheBasePath().$pContentId; + $subdir = $pContentId % 1000; + $path = LibertyContent::getCacheBasePath().$subdir.'/'.$pContentId; if( is_dir( $path ) || mkdir_p( $path ) ) { $ret = $path; } @@ -2249,6 +2342,43 @@ class LibertyContent extends LibertyBase { } /** + * Attempts to read from the specified cache file checking if the + * cached data has expired. + * + * @param the name of the cache file from getCacheFile() + * @return the contents of the cache file or NULL + */ + function readCacheFile($pCacheFile) { + global $gBitSystem; + $ret = NULL; + if (is_file($pCacheFile) && (time() - filemtime( $pCacheFile )) < $gBitSystem->getConfig('liberty_cache') && filesize( $pCacheFile ) > 0 ) { + // get contents from cache file + $h = fopen( $pCacheFile, 'r' ); + $ret = fread( $h, filesize( $pCacheFile ) ); + fclose( $h ); + } + return $ret; + } + + /** + * Unconditionally writes data to the cache file. + * Does not check for error assuming if write failed that the + * read will as well. + * + * @param the name of the cache file from getCacheFile() to write + * @param the contents to write to the file + */ + function writeCacheFile($pCacheFile, $pData) { + // Cowardly refuse to write nothing. + if (!empty($pData)) { + // write parsed contents to cache file + $h = fopen( $pCacheFile, 'w' ); + fwrite( $h, $pData ); + fclose( $h ); + } + } + + /** * Get the path to file where an individual cache item is stored * * @param array $pContentId Content id of cached item @@ -2264,7 +2394,7 @@ class LibertyContent extends LibertyBase { } /** - * Delete cache file for a given content item + * Delete cache files for a given content item * * @param array $pContentId * @access public diff --git a/LibertySystem.php b/LibertySystem.php index d13d430..ae1c0cf 100755 --- a/LibertySystem.php +++ b/LibertySystem.php @@ -3,7 +3,7 @@ * System class for handling the liberty package * * @package liberty -* @version $Header: /cvsroot/bitweaver/_bit_liberty/LibertySystem.php,v 1.70 2007/05/17 18:50:28 spiderr Exp $ +* @version $Header: /cvsroot/bitweaver/_bit_liberty/LibertySystem.php,v 1.71 2007/05/18 10:00:00 nickpalmer Exp $ * @author spider <spider@steelsun.com> */ @@ -115,15 +115,21 @@ class LibertySystem extends LibertyBase { 'simple' => "Simple Purifier"); } - /** - * Purify HTML from a string. - * - * @param string The string to be cleaned. - * @returns string The sanitized string + /** + * Purify HTML from a string. + * + * @param string The string to be cleaned. + * @returns string The sanitized string */ - function purifyHtml($pString) { + function purifyHtml($pString, $pForceHTMLPurifier = false) { global $gBitSystem; - switch($gBitSystem->getConfig('liberty_html_purifier', 'simple')) { + if ($pForceHTMLPurifier) { + $purifier = 'htmlpurifier'; + } + else { + $purifier = $gBitSystem->getConfig('liberty_html_purifier', 'simple'); + } + switch ($purifier) { case 'htmlpurifier': $pString = $this->advancedPurifyHtml($pString); break; diff --git a/plugins/format.tikiwiki.php b/plugins/format.tikiwiki.php index e2fd12a..c67f260 100644 --- a/plugins/format.tikiwiki.php +++ b/plugins/format.tikiwiki.php @@ -1,6 +1,6 @@ <?php /** - * @version $Revision: 1.94 $ + * @version $Revision: 1.95 $ * @package liberty */ global $gLibertySystem; @@ -43,7 +43,6 @@ function tikiwiki_save_data( &$pParamHash ) { if( $pParamHash['edit'] ) { $parser->storeLinks( $pParamHash ); } - LibertyContent::expungeCacheFile( $pParamHash['content_id'] ); } function tikiwiki_verify_data( &$pParamHash ) { @@ -55,7 +54,6 @@ function tikiwiki_verify_data( &$pParamHash ) { function tikiwiki_expunge( $pContentId ) { $parser = new TikiWikiParser(); $parser->expungeLinks( $pContentId ); - LibertyContent::expungeCacheFile( $pContentId ); } function tikiwiki_rename( $pContentId, $pOldName, $pNewName, &$pCommonObject ) { @@ -117,36 +115,12 @@ function tikiwiki_parse_data( &$pParseHash, &$pCommonObject ) { global $gBitSystem; $ret = ''; - // cache data if we are using liberty cache - if( $gBitSystem->isFeatureActive( 'liberty_cache' ) && !empty( $pParseHash['content_id'] ) && empty( $pParseHash['no_cache'] ) ) { - if( $cacheFile = LibertyContent::getCacheFile( $pParseHash['content_id'], $pParseHash['cache_extension'] ) ) { - // write / refresh cache if we are exceeding time limit of cache - if( !is_file( $cacheFile ) || ( $gBitSystem->getConfig( 'liberty_cache' ) < ( time() - filemtime( $cacheFile ) ) ) ) { - static $parser; - if( empty( $parser ) ) { - $parser = new TikiWikiParser(); - } - $ret = $parser->parse_data( $pParseHash, $pCommonObject ); - - // write parsed contents to cache file - $h = fopen( $cacheFile, 'w' ); - fwrite( $h, $ret ); - fclose( $h ); - } else { - // get contents from cache file - $h = fopen( $cacheFile, 'r' ); - $ret = fread( $h, filesize( $cacheFile ) ); - fclose( $h ); - $pCommonObject->mInfo['is_cached'] = TRUE; - } - } - } else { - static $parser; - if( empty( $parser ) ) { - $parser = new TikiWikiParser(); - } - $ret = $parser->parse_data( $pParseHash, $pCommonObject ); + static $parser; + if( empty( $parser ) ) { + $parser = new TikiWikiParser(); } + $ret = $parser->parse_data( $pParseHash, $pCommonObject ); + return $ret; } |
