Skip to content

Loading…

Fixed Parse and Merge of Cache URI in MySQL Class #268

Merged
merged 5 commits into from

3 participants

@devlinjunker

Before the MySQL constructor was using merge_array_recursion to merge the default options and the parsed URI. This would combine both values under the same key into an array under that key, rather than merge the newer value, and cause errors when trying to use the values to set up PDO.

@AJ
AJ commented

I was facing the same issue so implemented the change that you've made as a part of your pull request. However, this has an additional side-effect of removing the ['extras']['prefix'] variable and hence giving warnings of the sort:

[Sun Mar 03 13:11:57 2013] [error] [client ::1] PHP Notice: Undefined index: prefix in /SimplePie/Cache/MySQL.php on line 141, referer: http://localhost:8888/
[Sun Mar 03 13:11:57 2013] [error] [client ::1] PHP Notice: Undefined index: prefix in /SimplePie/Cache/MySQL.php on line 150, referer: http://localhost:8888/
[Sun Mar 03 13:11:57 2013] [error] [client ::1] PHP Notice: Undefined index: prefix in /SimplePie/Cache/MySQL.php on line 311, referer: http://localhost:8888/
[Sun Mar 03 13:11:57 2013] [error] [client ::1] PHP Notice: Undefined index: prefix in /SimplePie/Cache/MySQL.php on line 351, referer: http://localhost:8888/
[Sun Mar 03 13:11:57 2013] [error] [client ::1] PHP Notice: Undefined index: prefix in /SimplePie/Cache/MySQL.php on line 389, referer: http://localhost:8888/
[Sun Mar 03 13:11:59 2013] [error] [client ::1] PHP Notice: Undefined index: prefix in /SimplePie/Cache/MySQL.php on line 179, referer: http://localhost:8888/
[Sun Mar 03 13:11:59 2013] [error] [client ::1] PHP Notice: Undefined index: prefix in /SimplePie/Cache/MySQL.php on line 188, referer: http://localhost:8888/
[Sun Mar 03 13:11:59 2013] [error] [client ::1] PHP Notice: Undefined index: prefix in /SimplePie/Cache/MySQL.php on line 227, referer: http://localhost:8888/

@rmccue
SimplePie member

What we need to do is write our own merge function that does this for us. It should merge arrays, but prefer the latter string (e.g.).

@devlinjunker

Yeah, I didn't notice this because I was using a prefix when I made the change.

I wrote a few lines of code that stepped through each element of the parsed array and copy it into the options array if it exists, with a check to see if it is an array. If I turned it into a recursive function it would work even better because it wouldn't matter how deep the array recursion was.

If I do this, what class should I put the function in?

@rmccue
SimplePie member

If I turned it into a recursive function it would work even better because it wouldn't matter how deep the array recursion was.

This should definitely be a recursive function to make sure that it works with all drivers.

If I do this, what class should I put the function in?

It should be SimplePie_Misc::array_merge_recursive()

Also, make sure you're using tabs instead of spaces for your code. Coding standards also say that your conditionals should look like if ($condition) rather than if($condition)

Thanks for your work on this so far! :star2:

devlinjunker added some commits
@devlinjunker devlinjunker Created Array Merge Recursive Function in SimplePie_Misc, called from…
… Cache_MySQL

Recursive Merge Array function that keeps values in array1 unless there
are values with the same keys in array2.

Only called from MySQL Cache Class for now.
b3ab44a
@devlinjunker devlinjunker Fixed Tabs vs Spaces b6736e4
@devlinjunker devlinjunker Call SimplePie_Misc::array_merge_recursive from SimplePie_Cache_Memca…
…che class now, haven't tested
4d42a89
@devlinjunker

I've written the function and it's now called in the MySQL and Memcache constructors. I haven't tested it for the Memcache, but it's working for MySQL as far as I can tell.

@rmccue rmccue merged commit 7040218 into simplepie:master

1 check passed

Details default The Travis build passed
@rmccue
SimplePie member

Thanks for that! :sparkles: :cake:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Mar 1, 2013
  1. @devlinjunker
Commits on Mar 11, 2013
  1. @devlinjunker
Commits on Mar 12, 2013
  1. @devlinjunker

    Created Array Merge Recursive Function in SimplePie_Misc, called from…

    devlinjunker committed
    … Cache_MySQL
    
    Recursive Merge Array function that keeps values in array1 unless there
    are values with the same keys in array2.
    
    Only called from MySQL Cache Class for now.
  2. @devlinjunker

    Fixed Tabs vs Spaces

    devlinjunker committed
Commits on Mar 13, 2013
  1. @devlinjunker

    Call SimplePie_Misc::array_merge_recursive from SimplePie_Cache_Memca…

    devlinjunker committed
    …che class now, haven't tested
Showing with 21 additions and 5 deletions.
  1. +2 −4 library/SimplePie/Cache/Memcache.php
  2. +2 −1 library/SimplePie/Cache/MySQL.php
  3. +17 −0 library/SimplePie/Misc.php
View
6 library/SimplePie/Cache/Memcache.php
@@ -95,10 +95,8 @@ public function __construct($location, $name, $type)
'prefix' => 'simplepie_',
),
);
- $parsed = SimplePie_Cache::parse_URL($location);
- $this->options['host'] = empty($parsed['host']) ? $this->options['host'] : $parsed['host'];
- $this->options['port'] = empty($parsed['port']) ? $this->options['port'] : $parsed['port'];
- $this->options['extras'] = array_merge($this->options['extras'], $parsed['extras']);
+ $this->options = SimplePie_Misc::merge_array_recursive($this->options, SimplePie_Cache::parse_URL($location);
+
$this->name = $this->options['extras']['prefix'] . md5("$name:$type");
$this->cache = new Memcache();
View
3 library/SimplePie/Cache/MySQL.php
@@ -96,7 +96,8 @@ public function __construct($location, $name, $type)
'prefix' => '',
),
);
- $this->options = array_merge_recursive($this->options, SimplePie_Cache::parse_URL($location));
+
+ $this->options = SimplePie_Misc::array_merge_recursive($this->options, SimplePie_Cache::parse_URL($location));
// Path is prefixed with a "/"
$this->options['dbname'] = substr($this->options['path'], 1);
View
17 library/SimplePie/Misc.php
@@ -224,6 +224,23 @@ public static function fix_protocol($url, $http = 1)
}
}
+ public static function array_merge_recursive($array1, $array2)
+ {
+ foreach ($array2 as $key => $value)
+ {
+ if (is_array($value))
+ {
+ $array1[$key] = SimplePie_Misc::array_merge_recursive($array1[$key], $value);
+ }
+ else
+ {
+ $array1[$key] = $value;
+ }
+ }
+
+ return $array1;
+ }
+
public static function parse_url($url)
{
$iri = new SimplePie_IRI($url);
Something went wrong with that request. Please try again.