r82585 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r82584‎ | r82585 | r82586 >
Date:00:07, 22 February 2011
Author:simetrical
Status:ok (Comments)
Tags:
Comment:
Style fixes for Cite_body.php

Tested only with php -l.
Modified paths:
  • /trunk/extensions/Cite/Cite_body.php (modified) (history)

Diff [purge]

Index: trunk/extensions/Cite/Cite_body.php
@@ -21,7 +21,7 @@
2222 /**#@+
2323 * @access private
2424 */
25 -
 25+
2626 /**
2727 * Datastructure representing <ref> input, in the format of:
2828 * <code>
@@ -54,7 +54,7 @@
5555 * @var array
5656 **/
5757 var $mRefs = array();
58 -
 58+
5959 /**
6060 * Count for user displayed output (ref[1], ref[2], ...)
6161 *
@@ -88,16 +88,16 @@
8989 * @var array
9090 */
9191 var $mLinkLabels = array();
92 -
 92+
9393 /**
9494 * @var object
9595 */
9696 var $mParser;
97 -
 97+
9898 /**
9999 * True when a <ref> tag is being processed.
100100 * Used to avoid infinite recursion
101 - *
 101+ *
102102 * @var boolean
103103 */
104104 var $mInCite = false;
@@ -105,30 +105,30 @@
106106 /**
107107 * True when a <references> tag is being processed.
108108 * Used to detect the use of <references> to define refs
109 - *
 109+ *
110110 * @var boolean
111111 */
112112 var $mInReferences = false;
113113
114114 /**
115115 * Error stack used when defining refs in <references>
116 - *
 116+ *
117117 * @var array
118118 */
119119 var $mReferencesErrors = array();
120120
121121 /**
122122 * Group used when in <references> block
123 - *
 123+ *
124124 * @var string
125125 */
126126 var $mReferencesGroup = '';
127127
128128 /**
129 - * <ref> call stack
 129+ * <ref> call stack
130130 * Used to cleanup out of sequence ref calls created by #tag
131131 * See description of function rollbackRef.
132 - *
 132+ *
133133 * @var array
134134 */
135135 var $mRefCallStack = array();
@@ -164,10 +164,10 @@
165165
166166 function guardedRef( $str, $argv, $parser, $default_group = CITE_DEFAULT_GROUP ) {
167167 $this->mParser = $parser;
168 -
 168+
169169 # The key here is the "name" attribute.
170170 list( $key, $group, $follow ) = $this->refArg( $argv );
171 -
 171+
172172 # Split these into groups.
173173 if ( $group === null ) {
174174 if ( $this->mInReferences ) {
@@ -176,8 +176,8 @@
177177 $group = $default_group;
178178 }
179179 }
180 -
181 - # This section deals with constructions of the form
 180+
 181+ # This section deals with constructions of the form
182182 #
183183 # <references>
184184 # <ref name="foo"> BAR </ref>
@@ -224,7 +224,7 @@
225225 $str = null;
226226 }
227227 }
228 -
 228+
229229 if ( $key === false ) {
230230 # TODO: Comment this case; what does this condition mean?
231231 $this->mRefCallStack[] = false;
@@ -236,7 +236,7 @@
237237 $this->mRefCallStack[] = false;
238238 return $this->error( 'cite_error_ref_no_key' );
239239 }
240 -
 240+
241241 if ( preg_match( '/^[0-9]+$/', $key ) || preg_match( '/^[0-9]+$/', $follow ) ) {
242242 # Numeric names mess up the resulting id's, potentially produ-
243243 # cing duplicate id's in the XHTML. The Right Thing To Do
@@ -283,7 +283,7 @@
284284
285285 /**
286286 * Parse the arguments to the <ref> tag
287 - *
 287+ *
288288 * "name" : Key of the reference.
289289 * "group" : Group to which it belongs. Needs to be passed to <references /> too.
290290 * "follow" : If the current reference is the continuation of another, key of that reference.
@@ -301,11 +301,11 @@
302302 $key = null;
303303 $follow = null;
304304
305 - if ( $cnt > 2 )
 305+ if ( $cnt > 2 ) {
306306 // There should only be one key or follow parameter, and one group parameter
307 - // FIXME : this looks inconsistent, it should probably return a tuple
 307+ // FIXME : this looks inconsistent, it should probably return a tuple
308308 return false;
309 - else if ( $cnt >= 1 ) {
 309+ } elseif ( $cnt >= 1 ) {
310310 if ( isset( $argv['name'] ) && isset( $argv['follow'] ) ) {
311311 return array( false, false, false );
312312 }
@@ -322,22 +322,26 @@
323323 --$cnt;
324324 }
325325 if ( isset( $argv['group'] ) ) {
326 - if ( ! $wgAllowCiteGroups ) return array( false ); // remove when groups are fully tested.
 326+ if ( !$wgAllowCiteGroups ) {
 327+ // remove when groups are fully tested.
 328+ return array( false );
 329+ }
327330 // Group given.
328331 $group = $argv['group'];
329332 unset( $argv['group'] );
330333 --$cnt;
331334 }
332335
333 - if ( $cnt == 0 )
 336+ if ( $cnt == 0 ) {
334337 return array ( $key, $group, $follow );
335 - else
 338+ } else {
336339 // Invalid key
337340 return array( false, false, false );
338 - }
339 - else
 341+ }
 342+ } else {
340343 // No key
341344 return array( null, $group, false );
 345+ }
342346 }
343347
344348 /**
@@ -345,31 +349,35 @@
346350 *
347351 * @param string $str Input from the <ref> tag
348352 * @param mixed $key Argument to the <ref> tag as returned by $this->refArg()
349 - * @return string
 353+ * @return string
350354 */
351355 function stack( $str, $key = null, $group, $follow, $call ) {
352 - if ( ! isset( $this->mRefs[$group] ) )
 356+ if ( !isset( $this->mRefs[$group] ) ) {
353357 $this->mRefs[$group] = array();
354 - if ( ! isset( $this->mGroupCnt[$group] ) )
 358+ }
 359+ if ( !isset( $this->mGroupCnt[$group] ) ) {
355360 $this->mGroupCnt[$group] = 0;
 361+ }
356362
357363 if ( $follow != null ) {
358364 if ( isset( $this->mRefs[$group][$follow] ) && is_array( $this->mRefs[$group][$follow] ) ) {
359365 // add text to the note that is being followed
360 - $this->mRefs[$group][$follow]['text'] = $this->mRefs[$group][$follow]['text'] . ' '. $str;
 366+ $this->mRefs[$group][$follow]['text'] = $this->mRefs[$group][$follow]['text'] . ' ' . $str;
361367 } else {
362368 // insert part of note at the beginning of the group
363 - for( $k=0 ; $k< count( $this->mRefs[$group] ) ; $k++) {
364 - if( $this->mRefs[$group][$k]['follow'] == null ) break;
 369+ for ( $k = 0 ; $k < count( $this->mRefs[$group] ) ; $k++ ) {
 370+ if ( $this->mRefs[$group][$k]['follow'] == null ) {
 371+ break;
 372+ }
365373 }
366 - array_splice( $this->mRefs[$group], $k, 0,
367 - array( array( 'count' => - 1,
368 - 'text' => $str,
369 - 'key' => ++$this->mOutCnt ,
370 - 'follow' => $follow) ) );
371 - array_splice( $this->mRefCallStack, $k, 0,
 374+ array_splice( $this->mRefs[$group], $k, 0,
 375+ array( array( 'count' => - 1,
 376+ 'text' => $str,
 377+ 'key' => ++$this->mOutCnt ,
 378+ 'follow' => $follow ) ) );
 379+ array_splice( $this->mRefCallStack, $k, 0,
372380 array( array( 'new', $call, $str, $key, $group, $this->mOutCnt ) ) );
373 - $this->mInCnt++;
 381+ $this->mInCnt++;
374382 }
375383 // return an empty string : this is not a reference
376384 return '';
@@ -381,10 +389,10 @@
382390 $this->mRefCallStack[] = array( 'new', $call, $str, $key, $group, $this->mOutCnt );
383391
384392 return $this->linkRef( $group, $this->mInCnt++ );
385 - } else if ( is_string( $key ) ) {
 393+ } elseif ( is_string( $key ) ) {
386394 // Valid key
387 - if ( ! isset( $this->mRefs[$group][$key] ) || ! is_array( $this->mRefs[$group][$key] ) ) {
388 - // First occurance
 395+ if ( !isset( $this->mRefs[$group][$key] ) || !is_array( $this->mRefs[$group][$key] ) ) {
 396+ // First occurrence
389397 $this->mRefs[$group][$key] = array(
390398 'text' => $str,
391399 'count' => 0,
@@ -420,19 +428,19 @@
421429 $this->mRefs[$group][$key]['key'] . "-" . ++$this->mRefs[$group][$key]['count'],
422430 $this->mRefs[$group][$key]['number'],
423431 "-" . $this->mRefs[$group][$key]['key']
424 - ); }
 432+ );
 433+ }
 434+ } else {
 435+ $this->croak( 'cite_error_stack_invalid_input', serialize( array( $key, $str ) ) );
425436 }
 437+ }
426438
427 - else
428 - $this->croak( 'cite_error_stack_invalid_input', serialize( array( $key, $str ) ) );
429 - }
430 -
431439 /**
432440 * Partially undoes the effect of calls to stack()
433 - *
 441+ *
434442 * Called by guardedReferences()
435443 *
436 - * The option to define <ref> within <references> makes the
 444+ * The option to define <ref> within <references> makes the
437445 * behavior of <ref> context dependent. This is normally fine
438446 * but certain operations (especially #tag) lead to out-of-order
439447 * parser evaluation with the <ref> tags being processed before
@@ -444,7 +452,9 @@
445453 * corrupting certain links.
446454 */
447455 function rollbackRef( $type, $key, $group, $index ) {
448 - if ( !isset( $this->mRefs[$group] ) ) { return; }
 456+ if ( !isset( $this->mRefs[$group] ) ) {
 457+ return;
 458+ }
449459
450460 if ( $key === null ) {
451461 foreach ( $this->mRefs[$group] as $k => $v ) {
@@ -456,11 +466,17 @@
457467 }
458468
459469 # Sanity checks that specified element exists.
460 - if ( $key === null ) { return; }
461 - if ( !isset( $this->mRefs[$group][$key] ) ) { return; }
462 - if ( $this->mRefs[$group][$key]['key'] != $index ) { return; }
 470+ if ( $key === null ) {
 471+ return;
 472+ }
 473+ if ( !isset( $this->mRefs[$group][$key] ) ) {
 474+ return;
 475+ }
 476+ if ( $this->mRefs[$group][$key]['key'] != $index ) {
 477+ return;
 478+ }
463479
464 - switch( $type ) {
 480+ switch ( $type ) {
465481 case 'new':
466482 # Rollback the addition of new elements to the stack.
467483 unset( $this->mRefs[$group][$key] );
@@ -506,22 +522,24 @@
507523 global $wgAllowCiteGroups;
508524
509525 $this->mParser = $parser;
510 -
 526+
511527 if ( isset( $argv['group'] ) and $wgAllowCiteGroups ) {
512528 $group = $argv['group'];
513529 unset ( $argv['group'] );
514530 }
515 -
 531+
516532 if ( strval( $str ) !== '' ) {
517533 $this->mReferencesGroup = $group;
518 -
 534+
519535 # Detect whether we were sent already rendered <ref>s
520536 # Mostly a side effect of using #tag to call references
521537 $count = substr_count( $str, $parser->uniqPrefix() . "-ref-" );
522538 for ( $i = 1; $i <= $count; $i++ ) {
523 - if ( count( $this->mRefCallStack ) < 1 ) break;
 539+ if ( count( $this->mRefCallStack ) < 1 ) {
 540+ break;
 541+ }
524542
525 - # The following assumes that the parsed <ref>s sent within
 543+ # The following assumes that the parsed <ref>s sent within
526544 # the <references> block were the most recent calls to
527545 # <ref>. This assumption is true for all known use cases,
528546 # but not strictly enforced by the parser. It is possible
@@ -533,7 +551,7 @@
534552 list( $type, $ref_argv, $ref_str,
535553 $ref_key, $ref_group, $ref_index ) = $call;
536554
537 - # Undo effects of calling <ref> while unaware of containing <references>
 555+ # Undo effects of calling <ref> while unaware of containing <references>
538556 $this->rollbackRef( $type, $ref_key, $ref_group, $ref_index );
539557
540558 # Rerun <ref> call now that mInReferences is set.
@@ -548,14 +566,16 @@
549567 $this->mRefCallStack = array();
550568 }
551569
552 - if ( count( $argv ) && $wgAllowCiteGroups )
 570+ if ( count( $argv ) && $wgAllowCiteGroups ) {
553571 return $this->error( 'cite_error_references_invalid_parameters_group' );
554 - elseif ( count( $argv ) )
 572+ } elseif ( count( $argv ) ) {
555573 return $this->error( 'cite_error_references_invalid_parameters' );
556 - else {
 574+ } else {
557575 $s = $this->referencesFormat( $group );
558 - if ( $parser->getOptions()->getIsSectionPreview() ) return $s;
559 -
 576+ if ( $parser->getOptions()->getIsSectionPreview() ) {
 577+ return $s;
 578+ }
 579+
560580 # Append errors generated while processing <references>
561581 if ( count( $this->mReferencesErrors ) > 0 ) {
562582 $s .= "\n" . implode( "<br />\n", $this->mReferencesErrors );
@@ -571,15 +591,17 @@
572592 * @return string XHTML ready for output
573593 */
574594 function referencesFormat( $group ) {
575 - if ( ( count( $this->mRefs ) == 0 ) or ( empty( $this->mRefs[$group] ) ) )
 595+ if ( ( count( $this->mRefs ) == 0 ) or ( empty( $this->mRefs[$group] ) ) ) {
576596 return '';
577 -
 597+ }
 598+
578599 wfProfileIn( __METHOD__ );
579600 wfProfileIn( __METHOD__ . '-entries' );
580601 $ent = array();
581 - foreach ( $this->mRefs[$group] as $k => $v )
 602+ foreach ( $this->mRefs[$group] as $k => $v ) {
582603 $ent[] = $this->referencesFormatEntry( $k, $v );
583 -
 604+ }
 605+
584606 $prefix = wfMsgForContentNoTrans( 'cite_references_prefix' );
585607 $suffix = wfMsgForContentNoTrans( 'cite_references_suffix' );
586608 $content = implode( "\n", $ent );
@@ -590,36 +612,36 @@
591613 $cacheKey = wfMemcKey( 'citeref', md5( $parserInput ), $this->mParser->Title()->getArticleID() );
592614
593615 wfProfileOut( __METHOD__ . '-entries' );
594 -
 616+
595617 global $wgCiteCacheReferences;
596618 if ( $wgCiteCacheReferences ) {
597619 wfProfileIn( __METHOD__ . '-cache-get' );
598620 $data = $wgMemc->get( $cacheKey );
599621 wfProfileOut( __METHOD__ . '-cache-get' );
600622 }
601 -
 623+
602624 if ( empty( $data ) ) {
603625 wfProfileIn( __METHOD__ . '-parse' );
604 -
 626+
605627 // Live hack: parse() adds two newlines on WM, can't reproduce it locally -ævar
606628 $ret = rtrim( $this->parse( $parserInput ), "\n" );
607 -
 629+
608630 if ( $wgCiteCacheReferences ) {
609631 $serData = $this->mParser->serialiseHalfParsedText( $ret );
610632 $wgMemc->set( $cacheKey, $serData, 86400 );
611633 }
612 -
 634+
613635 wfProfileOut( __METHOD__ . '-parse' );
614636 } else {
615637 $ret = $this->mParser->unserialiseHalfParsedText( $data );
616638 }
617639
618640 wfProfileOut( __METHOD__ );
619 -
 641+
620642 // done, clean up so we can reuse the group
621 - unset ( $this->mRefs[$group] );
 643+ unset( $this->mRefs[$group] );
622644 unset( $this->mGroupCnt[$group] );
623 -
 645+
624646 return $ret;
625647 }
626648
@@ -640,13 +662,13 @@
641663 $this->refKey( $key ),
642664 $val
643665 );
644 - } else if ( isset( $val['follow'] ) ) {
 666+ } elseif ( isset( $val['follow'] ) ) {
645667 return wfMsgForContentNoTrans(
646668 'cite_references_no_link',
647669 $this->referencesKey( $val['follow'] ),
648670 $val['text']
649671 );
650 - } else if ( $val['text'] == '' ) {
 672+ } elseif ( $val['text'] == '' ) {
651673 return wfMsgForContentNoTrans(
652674 'cite_references_link_one',
653675 $this->referencesKey( $key ),
@@ -668,7 +690,7 @@
669691 // anonymous reference because displaying "1. 1.1 Ref text" is
670692 // overkill and users frequently use named references when they
671693 // don't need them for convenience
672 - } else if ( $val['count'] === 0 ) {
 694+ } elseif ( $val['count'] === 0 ) {
673695 return wfMsgForContentNoTrans(
674696 'cite_references_link_one',
675697 $this->referencesKey( $key . "-" . $val['key'] ),
@@ -744,8 +766,8 @@
745767
746768 /**
747769 * Generate a custom format link for a group given an offset, e.g.
748 - * the second <ref group="foo"> is b if $this->mLinkLabels["foo"] =
749 - * array( 'a', 'b', 'c', ...).
 770+ * the second <ref group="foo"> is b if $this->mLinkLabels["foo"] =
 771+ * array( 'a', 'b', 'c', ...).
750772 * Return an error if the offset > the # of array items
751773 *
752774 * @param int $offset The offset
@@ -754,16 +776,16 @@
755777 *
756778 * @return string
757779 */
758 - function getLinkLabel( $offset, $group, $label) {
 780+ function getLinkLabel( $offset, $group, $label ) {
759781 $message = "cite_link_label_group-$group";
760782 if ( !isset( $this->mLinkLabels[$group] ) ) {
761 - $this->genLinkLabels($group, $message);
 783+ $this->genLinkLabels( $group, $message );
762784 }
763 - if ($this->mLinkLabels[$group] === false) {
 785+ if ( $this->mLinkLabels[$group] === false ) {
764786 // Use normal representation, ie. "$group 1", "$group 2"...
765787 return $label;
766788 }
767 -
 789+
768790 if ( isset( $this->mLinkLabels[$group][$offset - 1] ) ) {
769791 return $this->mLinkLabels[$group][$offset - 1];
770792 } else {
@@ -786,10 +808,11 @@
787809 function refKey( $key, $num = null ) {
788810 $prefix = wfMsgForContent( 'cite_reference_link_prefix' );
789811 $suffix = wfMsgForContent( 'cite_reference_link_suffix' );
790 - if ( isset( $num ) )
 812+ if ( isset( $num ) ) {
791813 $key = wfMsgForContentNoTrans( 'cite_reference_link_key_with_num', $key, $num );
792 -
793 - return $prefix . $key . $suffix;
 814+ }
 815+
 816+ return "$prefix$key$suffix";
794817 }
795818
796819 /**
@@ -806,10 +829,11 @@
807830 function referencesKey( $key, $num = null ) {
808831 $prefix = wfMsgForContent( 'cite_references_link_prefix' );
809832 $suffix = wfMsgForContent( 'cite_references_link_suffix' );
810 - if ( isset( $num ) )
 833+ if ( isset( $num ) ) {
811834 $key = wfMsgForContentNoTrans( 'cite_reference_link_key_with_num', $key, $num );
812 -
813 - return $prefix . $key . $suffix;
 835+ }
 836+
 837+ return "$prefix$key$suffix";
814838 }
815839
816840 /**
@@ -827,14 +851,14 @@
828852 function linkRef( $group, $key, $count = null, $label = null, $subkey = '' ) {
829853 global $wgContLang;
830854 $label = is_null( $label ) ? ++$this->mGroupCnt[$group] : $label;
831 -
 855+
832856 return
833857 $this->parse(
834858 wfMsgForContentNoTrans(
835859 'cite_reference_link',
836860 $this->refKey( $key, $count ),
837861 $this->referencesKey( $key . $subkey ),
838 - $this->getLinkLabel( $label, $group,
 862+ $this->getLinkLabel( $label, $group,
839863 ( ( $group == CITE_DEFAULT_GROUP ) ? '':"$group " ) . $wgContLang->formatNum( $label ) )
840864 )
841865 );
@@ -858,10 +882,10 @@
859883 $sep = wfMsgForContentNoTrans( 'cite_references_link_many_sep' );
860884 $and = wfMsgForContentNoTrans( 'cite_references_link_many_and' );
861885
862 - if ( $cnt == 1 )
 886+ if ( $cnt == 1 ) {
863887 // Enforce always returning a string
864888 return (string)$arr[0];
865 - else {
 889+ } else {
866890 $t = array_slice( $arr, 0, $cnt - 1 );
867891 return implode( $sep, $t ) . $and . $arr[$cnt - 1];
868892 }
@@ -893,7 +917,7 @@
894918 false
895919 );
896920 $text = $ret->getText();
897 -
 921+
898922 return $this->fixTidy( $text );
899923 }
900924 }
@@ -910,13 +934,13 @@
911935 function fixTidy( $text ) {
912936 global $wgUseTidy;
913937
914 - if ( ! $wgUseTidy )
 938+ if ( !$wgUseTidy ) {
915939 return $text;
916 - else {
 940+ } else {
917941 $text = preg_replace( '~^<p>\s*~', '', $text );
918942 $text = preg_replace( '~\s*</p>\s*~', '', $text );
919943 $text = preg_replace( '~\n$~', '', $text );
920 -
 944+
921945 return $text;
922946 }
923947 }
@@ -938,13 +962,14 @@
939963 * 'cite_reference_link' message instead of numbers, the format is an
940964 * arbitrary number of tokens separated by [\t\n ]
941965 */
942 - function genLinkLabels($group, $message) {
 966+ function genLinkLabels( $group, $message ) {
943967 wfProfileIn( __METHOD__ );
944968 $text = false;
945969 $msg = wfMessage( $message )->inContentLanguage();
946 - if ( $msg->exists() )
 970+ if ( $msg->exists() ) {
947971 $text = $msg->plain();
948 - $this->mLinkLabels[$group] = ($text == '') ? false : preg_split( '#[\n\t ]#', $text );
 972+ }
 973+ $this->mLinkLabels[$group] = ( $text == '' ) ? false : preg_split( '#[\n\t ]#', $text );
949974 wfProfileOut( __METHOD__ );
950975 }
951976
@@ -955,9 +980,10 @@
956981 function clearState() {
957982 # Don't clear state when we're in the middle of parsing
958983 # a <ref> tag
959 - if ( $this->mInCite || $this->mInReferences )
 984+ if ( $this->mInCite || $this->mInReferences ) {
960985 return true;
961 -
 986+ }
 987+
962988 $this->mGroupCnt = array();
963989 $this->mOutCnt = - 1;
964990 $this->mInCnt = 0;
@@ -969,14 +995,18 @@
970996 }
971997
972998 /**
973 - * Called at the end of page processing to append an error if refs were
 999+ * Called at the end of page processing to append an error if refs were
9741000 * used without a references tag.
9751001 */
9761002 function checkRefsNoReferences( &$parser, &$text ) {
977 - if ( $parser->getOptions()->getIsSectionPreview() ) return true;
 1003+ if ( $parser->getOptions()->getIsSectionPreview() ) {
 1004+ return true;
 1005+ }
9781006
9791007 foreach ( $this->mRefs as $group => $refs ) {
980 - if ( count( $refs ) == 0 ) continue;
 1008+ if ( count( $refs ) == 0 ) {
 1009+ continue;
 1010+ }
9811011 $text .= "\n<br />";
9821012 if ( $group == CITE_DEFAULT_GROUP ) {
9831013 $text .= $this->error( 'cite_error_refs_without_references' );

Comments

#Comment by Aaron Schulz (talk | contribs)   20:52, 15 June 2011

Surprised you left an old "A or B" (not "A || B") in there :)

#Comment by Simetrical (talk | contribs)   22:09, 15 June 2011

Must have missed it. I rather like and/or instead of &&/|| anyway, but then, my favorite language is Python . . .

Status & tagging log