r43339 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r43338‎ | r43339 | r43340 >
Date:02:55, 10 November 2008
Author:werdna
Status:old
Tags:
Comment:
* Add the ConfigurationCache object - for saving memcached hits when retrieving the same sets of data every pageview.
* Allow logo changing via Special:SetLogo. Doesn't currently have any database interaction (just memcached) because I want to talk it over with Brion first.
Modified paths:
  • /branches/on_wiki_configuration/includes/AutoLoader.php (modified) (history)
  • /branches/on_wiki_configuration/includes/ConfigurationCache.php (added) (history)
  • /branches/on_wiki_configuration/includes/Exception.php (modified) (history)
  • /branches/on_wiki_configuration/includes/GlobalFunctions.php (modified) (history)
  • /branches/on_wiki_configuration/includes/RightsManager.php (modified) (history)
  • /branches/on_wiki_configuration/includes/Setup.php (modified) (history)
  • /branches/on_wiki_configuration/includes/Skin.php (modified) (history)
  • /branches/on_wiki_configuration/includes/SkinTemplate.php (modified) (history)
  • /branches/on_wiki_configuration/includes/SpecialPage.php (modified) (history)
  • /branches/on_wiki_configuration/includes/User.php (modified) (history)
  • /branches/on_wiki_configuration/includes/specials/SpecialSetLogo.php (added) (history)
  • /branches/on_wiki_configuration/languages/messages/MessagesEn.php (modified) (history)

Diff [purge]

Index: branches/on_wiki_configuration/includes/User.php
@@ -168,6 +168,8 @@
169169 'upload',
170170 'upload_by_url',
171171 'userrights',
 172+ 'setlogo',
 173+ 'grouprights'
172174 );
173175 /**
174176 * \string Cached results of getAllRights()
Index: branches/on_wiki_configuration/includes/GlobalFunctions.php
@@ -2969,3 +2969,27 @@
29702970 $name = preg_replace ( "/[^".Title::legalChars()."]|:/", '-', $name );
29712971 return $name;
29722972 }
 2973+
 2974+/**
 2975+ * Gets the path to the site logo.
 2976+ */
 2977+function wfGetLogoPath( $checkDB = true ) {
 2978+ ## In order of precedence:
 2979+ ## Configuration cache, database, $wgLogo, default location.
 2980+
 2981+ if ( $filename = ConfigurationCache::get( 'logo' ) ) {
 2982+ ## They specified an image name.
 2983+ $logo_file = wfFindFile( $filename );
 2984+ return $logo_file->getThumbnail(130, 130)->url;
 2985+ }
 2986+
 2987+ global $wgLogo;
 2988+
 2989+ if ($wgLogo) {
 2990+ return $wgLogo;
 2991+ }
 2992+
 2993+ global $wgStylePath;
 2994+
 2995+ return "$wgStylePath/common/images/wiki.png";
 2996+}
\ No newline at end of file
Index: branches/on_wiki_configuration/includes/ConfigurationCache.php
@@ -0,0 +1,56 @@
 2+<?php
 3+
 4+if (!defined('MEDIAWIKI'))
 5+ die();
 6+
 7+## Simple class for caching configuration data which needs to be retrieved per-pageview.
 8+
 9+class ConfigurationCache {
 10+ static $data = false;
 11+ static $dirty = false;
 12+
 13+ static function load() {
 14+ if (self::$data !== false) {
 15+ return;
 16+ }
 17+
 18+ global $wgMemc;
 19+
 20+ self::$data = $wgMemc->get( wfMemcKey( 'configuration-cache' ) );
 21+ self::$dirty = false;
 22+
 23+ if ( !is_array( self::$data ) ) {
 24+ self::$data = array();
 25+ }
 26+ }
 27+
 28+ static function get( $key ) {
 29+ self::load();
 30+
 31+ return @self::$data[$key];
 32+ }
 33+
 34+ static function set( $key, $value ) {
 35+ self::load();
 36+
 37+ self::$data[$key] = $value;
 38+ self::$dirty = true;
 39+ }
 40+
 41+ static function delete( $key ) {
 42+ self::load();
 43+
 44+ unset( self::$data[$key] );
 45+ self::$dirty = true;
 46+ }
 47+
 48+ static function save( ) {
 49+ if (!self::$dirty) {
 50+ return;
 51+ }
 52+
 53+ global $wgMemc;
 54+
 55+ $wgMemc->set( wfMemcKey( 'configuration-cache' ), self::$data, 86400 );
 56+ }
 57+}
\ No newline at end of file
Index: branches/on_wiki_configuration/includes/Setup.php
@@ -42,8 +42,6 @@
4343 if( $wgStylePath === false ) $wgStylePath = "$wgScriptPath/skins";
4444 if( $wgStyleDirectory === false) $wgStyleDirectory = "$IP/skins";
4545
46 -if( $wgLogo === false ) $wgLogo = "$wgStylePath/common/images/wiki.png";
47 -
4846 if( $wgUploadPath === false ) $wgUploadPath = "$wgScriptPath/images";
4947 if( $wgUploadDirectory === false ) $wgUploadDirectory = "$IP/images";
5048
Index: branches/on_wiki_configuration/includes/AutoLoader.php
@@ -30,6 +30,7 @@
3131 'ChangesFeed' => 'includes/ChangesFeed.php',
3232 'ChannelFeed' => 'includes/Feed.php',
3333 'ConcatenatedGzipHistoryBlob' => 'includes/HistoryBlob.php',
 34+ 'ConfigurationCache' => 'includes/ConfigurationCache.php',
3435 'ConstantDependency' => 'includes/CacheDependency.php',
3536 'CreativeCommonsRdf' => 'includes/Metadata.php',
3637 'Credits' => 'includes/Credits.php',
@@ -485,6 +486,7 @@
486487 'SpecialBookSources' => 'includes/specials/SpecialBooksources.php',
487488 'SpecialGroupRights' => 'includes/specials/SpecialGroupRights.php',
488489 'SpecialListGroupRights' => 'includes/specials/SpecialListgrouprights.php',
 490+ 'SpecialSetLogo' => 'includes/specials/SpecialSetLogo.php',
489491 'SpecialMostlinkedtemplates' => 'includes/specials/SpecialMostlinkedtemplates.php',
490492 'SpecialPrefixindex' => 'includes/specials/SpecialPrefixindex.php',
491493 'SpecialRandomredirect' => 'includes/specials/SpecialRandomredirect.php',
Index: branches/on_wiki_configuration/includes/SkinTemplate.php
@@ -138,7 +138,7 @@
139139 global $wgScript, $wgStylePath, $wgContLanguageCode;
140140 global $wgMimeType, $wgJsMimeType, $wgOutputEncoding, $wgRequest;
141141 global $wgXhtmlDefaultNamespace, $wgXhtmlNamespaces;
142 - global $wgDisableCounters, $wgLogo, $action, $wgFeedClasses, $wgHideInterlanguageLinks;
 142+ global $wgDisableCounters, $action, $wgFeedClasses, $wgHideInterlanguageLinks;
143143 global $wgMaxCredits, $wgShowCreditsIfMax;
144144 global $wgPageShowWatchingUsers;
145145 global $wgUseTrackbacks, $wgUseSiteJs;
@@ -263,7 +263,7 @@
264264 $tpl->setRef( 'articlepath', $wgArticlePath );
265265 $tpl->setRef( 'scriptpath', $wgScriptPath );
266266 $tpl->setRef( 'serverurl', $wgServer );
267 - $tpl->setRef( 'logopath', $wgLogo );
 267+ $tpl->setRef( 'logopath', wfGetLogoPath() );
268268 $tpl->setRef( "lang", $wgContLanguageCode );
269269 $tpl->set( 'dir', $wgContLang->isRTL() ? "rtl" : "ltr" );
270270 $tpl->set( 'rtl', $wgContLang->isRTL() );
Index: branches/on_wiki_configuration/includes/specials/SpecialSetLogo.php
@@ -0,0 +1,104 @@
 2+<?php
 3+if (!defined('MEDIAWIKI'))
 4+ die();
 5+
 6+class SpecialSetLogo extends SpecialPage {
 7+ function __construct( ) {
 8+ parent::__construct( 'SetLogo', 'setlogo' );
 9+ }
 10+
 11+ function execute() {
 12+ global $wgUser, $wgOut;
 13+
 14+ $sk = $wgUser->getSkin();
 15+
 16+ if (!$this->userCanExecute($wgUser)) {
 17+ $this->displayRestrictionError();
 18+ return;
 19+ }
 20+
 21+ $wgOut->setPageTitle( wfMsg( 'setlogo' ) );
 22+
 23+ if ( $this->trySubmit() ) {
 24+ return;
 25+ }
 26+
 27+ if (!$this->mLogo) {
 28+ $this->mLogo = ConfigurationCache::get( 'logo' );
 29+ }
 30+
 31+ $wgOut->addWikiMsg( 'setlogo-intro' );
 32+
 33+ ## Build form
 34+ $fields = array( 'setlogo-file' => wfInput( 'wpNewLogo', 45, $this->mLogo ) );
 35+ $form = Xml::buildForm( $fields, 'setlogo-preview-button' );
 36+ $form .= Xml::hidden( 'title', $this->getTitle()->getPrefixedText() );
 37+ $form .= Xml::hidden( 'action', 'preview' );
 38+ $form = Xml::tags( 'form', array( 'action' => $this->getTitle()->getLocalURL(), 'method' => 'post' ), $form );
 39+ $form = Xml::fieldset( wfMsg( 'setlogo-fieldset' ), $form );
 40+
 41+ $wgOut->addHTML( $form );
 42+ }
 43+
 44+ function trySubmit() {
 45+ global $wgRequest, $wgUser, $wgOut;
 46+
 47+ $sk = $wgUser->getSkin();
 48+
 49+ $action = $wgRequest->getVal( 'action' );
 50+ $this->mLogo = $selection = $wgRequest->getText( 'wpNewLogo' );
 51+ $tokenOK = $wgUser->matchEditToken( $wgRequest->getVal( 'wpEditToken' ), 'setlogo', $selection );
 52+
 53+ if ($action == 'success') {
 54+ ## We've successfully set the logo
 55+ $wgOut->addWikiMsg( 'setlogo-success' );
 56+ return true;
 57+ }
 58+
 59+ ## Sanity check on $selection
 60+ if (!$selection) {
 61+ return false;
 62+ }
 63+
 64+ $file = wfFindFile( $selection );
 65+
 66+ if ( !$file || !$file->exists() ) {
 67+ $error = wfMsgExt( 'setlogo-notfound', array( 'parseinline' ) );
 68+ $error = Xml::tags( 'div', array( 'class' => 'error' ), $error );
 69+
 70+ $wgOut->addHTML( $error );
 71+ return false;
 72+ }
 73+
 74+ if ($action == 'set' && $tokenOK ) {
 75+ ## The file exists, we've confirmed it properly (evidenced by the edit token), etc. Everything seems sweet.
 76+ ConfigurationCache::set( 'logo', $selection );
 77+ ConfigurationCache::save();
 78+
 79+ ## Redirect them -- we don't want to repeatedly purge the cache.
 80+ $wgOut->redirect( $this->getTitle()->getFullURL( 'action=success' ) );
 81+ return true;
 82+ } elseif ( $action == 'preview' || $action == 'set' ) {
 83+ $wgOut->addWikiMsg( 'setlogo-confirm' );
 84+ $title = Title::makeTitle( NS_IMAGE, $selection );
 85+
 86+ $display = $sk->makeImageLink2( $title, $file,
 87+ array('thumbnail' => 1, 'caption' => wfMsg('setlogo-confirm-caption'), 'align' => 'center' ),
 88+ array( 'width' => 130, 'height' => 130 )
 89+ );
 90+
 91+ $wgOut->addHTML( $display );
 92+
 93+ $form = Xml::hidden( 'wpNewLogo', $selection );
 94+ $form .= Xml::hidden( 'wpEditToken', $wgUser->editToken( 'setlogo', $selection ) );
 95+ $form .= Xml::hidden( 'title', $this->getTitle()->getPrefixedText() );
 96+ $form .= Xml::hidden( 'action', 'set' );
 97+ $form .= Xml::submitButton( wfMsg( 'setlogo-confirm-button' ) );
 98+ $form = Xml::tags( 'form', array( 'method' => 'post', 'action' => $this->getTitle()->getFullURL(), 'class' => 'mw-setlogo-confirm' ), $form );
 99+
 100+ $wgOut->addHTML( $form );
 101+
 102+ return true;
 103+ }
 104+ }
 105+}
\ No newline at end of file
Index: branches/on_wiki_configuration/includes/RightsManager.php
@@ -541,14 +541,8 @@
542542 function loadGroupPermissions( ) {
543543 static $groupPerms = null;
544544
545 - // In-process caching...
546 - if ( is_array($groupPerms) ) {
547 - return $groupPerms;
548 - }
549 -
550 - // Memcached caching
551 - global $wgMemc;
552 - $groupPerms = $wgMemc->get( wfMemcKey( 'grouprights' ) );
 545+ // Caching...
 546+ $groupPerms = ConfigurationCache::get( 'grouprights' );
553547 if ( is_array( $groupPerms ) ) {
554548 return $groupPerms;
555549 }
@@ -564,7 +558,8 @@
565559 $groupPerms[$row->gr_group][$row->gr_right] = (bool)$row->gr_enabled;
566560 }
567561
568 - $wgMemc->set( wfMemcKey( 'grouprights' ), $groupPerms );
 562+ ConfigurationCache::set( 'grouprights', $groupPerms );
 563+ ConfigurationCache::save();
569564
570565 return $groupPerms;
571566 }
@@ -595,8 +590,7 @@
596591 return $changeableGroups;
597592 }
598593
599 - global $wgMemc;
600 - $changeableGroups = $wgMemc->get( wfMemcKey( 'changeablegroups' ) );
 594+ $changeableGroups = ConfigurationCache::get( 'changeablegroups' );
601595 if ( is_array( $changeableGroups ) ) {
602596 return $changeableGroups;
603597 }
@@ -627,7 +621,8 @@
628622 $changeableGroups[$group] = array_merge_recursive( $groupTemplate, $changeableGroups[$group] );
629623 }
630624
631 - $wgMemc->set( wfMemcKey('changeablegroups'), $changeableGroups );
 625+ ConfigurationCache::set( 'changeablegroups', $changeableGroups );
 626+ ConfigurationCache::save();
632627
633628 return $changeableGroups;
634629 }
@@ -754,11 +749,10 @@
755750 $user->invalidateCache();
756751 }
757752
758 - function invalidateGroupCache( $group ) {
759 - global $wgMemc;
760 -
761 - $wgMemc->delete( wfMemcKey( 'changeablegroups' ) );
762 - $wgMemc->delete( wfMemcKey( 'grouprights' ) );
 753+ function invalidateGroupCache( $group ) {
 754+ ConfigurationCache::delete( 'changeablegroups' );
 755+ ConfigurationCache::delete( 'grouprights' );
 756+ ConfigurationCache::save();
763757 }
764758
765759 function invalidateUserCache( $user ) {
Index: branches/on_wiki_configuration/includes/Skin.php
@@ -676,8 +676,7 @@
677677 * URL to the logo
678678 */
679679 function getLogo() {
680 - global $wgLogo;
681 - return $wgLogo;
 680+ return wfGetLogoPath();
682681 }
683682
684683 /**
Index: branches/on_wiki_configuration/includes/SpecialPage.php
@@ -159,6 +159,7 @@
160160 'Randomredirect' => 'SpecialRandomredirect',
161161 'Withoutinterwiki' => array( 'SpecialPage', 'Withoutinterwiki' ),
162162 'Filepath' => array( 'SpecialPage', 'Filepath' ),
 163+ 'SetLogo' => 'SpecialSetLogo',
163164
164165 'Mypage' => array( 'SpecialMypage' ),
165166 'Mytalk' => array( 'SpecialMytalk' ),
Index: branches/on_wiki_configuration/includes/Exception.php
@@ -189,7 +189,9 @@
190190 * $wgOut to output the exception.
191191 */
192192 function htmlHeader() {
193 - global $wgLogo, $wgSitename, $wgOutputEncoding;
 193+ global $wgSitename, $wgOutputEncoding;
 194+
 195+ $logo = wfGetLogoPath( 'skipdb' );
194196
195197 if ( !headers_sent() ) {
196198 header( 'HTTP/1.0 500 Internal Server Error' );
@@ -204,7 +206,7 @@
205207 <title>$title</title>
206208 </head>
207209 <body>
208 - <h1><img src='$wgLogo' style='float:left;margin-right:1em' alt=''>$title</h1>
 210+ <h1><img src='$logo' style='float:left;margin-right:1em' alt=''>$title</h1>
209211 ";
210212 }
211213
Index: branches/on_wiki_configuration/languages/messages/MessagesEn.php
@@ -1649,6 +1649,7 @@
16501650 'right-userrights-interwiki' => 'Edit user rights of users on other wikis',
16511651 'right-siteadmin' => 'Lock and unlock the database',
16521652 'right-grouprights' => 'Edit permissions assigned to local groups',
 1653+'right-setlogo' => 'Change the site logo',
16531654
16541655 # User rights log
16551656 'rightslog' => 'User rights log',
@@ -3777,4 +3778,15 @@
37783779
37793780 #Put all regex fragments above this line. Leave this line exactly as it is</pre>',
37803781
 3782+# Special:SetLogo
 3783+'setlogo' => 'Set the site logo',
 3784+'setlogo-intro' => 'You can use this form to set the site logo.',
 3785+'setlogo-confirm' => 'Please click <tt>confirm logo selection</tt> to confirm that you wish to set the site logo to the image shown below.',
 3786+'setlogo-confirm-caption' => 'The new site logo',
 3787+'setlogo-confirm-button' => 'Confirm logo selection',
 3788+'setlogo-notfound' => "The image you selected doesn't seem to exist.",
 3789+'setlogo-file' => "Image to use as logo:<br/>''(excluding namespace)''",
 3790+'setlogo-preview-button' => 'Preview change',
 3791+'setlogo-fieldset' => 'Set the site logo',
 3792+'setlogo-success' => 'You have successfully set the site logo',
37813793 );

Status & tagging log