From caf5df17aba64eb980a12f596ce98ec1f476c801 Mon Sep 17 00:00:00 2001 From: Alexandre Emsenhuber Date: Sat, 14 Jan 2012 14:27:46 +0000 Subject: [PATCH] Per Aaron, fix for r108274: added canUseWikiPage() to context objects to know whether getWikiPage() can be safely called --- includes/context/ContextSource.php | 17 +++++++++++++++- includes/context/DerivativeContext.php | 23 ++++++++++++++++++++- includes/context/IContextSource.php | 15 +++++++++++++- includes/context/RequestContext.php | 28 +++++++++++++++++++++++++- 4 files changed, 79 insertions(+), 4 deletions(-) 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 -- 2.20.1