tags:

views:

138

answers:

1

Hello,

We have a wiki that was designed by a 3rd party developer. The problem we have had is that users' who have been made "inactive" still show up in searches. The work around to stop user from being seen is removing properties associated with that user.

To access the user properties, we would make a call to api passing a url encoded username is this format: http://www.ourwiki.com/@api/users/=john_smith%40ourwiki.com/properties

This will then return XML that has the properites itemized.

The next step is what I'm not really sure how to proceed. We need to somehow loop through all of the properties and get the name of the property so that we can delete them making a Delete call through the API. The format for Delete is DELETE:users/{userid}/properties/{key}

Ideally, it would be nice to pass the username as a parameter once we have a working script.

I'm somewhat new to working with APIs in PHP so if anyone has some ideas or can help with this it would be greatly appreciated.

Please see this link to download the properties xml file: http://queencitytech.com/properties.zip

Here is what is returned after retreving the user properties:

12010-04-29T04:39:29ZAdminAdmin0ee8d636cdd5e57469516332b9325cbchttp://www.avatar.com/avatar/0ee8d636cdd5e57469516332b9325cbc.pngJohnh2010-04-29T04:39:29ZAdminAdmin0ee8d636cdd5e57469516332b9325cbchttp://www.avatar.com/avatar/0ee8d636cdd5e57469516332b9325cbc.pngJohn2010-04-29T04:39:29ZAdminAdmin0ee8d636cdd5e57469516332b9325cbchttp://www.avatar.com/avatar/0ee8d636cdd5e57469516332b9325cbc.pngSmith2010-04-29T04:39:30ZAdminAdmin0ee8d636cdd5e57469516332b9325cbchttp://www.avatar.com/avatar/0ee8d636cdd5e57469516332b9325cbc.pngSmith, John2010-04-29T04:39:30ZAdminAdmin0ee8d636cdd5e57469516332b9325cbchttp://www.avatar.com/avatar/0ee8d636cdd5e57469516332b9325cbc.pngJohn2010-04-29T04:39:31ZAdminAdmin0ee8d636cdd5e57469516332b9325cbchttp://www.avatar.com/avatar/0ee8d636cdd5e57469516332b9325cbc.png757E2010-04-29T04:39:31ZAdminAdmin0ee8d636cdd5e57469516332b9325cbchttp://www.avatar.com/avatar/0ee8d636cdd5e57469516332b9325cbc.pngExt: 61322010-04-29T04:39:31ZAdminAdmin0ee8d636cdd5e57469516332b9325cbchttp://www.avatar.com/avatar/0ee8d636cdd5e57469516332b9325cbc.pngJohn_Smith@ourwiki.com2010-04-29T04:39:32ZAdminAdmin0ee8d636cdd5e57469516332b9325cbchttp://www.avatar.com/avatar/0ee8d636cdd5e57469516332b9325cbc.pngmiddleware/images/headshots/01_799/01_799_hs.jpg2010-04-29T04:39:32ZAdminAdmin0ee8d636cdd5e57469516332b9325cbchttp://www.avatar.com/avatar/0ee8d636cdd5e57469516332b9325cbc.pngmiddleware/images/lifestyle/01_799/01_799_ls.jpg2010-04-29T04:39:32ZAdminAdmin0ee8d636cdd5e57469516332b9325cbchttp://www.avatar.com/avatar/0ee8d636cdd5e57469516332b9325cbc.pngImplementation2010-04-29T04:39:33ZAdminAdmin0ee8d636cdd5e57469516332b9325cbchttp://www.avatar.com/avatar/0ee8d636cdd5e57469516332b9325cbc.pngWeil, T2010-04-29T04:39:33ZAdminAdmin0ee8d636cdd5e57469516332b9325cbchttp://www.avatar.com/avatar/0ee8d636cdd5e57469516332b9325cbc.pngCo-op2010-04-29T04:39:34ZAdminAdmin0ee8d636cdd5e57469516332b9325cbchttp://www.avatar.com/avatar/0ee8d636cdd5e57469516332b9325cbc.png0000-00-00 00:00:002010-04-29T04:39:34ZAdminAdmin0ee8d636cdd5e57469516332b9325cbchttp://www.avatar.com/avatar/0ee8d636cdd5e57469516332b9325cbc.pngCoop2010-05-02T04:40:35ZAdminAdmin0ee8d636cdd5e57469516332b9325cbchttp://www.avatar.com/avatar/0ee8d636cdd5e57469516332b9325cbc.pngfalse2010-05-03T19:09:[email protected][email protected][email protected]://www.avatar.com/avatar/52909eac4d19209592168dc96f3d4fca.pngCincinnati2010-05-14T04:15:13ZAdminAdmin0ee8d636cdd5e57469516332b9325cbchttp://www.avatar.com/avatar/0ee8d636cdd5e57469516332b9325cbc.png4572010-07-16T04:50:13ZAdminAdmin0ee8d636cdd5e57469516332b9325cbchttp://www.avatar.com/avatar/0ee8d636cdd5e57469516332b9325cbc.png2010-07-14 16:06:232010-07-16T04:50:17ZAdminAdmin0ee8d636cdd5e57469516332b9325cbchttp://www.avatar.com/avatar/0ee8d636cdd5e57469516332b9325cbc.png

Here is the file that includes var_dump(array($delete, $property)) http://queencitytech.com/deletescript.php.html

A: 

Assuming you have the XML stored in a variable name $xmlString and the user id in the variable $user_id you could use the following code:

$delete = "http://www.ourwiki.com/@api/DELETE:users/$user_id/properties/%s";
$xml = new SimpleXMLElement($xmlString);

foreach($xml->property as $property) {
  $name = $property['name']; // the name is stored in the attribute
  file_get_contents(sprintf($delete, $name));
}

Or if you know all the possible properties it would be easier to have a function which does all those calls in one go without the need to read the properties prior.

$properties = array(); // array of property names
$delete = "http://www.ourwiki.com/@api/DELETE:users/$user_id/properties/%s";

foreach($properties as $property) {
  $name = $property['name'];
  file_get_contents(sprintf($delete, $name));
}
mhitza
Thanks, I will try this. Apparently when making a call to /properties it will list all the properties associated with the user. The user properties is key value where the key and value can be anything so there is no way of being able to know all options unfortunately.
Aaron
I understand that I need to get the XML which I believe is done by calling api (http://www.ourwiki.com/@api/users/=john_smith%40ourwiki.com/properties) But how would I take that response and store it in a variable? It would be great if a user could type in email and it would make the call to get xml and perform the delete.
Aaron
By issuing a call to the `file_get_contents('ourwiki.com/@api/users/=john_smith%40ourwiki.com/properties')` will return the XML, which afterwards you can pass to the `SimpleXMLElement` class.
mhitza
Ok cool, so I can just assign this to the $xmlString and SimpleXMLElement should be able to read it?
Aaron
Yes, that is all to it.
mhitza
would it be easy to add a place for a user to type in the username and it pass that to the $userid? the username will be in the form [email protected]
Aaron
So I'm trying to run this script on the server but i'm not having much luck. When trying to troubleshoot where the problem is, it doesn't seem to be giving me the properties when I do #xmlString=file_get_contents('ourwiki.com/@api/users/=john_smith%40ourwiki.com/properties'). I've tried to echo xmlString and it is empty, however when i run ourwiki.../properties directly it gives me the xml
Aaron
The `allow_url_fopen` option may be disabled in your environment. Make sure to check the `phpinfo()` information for this case. If so, you should go the other way around: opening a socket, making a request, and parsing the response.
mhitza
allow_url_fopen says On for Local and Master values. I even tried testing using ini_get which tells me that its enabled. Does this mean I still would need to open socket, make request, etc? Also, I tried using wget on the server with ourwiki.com/.../properties, it connects, says HTTP request sent, awaiting response...401 Unauthorized Authorization failed. Could this be part of the problem? I'm not sure if wget will work since we're talking about getting xml but its just a thought. Any ideas?
Aaron
So i've been testing this on a VM and I've gotten much further. I was having an failed to open stream, and by supplying credentials it allowed file_get_contents to work! Now, it is telling me, "Warning: sprintf() Too few arguments" Do you know how I could fix this by any chance?
Aaron
Check that both `$delete` and `$property` hold the values they should, no `null` values.
mhitza
Whats the best way to check for that? It almost seems that each entry has this "too few arguments" since it printed it about 30 times when I tried running the script. I did attempt the api call for properties on a test user and the name is always set to an actual value so its not empty. Not sure if that helps at all.
Aaron
`var_dump(array($delete, $property))` inside the loop.
mhitza
Thanks, i'll have to do this once i get to work in the morning since i don't have access to the VM now. I'll update you with the results.
Aaron
I just added a link above at the end of the post, it includes what is returned after checking $delete and $property values. Before it you'll see var_dump($xmlString) just in case you weren't sure.
Aaron
I've edited my code. As it turned out I didn't read the attribute name in the second example.
mhitza
I was actually using the first example, since we aren't able to know all the possible properties. Is there something I might need to change there?
Aaron
Aha, I suspect he `%40` in the url, replace it with `@`
mhitza
Closer...the warning went away and now it just looks to be hanging on the api call with DELETE. It is saying, "failed to open stream: HTTP request failed! HTTP/1.1 404 Not Found" This is what I have for $delete... $delete = "http://admin:[email protected]/@api/DELETE:users/$user_id/properties/%s"
Aaron
So I was given an example of deleting property with curl (property "foo") curl -u username:password -X DELETE -i http://server.address/@api/pages/=bar/properties/foo . Is this something we could incorporate instead of using the current DELETE call as we have it? Apparently this is "supposed" to work
Aaron