Skip to content
This repository
Browse code

MINOR Added caching to SiteTree->getStatusFlags(), as its called twic…

…e for every node now in LeftAndMain->SiteTreeAsUL() (see #7410)
  • Loading branch information...
commit e50936fe9f128b355754edc03f1dd13ecad4ec19 1 parent bc2fb3c
Ingo Schommer authored June 12, 2012
64  code/model/SiteTree.php
@@ -195,11 +195,13 @@ class SiteTree extends DataObject implements PermissionProvider,i18nEntityProvid
195 195
 	 * See {@link batch_permission_check()} for details.
196 196
 	 */
197 197
 	public static $cache_permissions = array();
198  
-	
  198
+
199 199
 	/**
200 200
 	 * @var boolean
201 201
 	 */
202 202
 	protected static $enforce_strict_hierarchy = true;
  203
+
  204
+	protected $_cache_statusFlags = null;
203 205
 	
204 206
 	/**
205 207
 	 * Determines if the system should avoid orphaned pages
@@ -1493,6 +1495,11 @@ function onAfterDelete() {
1493 1495
 		
1494 1496
 		parent::onAfterDelete();
1495 1497
 	}
  1498
+
  1499
+	function flushCache($persistent = true) {
  1500
+		parent::flushCache($persistent);
  1501
+		$this->_cache_statusFlags = null;
  1502
+	}
1496 1503
 	
1497 1504
 	function validate() {
1498 1505
 		$result = parent::validate();
@@ -2469,38 +2476,43 @@ function setMenuTitle($value) {
2469 2476
 	 * 
2470 2477
 	 * Example (with optional title attribute): 
2471 2478
 	 * "deletedonlive" => array('text' => "Deleted", 'title' => 'This page has been deleted')
2472  
-	 * 
  2479
+	 *
  2480
+	 * @param Boolean $cached
2473 2481
 	 * @return array
2474 2482
 	 */
2475  
-	function getStatusFlags() {
2476  
-		$flags = array();
2477  
-		if($this->IsDeletedFromStage) {
2478  
-			if($this->ExistsOnLive) {
2479  
-				$flags['removedfromdraft'] = array(
2480  
-					'text' => _t('SiteTree.REMOVEDFROMDRAFTSHORT', 'Removed from draft'),
2481  
-					'title' => _t('SiteTree.REMOVEDFROMDRAFTHELP', 'Page is published, but has been deleted from draft'),
  2483
+	function getStatusFlags($cached = true) {
  2484
+		if(!$this->_cache_statusFlags || !$cached) {
  2485
+			$flags = array();
  2486
+			if($this->IsDeletedFromStage) {
  2487
+				if($this->ExistsOnLive) {
  2488
+					$flags['removedfromdraft'] = array(
  2489
+						'text' => _t('SiteTree.REMOVEDFROMDRAFTSHORT', 'Removed from draft'),
  2490
+						'title' => _t('SiteTree.REMOVEDFROMDRAFTHELP', 'Page is published, but has been deleted from draft'),
  2491
+					);
  2492
+				} else {
  2493
+					$flags['deletedonlive'] = array(
  2494
+						'text' => _t('SiteTree.DELETEDPAGESHORT', 'Deleted'),
  2495
+						'title' => _t('SiteTree.DELETEDPAGEHELP', 'Page is no longer published'),
  2496
+					);
  2497
+				}
  2498
+			} else if($this->IsAddedToStage) {
  2499
+				$flags['addedtodraft'] = array(
  2500
+					'text' => _t('SiteTree.ADDEDTODRAFTSHORT', 'Draft'),
  2501
+					'title' => _t('SiteTree.ADDEDTODRAFTHELP', "Page has not been published yet")
2482 2502
 				);
2483  
-			} else {
2484  
-				$flags['deletedonlive'] = array(
2485  
-					'text' => _t('SiteTree.DELETEDPAGESHORT', 'Deleted'),
2486  
-					'title' => _t('SiteTree.DELETEDPAGEHELP', 'Page is no longer published'),
  2503
+			} else if($this->IsModifiedOnStage) {
  2504
+				$flags['modified'] = array(
  2505
+					'text' => _t('SiteTree.MODIFIEDONDRAFTSHORT', 'Modified'),
  2506
+					'title' => _t('SiteTree.MODIFIEDONDRAFTHELP', 'Page has unpublished changes'),
2487 2507
 				);
2488 2508
 			}
2489  
-		} else if($this->IsAddedToStage) {
2490  
-			$flags['addedtodraft'] = array(
2491  
-				'text' => _t('SiteTree.ADDEDTODRAFTSHORT', 'Draft'),
2492  
-				'title' => _t('SiteTree.ADDEDTODRAFTHELP', "Page has not been published yet")
2493  
-			);
2494  
-		} else if($this->IsModifiedOnStage) {
2495  
-			$flags['modified'] = array(
2496  
-				'text' => _t('SiteTree.MODIFIEDONDRAFTSHORT', 'Modified'),
2497  
-				'title' => _t('SiteTree.MODIFIEDONDRAFTHELP', 'Page has unpublished changes'),
2498  
-			);
2499  
-		}
2500 2509
 
2501  
-		$this->extend('updateStatusFlags', $flags);
  2510
+			$this->extend('updateStatusFlags', $flags);
  2511
+
  2512
+			$this->_cache_statusFlags = $flags;
  2513
+		}
2502 2514
 		
2503  
-		return $flags;
  2515
+		return $this->_cache_statusFlags;
2504 2516
 	}
2505 2517
 
2506 2518
 	
4  tests/model/SiteTreeTest.php
@@ -956,8 +956,8 @@ class SiteTreeTest_NotRoot extends Page implements TestOnly {
956 956
 }
957 957
 
958 958
 class SiteTreeTest_StageStatusInherit extends SiteTree implements TestOnly {
959  
-	function getStatusFlags(){
960  
-		$flags = parent::getStatusFlags();
  959
+	function getStatusFlags($cached = true){
  960
+		$flags = parent::getStatusFlags($cached);
961 961
 		$flags['inherited-class'] = "InheritedTitle";
962 962
 		return $flags;
963 963
 	}

0 notes on commit e50936f

Please sign in to comment.
Something went wrong with that request. Please try again.