r82884 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r82883‎ | r82884 | r82885 >
Date:08:13, 27 February 2011
Author:aaron
Status:deferred
Tags:
Comment:
* Split off FlaggedPageConfig class from FlaggedRevs
* Added selectFields() to said class
Modified paths:
  • /trunk/extensions/FlaggedRevs/FlaggedArticle.php (modified) (history)
  • /trunk/extensions/FlaggedRevs/FlaggedPageConfig.php (added) (history)
  • /trunk/extensions/FlaggedRevs/FlaggedRevision.php (modified) (history)
  • /trunk/extensions/FlaggedRevs/FlaggedRevs.class.php (modified) (history)
  • /trunk/extensions/FlaggedRevs/FlaggedRevs.hooks.php (modified) (history)
  • /trunk/extensions/FlaggedRevs/FlaggedRevs.php (modified) (history)
  • /trunk/extensions/FlaggedRevs/forms/PageStabilityForm.php (modified) (history)
  • /trunk/extensions/FlaggedRevs/specialpages/ConfiguredPages_body.php (modified) (history)
  • /trunk/extensions/FlaggedRevs/specialpages/StablePages_body.php (modified) (history)

Diff [purge]

Index: trunk/extensions/FlaggedRevs/FlaggedRevs.php
@@ -268,6 +268,8 @@
269269 $wgAutoloadClasses['FlaggedArticleView'] = $dir . 'FlaggedArticleView.php';
270270 # Load FlaggedArticle object class
271271 $wgAutoloadClasses['FlaggedArticle'] = $dir . 'FlaggedArticle.php';
 272+# Load FlaggedPageConfig object class
 273+$wgAutoloadClasses['FlaggedPageConfig'] = $dir . 'FlaggedPageConfig.php';
272274 # Load FlaggedRevision object class
273275 $wgAutoloadClasses['FlaggedRevision'] = $dir . 'FlaggedRevision.php';
274276
Index: trunk/extensions/FlaggedRevs/FlaggedArticle.php
@@ -324,7 +324,7 @@
325325 */
326326 protected function loadStableRevAndConfig( $flags = 0 ) {
327327 $this->stableRev = false; // false => "found nothing"
328 - $this->pageConfig = FlaggedRevs::getDefaultVisibilitySettings(); // default
 328+ $this->pageConfig = FlaggedPageConfig::getDefaultVisibilitySettings(); // default
329329 if ( !FlaggedRevs::inReviewNamespace( $this->getTitle() ) ) {
330330 return; // short-circuit
331331 }
@@ -333,8 +333,7 @@
334334 wfGetDB( DB_MASTER ) : wfGetDB( DB_SLAVE );
335335 $row = $db->selectRow(
336336 array( 'page', 'flaggedpages', 'flaggedrevs', 'flaggedpage_config' ),
337 - array_merge( FlaggedRevision::selectFields(),
338 - array( 'fpc_override', 'fpc_level', 'fpc_expiry' ) ),
 337+ array_merge( FlaggedRevision::selectFields(), FlaggedPageConfig::selectFields() ),
339338 array( 'page_id' => $this->getID() ),
340339 __METHOD__,
341340 array(),
@@ -348,7 +347,7 @@
349348 return; // no page found at all
350349 }
351350 if ( $row->fpc_override !== null ) { // page config row found
352 - $this->pageConfig = FlaggedRevs::getVisibilitySettingsFromRow( $row );
 351+ $this->pageConfig = FlaggedPageConfig::getVisibilitySettingsFromRow( $row );
353352 }
354353 if ( $row->fr_rev_id !== null ) { // stable rev row found
355354 // Page may not reviewable, which implies no stable version
Index: trunk/extensions/FlaggedRevs/FlaggedRevs.class.php
@@ -951,170 +951,6 @@
952952 return false;
953953 }
954954
955 - # ################ Page configuration functions #################
956 -
957 - /**
958 - * Get visibility settings/restrictions for a page
959 - * @param Title $title, page title
960 - * @param int $flags, FR_MASTER
961 - * @returns array (associative) (select,override,autoreview,expiry)
962 - */
963 - public static function getPageStabilitySettings( Title $title, $flags = 0 ) {
964 - $db = ( $flags & FR_MASTER ) ?
965 - wfGetDB( DB_MASTER ) : wfGetDB( DB_SLAVE );
966 - $row = $db->selectRow( 'flaggedpage_config',
967 - array( 'fpc_override', 'fpc_level', 'fpc_expiry' ),
968 - array( 'fpc_page_id' => $title->getArticleID() ),
969 - __METHOD__
970 - );
971 - return self::getVisibilitySettingsFromRow( $row );
972 - }
973 -
974 - /**
975 - * Get page configuration settings from a DB row
976 - */
977 - public static function getVisibilitySettingsFromRow( $row ) {
978 - if ( $row ) {
979 - # This code should be refactored, now that it's being used more generally.
980 - $expiry = Block::decodeExpiry( $row->fpc_expiry );
981 - # Only apply the settings if they haven't expired
982 - if ( !$expiry || $expiry < wfTimestampNow() ) {
983 - $row = null; // expired
984 - self::purgeExpiredConfigurations();
985 - }
986 - }
987 - // Is there a non-expired row?
988 - if ( $row ) {
989 - $level = $row->fpc_level;
990 - if ( !self::isValidRestriction( $row->fpc_level ) ) {
991 - $level = ''; // site default; ignore fpc_level
992 - }
993 - $config = array(
994 - 'override' => $row->fpc_override ? 1 : 0,
995 - 'autoreview' => $level,
996 - 'expiry' => Block::decodeExpiry( $row->fpc_expiry ) // TS_MW
997 - );
998 - # If there are protection levels defined check if this is valid...
999 - if ( self::useProtectionLevels() ) {
1000 - $level = self::getProtectionLevel( $config );
1001 - if ( $level == 'invalid' || $level == 'none' ) {
1002 - // If 'none', make sure expiry is 'infinity'
1003 - $config = self::getDefaultVisibilitySettings(); // revert to default (none)
1004 - }
1005 - }
1006 - } else {
1007 - # Return the default config if this page doesn't have its own
1008 - $config = self::getDefaultVisibilitySettings();
1009 - }
1010 - return $config;
1011 - }
1012 -
1013 - /**
1014 - * Get default page configuration settings
1015 - */
1016 - public static function getDefaultVisibilitySettings() {
1017 - return array(
1018 - # Keep this consistent across settings:
1019 - # # 1 -> override, 0 -> don't
1020 - 'override' => self::isStableShownByDefault() ? 1 : 0,
1021 - 'autoreview' => '',
1022 - 'expiry' => 'infinity'
1023 - );
1024 - }
1025 -
1026 -
1027 - /**
1028 - * Find what protection level a config is in
1029 - * @param array $config
1030 - * @returns string
1031 - */
1032 - public static function getProtectionLevel( array $config ) {
1033 - if ( !self::useProtectionLevels() ) {
1034 - throw new MWException( 'getProtectionLevel() called with $wgFlaggedRevsProtection off' );
1035 - }
1036 - $defaultConfig = self::getDefaultVisibilitySettings();
1037 - # Check if the page is not protected at all...
1038 - if ( $config['override'] == $defaultConfig['override']
1039 - && $config['autoreview'] == '' )
1040 - {
1041 - return "none"; // not protected
1042 - }
1043 - # All protection levels have 'override' on
1044 - if ( $config['override'] ) {
1045 - # The levels are defined by the 'autoreview' settings
1046 - if ( in_array( $config['autoreview'], self::getRestrictionLevels() ) ) {
1047 - return $config['autoreview'];
1048 - }
1049 - }
1050 - return "invalid";
1051 - }
1052 -
1053 - /**
1054 - * Check if an fpc_level value is valid
1055 - * @param string $right
1056 - */
1057 - public static function isValidRestriction( $right ) {
1058 - if ( $right == '' ) {
1059 - return true; // no restrictions (none)
1060 - }
1061 - return in_array( $right, self::getRestrictionLevels(), true );
1062 - }
1063 -
1064 - /**
1065 - * Purge expired restrictions from the flaggedpage_config table.
1066 - * The stable version of pages may change and invalidation may be required.
1067 - */
1068 - public static function purgeExpiredConfigurations() {
1069 - if ( wfReadOnly() ) return;
1070 -
1071 - $dbw = wfGetDB( DB_MASTER );
1072 - $config = self::getDefaultVisibilitySettings(); // config is to be reset
1073 - $encCutoff = $dbw->addQuotes( $dbw->timestamp() );
1074 - $ret = $dbw->select(
1075 - array( 'flaggedpage_config', 'page' ),
1076 - array( 'fpc_page_id', 'page_namespace', 'page_title' ),
1077 - array( 'page_id = fpc_page_id', 'fpc_expiry < ' . $encCutoff ),
1078 - __METHOD__
1079 - // array( 'FOR UPDATE' )
1080 - );
1081 - $pagesClearConfig = array();
1082 - $pagesClearTracking = $titlesClearTracking = array();
1083 - foreach ( $ret as $row ) {
1084 - # If FlaggedRevs got "turned off" (in protection config)
1085 - # for this page, then clear it from the tracking tables...
1086 - if ( self::useOnlyIfProtected() && !$config['override'] ) {
1087 - $pagesClearTracking[] = $row->fpc_page_id; // no stable version
1088 - $titlesClearTracking[] = Title::newFromRow( $row ); // no stable version
1089 - }
1090 - $pagesClearConfig[] = $row->fpc_page_id; // page with expired config
1091 - }
1092 - # Clear the expired config for these pages...
1093 - if ( count( $pagesClearConfig ) ) {
1094 - $dbw->delete( 'flaggedpage_config',
1095 - array( 'fpc_page_id' => $pagesClearConfig, 'fpc_expiry < ' . $encCutoff ),
1096 - __METHOD__
1097 - );
1098 - }
1099 - # Clear the tracking rows and update page_touched for the
1100 - # pages in $pagesClearConfig that do now have a stable version...
1101 - if ( count( $pagesClearTracking ) ) {
1102 - self::clearTrackingRows( $pagesClearTracking );
1103 - $dbw->update( 'page',
1104 - array( 'page_touched' => $dbw->timestamp() ),
1105 - array( 'page_id' => $pagesClearTracking ),
1106 - __METHOD__
1107 - );
1108 - }
1109 - # Also, clear their squid caches and purge other pages that use this page.
1110 - # NOTE: all of these updates are deferred via $wgDeferredUpdateList.
1111 - foreach ( $titlesClearTracking as $title ) {
1112 - self::purgeSquid( $title );
1113 - if ( FlaggedRevs::inclusionSetting() == FR_INCLUDES_STABLE ) {
1114 - FlaggedRevs::HTMLCacheUpdates( $title ); // purge pages that use this page
1115 - }
1116 - }
1117 - }
1118 -
1119955 # ################ Other utility functions #################
1120956
1121957 /**
Index: trunk/extensions/FlaggedRevs/forms/PageStabilityForm.php
@@ -237,7 +237,7 @@
238238
239239 protected function loadOldConfig() {
240240 # Get the current page config
241 - $this->oldConfig = FlaggedRevs::getPageStabilitySettings( $this->page, FR_MASTER );
 241+ $this->oldConfig = FlaggedPageConfig::getPageStabilitySettings( $this->page, FR_MASTER );
242242 }
243243
244244 /*
@@ -309,7 +309,7 @@
310310 # Apply watchlist checkbox value (may be NULL)
311311 $this->updateWatchlist();
312312 # Take this opportunity to purge out expired configurations
313 - FlaggedRevs::purgeExpiredConfigurations();
 313+ FlaggedPageConfig::purgeExpiredConfigurations();
314314 return true;
315315 }
316316
@@ -340,7 +340,7 @@
341341 $settings = ''; // no level, expiry info
342342 } else {
343343 $params = $this->getLogParams();
344 - $action = ( $this->oldConfig === FlaggedRevs::getDefaultVisibilitySettings() )
 344+ $action = ( $this->oldConfig === FlaggedPageConfig::getDefaultVisibilitySettings() )
345345 ? 'config' // set a custom configuration
346346 : 'modify'; // modified an existing custom configuration
347347 $log->addEntry( $action, $this->page, $reason,
@@ -542,7 +542,7 @@
543543 global $wgFlaggedRevsProtectQuota;
544544 if ( isset( $wgFlaggedRevsProtectQuota ) // quota exists
545545 && $this->autoreview != '' // and we are protecting
546 - && FlaggedRevs::getProtectionLevel( $this->oldConfig ) == 'none' ) // page unprotected
 546+ && FlaggedPageConfig::getProtectionLevel( $this->oldConfig ) == 'none' ) // page unprotected
547547 {
548548 $dbw = wfGetDB( DB_MASTER );
549549 $count = $dbw->selectField( 'flaggedpage_config', 'COUNT(*)', '', __METHOD__ );
@@ -551,7 +551,7 @@
552552 }
553553 }
554554 # Autoreview only when protecting currently unprotected pages
555 - $this->reviewThis = ( FlaggedRevs::getProtectionLevel( $this->oldConfig ) == 'none' );
 555+ $this->reviewThis = ( FlaggedPageConfig::getProtectionLevel( $this->oldConfig ) == 'none' );
556556 # Autoreview restriction => use stable
557557 # No autoreview restriction => site default
558558 $this->override = ( $this->autoreview != '' )
@@ -562,7 +562,7 @@
563563 'override' => $this->override,
564564 'autoreview' => $this->autoreview
565565 );
566 - if ( FlaggedRevs::getProtectionLevel( $newConfig ) == 'invalid' ) {
 566+ if ( FlaggedPageConfig::getProtectionLevel( $newConfig ) == 'invalid' ) {
567567 return 'stabilize_invalid_level'; // double-check configuration
568568 }
569569 # Check autoreview restriction setting
Index: trunk/extensions/FlaggedRevs/FlaggedRevision.php
@@ -193,7 +193,7 @@
194194 }
195195 # Get visiblity settings...
196196 if ( empty( $config ) ) {
197 - $config = FlaggedRevs::getPageStabilitySettings( $title, $flags );
 197+ $config = FlaggedPageConfig::getPageStabilitySettings( $title, $flags );
198198 }
199199 if ( !$config['override'] && FlaggedRevs::useOnlyIfProtected() ) {
200200 return null; // page is not reviewable; no stable version
Index: trunk/extensions/FlaggedRevs/specialpages/ConfiguredPages_body.php
@@ -64,7 +64,7 @@
6565 $wgOut->addWikiMsg( 'configuredpages-none' );
6666 }
6767 # Take this opportunity to purge out expired configurations
68 - FlaggedRevs::purgeExpiredConfigurations();
 68+ FlaggedPageConfig::purgeExpiredConfigurations();
6969 }
7070
7171 public function formatRow( $row ) {
Index: trunk/extensions/FlaggedRevs/specialpages/StablePages_body.php
@@ -65,7 +65,7 @@
6666 $wgOut->addWikiMsg( 'stablepages-none' );
6767 }
6868 # Take this opportunity to purge out expired configurations
69 - FlaggedRevs::purgeExpiredConfigurations();
 69+ FlaggedPageConfig::purgeExpiredConfigurations();
7070 }
7171
7272 public function formatRow( $row ) {
Index: trunk/extensions/FlaggedRevs/FlaggedRevs.hooks.php
@@ -488,7 +488,7 @@
489489 if( !FlaggedRevs::inReviewNamespace( $title ) ) {
490490 $ret = '';
491491 } else {
492 - $config = FlaggedRevs::getPageStabilitySettings( $title );
 492+ $config = FlaggedPageConfig::getPageStabilitySettings( $title );
493493 $ret = $config['autoreview'];
494494 }
495495 }
@@ -1777,12 +1777,12 @@
17781778 array() : array( 'disabled' => 'disabled' );
17791779
17801780 # Get the current config/expiry
1781 - $config = FlaggedRevs::getPageStabilitySettings( $article->getTitle(), FR_MASTER );
 1781+ $config = FlaggedPageConfig::getPageStabilitySettings( $article->getTitle(), FR_MASTER );
17821782 $oldExpirySelect = ( $config['expiry'] == 'infinity' ) ? 'infinite' : 'existing';
17831783
17841784 # Load requested restriction level, default to current level...
17851785 $restriction = $wgRequest->getVal( 'mwStabilityLevel',
1786 - FlaggedRevs::getProtectionLevel( $config ) );
 1786+ FlaggedPageConfig::getProtectionLevel( $config ) );
17871787 # Load the requested expiry time (dropdown)
17881788 $expirySelect = $wgRequest->getVal( 'mwStabilizeExpirySelection', $oldExpirySelect );
17891789 # Load the requested expiry time (field)
Index: trunk/extensions/FlaggedRevs/FlaggedPageConfig.php
@@ -0,0 +1,173 @@
 2+<?php
 3+/*
 4+* Page stability configuration functions
 5+*/
 6+class FlaggedPageConfig {
 7+ /**
 8+ * Get visibility settings/restrictions for a page
 9+ * @param Title $title, page title
 10+ * @param int $flags, FR_MASTER
 11+ * @returns array (associative) (select,override,autoreview,expiry)
 12+ */
 13+ public static function getPageStabilitySettings( Title $title, $flags = 0 ) {
 14+ $db = ( $flags & FR_MASTER ) ?
 15+ wfGetDB( DB_MASTER ) : wfGetDB( DB_SLAVE );
 16+ $row = $db->selectRow( 'flaggedpage_config',
 17+ self::selectFields(),
 18+ array( 'fpc_page_id' => $title->getArticleID() ),
 19+ __METHOD__
 20+ );
 21+ return self::getVisibilitySettingsFromRow( $row );
 22+ }
 23+
 24+ /**
 25+ * @return Array basic select fields for FlaggedPageConfig DB row
 26+ */
 27+ public static function selectFields() {
 28+ return array( 'fpc_override', 'fpc_level', 'fpc_expiry' );
 29+ }
 30+
 31+ /**
 32+ * Get page configuration settings from a DB row
 33+ */
 34+ public static function getVisibilitySettingsFromRow( $row ) {
 35+ if ( $row ) {
 36+ # This code should be refactored, now that it's being used more generally.
 37+ $expiry = Block::decodeExpiry( $row->fpc_expiry );
 38+ # Only apply the settings if they haven't expired
 39+ if ( !$expiry || $expiry < wfTimestampNow() ) {
 40+ $row = null; // expired
 41+ self::purgeExpiredConfigurations();
 42+ }
 43+ }
 44+ // Is there a non-expired row?
 45+ if ( $row ) {
 46+ $level = $row->fpc_level;
 47+ if ( !self::isValidRestriction( $row->fpc_level ) ) {
 48+ $level = ''; // site default; ignore fpc_level
 49+ }
 50+ $config = array(
 51+ 'override' => $row->fpc_override ? 1 : 0,
 52+ 'autoreview' => $level,
 53+ 'expiry' => Block::decodeExpiry( $row->fpc_expiry ) // TS_MW
 54+ );
 55+ # If there are protection levels defined check if this is valid...
 56+ if ( FlaggedRevs::useProtectionLevels() ) {
 57+ $level = self::getProtectionLevel( $config );
 58+ if ( $level == 'invalid' || $level == 'none' ) {
 59+ // If 'none', make sure expiry is 'infinity'
 60+ $config = self::getDefaultVisibilitySettings(); // revert to default (none)
 61+ }
 62+ }
 63+ } else {
 64+ # Return the default config if this page doesn't have its own
 65+ $config = self::getDefaultVisibilitySettings();
 66+ }
 67+ return $config;
 68+ }
 69+
 70+ /**
 71+ * Get default page configuration settings
 72+ */
 73+ public static function getDefaultVisibilitySettings() {
 74+ return array(
 75+ # Keep this consistent: 1 => override, 0 => don't
 76+ 'override' => FlaggedRevs::isStableShownByDefault() ? 1 : 0,
 77+ 'autoreview' => '',
 78+ 'expiry' => 'infinity'
 79+ );
 80+ }
 81+
 82+
 83+ /**
 84+ * Find what protection level a config is in
 85+ * @param array $config
 86+ * @returns string
 87+ */
 88+ public static function getProtectionLevel( array $config ) {
 89+ if ( !FlaggedRevs::useProtectionLevels() ) {
 90+ throw new MWException( '$wgFlaggedRevsProtection is disabled' );
 91+ }
 92+ $defaultConfig = self::getDefaultVisibilitySettings();
 93+ # Check if the page is not protected at all...
 94+ if ( $config['override'] == $defaultConfig['override']
 95+ && $config['autoreview'] == '' )
 96+ {
 97+ return "none"; // not protected
 98+ }
 99+ # All protection levels have 'override' on
 100+ if ( $config['override'] ) {
 101+ # The levels are defined by the 'autoreview' settings
 102+ if ( in_array( $config['autoreview'], FlaggedRevs::getRestrictionLevels() ) ) {
 103+ return $config['autoreview'];
 104+ }
 105+ }
 106+ return "invalid";
 107+ }
 108+
 109+ /**
 110+ * Check if an fpc_level value is valid
 111+ * @param string $right
 112+ */
 113+ protected static function isValidRestriction( $right ) {
 114+ if ( $right == '' ) {
 115+ return true; // no restrictions (none)
 116+ }
 117+ return in_array( $right, FlaggedRevs::getRestrictionLevels(), true );
 118+ }
 119+
 120+ /**
 121+ * Purge expired restrictions from the flaggedpage_config table.
 122+ * The stable version of pages may change and invalidation may be required.
 123+ */
 124+ public static function purgeExpiredConfigurations() {
 125+ if ( wfReadOnly() ) return;
 126+
 127+ $dbw = wfGetDB( DB_MASTER );
 128+ $config = self::getDefaultVisibilitySettings(); // config is to be reset
 129+ $encCutoff = $dbw->addQuotes( $dbw->timestamp() );
 130+ $ret = $dbw->select(
 131+ array( 'flaggedpage_config', 'page' ),
 132+ array( 'fpc_page_id', 'page_namespace', 'page_title' ),
 133+ array( 'page_id = fpc_page_id', 'fpc_expiry < ' . $encCutoff ),
 134+ __METHOD__
 135+ // array( 'FOR UPDATE' )
 136+ );
 137+ $pagesClearConfig = array();
 138+ $pagesClearTracking = $titlesClearTracking = array();
 139+ foreach ( $ret as $row ) {
 140+ # If FlaggedRevs got "turned off" (in protection config)
 141+ # for this page, then clear it from the tracking tables...
 142+ if ( FlaggedRevs::useOnlyIfProtected() && !$config['override'] ) {
 143+ $pagesClearTracking[] = $row->fpc_page_id; // no stable version
 144+ $titlesClearTracking[] = Title::newFromRow( $row ); // no stable version
 145+ }
 146+ $pagesClearConfig[] = $row->fpc_page_id; // page with expired config
 147+ }
 148+ # Clear the expired config for these pages...
 149+ if ( count( $pagesClearConfig ) ) {
 150+ $dbw->delete( 'flaggedpage_config',
 151+ array( 'fpc_page_id' => $pagesClearConfig, 'fpc_expiry < ' . $encCutoff ),
 152+ __METHOD__
 153+ );
 154+ }
 155+ # Clear the tracking rows and update page_touched for the
 156+ # pages in $pagesClearConfig that do now have a stable version...
 157+ if ( count( $pagesClearTracking ) ) {
 158+ FlaggedRevs::clearTrackingRows( $pagesClearTracking );
 159+ $dbw->update( 'page',
 160+ array( 'page_touched' => $dbw->timestamp() ),
 161+ array( 'page_id' => $pagesClearTracking ),
 162+ __METHOD__
 163+ );
 164+ }
 165+ # Also, clear their squid caches and purge other pages that use this page.
 166+ # NOTE: all of these updates are deferred via $wgDeferredUpdateList.
 167+ foreach ( $titlesClearTracking as $title ) {
 168+ FlaggedRevs::purgeSquid( $title );
 169+ if ( FlaggedRevs::inclusionSetting() == FR_INCLUDES_STABLE ) {
 170+ FlaggedRevs::HTMLCacheUpdates( $title ); // purge pages that use this page
 171+ }
 172+ }
 173+ }
 174+}
Property changes on: trunk/extensions/FlaggedRevs/FlaggedPageConfig.php
___________________________________________________________________
Added: svn:eol-style
1175 + native

Status & tagging log