r102726 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r102725‎ | r102726 | r102727 >
Date:00:20, 11 November 2011
Author:foxtrott
Status:deferred (Comments)
Tags:
Comment:
Enable caching for formdefinitions
Modified paths:
  • /trunk/extensions/SemanticForms/SemanticForms.php (modified) (history)
  • /trunk/extensions/SemanticForms/includes/SF_FormPrinter.php (modified) (history)
  • /trunk/extensions/SemanticForms/includes/SF_FormUtils.php (modified) (history)
  • /trunk/extensions/SemanticForms/includes/SF_Utils.php (modified) (history)

Diff [purge]

Index: trunk/extensions/SemanticForms/SemanticForms.php
@@ -87,7 +87,8 @@
8888 $wgHooks['SkinTemplateNavigation'][] = 'SFFormEditTab::displayTab2';
8989 $wgHooks['smwInitProperties'][] = 'SFUtils::initProperties';
9090 $wgHooks['AdminLinks'][] = 'SFUtils::addToAdminLinks';
91 -$wgHooks['ParserBeforeStrip'][] = 'SFUtils::cacheFormDefinition';
 91+$wgHooks['ArticlePurge'][] = 'SFFormUtils::purgeCache';
 92+$wgHooks['ArticleSave'][] = 'SFFormUtils::purgeCache';
9293 $wgHooks['ParserFirstCallInit'][] = 'SFParserFunctions::registerFunctions';
9394 $wgHooks['MakeGlobalVariablesScript'][] = 'SFFormUtils::setGlobalJSVariables';
9495 $wgHooks['PageSchemasRegisterHandlers'][] = 'SFPageSchemas::registerClass';
Index: trunk/extensions/SemanticForms/includes/SF_Utils.php
@@ -694,35 +694,6 @@
695695 }
696696
697697 /**
698 - * Parse the form definition and store the resulting HTML in the
699 - * page_props table, if caching has been specified in LocalSettings.php
700 - */
701 - public static function cacheFormDefinition( $parser, $text ) {
702 - global $sfgCacheFormDefinitions;
703 - if ( ! $sfgCacheFormDefinitions )
704 - return true;
705 -
706 - $title = $parser->getTitle();
707 - if ( empty( $title ) ) return true;
708 - if ( $title->getNamespace() != SF_NS_FORM ) return true;
709 - // Remove <noinclude> sections and <includeonly> tags from form definition
710 - $form_def = StringUtils::delimiterReplace( '<noinclude>', '</noinclude>', '', $text );
711 - $form_def = strtr( $form_def, array( '<includeonly>' => '', '</includeonly>' => '' ) );
712 -
713 - // parse wiki-text
714 - // add '<nowiki>' tags around every triple-bracketed form
715 - // definition element, so that the wiki parser won't touch
716 - // it - the parser will remove the '<nowiki>' tags, leaving
717 - // us with what we need
718 - $form_def = "__NOEDITSECTION__" . strtr( $form_def, array( '{{{' => '<nowiki>{{{', '}}}' => '}}}</nowiki>' ) );
719 - $dummy_title = Title::newFromText( 'Form definition title for caching purposes' );
720 - $form_def = $parser->parse( $form_def, $dummy_title, $parser->mOptions )->getText();
721 -
722 - $parser->mOutput->setProperty( 'formdefinition', $form_def );
723 - return true;
724 - }
725 -
726 - /**
727698 * Loads messages only for MediaWiki versions that need it (< 1.16)
728699 */
729700 public static function loadMessages() {
Index: trunk/extensions/SemanticForms/includes/SF_FormPrinter.php
@@ -321,7 +321,7 @@
322322 global $sfgFieldNum; // used for setting various HTML IDs
323323
324324 wfProfileIn( __METHOD__ );
325 -
 325+
326326 // initialize some variables
327327 $sfgTabIndex = 1;
328328 $sfgFieldNum = 1;
@@ -408,37 +408,15 @@
409409 $wgOut->addHTML( "\n<hr />\n" );
410410 }
411411
412 - // Remove <noinclude> sections and <includeonly> tags from form definition.
413 - $form_def = StringUtils::delimiterReplace( '<noinclude>', '</noinclude>', '', $form_def );
414 - $form_def = strtr( $form_def, array( '<includeonly>' => '', '</includeonly>' => '' ) );
415 -
416 - // Parse wiki-text.
417 - // Add '<nowiki>' tags around every triple-bracketed form definition
418 - // element, so that the wiki parser won't touch it - the parser will
419 - // remove the '<nowiki>' tags, leaving us with what we need.
420 - $form_def = "__NOEDITSECTION__" . strtr( $form_def, array( '{{{' => '<nowiki>{{{', '}}}' => '}}}</nowiki>' ) );
421 -
422412 $oldParser = $wgParser;
423413
424414 $wgParser = unserialize( serialize( $oldParser ) ); // deep clone of parser
 415+ $wgParser->Options( ParserOptions::newFromUser( $wgUser ) );
 416+ $wgParser->Title( $this->mPageTitle );
 417+ $wgParser->clearState();
425418
426 - // Get the form definition from the cache, if we're using caching and it's
427 - // there.
428 -// $got_form_def_from_cache = false;
429 -// global $sfgCacheFormDefinitions;
430 -// if ( $sfgCacheFormDefinitions && ! is_null( $form_id ) ) {
431 -// $db = wfGetDB( DB_MASTER );
432 -// $res = $db->select( 'page_props', 'pp_value', "pp_propname = 'formdefinition' AND pp_page = '$form_id'" );
433 -// if ( $res->numRows() > 0 ) {
434 -// $form_def = $res->fetchObject()->pp_value;
435 -// $got_form_def_from_cache = true;
436 -// }
437 -// }
438 - // Otherwise, parse it.
439 -// if ( ! $got_form_def_from_cache ) {
440 - $form_def = $wgParser->parse($form_def, $this->mPageTitle, ParserOptions::newFromUser($wgUser))->getText();
441 -// }
442 -
 419+ $form_def = SFFormUtils::getFormDefinition( $wgParser, $form_def, $form_id );
 420+
443421 // Turn form definition file into an array of sections, one for each
444422 // template definition (plus the first section)
445423 $form_def_sections = array();
Index: trunk/extensions/SemanticForms/includes/SF_FormUtils.php
@@ -833,4 +833,99 @@
834834 END;
835835 return $javascript_text;
836836 }
 837+
 838+
 839+ /**
 840+ * Parse the form definition and store the resulting HTML in the
 841+ * main cache, if caching has been specified in LocalSettings.php
 842+ */
 843+ public static function getFormDefinition( $parser, $form_def = null, $form_id = null ) {
 844+
 845+ global $sfgCacheFormDefinitions, $wgRequest;
 846+
 847+ $cachekey = null;
 848+
 849+ // use cache if allowed
 850+ if ( $sfgCacheFormDefinitions && $form_id !== null ) {
 851+
 852+ $cachekey = wfMemcKey('ext.SemanticForms.formdefinition', $form_id);
 853+ $cached_def = wfGetMainCache()->get( $cachekey );
 854+
 855+ // Cache hit?
 856+ if ( $cached_def !== false && $cached_def !== null ) {
 857+
 858+ wfDebug( "Cache hit: Got formdefinition $cachekey from cache\n" );
 859+ return $cached_def;
 860+ } else {
 861+ wfDebug( "Cache miss: Formdefinition $cachekey not found in cache\n" );
 862+ }
 863+
 864+ }
 865+
 866+ if ( $form_id !== null ) {
 867+
 868+ $form_article = Article::newFromID( $form_id );
 869+ $form_def = $form_article->getContent();
 870+
 871+ } else if ( $form_def == null ) {
 872+
 873+ // No id, no text -> nothing to do
 874+ return '';
 875+
 876+ }
 877+
 878+ // Remove <noinclude> sections and <includeonly> tags from form definition
 879+ $form_def = StringUtils::delimiterReplace( '<noinclude>', '</noinclude>', '', $form_def );
 880+ $form_def = strtr( $form_def, array( '<includeonly>' => '', '</includeonly>' => '' ) );
 881+
 882+ // add '<nowiki>' tags around every triple-bracketed form
 883+ // definition element, so that the wiki parser won't touch
 884+ // it - the parser will remove the '<nowiki>' tags, leaving
 885+ // us with what we need
 886+ $form_def = "__NOEDITSECTION__" . strtr( $form_def, array( '{{{' => '<nowiki>{{{', '}}}' => '}}}</nowiki>' ) );
 887+
 888+ $title = new Title();
 889+ $tmpParser = unserialize( serialize( $parser ) ); // deep clone of parser
 890+
 891+ // parse wiki-text
 892+ $output = $tmpParser->parse( $form_def, $title, $tmpParser->getOptions() );
 893+ $form_def = $output->getText();
 894+
 895+ // store in cache if allowed
 896+ if ( $sfgCacheFormDefinitions && $form_id !== null ) {
 897+
 898+ if ( $output->getCacheTime() == -1 ) {
 899+ wfDebug( "Caching disabled for formdefinition $cachekey\n" );
 900+ self::purgeCache( $form_article );
 901+ } else {
 902+ wfGetMainCache()->add( $cachekey, $form_def );
 903+ wfDebug( "Cached formdefinition $cachekey\n" );
 904+ }
 905+
 906+ }
 907+
 908+ return $form_def;
 909+ }
 910+
 911+ /**
 912+ * Deletes the formdefinition associated with the given wikipage from the
 913+ * main cache.
 914+ *
 915+ * @param Page $wikipage
 916+ * @return Bool
 917+ */
 918+ public static function purgeCache ( &$wikipage ) {
 919+
 920+ if ( $wikipage->getTitle()->getNamespace() == SF_NS_FORM ) {
 921+
 922+ $id = $wikipage->getId();
 923+ $key = wfMemcKey('ext.SemanticForms.formdefinition', $id);
 924+
 925+ if ( wfGetMainCache()->delete($key) ) {
 926+ wfDebug( "Deleted cached formdefinition $key.\n" );
 927+ }
 928+ }
 929+
 930+ return true;
 931+ }
837932 }

Follow-up revisions

RevisionCommit summaryAuthorDate
r103212Minor fix to r102726 - changed "formdefinition" to "form definition"yaron19:22, 15 November 2011
r103234followup rr102726: Use optionsHash to create cache keyfoxtrott21:55, 15 November 2011

Comments

#Comment by Nikerabbit (talk | contribs)   10:43, 15 November 2011

Shouldn't this use ParserOptions::optionsHash? Otherwise if the form uses stuff like ⧼{{{1}}}⧽, it will be cached with the settings the first user uses?

#Comment by F.trott (talk | contribs)   10:53, 15 November 2011

Good point, will include it.

#Comment by Nikerabbit (talk | contribs)   10:55, 15 November 2011

That should read {{int:}}. Anyway, optionsHash() was introduced in 1.17, so you might need to check for that and use User::getPageRenderingHash for older versions.

#Comment by F.trott (talk | contribs)   22:11, 15 November 2011

Fixed in r103234 and r103241.

Status & tagging log