Defect #50

can't override sort_items in multifeed + merge_items can't be overridden

Added by Michael Shipley 860 days ago. Updated 429 days ago.

Status:New Start:2008-03-21
Priority:High Due date:
Assigned to:Ryan McCue % Done:

0%

Category:Miscellaneous
Target version:1.3
Affected Version:

1.1.3

PHP Version:

5.2.5

mbstring enabled:

Yes

iconv enabled:

Yes

cURL enabled:

Yes

zlib enabled:

Yes


Description

The SP function "merge_items" located at line 3010 in SP111 sorts items by calling the function "sort_items" statically, preventing "sort_items" from being overridden for a custom sort by extending the SP class.

To fix this, you need to replace "SimplePie" with "&$this" on line 3039 in SP111:

3039 usort($items, array('SimplePie', 'sort_items'));
should be:
3039 usort($items, array(&$this, 'sort_items'));

Also in the "get_items" function on line 2909 in SP111 the function "merge_items" is also called statically so its not possible to override that either by extending the SP class. It must be modified in the same way to allow it to be overridden.

2913 return SimplePie::merge_items($this->multifeed_objects, $start, $end, $this->item_limit);
should be:
2913 return $this->merge_items($this->multifeed_objects, $start, $end, $this->item_limit);

History

Updated by Geoffrey Sneddon 860 days ago

  • Status changed from Unconfirmed to New
  • Assigned to set to Ryan Parman

Updated by Michael Shipley 798 days ago

I'd like to take a stab at fixing this bug if its ok with everyone. Since this will be the first time using svn I'd like to make sure I do it correctly. I've installed TortoiseSVN, checked out the SP trunk, made the changes as I described above, and ran the unit tests. All the unit tests show the same result before and after my patch. I also ran some specific tests related to this bug and all passed. Is there anything else I should do? Do I just do a commit on simplepie.inc now?

proposed patch:

Index: simplepie.inc
===================================================================
--- simplepie.inc    (revision 973)
+++ simplepie.inc    (working copy)
@@ -2933,7 +2933,7 @@
         {
             if (!empty($this->multifeed_objects))
             {
-                $this->data['items'] = SimplePie::merge_items($this->multifeed_objects, $start, $end, $this->item_limit);
+                $this->data['items'] = $this->merge_items($this->multifeed_objects, $start, $end, $this->item_limit);
             }
             else
             {
@@ -3061,7 +3061,15 @@
             $item = null;
             if ($do_sort)
             {
-                usort($items, array('SimplePie', 'sort_items'));
+                // if called by multifeed, use $this, else, use SimplePie
+                if(isset($this))
+                {
+                    usort($items, array($this, 'sort_items'));               
+                }
+                else
+                {
+                    usort($items, array('SimplePie', 'sort_items'));
+                }
             }

Updated by Geoffrey Sneddon 798 days ago

  • Assigned to changed from Ryan Parman to Michael Shipley

SimplePie::merge_items() is a static method — we shouldn't be calling it dynamically. We can't change it to a dynamic method without breaking API compat., thus it must remain as is. If you want to add a way to over-ride it, it needs to be done some other way.

Updated by Michael Shipley 798 days ago

But doesn't the patch handle the dynamic/static problem by checking for a null $this?

- usort($items, array('SimplePie', 'sort_items'));
+ // if called by multifeed, use $this, else, use SimplePie
+ * if(isset($this))*
+ {
+ usort($items, array($this, 'sort_items'));
+ }
+ else
+ {
+ usort($items, array('SimplePie', 'sort_items'));
+ }

Updated by Michael Shipley 798 days ago

Yeah now that I think about it, it has to be done another way. Although it does work, it's not a good long term solution.

Updated by Ryan Parman 798 days ago

Take a look at how the other set_*_class() methods are implemented. SimplePie_Misc should be the default class, but people can override which class to use by setting $feed->set_misc_class('SimplePie_Misc_Custom');

Updated by Geoffrey Sneddon 798 days ago

That's not the dynamic/static problem: the problem is that on PHP 5 calling a non-static method dynamically causes an E_STRICT (and we should be able to just add the static keyword to what is defined as being static in comments to remove all those errors).

Updated by Ryan McCue 791 days ago

If you don't mind, I'll take a stab at this on the weekend. So much to do, so little time...

Updated by Michael Shipley 791 days ago

No problem, I could use the help. I was thinking the best solution would be to just duplicate merge_items, call it merge_items_internal and change SimplePie::sort_items call to $this->sort_items. Then change the static call to SimplePie::merge_items when processing multifeeds to $this->merge_items_internal. This would enable sort_items to be overridden for multifeeds, while at the same time keeping the API intact since the static merge_items method will still be there. What do you think?

Updated by Ryan McCue 699 days ago

  • Assigned to changed from Michael Shipley to Ryan McCue

Assigning this to me, I'll get around to it at some point.

Updated by Geoffrey Sneddon 638 days ago

  • Target version changed from 1.1.2 to 1.2

This isn't an urgent bug fix, and so shouldn't be 1.1.2.

Updated by Ryan McCue 624 days ago

What-say we move the sorting method to SimplePie_Misc and wait for #98?

Updated by Geoffrey Sneddon 620 days ago

  • Affected Version changed from 1.1.1 to 1.1.2

Updated by Geoffrey Sneddon 586 days ago

  • Affected Version changed from 1.1.2 to 1.1.3

Updated by Geoffrey Sneddon 429 days ago

  • Target version changed from 1.2 to 1.3

Also available in: Atom PDF