Per Aaron, fix for r108274: added canUseWikiPage() to context objects to know whether...
authorAlexandre Emsenhuber <ialex@users.mediawiki.org>
Sat, 14 Jan 2012 14:27:46 +0000 (14:27 +0000)
committerAlexandre Emsenhuber <ialex@users.mediawiki.org>
Sat, 14 Jan 2012 14:27:46 +0000 (14:27 +0000)
includes/context/ContextSource.php
includes/context/DerivativeContext.php
includes/context/IContextSource.php
includes/context/RequestContext.php

index 2fbc776..45bd6ff 100644 (file)
@@ -79,7 +79,22 @@ abstract class ContextSource implements IContextSource {
        }
 
        /**
-        * Get the WikiPage object
+        * Check whether a WikiPage object can be get with getWikiPage().
+        * Callers should expect that an exception is thrown from getWikiPage()
+        * if this method returns false.
+        *
+        * @since 1.19
+        * @return bool
+        */
+       public function canUseWikiPage() {
+               return $this->getContext()->canUseWikiPage();
+       }
+
+       /**
+        * Get the WikiPage object.
+        * May throw an exception if there's no Title object set or the Title object
+        * belongs to a special namespace that doesn't have WikiPage, so use first
+        * canUseWikiPage() to check whether this method can be called safely.
         *
         * @since 1.19
         * @return WikiPage
index ee817db..5adf362 100644 (file)
@@ -118,6 +118,24 @@ class DerivativeContext extends ContextSource {
                }
        }
 
+       /**
+        * Check whether a WikiPage object can be get with getWikiPage().
+        * Callers should expect that an exception is thrown from getWikiPage()
+        * if this method returns false.
+        *
+        * @since 1.19
+        * @return bool
+        */
+       public function canUseWikiPage() {
+               if ( $this->wikipage !== null ) {
+                       return true;
+               } elseif ( $this->title !== null ) {
+                       return $this->title->canExist();
+               } else {
+                       return $this->getContext()->canUseWikiPage();
+               }
+       }
+
        /**
         * Set the WikiPage object
         *
@@ -129,7 +147,10 @@ class DerivativeContext extends ContextSource {
        }
 
        /**
-        * Get the WikiPage object
+        * Get the WikiPage object.
+        * May throw an exception if there's no Title object set or the Title object
+        * belongs to a special namespace that doesn't have WikiPage, so use first
+        * canUseWikiPage() to check whether this method can be called safely.
         *
         * @since 1.19
         * @return WikiPage
index ed7aba0..476035b 100644 (file)
@@ -43,7 +43,20 @@ interface IContextSource {
        public function getTitle();
 
        /**
-        * Get the WikiPage object
+        * Check whether a WikiPage object can be get with getWikiPage().
+        * Callers should expect that an exception is thrown from getWikiPage()
+        * if this method returns false.
+        *
+        * @since 1.19
+        * @return bool
+        */
+       public function canUseWikiPage();
+
+       /**
+        * Get the WikiPage object.
+        * May throw an exception if there's no Title object set or the Title object
+        * belongs to a special namespace that doesn't have WikiPage, so use first
+        * canUseWikiPage() to check whether this method can be called safely.
         *
         * @since 1.19
         * @return WikiPage
index 7f00041..1ffbc08 100644 (file)
@@ -108,6 +108,29 @@ class RequestContext implements IContextSource {
                return $this->title;
        }
 
+       /**
+        * Check whether a WikiPage object can be get with getWikiPage().
+        * Callers should expect that an exception is thrown from getWikiPage()
+        * if this method returns false.
+        *
+        * @since 1.19
+        * @return bool
+        */
+       public function canUseWikiPage() {
+               if ( $this->wikipage !== null ) {
+                       # If there's a WikiPage object set, we can for sure get it
+                       return true;
+               }
+               $title = $this->getTitle();
+               if ( $title === null ) {
+                       # No Title, no WikiPage
+                       return false;
+               } else {
+                       # Only namespaces whose pages are stored in the database can have WikiPage
+                       return $title->canExist();
+               }
+       }
+
        /**
         * Set the WikiPage object
         *
@@ -119,7 +142,10 @@ class RequestContext implements IContextSource {
        }
 
        /**
-        * Get the WikiPage object
+        * Get the WikiPage object.
+        * May throw an exception if there's no Title object set or the Title object
+        * belongs to a special namespace that doesn't have WikiPage, so use first
+        * canUseWikiPage() to check whether this method can be called safely.
         *
         * @since 1.19
         * @return WikiPage