r106474 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r106473‎ | r106474 | r106475 >
Date:20:22, 16 December 2011
Author:foxtrott
Status:deferred
Tags:
Comment:
bugfix (form caching does not work with memcached)
Modified paths:
  • /trunk/extensions/SemanticForms/includes/SF_FormUtils.php (modified) (history)

Diff [purge]

Index: trunk/extensions/SemanticForms/includes/SF_FormUtils.php
@@ -836,34 +836,16 @@
837837
838838
839839 /**
840 - * Parse the form definition and store the resulting HTML in the
841 - * main cache, if caching has been specified in LocalSettings.php
 840+ * Parse the form definition and return it
842841 */
843 - public static function getFormDefinition( $parser, $form_def = null, $form_id = null ) {
 842+ public static function getFormDefinition( &$parser, &$form_def = null, &$form_id = null ) {
844843
845 - global $sfgCacheFormDefinitions, $wgRequest;
846 -
847 - $cachekey = null;
848 -
849 - // use cache if allowed
850 - if ( $sfgCacheFormDefinitions && $form_id !== null ) {
851 -
852 - // create a cache key consisting of owner name, article id and user options
853 - $cachekey = self::getCacheKey( $form_id, $parser );
854 -
855 - $cached_def = self::getFormCache()->get( $cachekey );
856 -
857 - // Cache hit?
858 - if ( $cached_def !== false && $cached_def !== null ) {
859 -
860 - wfDebug( "Cache hit: Got form definition $cachekey from cache\n" );
861 - return $cached_def;
862 - } else {
863 - wfDebug( "Cache miss: Form definition $cachekey not found in cache\n" );
864 - }
865 -
 844+ $cachedDef = self::getFormDefinitionFromCache( $form_id, $parser );
 845+
 846+ if ( $cachedDef ) {
 847+ return $cachedDef;
866848 }
867 -
 849+
868850 if ( $form_id !== null ) {
869851
870852 $form_article = Article::newFromID( $form_id );
@@ -892,28 +874,91 @@
893875 $output = $parser->parse( $form_def, $title, $parser->getOptions() );
894876 $form_def = $output->getText();
895877
 878+ self::cacheFormDefinition( $form_id, $parser, $output );
 879+
 880+ return $form_def;
 881+ }
 882+
 883+ /**
 884+ * Get a form definition from cache
 885+ */
 886+ protected static function getFormDefinitionFromCache ( &$form_id, &$parser ) {
 887+
 888+ global $sfgCacheFormDefinitions;
 889+
 890+ // use cache if allowed
 891+ if ( $sfgCacheFormDefinitions && $form_id !== null ) {
 892+
 893+ $cache = self::getFormCache();
 894+
 895+ // create a cache key consisting of owner name, article id and user options
 896+ $cachekey = self::getCacheKey( $form_id, $parser );
 897+
 898+ $cached_def = $cache->get( $cachekey );
 899+
 900+ // Cache hit?
 901+ if ( $cached_def !== false && $cached_def !== null ) {
 902+
 903+ wfDebug( "Cache hit: Got form definition $cachekey from cache\n" );
 904+ return $cached_def;
 905+
 906+ } else {
 907+ wfDebug( "Cache miss: Form definition $cachekey not found in cache\n" );
 908+ }
 909+ }
 910+
 911+ return null;
 912+ }
 913+
 914+ /**
 915+ * Store a form definition in cache
 916+ */
 917+ protected static function cacheFormDefinition ( &$form_id, &$parser, &$output ) {
 918+
 919+ global $sfgCacheFormDefinitions;
 920+
896921 // store in cache if allowed
897922 if ( $sfgCacheFormDefinitions && $form_id !== null ) {
898923
 924+ $cache = self::getFormCache();
 925+ $cachekey = self::getCacheKey( $form_id, $parser );
 926+
899927 if ( $output->getCacheTime() == -1 ) {
 928+
 929+ $form_article = Article::newFromID( $form_id );
900930 self::purgeCache( $form_article );
901931 wfDebug( "Caching disabled for form definition $cachekey\n" );
 932+
902933 } else {
903934
904 - if ( method_exists( $output, 'getCacheExpiry' ) ) { // MW 1.17+
905 - self::getFormCache()->set( $cachekey, $form_def, $output->getCacheExpiry() );
906 - } else { // MW 1.16
907 - self::getFormCache()->set( $cachekey, $form_def );
908 - }
 935+ $cachekeyForForm = self::getCacheKey( $form_id );
 936+
 937+ // update list of form definitions
 938+ $arrayOfStoredDatasets = $cache->get( $cachekeyForForm );
 939+ $arrayOfStoredDatasets[ $cachekey ] = $cachekey; // just need the key defined, don't care for the value
 940+
 941+ // We cache indefinitely ignoring $wgParserCacheExpireTime.
 942+ // The reasoning is that there really is not point in expiring
 943+ // rarely changed forms automatically (after one day per
 944+ // default). Instead the cache is purged on storing/purging a
 945+ // form definition.
 946+ // A side effect of this is, that there is no need to
 947+ // distinguish between MW <1.17 and >=1.17.
 948+
 949+ // store form definition with current user options
 950+ $cache->set( $cachekey, $output->getText() );
 951+
 952+ // store updated list of form definitions
 953+ $cache->set( $cachekeyForForm, $arrayOfStoredDatasets );
909954
910955 wfDebug( "Cached form definition $cachekey\n" );
911956 }
912957
913958 }
914 -
915 - return $form_def;
 959+
 960+ return null;
916961 }
917 -
 962+
918963 /**
919964 * Deletes the form definition associated with the given wiki page
920965 * from the main cache.
@@ -921,28 +966,31 @@
922967 * @param Page $wikipage
923968 * @return Bool
924969 */
925 - public static function purgeCache ( &$wikipage = null ) {
 970+ public static function purgeCache ( &$wikipage ) {
926971
927 - if ( is_null( $wikipage ) || ( $wikipage->getTitle()->getNamespace() == SF_NS_FORM ) ) {
 972+ if ( ! is_null( $wikipage ) && ( $wikipage->getTitle()->getNamespace() == SF_NS_FORM ) ) {
928973
929 - $keyToPurge = self::getCacheKey( ( is_null( $wikipage ) ) ? null : $wikipage->getId() );
930 -
931 - $len = strlen( $keyToPurge );
932 -
933974 $cache = self::getFormCache();
934 - $keysInCache = $cache->keys();
935975
936 - foreach ( $keysInCache as $curKey ) {
 976+ $cachekeyForForm = self::getCacheKey( $wikipage->getId() );
937977
938 - if ( strncmp( $curKey, $keyToPurge, $len ) === 0 ) {
939 -
940 - if ( self::getFormCache()->delete( $curKey ) ) {
941 - wfDebug( "Deleted cached form definition $curKey.\n" );
942 - }
943 -
 978+ // get references to stored datasets
 979+ $arrayOfStoredDatasets = $cache->get( $cachekeyForForm );
 980+
 981+ if ( $arrayOfStoredDatasets !== false ) {
 982+
 983+ // delete stored datasets
 984+ foreach ( $arrayOfStoredDatasets as $key ) {
 985+ $cache->delete( $key );
 986+ wfDebug( "Deleted cached form definition $key.\n" );
944987 }
 988+
 989+ // delete references to datasets
 990+ $cache->delete( $cachekeyForForm );
 991+ wfDebug( "Deleted cached form definition references $cachekeyForForm.\n" );
 992+ }
945993
946 - }
 994+
947995 }
948996
949997 return true;