Index: trunk/extensions/SemanticForms/includes/SF_FormUtils.php |
— | — | @@ -836,34 +836,16 @@ |
837 | 837 | |
838 | 838 | |
839 | 839 | /** |
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 |
842 | 841 | */ |
843 | | - public static function getFormDefinition( $parser, $form_def = null, $form_id = null ) { |
| 842 | + public static function getFormDefinition( &$parser, &$form_def = null, &$form_id = null ) { |
844 | 843 | |
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; |
866 | 848 | } |
867 | | - |
| 849 | + |
868 | 850 | if ( $form_id !== null ) { |
869 | 851 | |
870 | 852 | $form_article = Article::newFromID( $form_id ); |
— | — | @@ -892,28 +874,91 @@ |
893 | 875 | $output = $parser->parse( $form_def, $title, $parser->getOptions() ); |
894 | 876 | $form_def = $output->getText(); |
895 | 877 | |
| 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 | + |
896 | 921 | // store in cache if allowed |
897 | 922 | if ( $sfgCacheFormDefinitions && $form_id !== null ) { |
898 | 923 | |
| 924 | + $cache = self::getFormCache(); |
| 925 | + $cachekey = self::getCacheKey( $form_id, $parser ); |
| 926 | + |
899 | 927 | if ( $output->getCacheTime() == -1 ) { |
| 928 | + |
| 929 | + $form_article = Article::newFromID( $form_id ); |
900 | 930 | self::purgeCache( $form_article ); |
901 | 931 | wfDebug( "Caching disabled for form definition $cachekey\n" ); |
| 932 | + |
902 | 933 | } else { |
903 | 934 | |
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 ); |
909 | 954 | |
910 | 955 | wfDebug( "Cached form definition $cachekey\n" ); |
911 | 956 | } |
912 | 957 | |
913 | 958 | } |
914 | | - |
915 | | - return $form_def; |
| 959 | + |
| 960 | + return null; |
916 | 961 | } |
917 | | - |
| 962 | + |
918 | 963 | /** |
919 | 964 | * Deletes the form definition associated with the given wiki page |
920 | 965 | * from the main cache. |
— | — | @@ -921,28 +966,31 @@ |
922 | 967 | * @param Page $wikipage |
923 | 968 | * @return Bool |
924 | 969 | */ |
925 | | - public static function purgeCache ( &$wikipage = null ) { |
| 970 | + public static function purgeCache ( &$wikipage ) { |
926 | 971 | |
927 | | - if ( is_null( $wikipage ) || ( $wikipage->getTitle()->getNamespace() == SF_NS_FORM ) ) { |
| 972 | + if ( ! is_null( $wikipage ) && ( $wikipage->getTitle()->getNamespace() == SF_NS_FORM ) ) { |
928 | 973 | |
929 | | - $keyToPurge = self::getCacheKey( ( is_null( $wikipage ) ) ? null : $wikipage->getId() ); |
930 | | - |
931 | | - $len = strlen( $keyToPurge ); |
932 | | - |
933 | 974 | $cache = self::getFormCache(); |
934 | | - $keysInCache = $cache->keys(); |
935 | 975 | |
936 | | - foreach ( $keysInCache as $curKey ) { |
| 976 | + $cachekeyForForm = self::getCacheKey( $wikipage->getId() ); |
937 | 977 | |
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" ); |
944 | 987 | } |
| 988 | + |
| 989 | + // delete references to datasets |
| 990 | + $cache->delete( $cachekeyForForm ); |
| 991 | + wfDebug( "Deleted cached form definition references $cachekeyForForm.\n" ); |
| 992 | + } |
945 | 993 | |
946 | | - } |
| 994 | + |
947 | 995 | } |
948 | 996 | |
949 | 997 | return true; |