r85985 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r85984‎ | r85985 | r85986 >
Date:19:46, 13 April 2011
Author:diebuche
Status:ok
Tags:
Comment:
Followup to r85922: Adapt to MW-coding style
Modified paths:
  • /trunk/phase3/includes/parser/Parser.php (modified) (history)
  • /trunk/phase3/tests/parser/parserTests.txt (modified) (history)

Diff [purge]

Index: trunk/phase3/tests/parser/parserTests.txt
@@ -5481,10 +5481,13 @@
54825482 !! input
54835483 ==a==
54845484 {| STYLE=__TOC__
 5485+|foo
54855486 !! result
54865487 <h2><span class="editsection">[<a href="https://www.mediawiki.org/index.php?title=Parser_test&amp;action=edit&amp;section=1" title="Edit section: a">edit</a>]</span> <span class="mw-headline" id="a">a</span></h2>
54875488 <table style="&#95;_TOC&#95;_">
5488 -<tr><td></td></tr>
 5489+<tr>
 5490+<td>foo</td>
 5491+</tr>
54895492 </table>
54905493
54915494 !! end
@@ -5531,12 +5534,10 @@
55325535 http://===r:::https://b
55335536
55345537 {|
 5538+
55355539 !!result
55365540 <p><a rel="nofollow" class="external free" href="http://===r:::https://b">http://===r:::https://b</a>
55375541 </p>
5538 -<table>
5539 -<tr><td></td></tr>
5540 -</table>
55415542
55425543 !! end
55435544
Index: trunk/phase3/includes/parser/Parser.php
@@ -829,21 +829,21 @@
830830 foreach ( $lines as $outLine ) {
831831 $line = trim( $outLine );
832832
833 - if ( $line === '') { //empty line, go to next line
834 - $out .= $outLine."\n";
 833+ if ( $line === '' ) { // empty line, go to next line
 834+ $out .= $outLine . "\n";
835835 continue;
836836 }
837 - $first_chars = $line[0];
838 - if ( strlen($line) > 1) {
839 - $first_chars .= in_array($line[1], array('}', '+', '-')) ? $line[1] : '';
 837+ $firstChars = $line[0];
 838+ if ( strlen( $line ) > 1 ) {
 839+ $firstChars .= in_array( $line[1], array( '}', '+', '-' ) ) ? $line[1] : '';
840840 }
841841 $matches = array();
842842
843843 if ( preg_match( '/^(:*)\{\|(.*)$/', $line , $matches ) ) {
844844 $tables[] = array();
845 - $table =& $this->last($tables);
846 - $table[0] = array(); //first row
847 - $current_row =& $table[0];
 845+ $table =& $this->last( $tables );
 846+ $table[0] = array(); // first row
 847+ $currentRow =& $table[0];
848848
849849 $table['indent'] = strlen( $matches[1] );
850850
@@ -853,79 +853,79 @@
854854 if ( $attributes !== '' ) {
855855 $table['attributes'] = $attributes;
856856 }
857 - } else if ( !isset($tables[0]) ) {
 857+ } else if ( !isset( $tables[0] ) ) {
858858 // we're outside the table
859859
860 - $out .= $outLine."\n";
861 - } else if ( $first_chars === '|}' ) {
 860+ $out .= $outLine . "\n";
 861+ } else if ( $firstChars === '|}' ) {
862862 // trim the |} code from the line
863863 $line = substr ( $line , 2 );
864864
865865 // Shorthand for last row
866 - $last_row =& $this->last($table);
 866+ $lastRow =& $this->last( $table );
867867
868868 // a thead at the end becomes a tfoot, unless there is only one row
869869 // Do this before deleting empty last lines to allow headers at the bottom of tables
870 - if ( isset($last_row['type'] ) && $last_row['type'] == 'thead' && isset($table[1])) {
871 - $last_row['type'] = 'tfoot';
872 - for($i = 0; isset($last_row[$i]); $i++ ) {
873 - $last_row[$i]['type'] = 'th';
 870+ if ( isset( $lastRow['type'] ) && $lastRow['type'] == 'thead' && isset( $table[1] ) ) {
 871+ $lastRow['type'] = 'tfoot';
 872+ for ( $i = 0; isset( $lastRow[$i] ); $i++ ) {
 873+ $lastRow[$i]['type'] = 'th';
874874 }
875875 }
876876
877877 // Delete empty last lines
878 - if ( empty($last_row) ) {
879 - $last_row = NULL;
 878+ if ( empty( $lastRow ) ) {
 879+ $lastRow = NULL;
880880 }
881 - $curtable = array_pop($tables);
882 - $o = $this->printTableHtml( $curtable ) . $line;
 881+ $curtable = array_pop( $tables );
 882+ $o = $this->generateTableHTML( $curtable ) . $line;
883883
884 - if ( count($tables) > 0 ) {
885 - $table =& $this->last($tables);
886 - $current_row =& $this->last($table);
887 - $current_element =& $this->last($current_row);
 884+ if ( count( $tables ) > 0 ) {
 885+ $table =& $this->last( $tables );
 886+ $currentRow =& $this->last( $table );
 887+ $currentElement =& $this->last( $currentRow );
888888
889 - $output =& $current_element['content'];
 889+ $output =& $currentElement['content'];
890890 } else {
891891 $output =& $out;
892892 }
893893
894894 $output .= $o;
895895
896 - } else if ( $first_chars === '|-' ) {
 896+ } else if ( $firstChars === '|-' ) {
897897 // start a new row element
898898 // but only when we haven't started one already
899 - if( count($current_row) != 0 ) {
 899+ if ( count( $currentRow ) != 0 ) {
900900 $table[] = array();
901 - $current_row =& $this->last($table);
 901+ $currentRow =& $this->last( $table );
902902 }
903903 // Get the attributes, there's nothing else useful in $line now
904904 $line = substr ( $line , 2 );
905905 $attributes = $this->mStripState->unstripBoth( $line );
906906 $attributes = Sanitizer::fixTagAttributes( $attributes, 'tr' );
907 - if( $attributes !== '') {
908 - $current_row['attributes'] = $attributes;
 907+ if ( $attributes !== '' ) {
 908+ $currentRow['attributes'] = $attributes;
909909 }
910910
911 - } else if ( $first_chars === '|+' ) {
 911+ } else if ( $firstChars === '|+' ) {
912912 // a table caption
913913 $line = substr ( $line , 2 );
914914
915 - $c = $this->getCellAttr($line , 'caption');
 915+ $c = $this->getCellAttr( $line , 'caption' );
916916 $table['caption'] = array();
917917 $table['caption']['content'] = $c[0];
918 - if(isset($c[1])) $table['caption']['attributes'] = $c[1];
919 - unset($c);
 918+ if ( isset( $c[1] ) ) $table['caption']['attributes'] = $c[1];
 919+ unset( $c );
920920
921921 $output =& $table['caption'];
922 - } else if ( $first_chars === '|' || $first_chars === '!' || $first_chars === '!+' ) {
 922+ } else if ( $firstChars === '|' || $firstChars === '!' || $firstChars === '!+' ) {
923923 // Which kind of cells are we dealing with
924 - $this_tag = 'td';
 924+ $currentTag = 'td';
925925 $line = substr ( $line , 1 );
926926
927 - if ( $first_chars === '!' || $first_chars === '!+' ) {
 927+ if ( $firstChars === '!' || $firstChars === '!+' ) {
928928 $line = str_replace ( '!!' , '||' , $line );
929 - $this_tag = 'th';
 929+ $currentTag = 'th';
930930 }
931931
932932 // Split up multiple cells on the same line.
@@ -933,29 +933,29 @@
934934 $line = ''; // save memory
935935
936936 // decide whether thead to tbody
937 - if ( !array_key_exists('type', $current_row) ) {
938 - $current_row['type'] = ( $first_chars === '!' ) ? 'thead' : 'tbody' ;
939 - } else if( $first_chars === '|' ) {
940 - $current_row['type'] = 'tbody';
 937+ if ( !array_key_exists( 'type', $currentRow ) ) {
 938+ $currentRow['type'] = ( $firstChars === '!' ) ? 'thead' : 'tbody' ;
 939+ } else if ( $firstChars === '|' ) {
 940+ $currentRow['type'] = 'tbody';
941941 }
942942
943943 // Loop through each table cell
944944 foreach ( $cells as $cell ) {
945945 // a new cell
946 - $current_row[] = array();
947 - $current_element =& $this->last($current_row);
 946+ $currentRow[] = array();
 947+ $currentElement =& $this->last( $currentRow );
948948
949 - $current_element['type'] = $this_tag;
 949+ $currentElement['type'] = $currentTag;
950950
951 - $c = $this->getCellAttr($cell , $this_tag);
952 - $current_element['content'] = $c[0];
953 - if(isset($c[1])) $current_element['attributes'] = $c[1];
954 - unset($c);
 951+ $c = $this->getCellAttr( $cell , $currentTag );
 952+ $currentElement['content'] = $c[0];
 953+ if ( isset( $c[1] ) ) $currentElement['attributes'] = $c[1];
 954+ unset( $c );
955955 }
956 - $output =& $current_element['content'];
 956+ $output =& $currentElement['content'];
957957
958958 } else {
959 - $output .= $outLine."\n";
 959+ $output .= $outLine . "\n";
960960 }
961961 }
962962
@@ -964,11 +964,11 @@
965965 $out = substr( $out, 0, -1 );
966966 }
967967
968 - #Close any unclosed tables
969 - if (isset($tables) && count($tables) > 0 ) {
970 - for ($i = 0; $i < count($tables); $i++) {
971 - $curtable = array_pop($tables);
972 - $out .= $this->printTableHtml( $curtable );
 968+ # Close any unclosed tables
 969+ if ( isset( $tables ) && count( $tables ) > 0 ) {
 970+ for ( $i = 0; $i < count( $tables ); $i++ ) {
 971+ $curtable = array_pop( $tables );
 972+ $out .= $this->generateTableHTML( $curtable );
973973 }
974974 }
975975
@@ -985,30 +985,30 @@
986986 *
987987 * @private
988988 */
989 - function getCellAttr ($cell , $tag_name) {
 989+ function getCellAttr ( $cell, $tagName ) {
990990 $content = null;
991991 $attributes = null;
992992
993993 $cell = trim ( $cell );
994994
995995 // A cell could contain both parameters and data
996 - $cell_data = explode ( '|' , $cell , 2 );
 996+ $cellData = explode ( '|' , $cell , 2 );
997997
998998 // Bug 553: Note that a '|' inside an invalid link should not
999999 // be mistaken as delimiting cell parameters
1000 - if ( strpos( $cell_data[0], '[[' ) !== false ) {
 1000+ if ( strpos( $cellData[0], '[[' ) !== false ) {
10011001 $content = trim ( $cell );
10021002 }
1003 - else if ( count ( $cell_data ) == 1 ) {
1004 - $content = trim ( $cell_data[0] );
 1003+ else if ( count ( $cellData ) == 1 ) {
 1004+ $content = trim ( $cellData[0] );
10051005 }
10061006 else {
1007 - $attributes = $this->mStripState->unstripBoth( $cell_data[0] );
1008 - $attributes = Sanitizer::fixTagAttributes( $attributes , $tag_name );
 1007+ $attributes = $this->mStripState->unstripBoth( $cellData[0] );
 1008+ $attributes = Sanitizer::fixTagAttributes( $attributes , $tagName );
10091009
1010 - $content = trim ( $cell_data[1] );
 1010+ $content = trim ( $cellData[1] );
10111011 }
1012 - return array($content, $attributes);
 1012+ return array( $content, $attributes );
10131013 }
10141014
10151015
@@ -1017,79 +1017,79 @@
10181018 *
10191019 * @private
10201020 */
1021 - function printTableHtml (&$t) {
1022 - $r = "\n";
1023 - $r .= str_repeat( '<dl><dd>' , $t['indent'] );
1024 - $r .= '<table';
1025 - $r .= isset($t['attributes']) ? $t['attributes'] : '';
1026 - $r .= '>';
1027 - unset($t['attributes']);
 1021+ function generateTableHTML ( &$table ) {
 1022+ $return = "\n";
 1023+ $return .= str_repeat( '<dl><dd>' , $table['indent'] );
 1024+ $return .= '<table';
 1025+ $return .= isset( $table['attributes'] ) ? $table['attributes'] : '';
 1026+ $return .= '>';
 1027+ unset( $table['attributes'] );
10281028
1029 - if ( isset($t['caption']) ) {
1030 - $r .= "\n<caption";
1031 - $r .= isset($t['caption']['attributes']) ? $t['caption']['attributes'] : '';
1032 - $r .= '>';
1033 - $r .= $t['caption']['content'];
1034 - $r .= '</caption>';
 1029+ if ( isset( $table['caption'] ) ) {
 1030+ $return .= "\n<caption";
 1031+ $return .= isset( $table['caption']['attributes'] ) ? $table['caption']['attributes'] : '';
 1032+ $return .= '>';
 1033+ $return .= $table['caption']['content'];
 1034+ $return .= '</caption>';
10351035 }
1036 - $last_section = '';
 1036+ $lastSection = '';
10371037 $empty = true;
10381038 $simple = true;
10391039
1040 - //If we only have tbodies, mark table as simple
1041 - for($i = 0; isset($t[$i]); $i++ ) {
1042 - if ( !count( $t[$i]) ) continue;
1043 - if ( !$last_section ) {
1044 - $last_section = $t[$i]['type'];
1045 - } else if ($last_section != $t[$i]['type']) {
1046 - $simple = false;
1047 - break;
1048 - }
 1040+ // If we only have tbodies, mark table as simple
 1041+ for ( $i = 0; isset( $table[$i] ); $i++ ) {
 1042+ if ( !count( $table[$i] ) ) continue;
 1043+ if ( !$lastSection ) {
 1044+ $lastSection = $table[$i]['type'];
 1045+ } else if ( $lastSection != $table[$i]['type'] ) {
 1046+ $simple = false;
 1047+ break;
 1048+ }
10491049 }
1050 - $last_section = '';
1051 - for($i = 0; isset($t[$i]); $i++ ) {
 1050+ $lastSection = '';
 1051+ for ( $i = 0; isset( $table[$i] ); $i++ ) {
10521052 // Check for empty tables
1053 - if ( count( $t[$i]) ) {
 1053+ if ( count( $table[$i] ) ) {
10541054 $empty = false;
10551055 } else {
1056 - continue;
 1056+ continue;
10571057 }
1058 - if( $t[$i]['type'] != $last_section && !$simple ) {
1059 - $r .= "\n<" . $t[$i]['type'] . '>';
 1058+ if ( $table[$i]['type'] != $lastSection && !$simple ) {
 1059+ $return .= "\n<" . $table[$i]['type'] . '>';
10601060 }
10611061
1062 - $r .= "\n<tr";
1063 - $r .= isset($t[$i]['attributes']) ? $t[$i]['attributes'] : '';
1064 - $r .= '>';
1065 - for($j = 0; isset($t[$i][$j]); $j++ ) {
1066 - $r .= "\n<" . $t[$i][$j]['type'];
1067 - $r .= isset($t[$i][$j]['attributes']) ? $t[$i][$j]['attributes'] : '';
1068 - $r .= '>';
 1062+ $return .= "\n<tr";
 1063+ $return .= isset( $table[$i]['attributes'] ) ? $table[$i]['attributes'] : '';
 1064+ $return .= '>';
 1065+ for ( $j = 0; isset( $table[$i][$j] ); $j++ ) {
 1066+ $return .= "\n<" . $table[$i][$j]['type'];
 1067+ $return .= isset( $table[$i][$j]['attributes'] ) ? $table[$i][$j]['attributes'] : '';
 1068+ $return .= '>';
10691069
1070 - $r .= $t[$i][$j]['content'];
 1070+ $return .= $table[$i][$j]['content'];
10711071
1072 - $r .= '</' . $t[$i][$j]['type'] . '>';
1073 - unset($t[$i][$j]);
 1072+ $return .= '</' . $table[$i][$j]['type'] . '>';
 1073+ unset( $table[$i][$j] );
10741074 }
1075 - $r .= "\n</tr>";
 1075+ $return .= "\n</tr>";
10761076
1077 - if( ( !isset($t[$i+1]) && !$simple )|| ( isset($t[$i+1]) && ($t[$i]['type'] != $t[$i+1]['type'])) ) {
1078 - $r .= '</' . $t[$i]['type'] . '>';
 1077+ if ( ( !isset( $table[$i + 1] ) && !$simple ) || ( isset( $table[$i + 1] ) && ( $table[$i]['type'] != $table[$i + 1]['type'] ) ) ) {
 1078+ $return .= '</' . $table[$i]['type'] . '>';
10791079 }
1080 - $last_section = $t[$i]['type'];
1081 - unset($t[$i]);
 1080+ $lastSection = $table[$i]['type'];
 1081+ unset( $table[$i] );
10821082 }
10831083 if ( $empty ) {
1084 - if ( isset($t['caption']) ) {
1085 - $r .= "\n<tr><td></td></tr>";
 1084+ if ( isset( $table['caption'] ) ) {
 1085+ $return .= "\n<tr><td></td></tr>";
10861086 } else {
10871087 return '';
10881088 }
10891089 }
1090 - $r .= "\n</table>";
1091 - $r .= str_repeat( '</dd></dl>' , $t['indent'] );
 1090+ $return .= "\n</table>";
 1091+ $return .= str_repeat( '</dd></dl>' , $table['indent'] );
10921092
1093 - return $r;
 1093+ return $return;
10941094 }
10951095
10961096 /**
@@ -1100,8 +1100,8 @@
11011101 *
11021102 * @private
11031103 */
1104 - function &last (&$arr) {
1105 - for($i = count($arr); (!isset($arr[$i]) && $i > 0); $i--) { }
 1104+ function &last ( &$arr ) {
 1105+ for ( $i = count( $arr ); ( !isset( $arr[$i] ) && $i > 0 ); $i-- ) { }
11061106 return $arr[$i];
11071107 }
11081108

Past revisions this follows-up on

RevisionCommit summaryAuthorDate
r85922diebuche21:27, 12 April 2011

Status & tagging log