Index: trunk/phase3/includes/MessageCache.php |
— | — | @@ -7,9 +7,9 @@ |
8 | 8 | /** |
9 | 9 | * |
10 | 10 | */ |
11 | | -define( 'MSG_LOAD_TIMEOUT', 60); |
12 | | -define( 'MSG_LOCK_TIMEOUT', 10); |
13 | | -define( 'MSG_WAIT_TIMEOUT', 10); |
| 11 | +define( 'MSG_LOAD_TIMEOUT', 60 ); |
| 12 | +define( 'MSG_LOCK_TIMEOUT', 10 ); |
| 13 | +define( 'MSG_WAIT_TIMEOUT', 10 ); |
14 | 14 | define( 'MSG_CACHE_VERSION', 1 ); |
15 | 15 | |
16 | 16 | /** |
— | — | @@ -82,7 +82,7 @@ |
83 | 83 | } |
84 | 84 | |
85 | 85 | /** |
86 | | - * Destroy the signleton instance |
| 86 | + * Destroy the singleton instance |
87 | 87 | * |
88 | 88 | * @since 1.18 |
89 | 89 | */ |
— | — | @@ -100,7 +100,6 @@ |
101 | 101 | $this->mExpiry = $expiry; |
102 | 102 | } |
103 | 103 | |
104 | | - |
105 | 104 | /** |
106 | 105 | * ParserOptions is lazy initialised. |
107 | 106 | */ |
— | — | @@ -149,9 +148,9 @@ |
150 | 149 | return false; // Wrong hash |
151 | 150 | } |
152 | 151 | } else { |
153 | | - $localHash=substr(fread($file,40),8); |
154 | | - fclose($file); |
155 | | - if ($hash!=$localHash) { |
| 152 | + $localHash = substr( fread( $file, 40 ), 8 ); |
| 153 | + fclose( $file ); |
| 154 | + if ( $hash != $localHash ) { |
156 | 155 | return false; // Wrong hash |
157 | 156 | } |
158 | 157 | |
— | — | @@ -192,7 +191,7 @@ |
193 | 192 | wfMkdirParents( $wgCacheDirectory ); // might fail |
194 | 193 | |
195 | 194 | wfSuppressWarnings(); |
196 | | - $file = fopen( $tempFilename, 'w'); |
| 195 | + $file = fopen( $tempFilename, 'w' ); |
197 | 196 | wfRestoreWarnings(); |
198 | 197 | |
199 | 198 | if ( !$file ) { |
— | — | @@ -200,20 +199,20 @@ |
201 | 200 | return; |
202 | 201 | } |
203 | 202 | |
204 | | - fwrite($file,"<?php\n//$hash\n\n \$this->mCache = array("); |
| 203 | + fwrite( $file, "<?php\n//$hash\n\n \$this->mCache = array(" ); |
205 | 204 | |
206 | 205 | foreach ( $array as $key => $message ) { |
207 | | - $key = $this->escapeForScript($key); |
208 | | - $message = $this->escapeForScript($message); |
209 | | - fwrite($file, "'$key' => '$message',\n"); |
| 206 | + $key = $this->escapeForScript( $key ); |
| 207 | + $message = $this->escapeForScript( $message ); |
| 208 | + fwrite( $file, "'$key' => '$message',\n" ); |
210 | 209 | } |
211 | 210 | |
212 | | - fwrite($file,");\n?>"); |
213 | | - fclose($file); |
214 | | - rename($tempFilename, $filename); |
| 211 | + fwrite( $file, ");\n?>" ); |
| 212 | + fclose( $file); |
| 213 | + rename( $tempFilename, $filename ); |
215 | 214 | } |
216 | 215 | |
217 | | - function escapeForScript($string) { |
| 216 | + function escapeForScript( $string ) { |
218 | 217 | $string = str_replace( '\\', '\\\\', $string ); |
219 | 218 | $string = str_replace( '\'', '\\\'', $string ); |
220 | 219 | return $string; |
— | — | @@ -260,7 +259,9 @@ |
261 | 260 | } |
262 | 261 | |
263 | 262 | # Don't do double loading... |
264 | | - if ( isset($this->mLoadedLanguages[$code]) ) return true; |
| 263 | + if ( isset( $this->mLoadedLanguages[$code] ) ) { |
| 264 | + return true; |
| 265 | + } |
265 | 266 | |
266 | 267 | # 8 lines of code just to say (once) that message cache is disabled |
267 | 268 | if ( $this->mDisable ) { |
— | — | @@ -278,7 +279,6 @@ |
279 | 280 | $where = array(); # Debug info, delayed to avoid spamming debug log too much |
280 | 281 | $cacheKey = wfMemcKey( 'messages', $code ); # Key in memc for messages |
281 | 282 | |
282 | | - |
283 | 283 | # (1) local cache |
284 | 284 | # Hash of the contents is stored in memcache, to detect if local cache goes |
285 | 285 | # out of date (due to update in other thread?) |
— | — | @@ -306,14 +306,13 @@ |
307 | 307 | wfProfileOut( __METHOD__ . '-fromcache' ); |
308 | 308 | } |
309 | 309 | |
310 | | - |
311 | 310 | # (3) |
312 | 311 | # Nothing in caches... so we need create one and store it in caches |
313 | 312 | if ( !$success ) { |
314 | 313 | $where[] = 'cache is empty'; |
315 | 314 | $where[] = 'loading from database'; |
316 | 315 | |
317 | | - $this->lock($cacheKey); |
| 316 | + $this->lock( $cacheKey ); |
318 | 317 | |
319 | 318 | # Limit the concurrency of loadFromDB to a single process |
320 | 319 | # This prevents the site from going down when the cache expires |
— | — | @@ -328,7 +327,7 @@ |
329 | 328 | if ( $success ) { |
330 | 329 | $this->mMemc->delete( $statusKey ); |
331 | 330 | } else { |
332 | | - $this->mMemc->set( $statusKey, 'error', 60*5 ); |
| 331 | + $this->mMemc->set( $statusKey, 'error', 60 * 5 ); |
333 | 332 | wfDebug( "MemCached set error in MessageCache: restart memcached server!\n" ); |
334 | 333 | } |
335 | 334 | } |
— | — | @@ -357,8 +356,8 @@ |
358 | 357 | * $wgMaxMsgCacheEntrySize are assigned a special value, and are loaded |
359 | 358 | * on-demand from the database later. |
360 | 359 | * |
361 | | - * @param $code \string Language code. |
362 | | - * @return \array Loaded messages for storing in caches. |
| 360 | + * @param $code String: language code. |
| 361 | + * @return Array: loaded messages for storing in caches. |
363 | 362 | */ |
364 | 363 | function loadFromDB( $code ) { |
365 | 364 | wfProfileIn( __METHOD__ ); |
— | — | @@ -376,7 +375,9 @@ |
377 | 376 | if ( $wgAdaptiveMessageCache ) { |
378 | 377 | $mostused = $this->getMostUsedMessages(); |
379 | 378 | if ( $code !== $wgLanguageCode ) { |
380 | | - foreach ( $mostused as $key => $value ) $mostused[$key] = "$value/$code"; |
| 379 | + foreach ( $mostused as $key => $value ) { |
| 380 | + $mostused[$key] = "$value/$code"; |
| 381 | + } |
381 | 382 | } |
382 | 383 | } |
383 | 384 | |
— | — | @@ -406,9 +407,12 @@ |
407 | 408 | $smallConds[] = 'rev_text_id=old_id'; |
408 | 409 | $smallConds[] = 'page_len <= ' . intval( $wgMaxMsgCacheEntrySize ); |
409 | 410 | |
410 | | - $res = $dbr->select( array( 'page', 'revision', 'text' ), |
| 411 | + $res = $dbr->select( |
| 412 | + array( 'page', 'revision', 'text' ), |
411 | 413 | array( 'page_title', 'old_text', 'old_flags' ), |
412 | | - $smallConds, __METHOD__ . "($code)-small" ); |
| 414 | + $smallConds, |
| 415 | + __METHOD__ . "($code)-small" |
| 416 | + ); |
413 | 417 | |
414 | 418 | foreach ( $res as $row ) { |
415 | 419 | $cache[$row->page_title] = ' ' . Revision::getRevisionText( $row ); |
— | — | @@ -451,12 +455,10 @@ |
452 | 456 | # Article was deleted |
453 | 457 | $this->mCache[$code][$title] = '!NONEXISTENT'; |
454 | 458 | $this->mMemc->delete( $titleKey ); |
455 | | - |
456 | 459 | } elseif ( strlen( $text ) > $wgMaxMsgCacheEntrySize ) { |
457 | 460 | # Check for size |
458 | 461 | $this->mCache[$code][$title] = '!TOO BIG'; |
459 | 462 | $this->mMemc->set( $titleKey, ' ' . $text, $this->mExpiry ); |
460 | | - |
461 | 463 | } else { |
462 | 464 | $this->mCache[$code][$title] = ' ' . $text; |
463 | 465 | $this->mMemc->delete( $titleKey ); |
— | — | @@ -479,12 +481,12 @@ |
480 | 482 | $sidebarKey = wfMemcKey( 'sidebar', $code ); |
481 | 483 | $parserMemc->delete( $sidebarKey ); |
482 | 484 | } |
483 | | - |
| 485 | + |
484 | 486 | // Update the message in the message blob store |
485 | 487 | global $wgContLang; |
486 | 488 | MessageBlobStore::updateMessage( $wgContLang->lcfirst( $msg ) ); |
487 | 489 | |
488 | | - wfRunHooks( "MessageCacheReplace", array( $title, $text ) ); |
| 490 | + wfRunHooks( 'MessageCacheReplace', array( $title, $text ) ); |
489 | 491 | |
490 | 492 | wfProfileOut( __METHOD__ ); |
491 | 493 | } |
— | — | @@ -530,16 +532,16 @@ |
531 | 533 | * |
532 | 534 | * @return Boolean: success |
533 | 535 | */ |
534 | | - function lock($key) { |
| 536 | + function lock( $key ) { |
535 | 537 | $lockKey = $key . ':lock'; |
536 | | - for ($i=0; $i < MSG_WAIT_TIMEOUT && !$this->mMemc->add( $lockKey, 1, MSG_LOCK_TIMEOUT ); $i++ ) { |
537 | | - sleep(1); |
| 538 | + for ( $i = 0; $i < MSG_WAIT_TIMEOUT && !$this->mMemc->add( $lockKey, 1, MSG_LOCK_TIMEOUT ); $i++ ) { |
| 539 | + sleep( 1 ); |
538 | 540 | } |
539 | 541 | |
540 | 542 | return $i >= MSG_WAIT_TIMEOUT; |
541 | 543 | } |
542 | 544 | |
543 | | - function unlock($key) { |
| 545 | + function unlock( $key ) { |
544 | 546 | $lockKey = $key . ':lock'; |
545 | 547 | $this->mMemc->delete( $lockKey ); |
546 | 548 | } |
— | — | @@ -565,7 +567,7 @@ |
566 | 568 | global $wgLanguageCode, $wgContLang; |
567 | 569 | |
568 | 570 | if ( !is_string( $key ) ) { |
569 | | - throw new MWException( "Non-string key given" ); |
| 571 | + throw new MWException( 'Non-string key given' ); |
570 | 572 | } |
571 | 573 | |
572 | 574 | if ( strval( $key ) === '' ) { |
— | — | @@ -594,15 +596,17 @@ |
595 | 597 | $uckey = $wgContLang->ucfirst( $lckey ); |
596 | 598 | } |
597 | 599 | |
598 | | - /* Record each message request, but only once per request. |
| 600 | + /** |
| 601 | + * Record each message request, but only once per request. |
599 | 602 | * This information is not used unless $wgAdaptiveMessageCache |
600 | | - * is enabled. */ |
| 603 | + * is enabled. |
| 604 | + */ |
601 | 605 | $this->mRequestedMessages[$uckey] = true; |
602 | 606 | |
603 | 607 | # Try the MediaWiki namespace |
604 | 608 | if( !$this->mDisable && $useDB ) { |
605 | 609 | $title = $uckey; |
606 | | - if(!$isFullKey && ( $langcode != $wgLanguageCode ) ) { |
| 610 | + if( !$isFullKey && ( $langcode != $wgLanguageCode ) ) { |
607 | 611 | $title .= '/' . $langcode; |
608 | 612 | } |
609 | 613 | $message = $this->getMsgFromNamespace( $title, $langcode ); |
— | — | @@ -631,9 +635,9 @@ |
632 | 636 | } |
633 | 637 | |
634 | 638 | # Is this a custom message? Try the default language in the db... |
635 | | - if( ($message === false || $message === '-' ) && |
| 639 | + if( ( $message === false || $message === '-' ) && |
636 | 640 | !$this->mDisable && $useDB && |
637 | | - !$isFullKey && ($langcode != $wgLanguageCode) ) { |
| 641 | + !$isFullKey && ( $langcode != $wgLanguageCode ) ) { |
638 | 642 | $message = $this->getMsgFromNamespace( $uckey, $wgLanguageCode ); |
639 | 643 | } |
640 | 644 | |
— | — | @@ -678,12 +682,13 @@ |
679 | 683 | } else { |
680 | 684 | // XXX: This is not cached in process cache, should it? |
681 | 685 | $message = false; |
682 | | - wfRunHooks('MessagesPreLoad', array( $title, &$message ) ); |
| 686 | + wfRunHooks( 'MessagesPreLoad', array( $title, &$message ) ); |
683 | 687 | if ( $message !== false ) { |
684 | 688 | return $message; |
685 | 689 | } |
686 | 690 | |
687 | | - /* If message cache is in normal mode, it is guaranteed |
| 691 | + /** |
| 692 | + * If message cache is in normal mode, it is guaranteed |
688 | 693 | * (except bugs) that there is always entry (or placeholder) |
689 | 694 | * in the cache if message exists. Thus we can do minor |
690 | 695 | * performance improvement and return false early. |
— | — | @@ -754,9 +759,14 @@ |
755 | 760 | return $message; |
756 | 761 | } |
757 | 762 | |
758 | | - function disable() { $this->mDisable = true; } |
759 | | - function enable() { $this->mDisable = false; } |
| 763 | + function disable() { |
| 764 | + $this->mDisable = true; |
| 765 | + } |
760 | 766 | |
| 767 | + function enable() { |
| 768 | + $this->mDisable = false; |
| 769 | + } |
| 770 | + |
761 | 771 | /** |
762 | 772 | * Clear all stored messages. Mainly used after a mass rebuild. |
763 | 773 | */ |
— | — | @@ -774,13 +784,15 @@ |
775 | 785 | public function figureMessage( $key ) { |
776 | 786 | global $wgLanguageCode; |
777 | 787 | $pieces = explode( '/', $key ); |
778 | | - if( count( $pieces ) < 2 ) |
| 788 | + if( count( $pieces ) < 2 ) { |
779 | 789 | return array( $key, $wgLanguageCode ); |
| 790 | + } |
780 | 791 | |
781 | 792 | $lang = array_pop( $pieces ); |
782 | 793 | $validCodes = Language::getLanguageNames(); |
783 | | - if( !array_key_exists( $lang, $validCodes ) ) |
| 794 | + if( !array_key_exists( $lang, $validCodes ) ) { |
784 | 795 | return array( $key, $wgLanguageCode ); |
| 796 | + } |
785 | 797 | |
786 | 798 | $message = implode( '/', $pieces ); |
787 | 799 | return array( $message, $lang ); |
— | — | @@ -796,19 +808,27 @@ |
797 | 809 | $cache = wfGetCache( CACHE_DB ); |
798 | 810 | $data = $cache->get( $cachekey ); |
799 | 811 | |
800 | | - if ( !$data ) $data = array(); |
| 812 | + if ( !$data ) { |
| 813 | + $data = array(); |
| 814 | + } |
801 | 815 | |
802 | 816 | $age = self::$mAdaptiveDataAge; |
803 | | - $filterDate = substr( wfTimestamp( TS_MW, time()-$age ), 0, 8 ); |
| 817 | + $filterDate = substr( wfTimestamp( TS_MW, time() - $age ), 0, 8 ); |
804 | 818 | foreach ( array_keys( $data ) as $key ) { |
805 | | - if ( $key < $filterDate ) unset( $data[$key] ); |
| 819 | + if ( $key < $filterDate ) { |
| 820 | + unset( $data[$key] ); |
| 821 | + } |
806 | 822 | } |
807 | 823 | |
808 | 824 | $index = substr( wfTimestampNow(), 0, 8 ); |
809 | | - if ( !isset( $data[$index] ) ) $data[$index] = array(); |
| 825 | + if ( !isset( $data[$index] ) ) { |
| 826 | + $data[$index] = array(); |
| 827 | + } |
810 | 828 | |
811 | 829 | foreach ( self::$instance->mRequestedMessages as $message => $_ ) { |
812 | | - if ( !isset( $data[$index][$message] ) ) $data[$index][$message] = 0; |
| 830 | + if ( !isset( $data[$index][$message] ) ) { |
| 831 | + $data[$index][$message] = 0; |
| 832 | + } |
813 | 833 | $data[$index][$message]++; |
814 | 834 | } |
815 | 835 | |
— | — | @@ -816,17 +836,21 @@ |
817 | 837 | } |
818 | 838 | |
819 | 839 | public function getMostUsedMessages() { |
820 | | - $cachekey = wfMemckey( 'message-profiling' ); |
| 840 | + $cachekey = wfMemcKey( 'message-profiling' ); |
821 | 841 | $cache = wfGetCache( CACHE_DB ); |
822 | 842 | $data = $cache->get( $cachekey ); |
823 | | - if ( !$data ) return array(); |
| 843 | + if ( !$data ) { |
| 844 | + return array(); |
| 845 | + } |
824 | 846 | |
825 | 847 | $list = array(); |
826 | 848 | |
827 | 849 | foreach( $data as $messages ) { |
828 | 850 | foreach( $messages as $message => $count ) { |
829 | 851 | $key = $message; |
830 | | - if ( !isset( $list[$key] ) ) $list[$key] = 0; |
| 852 | + if ( !isset( $list[$key] ) ) { |
| 853 | + $list[$key] = 0; |
| 854 | + } |
831 | 855 | $list[$key] += $count; |
832 | 856 | } |
833 | 857 | } |