r22051 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r22050‎ | r22051 | r22052 >
Date:20:05, 9 May 2007
Author:hashar
Status:old
Tags:
Comment:
Split out Linker::formatComment using two new private functions:
* Linker::formatAutoComments: handles "/* section name */" stuff
* Linker::formatLinksInComment: renders [[wikilinks|click me]]
Modified paths:
  • /trunk/phase3/includes/Linker.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/Linker.php
@@ -816,14 +816,31 @@
817817 function formatComment($comment, $title = NULL, $local = false) {
818818 wfProfileIn( __METHOD__ );
819819
820 - global $wgContLang;
 820+ # Sanitize text a bit:
821821 $comment = str_replace( "\n", " ", $comment );
822822 $comment = htmlspecialchars( $comment );
823823
824 - # The pattern for autogen comments is / * foo * /, which makes for
825 - # some nasty regex.
826 - # We look for all comments, match any text before and after the comment,
827 - # add a separator where needed and format the comment itself with CSS
 824+ # Render autocomments and make links:
 825+ $comment = $this->formatAutoComments( $comment, $title, $local );
 826+ $comment = $this->formatLinksInComment( $comment );
 827+
 828+ wfProfileOut( __METHOD__ );
 829+ return $comment;
 830+ }
 831+
 832+ /**
 833+ * The pattern for autogen comments is / * foo * /, which makes for
 834+ * some nasty regex.
 835+ * We look for all comments, match any text before and after the comment,
 836+ * add a separator where needed and format the comment itself with CSS
 837+ * Called by Linker::formatComment.
 838+ *
 839+ * @param $comment Comment text
 840+ * @param $title An optional title object used to links to sections
 841+ *
 842+ * @todo Document the $local parameter.
 843+ */
 844+ private function formatAutocomments( $comment, $title = NULL, $local = false ) {
828845 $match = array();
829846 while (preg_match('/(.*)\/\*\s*(.*?)\s*\*\/(.*)/', $comment,$match)) {
830847 $pre=$match[1];
@@ -857,10 +874,21 @@
858875 $comment=$pre.$auto.$post;
859876 }
860877
861 - # format regular and media links - all other wiki formatting
862 - # is ignored
 878+ return $comment;
 879+ }
 880+
 881+ /**
 882+ * Format regular and media links - all other wiki formatting is ignored
 883+ * Called by Linker::formatComment.
 884+ * @param $comment The comment text.
 885+ * @return Comment text with links using HTML.
 886+ */
 887+ private function formatLinksInComment( $comment ) {
 888+ global $wgContLang;
 889+
863890 $medians = '(?:' . preg_quote( Namespace::getCanonicalName( NS_MEDIA ), '/' ) . '|';
864891 $medians .= preg_quote( $wgContLang->getNsText( NS_MEDIA ), '/' ) . '):';
 892+
865893 while(preg_match('/\[\[:?(.*?)(\|(.*?))*\]\](.*)$/',$comment,$match)) {
866894 # Handle link renaming [[foo|text]] will show link as "text"
867895 if( "" != $match[3] ) {
@@ -887,7 +915,7 @@
888916 }
889917 $comment = preg_replace( $linkRegexp, StringUtils::escapeRegexReplacement( $thelink ), $comment, 1 );
890918 }
891 - wfProfileOut( __METHOD__ );
 919+
892920 return $comment;
893921 }
894922
@@ -912,7 +940,7 @@
913941 return " <span class=\"comment\">($formatted)</span>";
914942 }
915943 }
916 -
 944+
917945 /**
918946 * Wrap and format the given revision's comment block, if the current
919947 * user is allowed to view it.