Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Plugin "Newsfeed"
#1
Alright,

I'm joining the fray and will try to create a plugin. I'm opening this thread in order to post questions and snapshots. I'm a newbie so please bear with me. I don't expect any ready-made solutions as replies to my questions, a simple "RTFM at [link]" and a few hints here and there will mostly do I believe.

My goal is to create a plugin which maintains an ATOM newsfeed and syncs feed items on creation/modification/deletion of pages.

I will update this thread irregularly.

04.04.2011 → Progress on basic file and folder checks, setting up the environment so SimpleXML can enter the stage soon

09.04.2011 → Handling the XML and stuff
Reply
#2
My first question is if I can somehow check for creation of a page with a hook?

I saw a page-delete and a changedata-save hook in the wiki docs, but none for when a page is created. Have I overlooked it?

Can I maybe use changedata-save with some additional check if the page already had a respective xml file in /data/pages? I'm thinking of reading in the existing pages into an array and then check for existence.
Reply
#3
polyfragmented Wrote:My first question is if I can somehow check for creation of a page with a hook?

I saw a page-delete and a changedata-save hook in the wiki docs, but none for when a page is created. Have I overlooked it?

Can I maybe use changedata-save with some additional check if the page already had a respective xml file in /data/pages? I'm thinking of reading in the existing pages into an array and then check for existence.

If I remember correctly, check for isset($_POST['post-id']) on changedata-save: if it's not set, it's a new page.
I18N, I18N Search, I18N Gallery, I18N Special Pages - essential plugins for multi-language sites.
Reply
#4
mvlcek Wrote:If I remember correctly, check for isset($_POST['post-id']) on changedata-save: if it's not set, it's a new page.

Thanks, will look into it.
Reply
#5
I'm having problems with the check, it always returns that the variable is set, so the message always is "Existing page has been modified". PHP version is 5.2.12, all GS requirements in health check are met.

Code:
# Check if an existing page has been modified or a new page has been page created
function thp_newsfeed_page_saved() {
    
    if (isset($_POST['post-id'])) {
        echo 'Existing page has been modified';        
    } else {
        echo 'New page has been created';
    }
    
}


Additionally, my page-delete event function doesn't display anything after deleting a page. I have set up the page-delete hook to fire my function.
Code:
# Existing page has been deleted
add_action('page-delete', 'thp_newsfeed_page_deleted');

Code:
function thp_newsfeed_page_deleted() {
    
    echo 'Page has been deleted.';
    
}

Suggestions?
Reply
#6
I just checked changedata.php. post-id is set but empty thus the condition is
Code:
if ($_POST['post-id']) { /* changed */ } else { /* new */ }

You won't see any output on page-delete as there is a redirect at the end - outputing text might prevent the redirect from happening, though.
I18N, I18N Search, I18N Gallery, I18N Special Pages - essential plugins for multi-language sites.
Reply
#7
Thanks, the check is working now. Nice. \o/
Reply
#8
Currently implementing basic file and folder checks, almost done: create a GSDATAOTHER/myplugin folder and copy a barebones atom.xml template file from GSDATAPLUGIN/myplugin folder. That template file will later be modified using SimpleXML (insertions of entries, update feed modification date,...)

My thinking is to use GSDATAOTHER/myplugin as a tmp folder where manipulations to the ATOM file are being staged. If no errors raise their ugly heads, a final copy to GSROOTPATH will be done.

On to SimpleXML goodness next!
Reply
#9
polyfragmented Wrote:On to SimpleXML goodness next!
It's fun isn't it? Some nights I just cant step away from the computer screen...
- Chris
Thanks for using GetSimple! - Download

Please do not email me directly for help regarding GetSimple. Please post all your questions/problems in the forum!
Reply
#10
ccagle8 Wrote:
polyfragmented Wrote:On to SimpleXML goodness next!
It's fun isn't it? Some nights I just cant step away from the computer screen...
Yes, it is fun, especially if what you do scratches an itch. I expected plugin development to be much harder, I'm glad it appears to be easier than I thought. Smile I found it to be very satisfying when the checks and copy command worked tonight, hehe.
Reply
#11
As an aside,

I just performed a Google search for 'basic atom newsfeed file', because I had forgotton what's in a barebones ATOM file... and found this thread to be on spot 7 of the results, hehe. Funny.
Reply
#12
I'm currently learning how to use SimpleXML and would like a hint on how to save modifications back to a XML file? Let's take this example with (still) hard-coded element content:

Code:
# Load the feed file
    $xml = simplexml_load_file($thp_newsfeed_atom_template_destination);
        
    # Add an entry element as  child
    $entry = $xml->addChild('entry');
        
    # Add child elements to entry
    $entry->addChild('title', 'Page XYZ has been created');
    $entry->addChild('link', 'http://');
    $entry->addChild('id', 'Generate UUID or other id');
    $entry->addChild('updated', 'Insert update time');
    $entry->addChild('summary', 'Repeat title?');

I read up on asXML(), but I don't get it. In example 1 given at php.net, the output includes <?xml version="1.0"?> which is not what I want. Example 2 doesn't have it, but uses XPath. Is example 2 possibly what I want?
Reply
#13
Another development question: how do I debug my PHP code best without echoing stuff to the screen? As Martin Vlcek pointed out, it may mess up GS. I'm currently getting stuck on changedata.php without page modifications being saved (because my code contains errors)

How do you guys debug your PHP code? Any opensource tools available? Does anybody use FirePHP? Or Aptana Studio?
Reply
#14
polyfragmented Wrote:I read up on asXML(), but I don't get it.

I always used XMLsave from admin/inc/basic.php, there you see that the usage is

Code:
$success = $xml->asXML($file) === TRUE;

where $file is the file name including path.

polyfragmented Wrote:How do you guys debug your PHP code? Any opensource tools available? Does anybody use FirePHP? Or Aptana Studio?

Debug mode on and echo and print_r($variable) (for arrays and more complex structures) are my friends ;-)
I18N, I18N Search, I18N Gallery, I18N Special Pages - essential plugins for multi-language sites.
Reply
#15
I couldn't find a function or variable to get the current user. Searched the wiki and here on the forum. Have I overlooked it? (it does happen *cough*)

I need this info for the author element in the ATOM feed.
Reply
#16
polyfragmented Wrote:I couldn't find a function or variable to get the current user. Searched the wiki and here on the forum. Have I overlooked it? (it does happen *cough*)

I need this info for the author element in the ATOM feed.

In admin/template/include-nav.php it uses $USR

I tried
global $USR;

and it seems to work in the back end.

-Rob A>
Reply
#17
polyfragmented Wrote:I couldn't find a function or variable to get the current user. Searched the wiki and here on the forum. Have I overlooked it? (it does happen *cough*)

I need this info for the author element in the ATOM feed.

The author of the Feed? It shouldn't be the current user - especially in GetSimple 3.0, where we have multiple users, but rather the name of the web site from the settings, compare heise.de:

Code:
<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
    <title>heise online News</title>
    <subtitle>Nachrichten nicht nur aus der Welt der Computer</subtitle>
    <link href="http://www.heise.de/newsticker/" />
    <link rel="self" href="http://www.heise.de/newsticker/heise-atom.xml" />
    <updated>2011-04-07T15:42:17+02:00</updated>
    <author>
        <name>heise online</name>
    </author>
    <id>http://www.heise.de/newsticker/</id>
...
I18N, I18N Search, I18N Gallery, I18N Special Pages - essential plugins for multi-language sites.
Reply
#18
mvlcek Wrote:The author of the Feed? It shouldn't be the current user - especially in GetSimple 3.0, where we have multiple users, but rather the name of the web site from the settings, compare heise.de: <snip>
Well, heise.de, as in the example, *is* the author, only it's a pool of authors for the website under a catch-all if you will.

The AtomEnabled website says:
Quote:<author> and <contributor> describe a person, corporation, or similar entity. It has one required element, name, and two optional elements: uri, email.

<name> conveys a human-readable name for the person.

Code:
<author>
  <name>John Doe</name>
  <email>JohnDoe@example.com</email>
  <uri>http://example.com/~johndoe</uri>
</author>

There are link elements reserved for the main feed URL and entries' URLs.

I'd say the user who created/modified the page should be mentioned. I'll try Rob's approach soon and report back.
Reply
#19
Slowly getting a grip on handling the XML.

I can load the file (easy), iterate through it (medium), edit elements plus their attributes (easy) and save to file (easy). asXML() was much easier than I thought, I just read up on it on some other tutorial site and it clicked.

While doing this I'm optimising my code writing which slows down progress of course -- I find it hard to resist though. I started off with speaking variables and functions which were "namespaced" (read prefixed), but too long/detailed and I didn't use constants for oft-used stuff like paths to data/plugin folders. I heavily comment my code which is partly for me and partly for others who might have a look-see at some point in the future -- this takes some time as well, but seems well-worth the effort.

Started making a list of system variables I can use for the main feed elements like site name, site url, page date, etc. I set up folder and file checks and catch creation/modification of pages now.

So, things are progressing slowly, but I take tremendous satisfaction from completed and working functions. (yay.) I may have a first snapshot for review sometime next week.
Reply
#20
RobA Wrote:global $USR;
Rob, Chris just revealed this in another thread:

Code:
get_cookie('GS_ADMIN_USERNAME')
which works. I echoed this for testing purposes and it showed my user name.
Reply
#21
Thanks for the follow-up. I hadn't seen that other thread.

-Rob A>
Reply
#22
Note to self: don't forget to put code in include files inside <?php ... ?>...
Reply
#23
I have an XML file which, for testing purposes, I'm reading in, then echoing. It seems not all nodes/elements are read in/echoed back though, one is missing.

The code
Code:
<?php

# Load the file into an object
$sxml = simplexml_load_file('atom.xml');

# Echo the object to the screen
echo $sxml->asXML();

?>

The file

Code:
<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">

  <entry>
    <title>Testeintrag 01</title>
    <link href="http://www.testing-simplexml.de">http://www.test.de</link>
    <id>000001</id>
    <updated>now</updated>
    <summary>...Lorem ipsum</summary>
  </entry>

</feed>

Any idea why <title> is missing from the output?

Code:
http://www.test.de 000001 now
...Lorem ipsum
Reply




Users browsing this thread: 2 Guest(s)