From: Andrew Garrett Date: Fri, 6 Nov 2009 10:27:44 +0000 (+0000) Subject: Enabling changes for r56828: some refactoring and code cleanup of the protection... X-Git-Tag: 1.31.0-rc.0~38951 X-Git-Url: https://git.cyclocoop.org/%7B%24www_url%7Dadmin/compta/comptes/ajouter.php?a=commitdiff_plain;h=f6f5e9088826d5c6925b5e8ce196ad068aa34fc5;p=lhc%2Fweb%2Fwiklou.git Enabling changes for r56828: some refactoring and code cleanup of the protection interface, including a new hook, generalisation to more restriction types than edit/move, and some other related changes. --- diff --git a/docs/hooks.txt b/docs/hooks.txt index 238a35b97a..ec49b52de2 100644 --- a/docs/hooks.txt +++ b/docs/hooks.txt @@ -1232,6 +1232,11 @@ $errorMsg: an html message string of an error $article: the page the form is shown for $out: OutputPage object +'ProtectionFormGetApplicableTypes': Allows extensions to modify the types of protection + that can be applied. +$title: The title in question. +&$types: The types of protection available. + 'RawPageViewBeforeOutput': Right before the text is blown out in action=raw &$obj: RawPage object &$text: The text that's going to be the output diff --git a/includes/Article.php b/includes/Article.php index 78e1c819d0..ca134a5c4d 100644 --- a/includes/Article.php +++ b/includes/Article.php @@ -2256,7 +2256,9 @@ class Article { * @return bool true on success */ public function updateRestrictions( $limit = array(), $reason = '', &$cascade = 0, $expiry = array() ) { - global $wgUser, $wgRestrictionTypes, $wgContLang; + global $wgUser, $wgContLang; + + $restrictionTypes = $this->mTitle->getProtectionTypes(); $id = $this->mTitle->getArticleID(); if ( $id <= 0 ) { @@ -2286,7 +2288,7 @@ class Article { $current = array(); $updated = Article::flattenRestrictions( $limit ); $changed = false; - foreach( $wgRestrictionTypes as $action ) { + foreach( $restrictionTypes as $action ) { if( isset( $expiry[$action] ) ) { # Get current restrictions on $action $aLimits = $this->mTitle->getRestrictions( $action ); diff --git a/includes/ProtectionForm.php b/includes/ProtectionForm.php index 4a32ecd5ac..f457e9a7ce 100644 --- a/includes/ProtectionForm.php +++ b/includes/ProtectionForm.php @@ -54,20 +54,29 @@ class ProtectionForm { var $mExistingExpiry = array(); function __construct( Article $article ) { - global $wgRequest, $wgUser; - global $wgRestrictionTypes, $wgRestrictionLevels; + global $wgUser; + // Set instance variables. $this->mArticle = $article; $this->mTitle = $article->mTitle; - $this->mApplicableTypes = $this->mTitle->exists() ? $wgRestrictionTypes : array('create'); - - $this->mCascade = $this->mTitle->areRestrictionsCascading(); - - // The form will be available in read-only to show levels. + $this->mApplicableTypes = $this->mTitle->getProtectionTypes(); + + // Check if the form should be disabled. + // If it is, the form will be available in read-only to show levels. $this->mPermErrors = $this->mTitle->getUserPermissionsErrors('protect',$wgUser); $this->disabled = wfReadOnly() || $this->mPermErrors != array(); $this->disabledAttrib = $this->disabled ? array( 'disabled' => 'disabled' ) : array(); + + $this->loadData(); + } + + // Loads the current state of protection into the object. + function loadData() { + global $wgRequest, $wgUser; + global $wgRestrictionLevels; + + $this->mCascade = $this->mTitle->areRestrictionsCascading(); $this->mReason = $wgRequest->getText( 'mwProtect-reason' ); $this->mReasonSelection = $wgRequest->getText( 'wpProtectReasonSelection' ); @@ -76,6 +85,8 @@ class ProtectionForm { foreach( $this->mApplicableTypes as $action ) { // Fixme: this form currently requires individual selections, // but the db allows multiples separated by commas. + + // Pull the actual restriction from the DB $this->mRestrictions[$action] = implode( '', $this->mTitle->getRestrictions( $action ) ); if ( !$this->mRestrictions[$action] ) { @@ -318,10 +329,6 @@ class ProtectionForm { Xml::openElement( 'tbody' ); foreach( $this->mRestrictions as $action => $selected ) { - // Special case: apply upload protection only on images - if ( $action == 'upload' && $this->mTitle->getNamespace() != NS_FILE ) - continue; - /* Not all languages have V_x <-> N_x relation */ $msg = wfMsg( 'restriction-' . $action ); if( wfEmptyMsg( 'restriction-' . $action, $msg ) ) { diff --git a/includes/Title.php b/includes/Title.php index d0b28e5d1d..5594c083b0 100644 --- a/includes/Title.php +++ b/includes/Title.php @@ -965,18 +965,20 @@ class Title { /** * Does the title correspond to a protected article? * @param $what \type{\string} the action the page is protected from, - * by default checks move and edit + * by default checks all actions. * @return \type{\bool} */ public function isProtected( $action = '' ) { - global $wgRestrictionLevels, $wgRestrictionTypes; + global $wgRestrictionLevels; + + $restrictionTypes = $this->getProtectionTypes(); # Special pages have inherent protection if( $this->getNamespace() == NS_SPECIAL ) return true; # Check regular protection levels - foreach( $wgRestrictionTypes as $type ){ + foreach( $restrictionTypes as $type ){ if( $action == $type || $action == '' ) { $r = $this->getRestrictions( $type ); foreach( $wgRestrictionLevels as $level ) { @@ -1774,12 +1776,7 @@ class Title { * The restriction array is an array of each type, each of which contains an array of unique groups. */ public function getCascadeProtectionSources( $get_pages = true ) { - global $wgRestrictionTypes; - - # Define our dimension of restrictions types $pagerestrictions = array(); - foreach( $wgRestrictionTypes as $action ) - $pagerestrictions[$action] = array(); if ( isset( $this->mCascadeSources ) && $get_pages ) { return array( $this->mCascadeSources, $this->mCascadingRestrictions ); @@ -1830,7 +1827,13 @@ class Title { $sources[$page_id] = Title::makeTitle($page_ns, $page_title); # Add groups needed for each restriction type if its not already there # Make sure this restriction type still exists - if ( isset($pagerestrictions[$row->pr_type]) && !in_array($row->pr_level, $pagerestrictions[$row->pr_type]) ) { + + if ( !isset( $pagerestrictions[$row->pr_type] ) ) { + $pagerestrictions[$row->pr_type] = array(); + } + + if ( isset($pagerestrictions[$row->pr_type]) && + !in_array($row->pr_level, $pagerestrictions[$row->pr_type]) ) { $pagerestrictions[$row->pr_type][]=$row->pr_level; } } else { @@ -1880,10 +1883,11 @@ class Title { } public function loadRestrictionsFromRows( $rows, $oldFashionedRestrictions = NULL ) { - global $wgRestrictionTypes; $dbr = wfGetDB( DB_SLAVE ); + + $restrictionTypes = $this->getProtectionTypes(); - foreach( $wgRestrictionTypes as $type ){ + foreach( $restrictionTypes as $type ){ $this->mRestrictions[$type] = array(); $this->mRestrictionsExpiry[$type] = Block::decodeExpiry(''); } @@ -1922,8 +1926,9 @@ class Title { foreach( $rows as $row ) { # Cycle through all the restrictions. - // Don't take care of restrictions types that aren't in $wgRestrictionTypes - if( !in_array( $row->pr_type, $wgRestrictionTypes ) ) + // Don't take care of restrictions types that aren't allowed + + if( !in_array( $row->pr_type, $restrictionTypes ) ) continue; // This code should be refactored, now that it's being used more generally, @@ -3762,4 +3767,18 @@ class Title { return !in_array( $this->mNamespace, $bannedNamespaces ); } + + public function getProtectionTypes() { + global $wgRestrictionTypes; + $types = $this->exists() ? $wgRestrictionTypes : array('create'); + + if ( $this->getNamespace() == NS_FILE ) { + $types[] = 'upload'; + } + + wfRunHooks( 'ProtectionFormGetApplicableTypes', + array( $this, &$types ) ); + + return $types; + } } diff --git a/includes/api/ApiProtect.php b/includes/api/ApiProtect.php index ceadccfe08..d0fa3f2bf1 100644 --- a/includes/api/ApiProtect.php +++ b/includes/api/ApiProtect.php @@ -68,6 +68,8 @@ class ApiProtect extends ApiBase { else $this->dieUsageMsg(array('toofewexpiries', count($expiry), count($params['protections']))); } + + $restrictionTypes = $titleObj->getProtectionTypes(); $protections = array(); $expiryarray = array(); @@ -80,7 +82,7 @@ class ApiProtect extends ApiBase { $this->dieUsageMsg(array('create-titleexists')); if(!$titleObj->exists() && $p[0] != 'create') $this->dieUsageMsg(array('missingtitles-createonly')); - if(!in_array($p[0], $wgRestrictionTypes) && $p[0] != 'create') + if(!in_array($p[0], $restrictionTypes) && $p[0] != 'create') $this->dieUsageMsg(array('protect-invalidaction', $p[0])); if(!in_array($p[1], $wgRestrictionLevels) && $p[1] != 'all') $this->dieUsageMsg(array('protect-invalidlevel', $p[1]));