Merge "Removed "Justify paragraphs" from MW user Preference."
[lhc/web/wiklou.git] / includes / content / WikitextContent.php
index 26337db..a92699b 100644 (file)
@@ -31,7 +31,6 @@
  * @ingroup Content
  */
 class WikitextContent extends TextContent {
-
        public function __construct( $text ) {
                parent::__construct( $text, CONTENT_MODEL_WIKITEXT );
        }
@@ -73,11 +72,14 @@ class WikitextContent extends TextContent {
 
                if ( $section === '' ) {
                        wfProfileOut( __METHOD__ );
+
                        return $with; # XXX: copy first?
-               } if ( $section == 'new' ) {
+               }
+
+               if ( $section == 'new' ) {
                        # Inserting a new section
                        $subject = $sectionTitle ? wfMessage( 'newsectionheaderdefaultlevel' )
-                               ->rawParams( $sectionTitle )->inContentLanguage()->text() . "\n\n" : '';
+                                       ->rawParams( $sectionTitle )->inContentLanguage()->text() . "\n\n" : '';
                        if ( wfRunHooks( 'PlaceNewSection', array( $this, $oldtext, $subject, &$text ) ) ) {
                                $text = strlen( trim( $oldtext ) ) > 0
                                        ? "{$oldtext}\n\n{$subject}{$text}"
@@ -93,6 +95,7 @@ class WikitextContent extends TextContent {
                $newContent = new WikitextContent( $text );
 
                wfProfileOut( __METHOD__ );
+
                return $newContent;
        }
 
@@ -149,29 +152,27 @@ class WikitextContent extends TextContent {
        }
 
        /**
-        * Implement redirect extraction for wikitext.
-        *
-        * @return null|Title
+        * Extract the redirect target and the remaining text on the page.
         *
         * @note: migrated here from Title::newFromRedirectInternal()
         *
-        * @see Content::getRedirectTarget
-        * @see AbstractContent::getRedirectTarget
+        * @since 1.23
+        * @return array 2 elements: Title|null and string
         */
-       public function getRedirectTarget() {
+       protected function getRedirectTargetAndText() {
                global $wgMaxRedirects;
                if ( $wgMaxRedirects < 1 ) {
                        // redirects are disabled, so quit early
-                       return null;
+                       return array( null, $this->getNativeData() );
                }
                $redir = MagicWord::get( 'redirect' );
-               $text = trim( $this->getNativeData() );
+               $text = ltrim( $this->getNativeData() );
                if ( $redir->matchStartAndRemove( $text ) ) {
                        // Extract the first link and see if it's usable
                        // Ensure that it really does come directly after #REDIRECT
                        // Some older redirects included a colon, so don't freak about that!
                        $m = array();
-                       if ( preg_match( '!^\s*:?\s*\[{2}(.*?)(?:\|.*?)?\]{2}!', $text, $m ) ) {
+                       if ( preg_match( '!^\s*:?\s*\[{2}(.*?)(?:\|.*?)?\]{2}\s*!', $text, $m ) ) {
                                // Strip preceding colon used to "escape" categories, etc.
                                // and URL-decode links
                                if ( strpos( $m[1], '%' ) !== false ) {
@@ -181,16 +182,31 @@ class WikitextContent extends TextContent {
                                $title = Title::newFromText( $m[1] );
                                // If the title is a redirect to bad special pages or is invalid, return null
                                if ( !$title instanceof Title || !$title->isValidRedirectTarget() ) {
-                                       return null;
+                                       return array( null, $this->getNativeData() );
                                }
-                               return $title;
+
+                               return array( $title, substr( $text, strlen( $m[0] ) ) );
                        }
                }
-               return null;
+
+               return array( null, $this->getNativeData() );
        }
 
        /**
-        * @see   Content::updateRedirect()
+        * Implement redirect extraction for wikitext.
+        *
+        * @return null|Title
+        *
+        * @see Content::getRedirectTarget
+        * @see AbstractContent::getRedirectTarget
+        */
+       public function getRedirectTarget() {
+               list( $title, ) = $this->getRedirectTargetAndText();
+               return $title;
+       }
+
+       /**
+        * @see Content::updateRedirect()
         *
         * This implementation replaces the first link on the page with the given new target
         * if this Content object is a redirect. Otherwise, this method returns $this.
@@ -199,7 +215,8 @@ class WikitextContent extends TextContent {
         *
         * @param Title $target
         *
-        * @return Content a new Content object with the updated redirect (or $this if this Content object isn't a redirect)
+        * @return Content a new Content object with the updated redirect (or $this
+        *   if this Content object isn't a redirect)
         */
        public function updateRedirect( Title $target ) {
                if ( !$this->isRedirect() ) {
@@ -220,7 +237,7 @@ class WikitextContent extends TextContent {
         * Returns true if this content is not a redirect, and this content's text
         * is countable according to the criteria defined by $wgArticleCountMethod.
         *
-        * @param bool $hasLinks  if it is known whether this content contains
+        * @param bool $hasLinks if it is known whether this content contains
         *    links, provide this information here, to avoid redundant parsing to
         *    find out (default: null).
         * @param $title Title: (default: null)
@@ -297,7 +314,22 @@ class WikitextContent extends TextContent {
                        $options = $this->getContentHandler()->makeParserOptions( 'canonical' );
                }
 
-               $po = $wgParser->parse( $this->getNativeData(), $title, $options, true, true, $revId );
+               list( $redir, $text ) = $this->getRedirectTargetAndText();
+               $po = $wgParser->parse( $text, $title, $options, true, true, $revId );
+
+               // Add redirect indicator at the top
+               if ( $redir ) {
+                       // Make sure to include the redirect link in pagelinks
+                       $po->addLink( $redir );
+                       if ( $generateHtml ) {
+                               $chain = $this->getRedirectChain();
+                               $po->setText(
+                                       Article::getRedirectHeaderHtml( $title->getPageLanguage(), $chain, false ) .
+                                       $po->getText()
+                               );
+                       }
+               }
+
                return $po;
        }