r114045 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r114044‎ | r114045 | r114046 >
Date:01:23, 17 March 2012
Author:jeroendedauw
Status:deferred
Tags:
Comment:
Follow up to r114044; decided upon an approach for the caching stuff and got rid of the rest
Modified paths:
  • /trunk/extensions/EducationProgram/EducationProgram.i18n.php (modified) (history)
  • /trunk/extensions/EducationProgram/specials/SpecialCachedPage.php (modified) (history)
  • /trunk/extensions/EducationProgram/specials/SpecialStudentActivity.php (modified) (history)

Diff [purge]

Index: trunk/extensions/EducationProgram/specials/SpecialStudentActivity.php
@@ -23,6 +23,8 @@
2424 $this->cacheExpiry = 600;
2525 }
2626
 27+ protected $cachedOut;
 28+
2729 /**
2830 * Main method.
2931 *
@@ -31,31 +33,13 @@
3234 * @param string $subPage
3335 */
3436 public function execute( $subPage ) {
35 - //parent::execute( $subPage );
 37+ parent::execute( $subPage );
3638
37 - if ( !is_null( $this->cacheExpiry ) ) {
38 - $cache = wfGetCache( CACHE_ANYTHING );
39 - $cacheKey = $this->getCacheKey();
40 - $cachedHTML = $cache->get( $cacheKey );
 39+ $this->displayNavigation();
4140
42 - $out = $this->getOutput();
 41+ $this->addCachedHTML( array( $this, 'displayCachedContent' ) );
4342
44 - if ( $this->getRequest()->getText( 'action' ) !== 'purge' && is_string( $cachedHTML ) ) {
45 - $html = $cachedHTML;
46 - }
47 - else {
48 - $this->displayCachedContent();
49 -
50 - $html = $out->getHTML();
51 - $cache->set( $cacheKey, $html, $this->cacheExpiry );
52 - }
53 -
54 - $out->clearHTML();
55 -
56 - $this->displayBeforeCached();
57 - $out->addHTML( $html );
58 - $this->displayAfterCached();
59 - }
 43+ $this->saveCache();
6044 }
6145
6246 protected function displayCachedContent() {
@@ -63,27 +47,25 @@
6448 wfTimestamp( TS_MW, time() - ( EPSettings::get( 'recentActivityLimit' ) ) )
6549 ) );
6650
67 - $this->displayStudentMeter( $conds );
68 - $this->displayPager( $conds );
 51+ return $this->displayStudentMeter( $conds ) .
 52+ $this->displayPager( $conds );
6953 }
7054
7155 public function displayPager( array $conds ) {
72 - $out = $this->getOutput();
73 -
7456 $pager = new EPStudentActivityPager( $this->getContext(), $conds );
7557
7658 if ( $pager->getNumRows() ) {
77 - $out->addHTML(
 59+ return
7860 $pager->getFilterControl() .
7961 $pager->getNavigationBar() .
8062 $pager->getBody() .
8163 $pager->getNavigationBar() .
82 - $pager->getMultipleItemControl()
83 - );
 64+ $pager->getMultipleItemControl();
8465 }
8566 else {
86 - $out->addHTML( $pager->getFilterControl( true ) );
87 - $out->addWikiMsg( 'ep-studentactivity-noresults' );
 67+ return $pager->getFilterControl( true )
 68+ . '<br />'
 69+ . wfMsgExt( 'ep-studentactivity-noresults', 'parseinline' );
8870 }
8971 }
9072
@@ -99,7 +81,7 @@
10082
10183 $message = $this->msg( 'ep-studentactivity-count', $studentCount )->escaped();
10284
103 - $this->getOutput()->addElement( 'img', array(
 85+ return Html::element( 'img', array(
10486 'src' => EPSettings::get( 'imageDir' ) . 'student-o-meter_morethan-' . $image . '.png',
10587 'alt' => $message,
10688 'title' => $message,
Index: trunk/extensions/EducationProgram/specials/SpecialCachedPage.php
@@ -1,29 +1,45 @@
22 <?php
33
4 -class CachedOutput {
 4+/**
 5+ * Abstract special page class with scaffolding for caching the HTML output.
 6+ *
 7+ * @since 0.1
 8+ *
 9+ * @file SpecialCachedPage.php
 10+ * @ingroup EducationProgram
 11+ *
 12+ * @licence GNU GPL v3 or later
 13+ * @author Jeroen De Dauw < jeroendedauw@gmail.com >
 14+ */
 15+abstract class SpecialCachedPage extends SpecialPage {
516
617 /**
718 * The time to live for the cache, in seconds or a unix timestamp indicating the point of expiry.
819 *
920 * @since 0.1
10 - * @var integer
 21+ * @var integer|null
1122 */
12 - protected $cacheExpiry = 3600;
 23+ protected $cacheExpiry = 300;
1324
1425 protected $cachedChunks;
1526
1627 protected $hasCached = null;
1728
18 - protected $out;
 29+ /**
 30+ * Main method.
 31+ *
 32+ * @since 0.1
 33+ *
 34+ * @param string $subPage
 35+ */
 36+ public function execute( $subPage ) {
 37+ //parent::execute( $subPage );
1938
20 - public function __construct( OutputPage $out ) {
21 - $this->out = $out;
 39+ if ( $this->getRequest()->getText( 'action' ) === 'purge' ) {
 40+ $this->hasCached = false;
 41+ }
2242 }
2343
24 - public function invalidateCache() {
25 - $this->hasCached = false;
26 - }
27 -
2844 /**
2945 * Initializes the caching.
3046 * Should be called ONCE before any of the caching functionality is used,
@@ -59,17 +75,17 @@
6076 $html = '';
6177
6278 if ( is_null( $key ) ) {
63 - $itemKey = array_flip( array_slice( $this->cachedChunks, 0, 1 ) );
 79+ $itemKey = array_keys( array_slice( $this->cachedChunks, 0, 1 ) );
6480 $itemKey = array_shift( $itemKey );
6581
66 - if ( is_integer( $itemKey ) ) {
 82+ if ( !is_integer( $itemKey ) ) {
6783 wfWarn( "Attempted to get item with non-numeric key while the next item in the queue has a key ($itemKey) in " . __METHOD__ );
6884 }
6985 elseif ( is_null( $itemKey ) ) {
7086 wfWarn( "Attempted to get an item while the queue is empty in " . __METHOD__ );
7187 }
7288 else {
73 - $html = array_shift( $key );
 89+ $html = array_shift( $this->cachedChunks );
7490 }
7591 }
7692 else {
@@ -93,13 +109,9 @@
94110 }
95111 }
96112
97 - $this->addHTML( $html );
 113+ $this->getOutput()->addHTML( $html );
98114 }
99115
100 - public function addHTML( $html ) {
101 - $this->out->addHTML( $html );
102 - }
103 -
104116 /**
105117 * Saves the HTML to the cache in case it got recomputed.
106118 * Should be called after the last time anything is added via addCachedHTML.
@@ -136,116 +148,3 @@
137149 }
138150
139151 }
140 -
141 -/**
142 - * Abstract special page class with scaffolding for caching the HTML output.
143 - *
144 - * @since 0.1
145 - *
146 - * @file SpecialCachedPage.php
147 - * @ingroup EducationProgram
148 - *
149 - * @licence GNU GPL v3 or later
150 - * @author Jeroen De Dauw < jeroendedauw@gmail.com >
151 - */
152 -abstract class SpecialCachedPage extends SpecialPage {
153 -
154 - /**
155 - * The time to live for the cache, in seconds or a unix timestamp indicating the point of expiry.
156 - *
157 - * @since 0.1
158 - * @var integer|null
159 - */
160 - protected $cacheExpiry = null;
161 -
162 - protected $cachedOutput = null;
163 -
164 - /**
165 - * Main method.
166 - *
167 - * @since 0.1
168 - *
169 - * @param string $subPage
170 - */
171 - public function execute( $subPage ) {
172 - //parent::execute( $subPage );
173 -
174 - if ( !is_null( $this->cacheExpiry ) ) {
175 - $cache = wfGetCache( CACHE_ANYTHING );
176 - $cacheKey = $this->getCacheKey();
177 - $cachedHTML = $cache->get( $cacheKey );
178 -
179 - $out = $this->getOutput();
180 -
181 - if ( $this->getRequest()->getText( 'action' ) !== 'purge' && is_string( $cachedHTML ) ) {
182 - $html = $cachedHTML;
183 - }
184 - else {
185 - $this->displayCachedContent();
186 -
187 - $html = $out->getHTML();
188 - $cache->set( $cacheKey, $html, $this->cacheExpiry );
189 - }
190 -
191 - $out->clearHTML();
192 -
193 - $this->displayBeforeCached();
194 - $out->addHTML( $html );
195 - $this->displayAfterCached();
196 - }
197 - }
198 -
199 - /**
200 - * Sets the time to live for the cache, in seconds or a unix timestamp indicating the point of expiry..
201 - *
202 - * @since 0.1
203 - *
204 - * @param integer $cacheExpiry
205 - */
206 - protected function setExpirey( $cacheExpiry ) {
207 - $this->cacheExpiry = $cacheExpiry;
208 - }
209 -
210 - /**
211 - * Returns the cache key to use to cache this page's HTML output.
212 - * Is constructed from the special page name and language code.
213 - *
214 - * @since 0.1
215 - *
216 - * @return string
217 - */
218 - protected function getCacheKey() {
219 - return wfMemcKey( $this->mName, $this->getLanguage()->getCode() );
220 - }
221 -
222 - /**
223 - * Display the cached content. Everything added to the output here
224 - * will be cached.
225 - *
226 - * @since 0.1
227 - */
228 - protected function displayCachedContent() {
229 -
230 - }
231 -
232 - /**
233 - * Display non-cached content that will be added to the final output
234 - * before the cached HTML.
235 - *
236 - * @since 0.1
237 - */
238 - protected function displayBeforeCached() {
239 -
240 - }
241 -
242 - /**
243 - * Display non-cached content that will be added to the final output
244 - * after the cached HTML.
245 - *
246 - * @since 0.1
247 - */
248 - protected function displayAfterCached() {
249 -
250 - }
251 -
252 -}
Index: trunk/extensions/EducationProgram/EducationProgram.i18n.php
@@ -777,6 +777,9 @@
778778 'epca-visible' => 'Publicly list me as Campus Ambassador',
779779
780780 // Special:StudentActivity
 781+ 'ep-studentactivity-noresults' => 'There are no students that where active in the last 24 hours :(
 782+
 783+You can find a full list of students on [[Special:Students|the student list]].',
781784 'ep-studentactivity-count' => 'There {{PLURAL:$1|is|are}} currently $1 {{PLURAL:$1|student|students}} that {{PLURAL:$1|was|where}} active in the last 24 hours.',
782785 );
783786

Follow-up revisions

RevisionCommit summaryAuthorDate
r114046Follow up to r114045; cleanup + docsjeroendedauw01:36, 17 March 2012

Past revisions this follows-up on

RevisionCommit summaryAuthorDate
r114044playing with some stuff to make partial caching of special pages easierjeroendedauw00:51, 17 March 2012

Status & tagging log