From: Alexandre Emsenhuber Date: Sat, 12 Feb 2011 21:24:05 +0000 (+0000) Subject: Moved wfGetSiteNotice(), wfGetNamespaceNotice() and wfGetCachedNotice() to Skin call... X-Git-Tag: 1.31.0-rc.0~32018 X-Git-Url: http://git.cyclocoop.org/%22.%28%24lien.?a=commitdiff_plain;h=b2a0cc74d509a75cd808a255fd60d2913c78ab3f;p=lhc%2Fweb%2Fwiklou.git Moved wfGetSiteNotice(), wfGetNamespaceNotice() and wfGetCachedNotice() to Skin call to allow passing the Skin object to the SiteNoticeBefore and SiteNoticeAfter hooks. I didn't left compatibility functions since there's no other call to these functions in core or extensions. --- diff --git a/docs/hooks.txt b/docs/hooks.txt index 2f79c83755..3e1d4cae3d 100644 --- a/docs/hooks.txt +++ b/docs/hooks.txt @@ -1459,11 +1459,13 @@ $page: The SpecialSearch object. 'SiteNoticeBefore': Before the sitenotice/anonnotice is composed &$siteNotice: HTML returned as the sitenotice +$skin: Skin object Return true to allow the normal method of notice selection/rendering to work, or change the value of $siteNotice and return false to alter it. 'SiteNoticeAfter': After the sitenotice/anonnotice is composed &$siteNotice: HTML sitenotice +$skin: Skin object Alter the contents of $siteNotice to add to/alter the sitenotice/anonnotice. 'SkinAfterBottomScripts': At the end of Skin::bottomScripts() diff --git a/includes/GlobalFunctions.php b/includes/GlobalFunctions.php index 71847cc0d3..e88afdad36 100644 --- a/includes/GlobalFunctions.php +++ b/includes/GlobalFunctions.php @@ -1996,108 +1996,6 @@ function swap( &$x, &$y ) { $y = $z; } -function wfGetCachedNotice( $name ) { - global $wgOut, $wgRenderHashAppend, $parserMemc; - $fname = 'wfGetCachedNotice'; - wfProfileIn( $fname ); - - $needParse = false; - - if( $name === 'default' ) { - // special case - global $wgSiteNotice; - $notice = $wgSiteNotice; - if( empty( $notice ) ) { - wfProfileOut( $fname ); - return false; - } - } else { - $msg = wfMessage( $name )->inContentLanguage(); - if( $msg->isDisabled() ) { - wfProfileOut( $fname ); - return( false ); - } - $notice = $msg->plain(); - } - - // Use the extra hash appender to let eg SSL variants separately cache. - $key = wfMemcKey( $name . $wgRenderHashAppend ); - $cachedNotice = $parserMemc->get( $key ); - if( is_array( $cachedNotice ) ) { - if( md5( $notice ) == $cachedNotice['hash'] ) { - $notice = $cachedNotice['html']; - } else { - $needParse = true; - } - } else { - $needParse = true; - } - - if( $needParse ) { - if( is_object( $wgOut ) ) { - $parsed = $wgOut->parse( $notice ); - $parserMemc->set( $key, array( 'html' => $parsed, 'hash' => md5( $notice ) ), 600 ); - $notice = $parsed; - } else { - wfDebug( 'wfGetCachedNotice called for ' . $name . ' with no $wgOut available' . "\n" ); - $notice = ''; - } - } - $notice = '
' .$notice . '
'; - wfProfileOut( $fname ); - return $notice; -} - -function wfGetNamespaceNotice() { - global $wgTitle; - - # Paranoia - if ( !isset( $wgTitle ) || !is_object( $wgTitle ) ) { - return ''; - } - - $fname = 'wfGetNamespaceNotice'; - wfProfileIn( $fname ); - - $key = 'namespacenotice-' . $wgTitle->getNsText(); - $namespaceNotice = wfGetCachedNotice( $key ); - if ( $namespaceNotice && substr( $namespaceNotice, 0, 7 ) != '

<' ) { - $namespaceNotice = '

' . $namespaceNotice . '
'; - } else { - $namespaceNotice = ''; - } - - wfProfileOut( $fname ); - return $namespaceNotice; -} - -function wfGetSiteNotice() { - global $wgUser; - $fname = 'wfGetSiteNotice'; - wfProfileIn( $fname ); - $siteNotice = ''; - - if( wfRunHooks( 'SiteNoticeBefore', array( &$siteNotice ) ) ) { - if( is_object( $wgUser ) && $wgUser->isLoggedIn() ) { - $siteNotice = wfGetCachedNotice( 'sitenotice' ); - } else { - $anonNotice = wfGetCachedNotice( 'anonnotice' ); - if( !$anonNotice ) { - $siteNotice = wfGetCachedNotice( 'sitenotice' ); - } else { - $siteNotice = $anonNotice; - } - } - if( !$siteNotice ) { - $siteNotice = wfGetCachedNotice( 'default' ); - } - } - - wfRunHooks( 'SiteNoticeAfter', array( &$siteNotice ) ); - wfProfileOut( $fname ); - return $siteNotice; -} - /** * BC wrapper for MimeMagic::singleton() * @deprecated No longer needed as of 1.17 (r68836). Remove in 1.19. diff --git a/includes/Skin.php b/includes/Skin.php index 587ac65882..3a671dc842 100644 --- a/includes/Skin.php +++ b/includes/Skin.php @@ -1550,4 +1550,115 @@ abstract class Skin extends Linker { return $ntl; } + + /** + * Get a cached notice + * + * @param $name String: message name, or 'default' for $wgSiteNotice + * @return String: HTML fragment + */ + private function getCachedNotice( $name ) { + global $wgOut, $wgRenderHashAppend, $parserMemc; + + wfProfileIn( __METHOD__ ); + + $needParse = false; + + if( $name === 'default' ) { + // special case + global $wgSiteNotice; + $notice = $wgSiteNotice; + if( empty( $notice ) ) { + wfProfileOut( __METHOD__ ); + return false; + } + } else { + $msg = wfMessage( $name )->inContentLanguage(); + if( $msg->isDisabled() ) { + wfProfileOut( __METHOD__ ); + return false; + } + $notice = $msg->plain(); + } + + // Use the extra hash appender to let eg SSL variants separately cache. + $key = wfMemcKey( $name . $wgRenderHashAppend ); + $cachedNotice = $parserMemc->get( $key ); + if( is_array( $cachedNotice ) ) { + if( md5( $notice ) == $cachedNotice['hash'] ) { + $notice = $cachedNotice['html']; + } else { + $needParse = true; + } + } else { + $needParse = true; + } + + if ( $needParse ) { + if( is_object( $wgOut ) ) { + $parsed = $wgOut->parse( $notice ); + $parserMemc->set( $key, array( 'html' => $parsed, 'hash' => md5( $notice ) ), 600 ); + $notice = $parsed; + } else { + wfDebug( 'wfGetCachedNotice called for ' . $name . ' with no $wgOut available' . "\n" ); + $notice = ''; + } + } + + $notice = '
' .$notice . '
'; + wfProfileOut( __METHOD__ ); + return $notice; + } + + /** + * Get a notice based on page's namespace + * + * @return String: HTML fragment + */ + function getNamespaceNotice() { + wfProfileIn( __METHOD__ ); + + $key = 'namespacenotice-' . $this->mTitle->getNsText(); + $namespaceNotice = wfGetCachedNotice( $key ); + if ( $namespaceNotice && substr( $namespaceNotice, 0, 7 ) != '

<' ) { + $namespaceNotice = '

' . $namespaceNotice . '
'; + } else { + $namespaceNotice = ''; + } + + wfProfileOut( $fname ); + return $namespaceNotice; + } + + /** + * Get the site notice + * + * @return String: HTML fragment + */ + function getSiteNotice() { + global $wgUser; + + wfProfileIn( __METHOD__ ); + $siteNotice = ''; + + if ( wfRunHooks( 'SiteNoticeBefore', array( &$siteNotice, $this ) ) ) { + if ( is_object( $wgUser ) && $wgUser->isLoggedIn() ) { + $siteNotice = $this->getCachedNotice( 'sitenotice' ); + } else { + $anonNotice = $this->getCachedNotice( 'anonnotice' ); + if ( !$anonNotice ) { + $siteNotice = $this->getCachedNotice( 'sitenotice' ); + } else { + $siteNotice = $anonNotice; + } + } + if ( !$siteNotice ) { + $siteNotice = $this->getCachedNotice( 'default' ); + } + } + + wfRunHooks( 'SiteNoticeAfter', array( &$siteNotice, $this ) ); + wfProfileOut( __METHOD__ ); + return $siteNotice; +} } diff --git a/includes/SkinLegacy.php b/includes/SkinLegacy.php index 4f93dba6ba..0f304a9cb6 100644 --- a/includes/SkinLegacy.php +++ b/includes/SkinLegacy.php @@ -134,7 +134,7 @@ class LegacyTemplate extends BaseTemplate { $s .= "\n\n\n"; $s .= "\n
\n"; - $notice = wfGetSiteNotice(); + $notice = $this->getSkin()->getSiteNotice(); if ( $notice ) { $s .= "\n
$notice
\n"; diff --git a/includes/SkinTemplate.php b/includes/SkinTemplate.php index 4704a1e534..d04a84cd61 100644 --- a/includes/SkinTemplate.php +++ b/includes/SkinTemplate.php @@ -453,7 +453,7 @@ class SkinTemplate extends Skin { } $tpl->set( 'reporttime', wfReportTime() ); - $tpl->set( 'sitenotice', wfGetSiteNotice() ); + $tpl->set( 'sitenotice', $this->getSiteNotice() ); $tpl->set( 'bottomscripts', $this->bottomScripts( $out ) ); $printfooter = "
\n" . $this->printSource() . "
\n"; diff --git a/skins/CologneBlue.php b/skins/CologneBlue.php index 0363072fd2..e157ed44a5 100644 --- a/skins/CologneBlue.php +++ b/skins/CologneBlue.php @@ -86,7 +86,7 @@ class CologneBlueTemplate extends LegacyTemplate { $s .= "\n
\n
"; - $notice = wfGetSiteNotice(); + $notice = $this->getSkin()->getSiteNotice(); if( $notice ) { $s .= "\n
$notice
\n"; } diff --git a/skins/Nostalgia.php b/skins/Nostalgia.php index 2df7ddfe3b..9e89787b49 100644 --- a/skins/Nostalgia.php +++ b/skins/Nostalgia.php @@ -37,7 +37,7 @@ class NostalgiaTemplate extends LegacyTemplate { $s .= '
'; $s .= $this->topLinks() . "\n
"; - $notice = wfGetSiteNotice(); + $notice = $this->getSkin()->getSiteNotice(); if( $notice ) { $s .= "\n
$notice
\n"; }