Enabling changes for r56828: some refactoring and code cleanup of the protection...
authorAndrew Garrett <werdna@users.mediawiki.org>
Fri, 6 Nov 2009 10:27:44 +0000 (10:27 +0000)
committerAndrew Garrett <werdna@users.mediawiki.org>
Fri, 6 Nov 2009 10:27:44 +0000 (10:27 +0000)
docs/hooks.txt
includes/Article.php
includes/ProtectionForm.php
includes/Title.php
includes/api/ApiProtect.php

index 238a35b..ec49b52 100644 (file)
@@ -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
index 78e1c81..ca134a5 100644 (file)
@@ -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 );
index 4a32ecd..f457e9a 100644 (file)
@@ -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 ) ) {
index d0b28e5..5594c08 100644 (file)
@@ -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;
+       }
 }
index ceadccf..d0fa3f2 100644 (file)
@@ -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]));