Index: trunk/phase3/includes/Parser.php |
— | — | @@ -4873,6 +4873,61 @@ |
4874 | 4874 | : $this->mTitle->getPrefixedText(); |
4875 | 4875 | } |
4876 | 4876 | } |
| 4877 | + |
| 4878 | + /** |
| 4879 | + * Try to guess the section anchor name based on a wikitext fragment |
| 4880 | + * presumably extracted from a heading, for example "Header" from |
| 4881 | + * "== Header ==". |
| 4882 | + */ |
| 4883 | + public function guessSectionNameFromWikiText( $text ) { |
| 4884 | + # Strip out wikitext links(they break the anchor) |
| 4885 | + $text = $this->stripSectionName( $text ); |
| 4886 | + $headline = Sanitizer::decodeCharReferences( $text ); |
| 4887 | + # strip out HTML |
| 4888 | + $headline = StringUtils::delimiterReplace( '<', '>', '', $headline ); |
| 4889 | + $headline = trim( $headline ); |
| 4890 | + $sectionanchor = '#' . urlencode( str_replace( ' ', '_', $headline ) ); |
| 4891 | + $replacearray = array( |
| 4892 | + '%3A' => ':', |
| 4893 | + '%' => '.' |
| 4894 | + ); |
| 4895 | + return str_replace( |
| 4896 | + array_keys( $replacearray ), |
| 4897 | + array_values( $replacearray ), |
| 4898 | + $sectionanchor ); |
| 4899 | + } |
| 4900 | + |
| 4901 | + /** |
| 4902 | + * Strips a text string of wikitext for use in a section anchor |
| 4903 | + * |
| 4904 | + * Accepts a text string and then removes all wikitext from the |
| 4905 | + * string and leaves only the resultant text (i.e. the result of |
| 4906 | + * [[User:WikiSysop|Sysop]] would be "Sysop" and the result of |
| 4907 | + * [[User:WikiSysop]] would be "User:WikiSysop") - this is intended |
| 4908 | + * to create valid section anchors by mimicing the output of the |
| 4909 | + * parser when headings are parsed. |
| 4910 | + * |
| 4911 | + * @param $text string Text string to be stripped of wikitext |
| 4912 | + * for use in a Section anchor |
| 4913 | + * @return Filtered text string |
| 4914 | + */ |
| 4915 | + public function stripSectionName( $text ) { |
| 4916 | + # Strip internal link markup |
| 4917 | + $text = preg_replace('/\[\[:?([^[|]+)\|([^[]+)\]\]/','$2',$text); |
| 4918 | + $text = preg_replace('/\[\[:?([^[]+)\|?\]\]/','$1',$text); |
| 4919 | + |
| 4920 | + # Strip external link markup (FIXME: Not Tolerant to blank link text |
| 4921 | + # I.E. [http://www.mediawiki.org] will render as [1] or something depending |
| 4922 | + # on how many empty links there are on the page - need to figure that out. |
| 4923 | + $text = preg_replace('/\[(?:' . wfUrlProtocols() . ')([^ ]+?) ([^[]+)\]/','$2',$text); |
| 4924 | + |
| 4925 | + # Parse wikitext quotes (italics & bold) |
| 4926 | + $text = $this->doQuotes($text); |
| 4927 | + |
| 4928 | + # Strip HTML tags |
| 4929 | + $text = StringUtils::delimiterReplace( '<', '>', '', $text ); |
| 4930 | + return $text; |
| 4931 | + } |
4877 | 4932 | } |
4878 | 4933 | |
4879 | 4934 | /** |
Index: trunk/phase3/includes/EditPage.php |
— | — | @@ -623,7 +623,7 @@ |
624 | 624 | * @return bool false if output is done, true if the rest of the form should be displayed |
625 | 625 | */ |
626 | 626 | function attemptSave() { |
627 | | - global $wgSpamRegex, $wgFilterCallback, $wgUser, $wgOut; |
| 627 | + global $wgSpamRegex, $wgFilterCallback, $wgUser, $wgOut, $wgParser; |
628 | 628 | global $wgMaxArticleSize; |
629 | 629 | |
630 | 630 | $fname = 'EditPage::attemptSave'; |
— | — | @@ -841,10 +841,10 @@ |
842 | 842 | return true; |
843 | 843 | } |
844 | 844 | if( $this->summary != '' ) { |
845 | | - $sectionanchor = $this->sectionAnchor( $this->summary ); |
| 845 | + $sectionanchor = $wgParser->guessSectionNameFromWikiText( $this->summary ); |
846 | 846 | # This is a new section, so create a link to the new section |
847 | 847 | # in the revision summary. |
848 | | - $cleanSummary = $this->pseudoParseSectionAnchor( $this->summary ); |
| 848 | + $cleanSummary = $wgParser->stripSectionName( $this->summary ); |
849 | 849 | $this->summary = wfMsgForContent( 'newsectionsummary', $cleanSummary ); |
850 | 850 | } |
851 | 851 | } elseif( $this->section != '' ) { |
— | — | @@ -855,7 +855,7 @@ |
856 | 856 | # we can't deal with anchors, includes, html etc in the header for now, |
857 | 857 | # headline would need to be parsed to improve this |
858 | 858 | if($hasmatch and strlen($matches[2]) > 0) { |
859 | | - $sectionanchor = $this->sectionAnchor( $matches[2] ); |
| 859 | + $sectionanchor = $wgParser->guessSectionNameFromWikiText( $matches[2] ); |
860 | 860 | } |
861 | 861 | } |
862 | 862 | wfProfileOut( "$fname-sectionanchor" ); |
— | — | @@ -949,8 +949,9 @@ |
950 | 950 | $this->textbox1, |
951 | 951 | $matches ); |
952 | 952 | if( !empty( $matches[2] ) ) { |
| 953 | + global $wgParser; |
953 | 954 | $this->summary = "/* " . |
954 | | - $this->pseudoParseSectionAnchor(trim($matches[2])) . |
| 955 | + $wgParser->stripSectionName(trim($matches[2])) . |
955 | 956 | " */ "; |
956 | 957 | } |
957 | 958 | } |
— | — | @@ -1607,37 +1608,12 @@ |
1608 | 1609 | return true; |
1609 | 1610 | } |
1610 | 1611 | |
1611 | | - /// Strips a text string of wikitext for use in a section anchor |
1612 | 1612 | /** |
1613 | | - * Accepts a text string and then removes all wikitext from the |
1614 | | - * string and leaves only the resultant text (i.e. the result of |
1615 | | - * [[User:WikiSysop|Sysop]] would be "Sysop" and the result of |
1616 | | - * [[User:WikiSysop]] would be "User:WikiSysop") - this is intended |
1617 | | - * to create valid section anchors by mimicing the output of the |
1618 | | - * parser when headings are parsed. |
1619 | | - * |
1620 | | - * @param $text string Text string to be stripped of wikitext |
1621 | | - * for use in a Section anchor |
1622 | | - * @return Filtered text string |
| 1613 | + * @deprecated use $wgParser->stripSectionName() |
1623 | 1614 | */ |
1624 | 1615 | function pseudoParseSectionAnchor( $text ) { |
1625 | | - # Pseudo-Parse sectionanchor |
1626 | | - |
1627 | | - # Strip internal link markup |
1628 | | - $text = preg_replace('/\[\[:?([^[|]+)\|([^[]+)\]\]/','$2',$text); |
1629 | | - $text = preg_replace('/\[\[:?([^[]+)\|?\]\]/','$1',$text); |
1630 | | - |
1631 | | - # Strip external link markup (FIXME: Not Tolerant to blank link text |
1632 | | - # I.E. [http://www.mediawiki.org] will render as [1] or something depending |
1633 | | - # on how many empty links there are on the page - need to figure that out. |
1634 | | - $text = preg_replace('/\[(?:' . wfUrlProtocols() . ')([^ ]+?) ([^[]+)\]/','$2',$text); |
1635 | | - |
1636 | | - # Parse wikitext quotes (italics & bold) |
1637 | | - $text = Parser::doQuotes($text); |
1638 | | - |
1639 | | - # Strip HTML tags |
1640 | | - $text = preg_replace( '/<.*?' . '>/', '', $text ); |
1641 | | - return $text; |
| 1616 | + global $wgParser; |
| 1617 | + return $wgParser->stripSectionName( $text ); |
1642 | 1618 | } |
1643 | 1619 | |
1644 | 1620 | /** |
— | — | @@ -1647,21 +1623,8 @@ |
1648 | 1624 | * @private |
1649 | 1625 | */ |
1650 | 1626 | function sectionAnchor( $text ) { |
1651 | | - # Strip out wikitext links(they break the anchor) |
1652 | | - $text = $this->pseudoParseSectionAnchor( $text ); |
1653 | | - $headline = Sanitizer::decodeCharReferences( $text ); |
1654 | | - # strip out HTML |
1655 | | - $headline = preg_replace( '/<.*?' . '>/', '', $headline ); |
1656 | | - $headline = trim( $headline ); |
1657 | | - $sectionanchor = '#' . urlencode( str_replace( ' ', '_', $headline ) ); |
1658 | | - $replacearray = array( |
1659 | | - '%3A' => ':', |
1660 | | - '%' => '.' |
1661 | | - ); |
1662 | | - return str_replace( |
1663 | | - array_keys( $replacearray ), |
1664 | | - array_values( $replacearray ), |
1665 | | - $sectionanchor ); |
| 1627 | + global $wgParser; |
| 1628 | + return $wgParser->guessSectionNameFromWikiText( $text ); |
1666 | 1629 | } |
1667 | 1630 | |
1668 | 1631 | /** |