r87436 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r87435‎ | r87436 | r87437 >
Date:19:19, 4 May 2011
Author:svip
Status:deferred
Tags:
Comment:
New code to supply styles for the citations.
Modified paths:
  • /trunk/extensions/TemplateAdventures/TemplateAdventures.php (modified) (history)
  • /trunk/extensions/TemplateAdventures/Templates/Citation.php (modified) (history)

Diff [purge]

Index: trunk/extensions/TemplateAdventures/TemplateAdventures.php
@@ -42,7 +42,7 @@
4343 'author' => array( 'Svip' ),
4444 'url' => 'http://www.mediawiki.org/wiki/Extension:TemplateAdventures',
4545 'descriptionmsg' => 'ta-desc',
46 - 'version' => '0.2'
 46+ 'version' => '0.3'
4747 );
4848
4949
@@ -50,7 +50,10 @@
5151 $wgExtensionMessagesFiles['TemplateAdventures'] = "$dir/TemplateAdventures.i18n.php";
5252 $wgExtensionMessagesFiles['TemplateAdventuresMagic'] = "$dir/TemplateAdventures.i18n.magic.php";
5353
 54+# Citation classes.
5455 $wgAutoloadClasses['Citation'] = $dir . '/Templates/Citation.php';
 56+$wgAutoloadClasses['CitationStyleWiki'] = $dir . '/Templates/CitationStyleWiki.php';
 57+$wgAutoloadClasses['CitationStyleChicago'] = $dir . '/Templates/CitationStyleChicago.php';
5558
5659 $wgHooks['ParserFirstCallInit'][] = 'TemplateAdventures::onParserFirstCallInit';
5760
@@ -78,11 +81,56 @@
7982 public static function citation( $parser, $frame, $args ) {
8083 if ( count( $args ) == 0 )
8184 return '';
82 - $obj = new Citation( $parser, $frame, $args );
 85+ $style = self::getCitationStyle ( $frame, $args );
 86+ switch ( strtolower( $style ) ) {
 87+ case 'chicago':
 88+ $obj = new CitationStyleChicago( $parser, $frame, $args );
 89+ break;
 90+ case 'wiki':
 91+ # In the future, default: will depend on a global variable.
 92+ default:
 93+ $obj = new CitationStyleWiki( $parser, $frame, $args );
 94+ break;
 95+ }
8396 $obj->render();
8497
8598 return $obj->output();
8699 }
 100+
 101+ /**
 102+ * Find the style for a citation. However, this usage means that arguments
 103+ * will in worse case scenarios always be run twice, which on heavy pages
 104+ * might decrease speed significantly.
 105+ *
 106+ * It might be possible to find a way to parse the data while finding the
 107+ * style, then transferring the found data to the new Style classes. But
 108+ * for now, this will do.
 109+ *
 110+ * @param $frame The frame.
 111+ * @param $args The arguments.
 112+ * @return The style; if none found, it returns null.
 113+ */
 114+ private static function getCitationStyle ( $frame, $args ) {
 115+ foreach ( $args as $arg ) {
 116+ if ( $arg instanceof PPNode_DOM ) {
 117+ $bits = $arg->splitArg();
 118+ $index = $bits['index'];
 119+ if ( $index === '' ) { # Found
 120+ $var = trim( $frame->expand( $bits['name'] ) );
 121+ if ( strtolower( $var ) == 'style' )
 122+ return trim( $frame->expand( $bits['value'] ) );
 123+ }
 124+ } else {
 125+ $parts = array_map( 'trim', explode( '=', $arg, 2 ) );
 126+ if ( count( $parts ) == 2 ) { # Found "="
 127+ $var = $parts[0];
 128+ if ( strtolower( $var ) == 'style' )
 129+ return $parts[1];
 130+ }
 131+ }
 132+ }
 133+ return null;
 134+ }
87135 }
88136
89137 class TemplateAdventureBasic {
Index: trunk/extensions/TemplateAdventures/Templates/Citation.php
@@ -2,9 +2,10 @@
33
44 class Citation extends TemplateAdventureBasic {
55
6 - private $citeType = null; # type of citation, e.g. 'news'
 6+ protected $citeStyle = null; # citation style
 7+ protected $citeType = null; # type of citation, e.g. 'news'
78 # currently only 'news' supported.
8 - private $dSeparators = array( # separators between names, items, etc.
 9+ protected $dSeparators = array( # separators between names, items, etc.
910 'section' => ',',
1011 'end' => '.',
1112 'author' => '&#059;',
@@ -12,22 +13,22 @@
1314 'authorlast' => ' &',
1415 'beforepublication' => ':',
1516 );
16 - private $dAuthorTruncate = 8; # the amount of authors it should display,
 17+ protected $dAuthorTruncate = 8; # the amount of authors it should display,
1718 # if truncated, 'et al' will be used instead.
18 - private $dAuthors = array(null); # array of authors
19 - private $dAuthorLinks = array(null); # array of authorlinks (tied to authors).
20 - private $dCoAuthors = null; # coauthors is as far as I understand it
 19+ protected $dAuthors = array(null); # array of authors
 20+ protected $dAuthorLinks = array(null); # array of authorlinks (tied to authors).
 21+ protected $dCoAuthors = null; # coauthors is as far as I understand it
2122 # just a string, but prove me wrong!
22 - private $dEditors = array(null); # array of editors
23 - private $dEditorLinks = array(null); # array of editorlinks (tied to editors).
 23+ protected $dEditors = array(null); # array of editors
 24+ protected $dEditorLinks = array(null); # array of editorlinks (tied to editors).
2425 # they all contain 'junk' to avoid the
2526 # usage of [0].
26 - private $dAuthorBlock = null; # authorblock of em length
27 - private $dDate = null; # the date set
28 - private $dAccessDate = null; # date accessed
29 - private $dYear = null; # year of authorship or publication
30 - private $dYearNote = null; # note to accompany the year
31 - private $dWorkTitle = array( # data related to the title
 27+ protected $dAuthorBlock = null; # authorblock of em length
 28+ protected $dDate = null; # the date set
 29+ protected $dAccessDate = null; # date accessed
 30+ protected $dYear = null; # year of authorship or publication
 31+ protected $dYearNote = null; # note to accompany the year
 32+ protected $dWorkTitle = array( # data related to the title
3233 'title' => null,
3334 'transtitle' => null, # translated title (if original title is
3435 # in a foreign language).
@@ -36,60 +37,60 @@
3738 'type' => null, # the title type
3839 'note' => null,
3940 );
40 - private $dWorkLink = array( # data related to the link
 41+ protected $dWorkLink = array( # data related to the link
4142 'url' => null,
4243 'originalurl' => null,
4344 'includedwork' => null,
4445 );
45 - private $dAt = null; # wherein the source
46 - private $dArchived = array( # information about its archiving if archived
 46+ protected $dAt = null; # wherein the source
 47+ protected $dArchived = array( # information about its archiving if archived
4748 'url' => null,
4849 'date' => null,
4950 );
50 - private $dPubMed = array(
 51+ protected $dPubMed = array(
5152 'pmc' => null, # PMC link
5253 'pmid' => null, # PMID
5354 );
54 - private $dSeries = null; # whether it is part of a series
55 - private $dQuote = null; # quote
56 - private $dPublisher = null; # publisher
57 - private $dPublication = array( # publication
 55+ protected $dSeries = null; # whether it is part of a series
 56+ protected $dQuote = null; # quote
 57+ protected $dPublisher = null; # publisher
 58+ protected $dPublication = array( # publication
5859 'data' => null,
5960 'place' => null,
6061 );
61 - private $dPlace = null; # place of writing
62 - private $dPeriodical = array( # name of periodical, journal or magazine.
 62+ protected $dPlace = null; # place of writing
 63+ protected $dPeriodical = array( # name of periodical, journal or magazine.
6364 'name' => null, # ensures it will be rendered as such
6465 'issue' => null,
6566 'issn' => null,
6667 );
67 - private $dLibrary = null; # library id
68 - private $dBook = array( # a book
 68+ protected $dLibrary = null; # library id
 69+ protected $dBook = array( # a book
6970 'title' => null,
7071 'isbn' => null,
7172 'page' => null
7273 );
73 - private $dLayman = array( # an article of the same publication, but
 74+ protected $dLayman = array( # an article of the same publication, but
7475 # written in more layman friendly fashion.
7576 'data' => null,
7677 'summary' => null
7778 );
78 - private $dLanguage = null; # language of the publication
79 - private $dId = null; # misc id
80 - private $dEdition = null; # edition
81 - private $dDoi = array( # digital object identifier
 79+ protected $dLanguage = null; # language of the publication
 80+ protected $dId = null; # misc id
 81+ protected $dEdition = null; # edition
 82+ protected $dDoi = array( # digital object identifier
8283 'id' => null,
8384 'broken' => null, # date broken
8485 );
85 - private $dBibcode = null; # bibcode id
86 - private $dJournal = array( # journal and its page
 86+ protected $dBibcode = null; # bibcode id
 87+ protected $dJournal = array( # journal and its page
8788 'title' => null,
8889 'page' => null,
8990 );
90 - private $dOther = null; # other stuff
 91+ protected $dOther = null; # other stuff
9192
92 - private $mSections = array(); # sections of the citation.
93 - private $mTags = array(); # all tags
 93+ protected $mSections = array(); # sections of the citation.
 94+ protected $mTags = array(); # all tags
9495
9596 /**
9697 * Our construct function.
@@ -115,7 +116,7 @@
116117 * @param $separator [default: true] Whether it shall have a separator at
117118 * the end.
118119 */
119 - private function addSection ( $content, $tags = array (), $separator = true ) {
 120+ protected function addSection ( $content, $tags = array (), $separator = true ) {
120121 $this->mSections[] = array (
121122 $content,
122123 $tags,
@@ -136,7 +137,7 @@
137138 * @param $parent [optional] The tag's parent.
138139 * @return Boolean True if found, false if not.
139140 */
140 - private function isTagInSections ( $tag, $parent = null ) {
 141+ protected function isTagInSections ( $tag, $parent = null ) {
141142 if ( $this->notNull ( $parent ) ) {
142143 foreach ( $this->mSection as $section ) {
143144 if ( in_array ( $tag, $section[1] )
@@ -620,6 +621,13 @@
621622 # COinS? waaaaat
622623 # TODO
623624
 625+ $this->finishRender();
 626+ }
 627+
 628+ /**
 629+ * Combines the sections to output.
 630+ */
 631+ protected function finishRender () {
624632 $this->mOutput = '';
625633
626634 $len = count ( $this->mSections );
@@ -635,6 +643,7 @@
636644
637645 # if the end 'separator' is blank, so we trim
638646 $this->mOutput = wfMsg ( 'ta-citationspan', trim($this->mOutput), $this->citeType );
 647+
639648 }
640649
641650 /**
@@ -643,7 +652,7 @@
644653 * @param $isbn The raw ISBN number.
645654 * @return $isbn The rendered version.
646655 */
647 - private function createDisplayISBN ( $isbn ) {
 656+ protected function createDisplayISBN ( $isbn ) {
648657 return $isbn[0] . '-' . $isbn[1] . $isbn[2] . $isbn[3] . $isbn[4] . '-' . $isbn[5] . $isbn[6] . $isbn[7] . $isbn[8] . '-' . $isbn[9];
649658 }
650659
@@ -654,7 +663,7 @@
655664 * @param $string
656665 * @return $string
657666 */
658 - private function printOnly ( $string ) {
 667+ protected function printOnly ( $string ) {
659668 return wfMsg ( 'ta-citeprintonlyspan', $string );
660669 }
661670
@@ -665,7 +674,7 @@
666675 * @param $add String to add.
667676 * @return $add if check is not null else ''
668677 */
669 - private function addNotNull ( $check, $add ) {
 678+ protected function addNotNull ( $check, $add ) {
670679 if ( $this->notNull ( $check ) )
671680 return $add;
672681 return '';
@@ -677,7 +686,7 @@
678687 * @param $value The string to be cleaned.
679688 * @return The cleaned string.
680689 */
681 - private function clean ( $value ) {
 690+ protected function clean ( $value ) {
682691 return str_replace ( "'", ''', $value );
683692 }
684693
@@ -689,7 +698,7 @@
690699 * @param $truncate When to truncate the amount of data.
691700 * @return The created area.
692701 */
693 - private function createWriterSection ( $writers, $links, $truncate ) {
 702+ protected function createWriterSection ( $writers, $links, $truncate ) {
694703 $area = '';
695704 $n = 1;
696705 foreach ( $writers as $i => $writer ) {
@@ -734,7 +743,7 @@
735744 * @param $title Title for the URL.
736745 * @return $title if no $url otherwise the link.
737746 */
738 - private function makeLink ( $url, $title ) {
 747+ protected function makeLink ( $url, $title ) {
739748 if ( !$url )
740749 return $title;
741750 return "[$url $title]";
@@ -746,7 +755,7 @@
747756 * @param $check Variable to check
748757 * @return Boolean
749758 */
750 - private function notNull ( $check ) {
 759+ protected function notNull ( $check ) {
751760 return !( $check == null && trim ( $check ) === '' );
752761 }
753762
@@ -758,7 +767,7 @@
759768 * @param $addSpace Whether to add a space at the end; default true
760769 * @return $separator Blank if none found.
761770 */
762 - private function getSeparator ( $name, $addSpace=true ) {
 771+ protected function getSeparator ( $name, $addSpace=true ) {
763772 if ( !isset($this->dSeparators[$name]) )
764773 return '';
765774 $sep = $this->dSeparators[$name];
@@ -772,7 +781,7 @@
773782 * run. Basically to disregard data and such that has been found to be
774783 * outside the allowed logic of this 'template'.
775784 */
776 - private function parseData() {
 785+ protected function parseData() {
777786 # check $dAuthors for only 'given' names.
778787 $tmpAuthors = array(null);
779788 foreach( $this->dAuthors as $i => $author ) {
@@ -795,7 +804,7 @@
796805 * @param $name Editor-reference.
797806 * @param $value Link
798807 */
799 - private function addEditorLink( $name, $value ) {
 808+ protected function addEditorLink( $name, $value ) {
800809 if ( $name[1] == null )
801810 return;
802811 $this->dEditorLinks[$name[1]] = $value;
@@ -807,7 +816,7 @@
808817 * @param $name Editor-reference.
809818 * @param $value Name
810819 */
811 - private function addEditor( $name, $value ) {
 820+ protected function addEditor( $name, $value ) {
812821 $this->appendEditorData ( $name[1], $value );
813822 }
814823
@@ -817,7 +826,7 @@
818827 * @param $name Editor-reference.
819828 * @param $value Surname
820829 */
821 - private function addEditorSurname( $name, $value ) {
 830+ protected function addEditorSurname( $name, $value ) {
822831 $this->appendEditorData ( $name[1], array ( null, $value ) );
823832 }
824833
@@ -827,7 +836,7 @@
828837 * @param $name Editor-reference.
829838 * @param $value Given name
830839 */
831 - private function addEditorGivenName ( $name, $value ) {
 840+ protected function addEditorGivenName ( $name, $value ) {
832841 $this->appendEditorData ( $name[1], array ( $value, null ) );
833842 }
834843
@@ -837,7 +846,7 @@
838847 * @param $num Editor-reference.
839848 * @param $name Details
840849 */
841 - private function appendEditorData( $num, $name ) {
 850+ protected function appendEditorData( $num, $name ) {
842851 $this->appendWriterData( $this->dEditors, $num, $name );
843852 }
844853
@@ -850,7 +859,7 @@
851860 * @param $name Author-reference
852861 * @param $value Link
853862 */
854 - private function addAuthorLink( $name, $value ) {
 863+ protected function addAuthorLink( $name, $value ) {
855864 if ( $name[1] == null )
856865 return;
857866 $this->dAuthorLinks[$name[1]] = $value;
@@ -860,7 +869,7 @@
861870 * @param $name Author-reference
862871 * @param $value Full name
863872 */
864 - private function addAuthor( $name, $value ) {
 873+ protected function addAuthor( $name, $value ) {
865874 $this->appendAuthorData ( $name[1], $value );
866875 }
867876
@@ -868,7 +877,7 @@
869878 * @param $name Author-reference
870879 * @param $value Surname
871880 */
872 - private function addAuthorSurname( $name, $value ) {
 881+ protected function addAuthorSurname( $name, $value ) {
873882 $this->appendAuthorData ( $name[1], array ( null, $value ) );
874883 }
875884
@@ -876,7 +885,7 @@
877886 * @param $name Author-reference
878887 * @param $value Given name
879888 */
880 - private function addAuthorGivenName ( $name, $value ) {
 889+ protected function addAuthorGivenName ( $name, $value ) {
881890 $this->appendAuthorData ( $name[1], array ( $value, null ) );
882891 }
883892
@@ -884,7 +893,7 @@
885894 * @param $num Author-reference
886895 * @param $name Details
887896 */
888 - private function appendAuthorData( $num, $name ) {
 897+ protected function appendAuthorData( $num, $name ) {
889898 $this->appendWriterData( $this->dAuthors, $num, $name );
890899 }
891900
@@ -896,7 +905,7 @@
897906 * @param $num The location in the array (0 is always set, but never used)
898907 * @param $name The name and link of the author/editor.
899908 */
900 - private function appendWriterData( &$array, $num, $name ) {
 909+ protected function appendWriterData( &$array, $num, $name ) {
901910 $split = is_array( $name );
902911 if ( $num == null )
903912 # if no number, assume it is the first.
@@ -921,7 +930,7 @@
922931 * @param $name Name of the parameter.
923932 * @param $value The value to be inserted.
924933 */
925 - private function addOtherStringValue ( $name, $value ) {
 934+ protected function addOtherStringValue ( $name, $value ) {
926935 switch ( $name[0] ) {
927936 case 'url':
928937 $this->dWorkLink['url'] = $value;
@@ -1055,8 +1064,6 @@
10561065 * the first item defines the type of the citation; which is important the
10571066 * rendering of the function.
10581067 *
1059 - * Right now only 'news' is an acceptable citation type.
1060 - *
10611068 * @param $item The raw item.
10621069 */
10631070 protected function handlePrimaryItem( $item ) {

Status & tagging log