Index: trunk/extensions/TemplateAdventures/TemplateAdventures.php |
— | — | @@ -42,7 +42,7 @@ |
43 | 43 | 'author' => array( 'Svip' ), |
44 | 44 | 'url' => 'http://www.mediawiki.org/wiki/Extension:TemplateAdventures', |
45 | 45 | 'descriptionmsg' => 'ta-desc', |
46 | | - 'version' => '0.2' |
| 46 | + 'version' => '0.3' |
47 | 47 | ); |
48 | 48 | |
49 | 49 | |
— | — | @@ -50,7 +50,10 @@ |
51 | 51 | $wgExtensionMessagesFiles['TemplateAdventures'] = "$dir/TemplateAdventures.i18n.php"; |
52 | 52 | $wgExtensionMessagesFiles['TemplateAdventuresMagic'] = "$dir/TemplateAdventures.i18n.magic.php"; |
53 | 53 | |
| 54 | +# Citation classes. |
54 | 55 | $wgAutoloadClasses['Citation'] = $dir . '/Templates/Citation.php'; |
| 56 | +$wgAutoloadClasses['CitationStyleWiki'] = $dir . '/Templates/CitationStyleWiki.php'; |
| 57 | +$wgAutoloadClasses['CitationStyleChicago'] = $dir . '/Templates/CitationStyleChicago.php'; |
55 | 58 | |
56 | 59 | $wgHooks['ParserFirstCallInit'][] = 'TemplateAdventures::onParserFirstCallInit'; |
57 | 60 | |
— | — | @@ -78,11 +81,56 @@ |
79 | 82 | public static function citation( $parser, $frame, $args ) { |
80 | 83 | if ( count( $args ) == 0 ) |
81 | 84 | 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 | + } |
83 | 96 | $obj->render(); |
84 | 97 | |
85 | 98 | return $obj->output(); |
86 | 99 | } |
| 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 | + } |
87 | 135 | } |
88 | 136 | |
89 | 137 | class TemplateAdventureBasic { |
Index: trunk/extensions/TemplateAdventures/Templates/Citation.php |
— | — | @@ -2,9 +2,10 @@ |
3 | 3 | |
4 | 4 | class Citation extends TemplateAdventureBasic { |
5 | 5 | |
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' |
7 | 8 | # currently only 'news' supported. |
8 | | - private $dSeparators = array( # separators between names, items, etc. |
| 9 | + protected $dSeparators = array( # separators between names, items, etc. |
9 | 10 | 'section' => ',', |
10 | 11 | 'end' => '.', |
11 | 12 | 'author' => ';', |
— | — | @@ -12,22 +13,22 @@ |
13 | 14 | 'authorlast' => ' &', |
14 | 15 | 'beforepublication' => ':', |
15 | 16 | ); |
16 | | - private $dAuthorTruncate = 8; # the amount of authors it should display, |
| 17 | + protected $dAuthorTruncate = 8; # the amount of authors it should display, |
17 | 18 | # 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 |
21 | 22 | # 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). |
24 | 25 | # they all contain 'junk' to avoid the |
25 | 26 | # 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 |
32 | 33 | 'title' => null, |
33 | 34 | 'transtitle' => null, # translated title (if original title is |
34 | 35 | # in a foreign language). |
— | — | @@ -36,60 +37,60 @@ |
37 | 38 | 'type' => null, # the title type |
38 | 39 | 'note' => null, |
39 | 40 | ); |
40 | | - private $dWorkLink = array( # data related to the link |
| 41 | + protected $dWorkLink = array( # data related to the link |
41 | 42 | 'url' => null, |
42 | 43 | 'originalurl' => null, |
43 | 44 | 'includedwork' => null, |
44 | 45 | ); |
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 |
47 | 48 | 'url' => null, |
48 | 49 | 'date' => null, |
49 | 50 | ); |
50 | | - private $dPubMed = array( |
| 51 | + protected $dPubMed = array( |
51 | 52 | 'pmc' => null, # PMC link |
52 | 53 | 'pmid' => null, # PMID |
53 | 54 | ); |
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 |
58 | 59 | 'data' => null, |
59 | 60 | 'place' => null, |
60 | 61 | ); |
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. |
63 | 64 | 'name' => null, # ensures it will be rendered as such |
64 | 65 | 'issue' => null, |
65 | 66 | 'issn' => null, |
66 | 67 | ); |
67 | | - private $dLibrary = null; # library id |
68 | | - private $dBook = array( # a book |
| 68 | + protected $dLibrary = null; # library id |
| 69 | + protected $dBook = array( # a book |
69 | 70 | 'title' => null, |
70 | 71 | 'isbn' => null, |
71 | 72 | 'page' => null |
72 | 73 | ); |
73 | | - private $dLayman = array( # an article of the same publication, but |
| 74 | + protected $dLayman = array( # an article of the same publication, but |
74 | 75 | # written in more layman friendly fashion. |
75 | 76 | 'data' => null, |
76 | 77 | 'summary' => null |
77 | 78 | ); |
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 |
82 | 83 | 'id' => null, |
83 | 84 | 'broken' => null, # date broken |
84 | 85 | ); |
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 |
87 | 88 | 'title' => null, |
88 | 89 | 'page' => null, |
89 | 90 | ); |
90 | | - private $dOther = null; # other stuff |
| 91 | + protected $dOther = null; # other stuff |
91 | 92 | |
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 |
94 | 95 | |
95 | 96 | /** |
96 | 97 | * Our construct function. |
— | — | @@ -115,7 +116,7 @@ |
116 | 117 | * @param $separator [default: true] Whether it shall have a separator at |
117 | 118 | * the end. |
118 | 119 | */ |
119 | | - private function addSection ( $content, $tags = array (), $separator = true ) { |
| 120 | + protected function addSection ( $content, $tags = array (), $separator = true ) { |
120 | 121 | $this->mSections[] = array ( |
121 | 122 | $content, |
122 | 123 | $tags, |
— | — | @@ -136,7 +137,7 @@ |
137 | 138 | * @param $parent [optional] The tag's parent. |
138 | 139 | * @return Boolean True if found, false if not. |
139 | 140 | */ |
140 | | - private function isTagInSections ( $tag, $parent = null ) { |
| 141 | + protected function isTagInSections ( $tag, $parent = null ) { |
141 | 142 | if ( $this->notNull ( $parent ) ) { |
142 | 143 | foreach ( $this->mSection as $section ) { |
143 | 144 | if ( in_array ( $tag, $section[1] ) |
— | — | @@ -620,6 +621,13 @@ |
621 | 622 | # COinS? waaaaat |
622 | 623 | # TODO |
623 | 624 | |
| 625 | + $this->finishRender(); |
| 626 | + } |
| 627 | + |
| 628 | + /** |
| 629 | + * Combines the sections to output. |
| 630 | + */ |
| 631 | + protected function finishRender () { |
624 | 632 | $this->mOutput = ''; |
625 | 633 | |
626 | 634 | $len = count ( $this->mSections ); |
— | — | @@ -635,6 +643,7 @@ |
636 | 644 | |
637 | 645 | # if the end 'separator' is blank, so we trim |
638 | 646 | $this->mOutput = wfMsg ( 'ta-citationspan', trim($this->mOutput), $this->citeType ); |
| 647 | + |
639 | 648 | } |
640 | 649 | |
641 | 650 | /** |
— | — | @@ -643,7 +652,7 @@ |
644 | 653 | * @param $isbn The raw ISBN number. |
645 | 654 | * @return $isbn The rendered version. |
646 | 655 | */ |
647 | | - private function createDisplayISBN ( $isbn ) { |
| 656 | + protected function createDisplayISBN ( $isbn ) { |
648 | 657 | return $isbn[0] . '-' . $isbn[1] . $isbn[2] . $isbn[3] . $isbn[4] . '-' . $isbn[5] . $isbn[6] . $isbn[7] . $isbn[8] . '-' . $isbn[9]; |
649 | 658 | } |
650 | 659 | |
— | — | @@ -654,7 +663,7 @@ |
655 | 664 | * @param $string |
656 | 665 | * @return $string |
657 | 666 | */ |
658 | | - private function printOnly ( $string ) { |
| 667 | + protected function printOnly ( $string ) { |
659 | 668 | return wfMsg ( 'ta-citeprintonlyspan', $string ); |
660 | 669 | } |
661 | 670 | |
— | — | @@ -665,7 +674,7 @@ |
666 | 675 | * @param $add String to add. |
667 | 676 | * @return $add if check is not null else '' |
668 | 677 | */ |
669 | | - private function addNotNull ( $check, $add ) { |
| 678 | + protected function addNotNull ( $check, $add ) { |
670 | 679 | if ( $this->notNull ( $check ) ) |
671 | 680 | return $add; |
672 | 681 | return ''; |
— | — | @@ -677,7 +686,7 @@ |
678 | 687 | * @param $value The string to be cleaned. |
679 | 688 | * @return The cleaned string. |
680 | 689 | */ |
681 | | - private function clean ( $value ) { |
| 690 | + protected function clean ( $value ) { |
682 | 691 | return str_replace ( "'", ''', $value ); |
683 | 692 | } |
684 | 693 | |
— | — | @@ -689,7 +698,7 @@ |
690 | 699 | * @param $truncate When to truncate the amount of data. |
691 | 700 | * @return The created area. |
692 | 701 | */ |
693 | | - private function createWriterSection ( $writers, $links, $truncate ) { |
| 702 | + protected function createWriterSection ( $writers, $links, $truncate ) { |
694 | 703 | $area = ''; |
695 | 704 | $n = 1; |
696 | 705 | foreach ( $writers as $i => $writer ) { |
— | — | @@ -734,7 +743,7 @@ |
735 | 744 | * @param $title Title for the URL. |
736 | 745 | * @return $title if no $url otherwise the link. |
737 | 746 | */ |
738 | | - private function makeLink ( $url, $title ) { |
| 747 | + protected function makeLink ( $url, $title ) { |
739 | 748 | if ( !$url ) |
740 | 749 | return $title; |
741 | 750 | return "[$url $title]"; |
— | — | @@ -746,7 +755,7 @@ |
747 | 756 | * @param $check Variable to check |
748 | 757 | * @return Boolean |
749 | 758 | */ |
750 | | - private function notNull ( $check ) { |
| 759 | + protected function notNull ( $check ) { |
751 | 760 | return !( $check == null && trim ( $check ) === '' ); |
752 | 761 | } |
753 | 762 | |
— | — | @@ -758,7 +767,7 @@ |
759 | 768 | * @param $addSpace Whether to add a space at the end; default true |
760 | 769 | * @return $separator Blank if none found. |
761 | 770 | */ |
762 | | - private function getSeparator ( $name, $addSpace=true ) { |
| 771 | + protected function getSeparator ( $name, $addSpace=true ) { |
763 | 772 | if ( !isset($this->dSeparators[$name]) ) |
764 | 773 | return ''; |
765 | 774 | $sep = $this->dSeparators[$name]; |
— | — | @@ -772,7 +781,7 @@ |
773 | 782 | * run. Basically to disregard data and such that has been found to be |
774 | 783 | * outside the allowed logic of this 'template'. |
775 | 784 | */ |
776 | | - private function parseData() { |
| 785 | + protected function parseData() { |
777 | 786 | # check $dAuthors for only 'given' names. |
778 | 787 | $tmpAuthors = array(null); |
779 | 788 | foreach( $this->dAuthors as $i => $author ) { |
— | — | @@ -795,7 +804,7 @@ |
796 | 805 | * @param $name Editor-reference. |
797 | 806 | * @param $value Link |
798 | 807 | */ |
799 | | - private function addEditorLink( $name, $value ) { |
| 808 | + protected function addEditorLink( $name, $value ) { |
800 | 809 | if ( $name[1] == null ) |
801 | 810 | return; |
802 | 811 | $this->dEditorLinks[$name[1]] = $value; |
— | — | @@ -807,7 +816,7 @@ |
808 | 817 | * @param $name Editor-reference. |
809 | 818 | * @param $value Name |
810 | 819 | */ |
811 | | - private function addEditor( $name, $value ) { |
| 820 | + protected function addEditor( $name, $value ) { |
812 | 821 | $this->appendEditorData ( $name[1], $value ); |
813 | 822 | } |
814 | 823 | |
— | — | @@ -817,7 +826,7 @@ |
818 | 827 | * @param $name Editor-reference. |
819 | 828 | * @param $value Surname |
820 | 829 | */ |
821 | | - private function addEditorSurname( $name, $value ) { |
| 830 | + protected function addEditorSurname( $name, $value ) { |
822 | 831 | $this->appendEditorData ( $name[1], array ( null, $value ) ); |
823 | 832 | } |
824 | 833 | |
— | — | @@ -827,7 +836,7 @@ |
828 | 837 | * @param $name Editor-reference. |
829 | 838 | * @param $value Given name |
830 | 839 | */ |
831 | | - private function addEditorGivenName ( $name, $value ) { |
| 840 | + protected function addEditorGivenName ( $name, $value ) { |
832 | 841 | $this->appendEditorData ( $name[1], array ( $value, null ) ); |
833 | 842 | } |
834 | 843 | |
— | — | @@ -837,7 +846,7 @@ |
838 | 847 | * @param $num Editor-reference. |
839 | 848 | * @param $name Details |
840 | 849 | */ |
841 | | - private function appendEditorData( $num, $name ) { |
| 850 | + protected function appendEditorData( $num, $name ) { |
842 | 851 | $this->appendWriterData( $this->dEditors, $num, $name ); |
843 | 852 | } |
844 | 853 | |
— | — | @@ -850,7 +859,7 @@ |
851 | 860 | * @param $name Author-reference |
852 | 861 | * @param $value Link |
853 | 862 | */ |
854 | | - private function addAuthorLink( $name, $value ) { |
| 863 | + protected function addAuthorLink( $name, $value ) { |
855 | 864 | if ( $name[1] == null ) |
856 | 865 | return; |
857 | 866 | $this->dAuthorLinks[$name[1]] = $value; |
— | — | @@ -860,7 +869,7 @@ |
861 | 870 | * @param $name Author-reference |
862 | 871 | * @param $value Full name |
863 | 872 | */ |
864 | | - private function addAuthor( $name, $value ) { |
| 873 | + protected function addAuthor( $name, $value ) { |
865 | 874 | $this->appendAuthorData ( $name[1], $value ); |
866 | 875 | } |
867 | 876 | |
— | — | @@ -868,7 +877,7 @@ |
869 | 878 | * @param $name Author-reference |
870 | 879 | * @param $value Surname |
871 | 880 | */ |
872 | | - private function addAuthorSurname( $name, $value ) { |
| 881 | + protected function addAuthorSurname( $name, $value ) { |
873 | 882 | $this->appendAuthorData ( $name[1], array ( null, $value ) ); |
874 | 883 | } |
875 | 884 | |
— | — | @@ -876,7 +885,7 @@ |
877 | 886 | * @param $name Author-reference |
878 | 887 | * @param $value Given name |
879 | 888 | */ |
880 | | - private function addAuthorGivenName ( $name, $value ) { |
| 889 | + protected function addAuthorGivenName ( $name, $value ) { |
881 | 890 | $this->appendAuthorData ( $name[1], array ( $value, null ) ); |
882 | 891 | } |
883 | 892 | |
— | — | @@ -884,7 +893,7 @@ |
885 | 894 | * @param $num Author-reference |
886 | 895 | * @param $name Details |
887 | 896 | */ |
888 | | - private function appendAuthorData( $num, $name ) { |
| 897 | + protected function appendAuthorData( $num, $name ) { |
889 | 898 | $this->appendWriterData( $this->dAuthors, $num, $name ); |
890 | 899 | } |
891 | 900 | |
— | — | @@ -896,7 +905,7 @@ |
897 | 906 | * @param $num The location in the array (0 is always set, but never used) |
898 | 907 | * @param $name The name and link of the author/editor. |
899 | 908 | */ |
900 | | - private function appendWriterData( &$array, $num, $name ) { |
| 909 | + protected function appendWriterData( &$array, $num, $name ) { |
901 | 910 | $split = is_array( $name ); |
902 | 911 | if ( $num == null ) |
903 | 912 | # if no number, assume it is the first. |
— | — | @@ -921,7 +930,7 @@ |
922 | 931 | * @param $name Name of the parameter. |
923 | 932 | * @param $value The value to be inserted. |
924 | 933 | */ |
925 | | - private function addOtherStringValue ( $name, $value ) { |
| 934 | + protected function addOtherStringValue ( $name, $value ) { |
926 | 935 | switch ( $name[0] ) { |
927 | 936 | case 'url': |
928 | 937 | $this->dWorkLink['url'] = $value; |
— | — | @@ -1055,8 +1064,6 @@ |
1056 | 1065 | * the first item defines the type of the citation; which is important the |
1057 | 1066 | * rendering of the function. |
1058 | 1067 | * |
1059 | | - * Right now only 'news' is an acceptable citation type. |
1060 | | - * |
1061 | 1068 | * @param $item The raw item. |
1062 | 1069 | */ |
1063 | 1070 | protected function handlePrimaryItem( $item ) { |