From: Alexandre Emsenhuber Date: Sat, 14 Jan 2012 14:27:46 +0000 (+0000) Subject: Per Aaron, fix for r108274: added canUseWikiPage() to context objects to know whether... X-Git-Tag: 1.31.0-rc.0~25274 X-Git-Url: http://git.cyclocoop.org/%24href?a=commitdiff_plain;h=caf5df17aba64eb980a12f596ce98ec1f476c801;p=lhc%2Fweb%2Fwiklou.git Per Aaron, fix for r108274: added canUseWikiPage() to context objects to know whether getWikiPage() can be safely called --- diff --git a/includes/context/ContextSource.php b/includes/context/ContextSource.php index 2fbc776712..45bd6fffb4 100644 --- a/includes/context/ContextSource.php +++ b/includes/context/ContextSource.php @@ -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 diff --git a/includes/context/DerivativeContext.php b/includes/context/DerivativeContext.php index ee817dbd26..5adf3621b3 100644 --- a/includes/context/DerivativeContext.php +++ b/includes/context/DerivativeContext.php @@ -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 diff --git a/includes/context/IContextSource.php b/includes/context/IContextSource.php index ed7aba0359..476035b525 100644 --- a/includes/context/IContextSource.php +++ b/includes/context/IContextSource.php @@ -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 diff --git a/includes/context/RequestContext.php b/includes/context/RequestContext.php index 7f000410e0..1ffbc08ce2 100644 --- a/includes/context/RequestContext.php +++ b/includes/context/RequestContext.php @@ -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