Parsing recursive structures without a stack



  • I support a website which provides Atom feeds. The client has recently changed direction, and now wants to have their in-house developer (Declan) create a front end for our site, embedded in another site which they control. This other site runs on Drupal, which has a feed aggregator in its core modules, so that should be simple, right?

    We noticed something curious: Declan's new front end appeared to have the right content, but all of its links went to the last entry in the feed. He assumed that the feed was wrong. I assumed that he was processing the feed incorrectly. Today we tracked it down to a bug in Drupal, and what a bug! Ok, lots of people here have strong opinions against PHP, but Drupal is such a big name that I assumed its core would be well written.

    It sets up some handlers:

      ...
      // Parse the data.
      $xml_parser = drupal_xml_parser_create($data);
      xml_set_element_handler($xml_parser, 'aggregator_element_start', 'aggregator_element_end');
      xml_set_character_data_handler($xml_parser, 'aggregator_element_data');
    

    if (!xml_parse($xml_parser, $data, 1)) {
    ...

    Let's look at them.

    function aggregator_element_start($parser, $name, $attributes) {
      global $item, $element, $tag, $items, $channel;
    

    $name = strtolower($name);
    switch ($name) {
    case 'image':
    case 'textinput':
    case 'summary':
    case 'tagline':
    case 'subtitle':
    case 'logo':
    case 'info':
    $element = $name;
    break;
    case 'id':
    case 'content':
    if ($element != 'item') {
    $element = $name;
    }
    case 'link':
    // According to RFC 4287, link elements in Atom feeds without a 'rel'
    // attribute should be interpreted as though the relation type is
    // "alternate".
    if (!empty($attributes['HREF']) && (empty($attributes['REL']) || $attributes['REL'] == 'alternate')) {
    if ($element == 'item') {
    $items[$item]['link'] = $attributes['HREF'];
    }
    else {
    $channel['link'] = $attributes['HREF'];
    }
    }
    break;
    case 'item':
    $element = $name;
    $item += 1;
    break;
    case 'entry':
    $element = 'item';
    $item += 1;
    break;
    }

    $tag = $name;
    }

    function aggregator_element_end($parser, $name) {
    global $element;

    switch ($name) {
    case 'image':
    case 'textinput':
    case 'item':
    case 'entry':
    case 'info':
    $element = '';
    break;
    case 'id':
    case 'content':
    if ($element == $name) {
    $element = '';
    }
    }
    }

    The schema for an Atom feed allows an atom:entry element to contain some elements (e.g. atom:summary, atom:author) which themselves contain elements, so you might think that it would be useful to have a stack of the elements we're currently inside. But stacks are clearly for wimps.

    The cause of the bug that Declan and I observed is now clear. When we encounter the atom:link element, it will be attributed to the entry only if we haven't encountered an image, textinput, summary, tagline, subtitle, logo, or info since entering the entry. If the feed was written by Zend, which puts the summary before the link, then each entry's link will overwrite the feed's URL. I've skipped this code because I've pasted enough already, but once it finishes "parsing" the XML then any entry which doesn't have its own link will borrow the feed's one, giving the observed behaviour.

    (Bonus WTF: compare the list of tag names in the two functions.)



  • @pjt33 said:

    but Drupal is such a big name that I assumed its core would be well written.

    You assumed wrong.



  • @pjt33 said:

    but Drupal is such a big name that I assumed its core would be well written.

    Isn't that kinda like "Oracle is big, they must do good things"?



  • Considering we sacked a Drupal contributer for being a fecking idiot (and spending the time reading the Morrocan newspaper when he was a month behind on a project) I am not surprised.



  •  fucking



  •  fcking, focking, ficking, fackin



  •  What's fecking, precious?



  • @brian banana said:

    fucking

    @brian banana said:

    fcking, focking, ficking, fackin

    @brian banana said:

    What's fecking, precious?

    How's that stroke working out for ya?



  • @morbiuswilters said:

    @brian banana said:
    fucking

    @brian banana said:

    fcking, focking, ficking, fackin

    @brian banana said:

    What's fecking, precious?

    How's that stroke working out for ya?

     

     

    Nice one. I can feel your sense of humour, as your signature suggests. There's a 50% chance you die of cancer. Was that also funny? Just learning the internetz, you know!



  • @brian banana said:

    There's a 50% chance you die of cancer.

    It's probably waaay higher than that.

    Although, most people around here would probably guess "redneck hunting and/or gay sex accident".

    @brian banana said:

    Was that also funny?

    Wow, were you really having a stroke or something? Seems like I hit a nerve..



  • I've had the misfortune of working with a bunch of different php frameworks, and Drupal (my Druenemy) is bar far the worst pile of crap ever spewed on the face of this beautiful planet. A customer insisted on using Drupal, so I had to give it a go. Within the first day, I'd found 3 big bugs in core modules, and within a week I'd located 5 serious security problems IN THE CORE. Not to mention the shitheap that is contributed modules. I've taken to saying no to any project involving Drupal. If the client insists Drupal is the way to go, they have to go away.



  •  @morbiuswilters said:

    @brian banana said:
    There's a 50% chance you die of cancer.

    It's probably waaay higher than that.

    Standard American Diet does that, true.

     

    @morbiuswilters said:

    Wow, were you really having a stroke or something? Seems like I hit a nerve..

    It's common in most people they see the world only through their perspective. Girlfriend of mine treats a patient, used to be a programmer and know he can't even remember his name. Before she told me that story I was like: fukk stroked people. But now I'm like: C'mon, the guy can't even code no more.That sucks. So, yeah, you hit a nerve.

     Enjoy!



  • You need to relax a bit, there, Mr. Banana - this site isn't worth getting your panties all twisted over...



  • @skotl said:

    You need to relax a bit, there, Mr. Banana - this site isn't worth getting your panties all twisted over...
     

     

    I think this site is worth even more than that. You seem to lack passion for (at least) one site.

    Be well!

     

     


  • ♿ (Parody)

    @brian banana said:

    I think this site is worth even more than that. You seem to lack passion for (at least) one site.

    Sounds like the stroke talking again.


  • Considered Harmful

    I keep confusing him for El_Jeffe and being momentarily confused when he spews nonsense.



  • @joe.edwards said:

    I keep confusing him for El_Jeffe and being momentarily confused when he spews nonsense.
    I guess I have an . . . . admirer?  At least most of my posts (sort of) make sense.


  • ♿ (Parody)

    @El_Heffe said:

    @joe.edwards said:

    I keep confusing him for El_Jeffe and being momentarily confused when he spews nonsense.
    I guess I have an . . . . admirer?  At least most of my posts (sort of) make sense.

    I assume it's some meta joke about him being the top banana.



  • @boomzilla said:

    @brian banana said:
    I think this site is worth even more than that. You seem to lack passion for (at least) one site.

    Sounds like the stroke talking again.

     

    You obviously know a lot about human physiology and neurology. That is a good thing.


  • ♿ (Parody)

    @brian banana said:

    You obviously know a lot about human physiology and neurology. That is a good thing.

    I'm very knowledgeable about lots of stuff. Just ask.



  •  Veal, for instance.



  • @boomzilla said:

    @brian banana said:
    You obviously know a lot about human physiology and neurology. That is a good thing.

    I'm very knowledgeable about lots of stuff. Just ask.

     

     

    First, you mock me, and now you want to answer hypothetical questions I might have?



  • @brian banana said:

    @boomzilla said:

    @brian banana said:
    You obviously know a lot about human physiology and neurology. That is a good thing.
    I'm very knowledgeable about lots of stuff. Just ask.
     

    First, you mock me, and now you want to answer hypothetical questions I might have?

    OK, if you failed to understand that his response was also mocking you english can't be your first language.  Many people here who have it as a second (or third or whatever) language are still able to catch things like that.  Sorry for how you have been mocked for what is apparently a language barrier.


  • ♿ (Parody)

    @locallunatic said:

    @brian banana said:

    @boomzilla said:

    @brian banana said:
    You obviously know a lot about human physiology and neurology. That is a good thing.
    I'm very knowledgeable about lots of stuff. Just ask.
     

    First, you mock me, and now you want to answer hypothetical questions I might have?

    OK, if you failed to understand that his response was also mocking you english can't be your first language.  Many people here who have it as a second (or third or whatever) language are still able to catch things like that.  Sorry for how you have been mocked for what is apparently a language barrier.

    More likely, it's an effect of lack of oxygen to parts of the brain due to stroke.



  • @locallunatic said:

    @brian banana said:

    @boomzilla said:

    @brian banana said:
    You obviously know a lot about human physiology and neurology. That is a good thing.
    I'm very knowledgeable about lots of stuff. Just ask.
     

    First, you mock me, and now you want to answer hypothetical questions I might have?

    OK, if you failed to understand that his response was also mocking you english can't be your first language.  Many people here who have it as a second (or third or whatever) language are still able to catch things like that.  Sorry for how you have been mocked for what is apparently a language barrier.

     

     

    Thanks locallunatic. There might have been mistakes in the stuff I wrote because my first language is the language of Love and not the language of English. Due to these syntactic errors the semantics sometimes get messed up. But I was able to understand that the man who goes by the name of boomzilla was indeed mocking me in every post in here. The "Just aks" was supposed to be understood ironically. I got that. And it was indeed very funny. As for your name: you do seem to be local (somewhere) but no lunatic. So I like you for excusing boomzilla's behaviour.

    Live long and prosper!

     



  • @brian banana said:

    As for your name: you do seem to be local (somewhere) but no lunatic.

    Lunatic is an informal term for someone with a mental illness (though some groups take offense to calling them illnesses) or for someone that acts in wild and silly ways.  It can be either one.



  • @boomzilla said:

    @locallunatic said:

    @brian banana said:

    @boomzilla said:

    @brian banana said:
    You obviously know a lot about human physiology and neurology. That is a good thing.
    I'm very knowledgeable about lots of stuff. Just ask.
     

    First, you mock me, and now you want to answer hypothetical questions I might have?

    OK, if you failed to understand that his response was also mocking you english can't be your first language.  Many people here who have it as a second (or third or whatever) language are still able to catch things like that.  Sorry for how you have been mocked for what is apparently a language barrier.

    More likely, it's an effect of lack of oxygen to parts of the brain due to stroke.


     

    A very creative and funny way to educate people about strokes. Have a nice day!

     

     


  • Considered Harmful

    @locallunatic said:

    @brian banana said:

    As for your name: you do seem to be local (somewhere) but no lunatic.

    Lunatic is an informal term for someone with a mental illness (though some groups take offense to calling them illnesses) or for someone that acts in wild and silly ways.  It can be either one.

    Lunacy was believed to be related to the phases of the moon, thus the prefix luna-; similarly, there is hysteria, from hystera, or womb.

  • Trolleybus Mechanic

    @skotl said:

    this site isn't worth getting your panties all twisted over...
     

    Who said I was wearing panties?



  • Wow the trolls on this site keep getting more and more obvious. In a year or two, we're gonna go from a site with good natured IT-themed trolling to /b/ without the pictures.



  • @joe.edwards said:

    @locallunatic said:

    @brian banana said:

    As for your name: you do seem to be local (somewhere) but no lunatic.

    Lunatic is an informal term for someone with a mental illness (though some groups take offense to calling them illnesses) or for someone that acts in wild and silly ways.  It can be either one.

    Lunacy was believed to be related to the phases of the moon, thus the prefix luna-; similarly, there is hysteria, from hystera, or womb.
     

     

    Thx for that clarification, you guys! One can learn a lot from TDWTF: good code, how netiquette works, what or when a lunatic is. Nice! Now to forget the prior exchange with my special knowledgeable friend.

     

    Bye, my shift is over!

     

     



  • @joe.edwards said:

    Lunacy was believed to be related to the phases of the moon, thus the prefix luna-; similarly, there is hysteria, from hystera, or womb.
     

    So hysterical lunacy is menstruation?



  • @dhromed said:

    @joe.edwards said:

    Lunacy was believed to be related to the phases of the moon, thus the prefix luna-; similarly, there is hysteria, from hystera, or womb.
     

    So hysterical lunacy is menstruation?

    Isn't it?



  • @joe.edwards said:

    @locallunatic said:

    @brian banana said:

    As for your name: you do seem to be local (somewhere) but no lunatic.

    Lunatic is an informal term for someone with a mental illness (though some groups take offense to calling them illnesses) or for someone that acts in wild and silly ways.  It can be either one.

    Lunacy was believed to be related to the phases of the moon, thus the prefix luna-; similarly, there is hysteria, from hystera, or womb.

    Well yeah, and when it was used as a "diagnosis" they actually thought that the moon phases were the cause of problems for people.  I was just trying to point out that under modern usage it can be applicable to a wide range of individuals.



  • @dhromed said:

    @joe.edwards said:

    Lunacy was believed to be related to the phases of the moon, thus the prefix luna-; similarly, there is hysteria, from hystera, or womb.
     

    So hysterical lunacy is menstruation?

    Or Lady Orgasm. Which is just a myth.



  • @Psychosmurf said:

    Drupal (my Druenemy) is bar far the worst pile of crap ever spewed on the face of this beautiful planet.

    That can't be right. Plone exists.



  • @brian banana said:

    @boomzilla said:

    @brian banana said:
    You obviously know a lot about human physiology and neurology. That is a good thing.

    I'm very knowledgeable about lots of stuff. Just ask.

     

     

    First, you mock me, and now you want to answer hypothetical questions I might have?

    Would you like to ask about Excel empties Undo stack on Save?



  • @MiffTheFox said:

    Wow the trolls on this site keep getting more and more obvious. In a year or two, we're gonna go from a site with good natured IT-themed trolling to /b/ without the pictures.

    How the hell do people like that find this site?



  • @brian banana said:

    @morbiuswilters said:

    @brian banana said:
    There's a 50% chance you die of cancer.

    It's probably waaay higher than that.

    Standard American Diet does that, true.

    I think that's obesity, heart disease and stroke. The American diet isn't very cancer-ful (at least compared to most countries). Well, except possibly breast cancer, but that's just any diet with adequate nutrition.

    @brian banana said:

    Girlfriend of mine treats a patient, used to be a programmer and know he can't even remember his name. Before she told me that story I was like: fukk stroked people. But now I'm like: C'mon, the guy can't even code no more.That sucks. So, yeah, you hit a nerve.

    Is this incoherent rambling supposed to mean something? Listen, Cracky McCracksalot: put down the crack pipe and try to engage your brain before you start typing.



  • @morbiuswilters said:

    @dhromed said:

    @joe.edwards said:

    Lunacy was believed to be related to the phases of the moon, thus the prefix luna-; similarly, there is hysteria, from hystera, or womb.
     

    So hysterical lunacy is menstruation?

    Or Lady Orgasm. Which is just a myth.

    That sounds like a brand of deodorant.  "Lady Orgasm - now in peach and black cherry."

     



  • @morbiuswilters said:

    @MiffTheFox said:

    Wow the trolls on this site keep getting more and more obvious. In a year or two, we're gonna go from a site with good natured IT-themed trolling to /b/ without the pictures.

    How the hell do people like that find this site?

    It was linked on a HVAC message board, as a comment about "look how much worse those white-collar motherfuckers have it than us".

     



  • @flabdablet said:

    @brian banana said:

    @boomzilla said:

    @brian banana said:
    You obviously know a lot about human physiology and neurology. That is a good thing.

    I'm very knowledgeable about lots of stuff. Just ask.

     

     

    First, you mock me, and now you want to answer hypothetical questions I might have?

    Would you like to ask about Excel empties Undo stack on Save?

    I disable undo in Excel. Instead I have a macro that does a partial restore of pagefile snapshots.


Log in to reply