r83325 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r83324‎ | r83325 | r83326 >
Date:20:30, 5 March 2011
Author:jeroendedauw
Status:deferred
Tags:
Comment:
added auto cache invalidation when adding, moving or deleting subpages
Modified paths:
  • /trunk/extensions/SubPageList/SubPageList.hooks.php (added) (history)
  • /trunk/extensions/SubPageList/SubPageList.php (modified) (history)
  • /trunk/extensions/SubPageList/SubPageList.settings.php (modified) (history)

Diff [purge]

Index: trunk/extensions/SubPageList/SubPageList.settings.php
@@ -20,3 +20,7 @@
2121 die( 'Not an entry point.' );
2222 }
2323
 24+# Automatically invalidate the cache of "base pages" when creating, moving or deleting a subpage?
 25+# This covers most cases where people expect automatisc refresh of the sub page list.
 26+# However note that this will not update lists displaying subpages from pages different then themselves.
 27+$egSPLAutorefresh = false;
Index: trunk/extensions/SubPageList/SubPageList.php
@@ -56,6 +56,7 @@
5757 $wgExtensionMessagesFiles['SubPageList'] = $egSPLIP . '/SubPageList.i18n.php';
5858
5959 $wgAutoloadClasses['SubPageList'] = $egSPLIP . '/SubPageList.class.php';
 60+$wgAutoloadClasses['SPLHooks'] = $egSPLIP . '/SubPageList.hooks.php';
6061
6162 if ( version_compare( $wgVersion, '1.16alpha', '>=' ) ) {
6263 $wgExtensionMessagesFiles['SubPageListMagic'] = $egSPLIP . '/SubPageList.i18n.magic.php';
@@ -64,6 +65,10 @@
6566 $wgHooks['ParserFirstCallInit'][] = 'SubPageList::staticInit';
6667 $wgHooks['LanguageGetMagic'][] = 'SubPageList::staticMagic';
6768
 69+$wgHooks['ArticleInsertComplete'][] = 'SPLHooks::onArticleInsertComplete';
 70+$wgHooks['ArticleDeleteComplete'][] = 'SPLHooks::onArticleDeleteComplete';
 71+$wgHooks['TitleMoveComplete'][] = 'SPLHooks::onTitleMoveComplete';
 72+
6873 $wgExtensionFunctions[] = 'efSPLSetup';
6974
7075 /**
Index: trunk/extensions/SubPageList/SubPageList.hooks.php
@@ -0,0 +1,131 @@
 2+<?php
 3+
 4+/**
 5+ * Static class for hooks handled by the SubPageList extension.
 6+ *
 7+ * @since 0.3
 8+ *
 9+ * @file SubPageList.hooks.php
 10+ * @ingroup SubPageList
 11+ *
 12+ * @licence GNU GPL v3
 13+ * @author Jeroen De Dauw < jeroendedauw@gmail.com >
 14+ */
 15+final class SPLHooks {
 16+
 17+ /**
 18+ * Occurs after a new article has been created.
 19+ * https://secure.wikimedia.org/wikipedia/mediawiki/wiki/Manual:Hooks/ArticleInsertComplete
 20+ *
 21+ * @since 0.3
 22+ *
 23+ * @return true
 24+ */
 25+ public static function onArticleInsertComplete(
 26+ Article &$article, User &$user, $text, $summary, $minoredit,
 27+ $watchthis, $sectionanchor, &$flags, Revision $revision ) {
 28+
 29+ self::invalidateBasePages( $article->getTitle() );
 30+
 31+ return true;
 32+ }
 33+
 34+ /**
 35+ * Occurs after the delete article request has been processed.
 36+ * https://secure.wikimedia.org/wikipedia/mediawiki/wiki/Manual:Hooks/ArticleDeleteComplete
 37+ *
 38+ * @since 0.3
 39+ *
 40+ * @return true
 41+ */
 42+ public static function onArticleDeleteComplete( Article &$article, User &$user, $reason, $id ) {
 43+ self::invalidateBasePages( $article->getTitle() );
 44+
 45+ return true;
 46+ }
 47+
 48+ /**
 49+ * Occurs whenever a request to move an article is completed.
 50+ * https://secure.wikimedia.org/wikipedia/mediawiki/wiki/Manual:Hooks/TitleMoveComplete
 51+ *
 52+ * @since 0.3
 53+ *
 54+ * @return true
 55+ */
 56+ public static function onTitleMoveComplete( Title &$title, Title &$newtitle, User &$user, $oldid, $newid ) {
 57+ self::invalidateBasePages( $title );
 58+ self::invalidateBasePages( $newtitle );
 59+
 60+ return true;
 61+ }
 62+
 63+ /**
 64+ * Invalidate the base pages for this title, so that any SubPageList
 65+ * there gets refreshed after doing a subpage delete, move or creation.
 66+ *
 67+ * @since 0.3
 68+ *
 69+ * @param Title $title
 70+ */
 71+ protected static function invalidateBasePages( Title $title ) {
 72+ global $egSPLAutorefresh;
 73+
 74+ if ( !$egSPLAutorefresh ) {
 75+ return;
 76+ }
 77+
 78+ $slashPosition = strpos( $title->getDBkey(), '/' );
 79+
 80+ if ( $slashPosition !== false ) {
 81+ $baseTitleText = substr( $title->getDBkey(), 0, $slashPosition );
 82+
 83+ $titleArray = self::getBaseSubPages(
 84+ $baseTitleText,
 85+ $title->getNamespace()
 86+ );
 87+
 88+ foreach ( $titleArray as $parentTitle ) {
 89+ // No point in invalidating the page itself
 90+ if ( $parentTitle->getArticleID() != $title->getArticleID() ) {
 91+ $parentTitle->invalidateCache();
 92+ }
 93+ }
 94+
 95+ $baseTitle = Title::newFromText( $baseTitleText, $title->getNamespace() );
 96+ if ( $baseTitle->getArticleID() != $title->getArticleID() ) {
 97+ $baseTitle->invalidateCache();
 98+ }
 99+ }
 100+ }
 101+
 102+ /**
 103+ * Returns a title iterator with all the subpages of the base page
 104+ * for the provided title. This will include the provided title itself,
 105+ * unless the provided title is a base page.
 106+ *
 107+ * @since 0.3
 108+ *
 109+ * @param string $baseTitle
 110+ * @param integer $ns
 111+ *
 112+ * @return TitleArray
 113+ */
 114+ protected static function getBaseSubPages( $baseTitle, $ns ) {
 115+ $dbr = wfGetDB( DB_SLAVE );
 116+
 117+ $titleArray = TitleArray::newFromResult(
 118+ $dbr->select( 'page',
 119+ array( 'page_id', 'page_namespace', 'page_title', 'page_is_redirect' ),
 120+ array(
 121+ 'page_namespace' => $ns,
 122+ 'page_title' => $dbr->buildLike( $baseTitle . '/', $dbr->anyString() )
 123+ ),
 124+ __METHOD__,
 125+ array( 'LIMIT' => 500 )
 126+ )
 127+ );
 128+
 129+ return $titleArray;
 130+ }
 131+
 132+}
\ No newline at end of file
Property changes on: trunk/extensions/SubPageList/SubPageList.hooks.php
___________________________________________________________________
Added: svn:eol-style
1133 + native

Status & tagging log