r74717 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r74716‎ | r74717 | r74718 >
Date:16:18, 13 October 2010
Author:yaron
Status:deferred
Tags:
Comment:
Changes and added funtions for use by the new #get_web_data parser function
Modified paths:
  • /trunk/extensions/ExternalData/ED_Utils.php (modified) (history)

Diff [purge]

Index: trunk/extensions/ExternalData/ED_Utils.php
@@ -53,20 +53,28 @@
5454 return $args;
5555 }
5656
57 - // This function parses the data argument
58 - static function parseMappings( $dataArg ) {
59 - $dataArg = preg_replace ( "/\s\s+/" , " " , $dataArg ); // whitespace
60 - $rawMappings = split( ",", $dataArg );
61 - $mappings = Array();
62 - foreach ( $rawMappings as $rawMapping ) {
63 - $vals = split( "=", $rawMapping, 2 );
64 - if ( count( $vals ) == 2 ) {
65 - $intValue = trim( $vals[0] );
66 - $extValue = trim( $vals[1] );
67 - $mappings[$intValue] = $extValue;
 57+ /**
 58+ * Parses an argument of the form "a=b,c=d,..." into an array
 59+ */
 60+ static function paramToArray( $arg, $lowercaseKeys = false, $lowercaseValues = false ) {
 61+ $arg = preg_replace ( "/\s\s+/" , " " , $arg ); // whitespace
 62+ $keyValuePairs = split( ",", $arg );
 63+ $returnArray = Array();
 64+ foreach ( $keyValuePairs as $keyValuePair ) {
 65+ $keyAndValue = split( "=", $keyValuePair, 2 );
 66+ if ( count( $keyAndValue ) == 2 ) {
 67+ $key = trim( $keyAndValue[0] );
 68+ if ( $lowercaseKeys ) {
 69+ $key = strtolower( $key );
 70+ }
 71+ $value = trim( $keyAndValue[1] );
 72+ if ( $lowercaseValues ) {
 73+ $value = strtolower( $value );
 74+ }
 75+ $returnArray[$key] = $value;
6876 }
6977 }
70 - return $mappings;
 78+ return $returnArray;
7179 }
7280
7381 static function getLDAPData ( $filter, $domain, $params ) {
@@ -422,4 +430,54 @@
423431 return $row->result;
424432 }
425433 }
 434+
 435+ /**
 436+ * Checks whether this URL is allowed, based on the
 437+ * $edgAllowExternalDataFrom whitelist
 438+ */
 439+ static public function isURLAllowed( $url ) {
 440+ // this code is based on Parser::maybeMakeExternalImage()
 441+ global $edgAllowExternalDataFrom;
 442+ $data_from = $edgAllowExternalDataFrom;
 443+ $text = false;
 444+ if ( empty( $data_from ) ) {
 445+ return true;
 446+ } elseif ( is_array( $data_from ) ) {
 447+ foreach ( $data_from as $match ) {
 448+ if ( strpos( $url, $match ) === 0 ) {
 449+ return true;
 450+ }
 451+ }
 452+ return false;
 453+ } else {
 454+ if ( strpos( $url, $data_from ) === 0 ) {
 455+ return true;
 456+ } else {
 457+ return false;
 458+ }
 459+ }
 460+ // we shouldn't ever get here, but just in case...
 461+ return false;
 462+ }
 463+
 464+ static public function getDataFromURL( $url, $format ) {
 465+ $url_contents = EDUtils::fetchURL( $url );
 466+ // exit if there's nothing there
 467+ if ( empty( $url_contents ) )
 468+ return array();
 469+
 470+ if ( $format == 'xml' ) {
 471+ return self::getXMLData( $url_contents );
 472+ } elseif ( $format == 'csv' ) {
 473+ return self::getCSVData( $url_contents, false );
 474+ } elseif ( $format == 'csv with header' ) {
 475+ return self::getCSVData( $url_contents, true );
 476+ } elseif ( $format == 'json' ) {
 477+ return self::getJSONData( $url_contents );
 478+ } elseif ( $format == 'gff' ) {
 479+ return self::getGFFData( $url_contents );
 480+ }
 481+ return array();
 482+ }
 483+
426484 }

Status & tagging log