r68018 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r68017‎ | r68018 | r68019 >
Date:19:01, 14 June 2010
Author:daniel
Status:deferred
Tags:
Comment:
adapter for OpenLibrarySource; removed overly complex path evaulation from WebDataTransclusionSource
Modified paths:
  • /trunk/extensions/DataTransclusion/DBDataTransclusionSource.php (modified) (history)
  • /trunk/extensions/DataTransclusion/DataTransclusion.php (modified) (history)
  • /trunk/extensions/DataTransclusion/OpenLibrarySource.php (added) (history)
  • /trunk/extensions/DataTransclusion/WebDataTransclusionSource.php (modified) (history)

Diff [purge]

Index: trunk/extensions/DataTransclusion/WebDataTransclusionSource.php
@@ -1,6 +1,6 @@
22 <?php
33 /**
4 - * DataTransclusion Source base class
 4+ * DataTransclusion Source implementation
55 *
66 * @file
77 * @ingroup Extensions
@@ -252,49 +252,25 @@
253253
254254 $p = array_shift( $path );
255255
256 - if ( strpos( $p, '|' ) ) { //alternatives
257 - $alternatives = explode( '|', $p );
258 - foreach ( $alternatives as $a ) {
259 - $ap = array_merge( array( $a ), $path );
260 - $v = $this->extractField( $data, $ap );
 256+ if ( is_string( $p ) && preg_match( '/^(@)?(\d+)$/', $p, $m ) ) { //numberic index
 257+ $i = (int)$m[2];
261258
262 - if ( $v !== null && $v !== false ) {
263 - return $v;
264 - }
 259+ if ( $m[1] ) { //meta-index
 260+ $k = array_keys( $data );
 261+ $p = $k[ $i ];
265262 }
266 - } else if ( is_string( $p ) && preg_match( '/^\(([^\w\d])\)$/', $p, $m ) ) { //concat all
267 - $s = "";
268 - foreach ( $data as $d ) {
269 - $v = $this->extractField( $d, $path );
 263+ }
270264
271 - if ( $v !== null && $v !== false ) {
272 - if ( $s != "" ) $s .= $m[1];
273 - $s .= $v;
274 - }
275 - }
 265+ if ( !isset( $data[ $p ] ) ) {
 266+ return false;
 267+ }
276268
277 - return $s;
278 - } else {
279 - if ( is_string( $p ) && preg_match( '/^(@)?(\d+)$/', $p, $m ) ) { //numberic index
280 - $i = (int)$m[2];
 269+ $next = $data[ $p ];
281270
282 - if ( $m[1] ) { //meta-index
283 - $k = array_keys( $data );
284 - $p = $k[ $i ];
285 - }
286 - }
287 -
288 - if ( !isset( $data[ $p ] ) ) {
289 - return false;
290 - }
291 -
292 - $next = $data[ $p ];
293 -
294 - if ( $next && $path ) {
295 - return $this->extractField( $next, $path );
296 - } else {
297 - return $next;
298 - }
 271+ if ( $next && $path ) {
 272+ return $this->extractField( $next, $path );
 273+ } else {
 274+ return $next;
299275 }
300276
301277 //TODO: named components. separator??
Index: trunk/extensions/DataTransclusion/DataTransclusion.php
@@ -33,6 +33,7 @@
3434 $wgAutoloadClasses['FakeDataTransclusionSource'] = $dir . 'DataTransclusionSource.php';
3535 $wgAutoloadClasses['DBDataTransclusionSource'] = $dir . 'DBDataTransclusionSource.php';
3636 $wgAutoloadClasses['WebDataTransclusionSource'] = $dir . 'WebDataTransclusionSource.php';
 37+$wgAutoloadClasses['OpenLibrarySource'] = $dir . 'OpenLibrarySource.php';
3738
3839 $wgHooks['ParserFirstCallInit'][] = 'efDataTransclusionSetHooks';
3940
Index: trunk/extensions/DataTransclusion/DBDataTransclusionSource.php
@@ -1,6 +1,6 @@
22 <?php
33 /**
4 - * DataTransclusion Source base class
 4+ * DataTransclusion Source implementation
55 *
66 * @file
77 * @ingroup Extensions
Index: trunk/extensions/DataTransclusion/OpenLibrarySource.php
@@ -0,0 +1,112 @@
 2+<?php
 3+/**
 4+ * DataTransclusion Source implementation
 5+ *
 6+ * @file
 7+ * @ingroup Extensions
 8+ * @author Daniel Kinzler for Wikimedia Deutschland
 9+ * @copyright © 2010 Wikimedia Deutschland (Author: Daniel Kinzler)
 10+ * @licence GNU General Public Licence 2.0 or later
 11+ */
 12+
 13+if ( !defined( 'MEDIAWIKI' ) ) {
 14+ echo( "This file is an extension to the MediaWiki software and cannot be used standalone.\n" );
 15+ die( 1 );
 16+}
 17+
 18+/**
 19+ * Implementations of DataTransclusionSource, fetching data records via HTTP,
 20+ * from the OpenLibrary RESTful API.
 21+ *
 22+ * This class provides all necessary $spec options per default. However, some
 23+ * may be specifically useful to override:
 24+ *
 25+ * * $spec['url']: use an alternative URL to access the API. The default is
 26+ * http://openlibrary.org/api/books?bibkeys=ISBN:{isbn}&details=true
 27+ * * $spec['httpOptions']: array of options to pass to Http::get.
 28+ * For details, see Http::request.
 29+ * * $spec['timeout']: seconds before the request times out. If not given,
 30+ * $spec['httpOptions']['timeout'] is used. If both are not givern,
 31+ * 5 seconds are assumed.
 32+ *
 33+ * For more information on options supported by DataTransclusionSource and
 34+ * WebDataTransclusionSource, see the class-level documentation there.
 35+ */
 36+class OpenLibrarySource extends WebDataTransclusionSource {
 37+
 38+ function __construct( $spec ) {
 39+ if ( !isset( $spec['url'] ) ) {
 40+ $spec['url'] = 'http://openlibrary.org/api/books?bibkeys=ISBN:{isbn}&details=true';
 41+ }
 42+
 43+ if ( !isset( $spec['dataFormat'] ) ) {
 44+ $spec['dataFormat'] = 'json';
 45+ }
 46+
 47+ if ( !isset( $spec['errorPath'] ) ) {
 48+ $spec['errorPath'] = '?';
 49+ }
 50+
 51+ if ( !isset( $spec['keyFields'] ) ) {
 52+ $spec['keyFields'] = 'isbn';
 53+ }
 54+
 55+ if ( !isset( $spec['fieldNames'] ) ) {
 56+ $spec['fieldNames'] = 'author,date,publisher,title,url';
 57+ }
 58+
 59+ if ( !isset( $spec['sourceInfo'] ) ) {
 60+ $spec['sourceInfo'] = array();
 61+ }
 62+
 63+ if ( !isset( $spec['sourceInfo']['description'] ) ) {
 64+ $spec['sourceInfo']['description'] = 'The Open Library Project';
 65+ }
 66+
 67+ if ( !isset( $spec['sourceInfo']['homepage'] ) ) {
 68+ $spec['sourceInfo']['homepage'] = 'http://openlibrary.org';
 69+ }
 70+
 71+ if ( !isset( $spec['sourceInfo']['license'] ) ) {
 72+ $spec['sourceInfo']['license'] = 'PD';
 73+ }
 74+
 75+ WebDataTransclusionSource::__construct( $spec );
 76+ }
 77+
 78+ public function flattenRecord( $rec ) {
 79+ $r = array();
 80+ $r['date'] = $rec['details']['publish_date'];
 81+
 82+ $r['title'] = $rec['details']['title'];
 83+ if ( @$rec['details']['title_prefix'] ) {
 84+ $r['title'] = trim( $rec['details']['title_prefix'] )
 85+ . ' ' . trim( $r['title'] );
 86+ }
 87+
 88+ $r['url'] = $rec['info_url'];
 89+
 90+ $r['publisher'] = "";
 91+ foreach ( $rec['details']['publishers'] as $publisher ) {
 92+ if ( $r['publisher'] != "" ) $r['publisher'] .= '; ';
 93+ $r['publisher'] .= $publisher;
 94+ }
 95+
 96+ $r['author'] = "";
 97+ foreach ( $rec['details']['authors'] as $author ) {
 98+ if ( $r['author'] != "" ) $r['author'] .= ', ';
 99+ $r['author'] .= $author['name'];
 100+ }
 101+
 102+ return $r;
 103+ }
 104+
 105+ public function extractRecord( $data ) {
 106+ $data = array_values( $data );
 107+
 108+ $rec = $data[0];
 109+ $rec = $this->flattenRecord( $rec );
 110+
 111+ return $rec;
 112+ }
 113+}
Property changes on: trunk/extensions/DataTransclusion/OpenLibrarySource.php
___________________________________________________________________
Name: svn:mergeinfo
1114 +
Name: svn:eol-style
2115 + native

Status & tagging log