Platonides has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/53189


Change subject: Allow redirecting Message objects.
......................................................................

Allow redirecting Message objects.

In order for a message to be redirectable, you must call
followRedirect() on it. Note that if it is redirected outside
of MediaWiki: namespace the contents won't be cached in the
MessageCache.

Change-Id: I14e35ef2d28f1bf8d57505f57ccf38d194498ac4
---
M includes/Message.php
1 file changed, 57 insertions(+), 0 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/core 
refs/changes/89/53189/1

diff --git a/includes/Message.php b/includes/Message.php
index 3bfaf4c..39763a4 100644
--- a/includes/Message.php
+++ b/includes/Message.php
@@ -456,6 +456,63 @@
        }
 
        /**
+        * If the message is a redirect, return the contents of the page
+        * it redirects to. This function follows only one level of redirects
+        * (although you can call it several times).
+        * If following the redirect fails, the message returns the
+        * raw wikitext used («#REDIRECT [[title]]»).
+        * @since 1.21
+        * @return Message: $this
+        */
+       public function followRedirect() {
+               $text = $this->fetchMessage();
+
+               if ( $text === false ) {
+                       return $this;
+               }
+
+               $content = new WikitextContent( $text );
+               $title = $content->getRedirectTarget();
+
+               if ( !$title ) {
+                       return $this;
+               }
+
+               if ( $title->getNamespace() == NS_MEDIAWIKI ) {
+                       /* Great, it will be in the message cache */
+                       $cache = MessageCache::singleton();
+                       $text = $cache->get( $title->getDBkey(), true, true );
+
+                       if ( $text ) {
+                               $this->message = $text;
+                       }
+
+                       return $this;
+               }
+
+               if ( !$title->exists() ) {
+                       /* Broken redirect */
+                       return $this;
+               }
+
+               $page = WikiPage::factory( $title );
+               $content = $page->getContent( Revision::FOR_PUBLIC );
+               if ( !$content ) {
+                       /* Missing page */
+                       return $this;
+               }
+
+               if ( !$content instanceof TextContent ) {
+                       /* This redirect makes no sense! */
+                       return $this;
+               }
+
+               $this->message = $content->getNativeData();
+
+               return $this;
+       }
+
+       /**
         * Returns the message parsed from wikitext to HTML.
         * @since 1.17
         * @return String: HTML

-- 
To view, visit https://gerrit.wikimedia.org/r/53189
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I14e35ef2d28f1bf8d57505f57ccf38d194498ac4
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/core
Gerrit-Branch: master
Gerrit-Owner: Platonides <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to