r25653 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r25652‎ | r25653 | r25654 >
Date:02:08, 8 September 2007
Author:tstarling
Status:old
Tags:
Comment:
Moved EditPage::sectionAnchor() and EditPage::pseudoParseSectionAnchor() to Parser, names slightly changed. Replaced O(N^2) regexes with calls to StringUtils. This should fix the breakage of ActiveAbstract extension, which was calling EditPage::sectionAnchor() statically.

We could probably all benefit from implementing a general rule *not* to call random non-static functions statically. This style is very vulnerable to breakage on later refactoring.
Modified paths:
  • /trunk/phase3/includes/EditPage.php (modified) (history)
  • /trunk/phase3/includes/Parser.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/Parser.php
@@ -4873,6 +4873,61 @@
48744874 : $this->mTitle->getPrefixedText();
48754875 }
48764876 }
 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+ }
48774932 }
48784933
48794934 /**
Index: trunk/phase3/includes/EditPage.php
@@ -623,7 +623,7 @@
624624 * @return bool false if output is done, true if the rest of the form should be displayed
625625 */
626626 function attemptSave() {
627 - global $wgSpamRegex, $wgFilterCallback, $wgUser, $wgOut;
 627+ global $wgSpamRegex, $wgFilterCallback, $wgUser, $wgOut, $wgParser;
628628 global $wgMaxArticleSize;
629629
630630 $fname = 'EditPage::attemptSave';
@@ -841,10 +841,10 @@
842842 return true;
843843 }
844844 if( $this->summary != '' ) {
845 - $sectionanchor = $this->sectionAnchor( $this->summary );
 845+ $sectionanchor = $wgParser->guessSectionNameFromWikiText( $this->summary );
846846 # This is a new section, so create a link to the new section
847847 # in the revision summary.
848 - $cleanSummary = $this->pseudoParseSectionAnchor( $this->summary );
 848+ $cleanSummary = $wgParser->stripSectionName( $this->summary );
849849 $this->summary = wfMsgForContent( 'newsectionsummary', $cleanSummary );
850850 }
851851 } elseif( $this->section != '' ) {
@@ -855,7 +855,7 @@
856856 # we can't deal with anchors, includes, html etc in the header for now,
857857 # headline would need to be parsed to improve this
858858 if($hasmatch and strlen($matches[2]) > 0) {
859 - $sectionanchor = $this->sectionAnchor( $matches[2] );
 859+ $sectionanchor = $wgParser->guessSectionNameFromWikiText( $matches[2] );
860860 }
861861 }
862862 wfProfileOut( "$fname-sectionanchor" );
@@ -949,8 +949,9 @@
950950 $this->textbox1,
951951 $matches );
952952 if( !empty( $matches[2] ) ) {
 953+ global $wgParser;
953954 $this->summary = "/* " .
954 - $this->pseudoParseSectionAnchor(trim($matches[2])) .
 955+ $wgParser->stripSectionName(trim($matches[2])) .
955956 " */ ";
956957 }
957958 }
@@ -1607,37 +1608,12 @@
16081609 return true;
16091610 }
16101611
1611 - /// Strips a text string of wikitext for use in a section anchor
16121612 /**
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()
16231614 */
16241615 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 );
16421618 }
16431619
16441620 /**
@@ -1647,21 +1623,8 @@
16481624 * @private
16491625 */
16501626 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 );
16661629 }
16671630
16681631 /**

Follow-up revisions

RevisionCommit summaryAuthorDate
r25754Merged revisions 25607-25751 via svnmerge from...david23:02, 10 September 2007

Status & tagging log