r89501 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r89500‎ | r89501 | r89502 >
Date:14:16, 5 June 2011
Author:jeroendedauw
Status:deferred
Tags:
Comment:
made compat fixes for SMW 1.6 to BibTeX format and somewhat decreased it's codewise insanity level
Modified paths:
  • /trunk/extensions/SemanticResultFormats/BibTeX/SRF_BibTeX.php (modified) (history)

Diff [purge]

Index: trunk/extensions/SemanticResultFormats/BibTeX/SRF_BibTeX.php
@@ -56,249 +56,143 @@
5757 protected function getResultText( SMWQueryResult $res, $outputmode ) {
5858 global $wgSitename;
5959 $result = '';
60 - $items = array();
 60+
6161 if ( $outputmode == SMW_OUTPUT_FILE ) { // make file
6262 if ( $this->m_title == '' ) {
6363 $this->m_title = $wgSitename;
6464 }
65 - $row = $res->getNext();
66 - while ( $row !== false ) {
67 - $type = '';
68 - $address = '';
69 - $annote = '';
70 - $author = '';
71 - $booktitle = '';
72 - $chapter = '';
73 - $crossref = '';
74 - $doi = '';
75 - $edition = '';
76 - $editor = '';
77 - $eprint = '';
78 - $howpublished = '';
79 - $institution = '';
80 - $journal = '';
81 - $key = '';
82 - $month = '';
83 - $note = '';
84 - $number = '';
85 - $organization = '';
86 - $pages = '';
87 - $publisher = '';
88 - $school = '';
89 - $series = '';
90 - $title = '';
91 - $type = '';
92 - $url = '';
93 - $volume = '';
94 - $year = '';
95 -
96 - foreach ( $row as $field ) {
97 - $req = $field->getPrintRequest();
98 -
99 - if ( ( strtolower( $req->getLabel() ) == "type" ) ) {
100 - $value = current( $field->getContent() );
101 - if ( $value !== false ) {
102 - $type = $value->getShortWikiText();
103 - }
104 - }
105 -
106 -
107 - if ( ( strtolower( $req->getLabel() ) == "address" ) ) {
108 - $value = current( $field->getContent() );
109 - if ( $value !== false ) {
110 - $address = $value->getShortWikiText();
111 - }
112 - }
113 - if ( ( strtolower( $req->getLabel() ) == "annote" ) ) {
114 - $value = current( $field->getContent() );
115 - if ( $value !== false ) {
116 - $annote = $value->getShortWikiText();
117 - }
118 - }
119 - /* for flexibility, we permit 'author' or 'authors' */
120 - if ( ( strtolower( $req->getLabel() ) == "author" ) || ( strtolower( $req->getLabel() ) == "authors" ) ) {
121 - foreach ( $field->getContent() as $value ) {
122 - $author .= ( $author ? ' and ':'' ) . $value->getShortWikiText();
123 - }
124 - }
125 - if ( ( strtolower( $req->getLabel() ) == "booktitle" ) ) {
126 - $value = current( $field->getContent() );
127 - if ( $value !== false ) {
128 - $booktitle = $value->getShortWikiText();
129 - }
130 - }
131 - if ( ( strtolower( $req->getLabel() ) == "chapter" ) ) {
132 - $value = current( $field->getContent() );
133 - if ( $value !== false ) {
134 - $chapter = $value->getShortWikiText();
135 - }
136 - }
137 - if ( ( strtolower( $req->getLabel() ) == "crossref" ) ) {
138 - $value = current( $field->getContent() );
139 - if ( $value !== false ) {
140 - $crossref = $value->getShortWikiText();
141 - }
142 - }
143 - if ( ( strtolower( $req->getLabel() ) == "doi" ) ) {
144 - $value = current( $field->getContent() );
145 - if ( $value !== false ) {
146 - $doi = $value->getShortWikiText();
147 - }
148 - }
149 - if ( ( strtolower( $req->getLabel() ) == "edition" ) ) {
150 - $value = current( $field->getContent() );
151 - if ( $value !== false ) {
152 - $edition = $value->getShortWikiText();
153 - }
154 - }
155 - /* for flexibility, we permit 'editor' or 'editors' */
156 - if ( ( strtolower( $req->getLabel() ) == "editor" ) || ( strtolower( $req->getLabel() ) == "editors" ) ) {
157 - foreach ( $field->getContent() as $value ) {
158 - $editor .= ( $editor ? ' and ':'' ) . $value->getShortWikiText();
159 - }
160 - }
161 - if ( ( strtolower( $req->getLabel() ) == "eprint" ) ) {
162 - $value = current( $field->getContent() );
163 - if ( $value !== false ) {
164 - $eprint = $value->getShortWikiText();
165 - }
166 - }
167 - if ( ( strtolower( $req->getLabel() ) == "howpublished" ) ) {
168 - $value = current( $field->getContent() );
169 - if ( $value !== false ) {
170 - $howpublished = $value->getShortWikiText();
171 - }
172 - }
173 - if ( ( strtolower( $req->getLabel() ) == "institution" ) ) {
174 - $value = current( $field->getContent() );
175 - if ( $value !== false ) {
176 - $institution = $value->getShortWikiText();
177 - }
178 - }
179 - if ( ( strtolower( $req->getLabel() ) == "journal" ) ) {
180 - $value = current( $field->getContent() );
181 - if ( $value !== false ) {
182 - $journal = $value->getShortWikiText();
183 - }
184 - }
185 - if ( ( strtolower( $req->getLabel() ) == "key" ) ) {
186 - $value = current( $field->getContent() );
187 - if ( $value !== false ) {
188 - $key = $value->getShortWikiText();
189 - }
190 - }
191 - if ( ( strtolower( $req->getLabel() ) == "note" ) ) {
192 - $value = current( $field->getContent() );
193 - if ( $value !== false ) {
194 - $note = $value->getShortWikiText();
195 - }
196 - }
197 - if ( ( strtolower( $req->getLabel() ) == "number" ) ) {
198 - $value = current( $field->getContent() );
199 - if ( $value !== false ) {
200 - $number = $value->getShortWikiText();
201 - }
202 - }
203 - if ( ( strtolower( $req->getLabel() ) == "organization" ) ) {
204 - $value = current( $field->getContent() );
205 - if ( $value !== false ) {
206 - $organization = $value->getShortWikiText();
207 - }
208 - }
209 - if ( ( strtolower( $req->getLabel() ) == "pages" ) ) {
210 - $value = current( $field->getContent() );
211 - if ( $value !== false ) {
212 - $pages = $value->getShortWikiText();
213 - }
214 - }
215 - if ( ( strtolower( $req->getLabel() ) == "publisher" ) ) {
216 - $value = current( $field->getContent() );
217 - if ( $value !== false ) {
218 - $publisher = $value->getShortWikiText();
219 - }
220 - }
221 - if ( ( strtolower( $req->getLabel() ) == "school" ) ) {
222 - $value = current( $field->getContent() );
223 - if ( $value !== false ) {
224 - $school = $value->getShortWikiText();
225 - }
226 - }
227 - if ( ( strtolower( $req->getLabel() ) == "series" ) ) {
228 - $value = current( $field->getContent() );
229 - if ( $value !== false ) {
230 - $series = $value->getShortWikiText();
231 - }
232 - }
233 - if ( ( strtolower( $req->getLabel() ) == "title" ) ) {
234 - $value = current( $field->getContent() );
235 - if ( $value !== false ) {
236 - $title = $value->getShortWikiText();
237 - }
238 - }
239 - if ( ( strtolower( $req->getLabel() ) == "url" ) ) {
240 - $value = current( $field->getContent() );
241 - if ( $value !== false ) {
242 - $url = $value->getShortWikiText();
243 - }
244 - }
245 - if ( ( strtolower( $req->getLabel() ) == "volume" ) || ( strtolower( $req->getLabel() ) == "journal_volume" ) ) {
246 - $value = current( $field->getContent() );
247 - if ( $value !== false ) {
248 - $volume = $value->getShortWikiText();
249 - }
250 - }
251 -
252 -
253 -
254 - /*if we input a full date for the "month" and "year" BibTeX attributes, extract the month and year*/
255 - if ( ( strtolower( $req->getLabel() ) == "date" ) ) {
256 - $value = current( $field->getContent() );
257 - if ( get_class( $value ) == 'SMWTimeValue' ) {
258 - if ( $value !== false ) {
259 - $year = $value->getYear();
260 - $month = $value->getMonth();
261 - }
262 - }
263 - }
264 -
265 - if ( ( strtolower( $req->getLabel() ) == "year" ) ) {
266 - $value = current( $field->getContent() );
267 - if ( $value !== false ) {
268 - $year = $value->getShortWikiText();
269 - }
270 - }
271 - if ( ( strtolower( $req->getLabel() ) == "month" ) ) {
272 - $value = current( $field->getContent() );
273 - if ( $value !== false ) {
274 - $month = $value->getShortWikiText();
275 - }
276 - }
277 -
278 -
279 - }
280 - $items[] = new SMWBibTeXEntry( $type, $address, $annote, $author, $booktitle, $chapter, $crossref, $doi, $edition, $editor, $eprint, $howpublished, $institution, $journal, $key, $month, $note, $number, $organization, $pages, $publisher, $school, $series, $title, $url, $volume, $year );
281 - $row = $res->getNext();
 65+
 66+ $items = array();
 67+
 68+ while ( $row = $res->getNext() ) {
 69+ $items[] = $this->getItemForResultRow( $row )->text();
28270 }
283 - foreach ( $items as $item ) {
284 - $result .= $item->text();
285 - }
 71+
 72+ $result = implode( '', $items );
28673 } else { // just make link to export
28774 if ( $this->getSearchLabel( $outputmode ) ) {
28875 $label = $this->getSearchLabel( $outputmode );
28976 } else {
29077 $label = wfMsgForContent( 'srf_bibtex_link' );
29178 }
 79+
29280 $link = $res->getQueryLink( $label );
29381 $link->setParameter( 'bibtex', 'format' );
 82+
29483 if ( $this->getSearchLabel( SMW_OUTPUT_WIKI ) != '' ) {
29584 $link->setParameter( $this->getSearchLabel( SMW_OUTPUT_WIKI ), 'searchlabel' );
29685 }
 86+
29787 $result .= $link->getText( $outputmode, $this->mLinker );
29888 $this->isHTML = ( $outputmode == SMW_OUTPUT_HTML ); // yes, our code can be viewed as HTML if requested, no more parsing needed
29989 }
 90+
30091 return $result;
30192 }
 93+
 94+ /**
 95+ * Gets a SMWBibTeXEntry for the row.
 96+ *
 97+ * @since 1.6
 98+ *
 99+ * @param array of SMWResultArray $row
 100+ *
 101+ * @return SMWBibTeXEntry
 102+ */
 103+ protected function getItemForResultRow( array /* of SMWResultArray */ $row ) {
 104+ $type = '';
 105+ $address = '';
 106+ $annote = '';
 107+ $author = '';
 108+ $booktitle = '';
 109+ $chapter = '';
 110+ $crossref = '';
 111+ $doi = '';
 112+ $edition = '';
 113+ $editor = '';
 114+ $eprint = '';
 115+ $howpublished = '';
 116+ $institution = '';
 117+ $journal = '';
 118+ $key = '';
 119+ $month = '';
 120+ $note = '';
 121+ $number = '';
 122+ $organization = '';
 123+ $pages = '';
 124+ $publisher = '';
 125+ $school = '';
 126+ $series = '';
 127+ $title = '';
 128+ $type = '';
 129+ $url = '';
 130+ $volume = '';
 131+ $year = '';
 132+
 133+ $var = false;
302134
 135+ foreach ( $row as /* SMWResultArray */ $field ) {
 136+ $req = $field->getPrintRequest();
 137+ $label = strtolower( $req->getLabel() );
 138+
 139+ switch ( $label ) {
 140+ case 'type': $var =& $type; break;
 141+ case 'address': $var =& $address; break;
 142+ case 'annote': $var =& $annote; break;
 143+ case 'booktitle': $var =& $booktitle; break;
 144+ case 'chapter': $var =& $chapter; break;
 145+ case 'crossref': $var =& $crossref; break;
 146+ case 'doi': $var =& $doi; break;
 147+ case 'edition': $var =& $edition; break;
 148+ case 'eprint': $var =& $eprint; break;
 149+ case 'howpublished': $var =& $howpublished; break;
 150+ case 'institution': $var =& $institution; break;
 151+ case 'journal': $var =& $journal; break;
 152+ case 'key': $var =& $key; break;
 153+ case 'note': $var =& $note; break;
 154+ case 'number': $var =& $number; break;
 155+ case 'organization': $var =& $organization; break;
 156+ case 'pages': $var =& $pages; break;
 157+ case 'publisher': $var =& $publisher; break;
 158+ case 'school': $var =& $school; break;
 159+ case 'series': $var =& $series; break;
 160+ case 'title': $var =& $title; break;
 161+ case 'url': $var =& $url; break;
 162+ case 'year': $var =& $year; break;
 163+ case 'month': $var =& $month; break;
 164+ case 'volume': case 'journal_volume': $var =& $volume; break;
 165+ }
 166+
 167+ if ( $var !== false ) {
 168+ $var = efSRFGetNextDV( $field )->getShortWikiText();
 169+ }
 170+ else {
 171+ switch ( $label ) {
 172+ case 'author': case 'authors': case 'editor' : case 'editors':
 173+ $wikiTexts = array();
 174+ while ( ( /* SMWDataValue */ $dataValue = efSRFGetNextDV( $field ) ) !== false ) {
 175+ $wikiTexts[] = $dataValue->getShortWikiText();
 176+ }
 177+ $var =& $label == 'author' || $label == 'authors' ? $author : $editor;
 178+ $var = $GLOBALS['wgLang']->listToText( $wikiTexts );
 179+ break;
 180+ case 'date':
 181+ $value = efSRFGetNextDV( $field );
 182+
 183+ if ( get_class( $value ) == 'SMWTimeValue' ) {
 184+ if ( $value !== false ) {
 185+ $year = $value->getYear();
 186+ $month = $value->getMonth();
 187+ }
 188+ }
 189+ break;
 190+ }
 191+ }
 192+ }
 193+
 194+ return new SMWBibTeXEntry( $type, $address, $annote, $author, $booktitle, $chapter, $crossref, $doi, $edition, $editor, $eprint, $howpublished, $institution, $journal, $key, $month, $note, $number, $organization, $pages, $publisher, $school, $series, $title, $url, $volume, $year );
 195+ }
 196+
303197 public function getParameters() {
304198 if ( defined( 'SMW_SUPPORTS_VALIDATOR' ) ) {
305199 return array_merge( parent::getParameters(), $this->exportFormatParameters() );

Follow-up revisions

RevisionCommit summaryAuthorDate
r89511follow up to r89501jeroendedauw17:46, 5 June 2011

Status & tagging log