Skip Navigation

Archive for March, 2009

paginating ad-hoc joins in CakePHP

Saturday, March 28th, 2009

Couple months ago, Nate published in the bakery a quick tip to do what he called ad-hoc joins in cakephp’s find()
Basically the trick was to use the joins parameter of the method find() to define the associations.
I have noticed a lot of people asking about how to paginate the results.
So This post is exactly for doing that..

I’ll be using the modified version found in the comments by Bambou

Basically His version allows to define the type of find() using a parameter called operation

just copy it to your AppModel, or the model that you want to do ad-hoc joins on:

Now for pagination to work with the new ‘matches’ type, you’ll have to write a custom paginateCount()
copy this to your AppModel or the model in question:

  1.  
  2. function paginateCount($conditions = array(), $recursive = 0, $extra = array()) {
  3.  
  4.         $parameters = compact(‘conditions’);
  5.         if ($recursive != $this->recursive) {
  6.                 $parameters[‘recursive’] = $recursive;
  7.         }
  8.         if(isset($extra[‘type’]) && ($extra[‘type’] == ‘matches’)) {
  9.                 $extra[‘operation’] = ‘count’;
  10.                 return $this->find(‘matches’, array_merge($parameters, $extra));
  11.         } else {
  12.                 return $this->find(‘count’, array_merge($parameters, $extra));
  13.         }       
  14. }

Usage is nearly identical to the normal paginate() except that instead of doing:

  1.  $markers = $this->Marker->find(‘matches’, array(
  2.        ‘operation’ => ‘all’,
  3.         ‘model’ => ‘Tag’,
  4.         ’scope’ => array(‘Tag.tag’ => $tags)
  5.    )
  6. );

You’ll do:

  1.  array_unshift($this->paginate, ‘matches’);
  2. $this->paginate[‘model’] = ‘Tag’;
  3. $this->paginate[‘operation’] = ‘all’;
  4. $this->paginate[’scope’] = array(‘Tag.tag’=>$tags);
  5. $result = $this->paginate();

The trick indeed is in array_unshift($this->paginate, ‘matches’);
and has to do with how paginate() works internally ( makes the assumption/convention that the first element in the paginate array is the type )

So that’s it, hopefully this will help some misguided cakephp souls ;)

w00t! I’ve got wordpress spam-hacked!

Friday, March 27th, 2009

It’s been a while ( like really really a while, since I looked here )
I won’t get on the details of what-why, mainly family, sickness and a lot of other work experiments.
Anyhow, I apologize for not being here, answering comments and posting new stuff.
But no worries I’m back on the rails and hope to be able to post some new exciting stuff

Today I’ve got an email from google telling me that this blog is scheduled for removal from their indexes, whaaat!
Here is the complete email:

Dear site owner or webmaster of devmoz.com,

While we were indexing your webpages, we detected that some of your pages were using techniques that are outside our quality guidelines, which can be found here: http://www.google.com/support/webmasters/bin/answer.py?answer=35769&hl=en. This appears to be because your site has been modified by a third party. Typically, the offending party gains access to an insecure directory that has open permissions. Many times, they will upload files or modify existing ones, which then show up as spam in our index.

The following is some example hidden text we found at http://www.devmoz.com/blog/category/cakephp/:

[bunch of spam words here]

In order to preserve the quality of our search engine, pages from devmoz.com are scheduled to be removed temporarily from our search results for at least 30 days.

We would prefer to keep your pages in Google’s index. If you wish to be reconsidered, please correct or remove all pages (may not be limited to the examples provided) that are outside our quality guidelines. One potential remedy is to contact your web host technical support for assistance. For more information about security for webmasters, see http://googlewebmastercentral.blogspot.com/2008/04/my-sites-been-hacked-now-what.html. When such changes have been made, please visit https://www.google.com/webmasters/tools/reconsideration?hl=en to learn more and submit your site for reconsideration.

Sincerely, Google Search Quality Team
Note: if you have an account in Google’s Webmaster Tools, you can verify the authenticity of this message by logging into https://www.google.com/webmasters/tools/siteoverview?hl=en and going to the Message Center.

I’ve got really shocked ! I do HATE spam so how can this be possible.
I’ve looked at the page and found indeed a hidden div containing tons of links pointing to some spam websites. It was clear I’ve got SPAM HACKED.
I quickly ssh-ed to the wordpress files, run through the web logs, some greps.. and found out the links in footer.php, I removed the huge list of links, chmod-ed the file to readonly.
I was running wordpress version 2.2.1 or something, haven’t had any plugins installed other than akismet, openid and dean’s code highlighter. so must have been a vulnerability in the version of wordpress I was running.
Well since I haven’t been here in a while I couldn’t upgrade, duh!

I looked through http://codex.wordpress.org/Upgrading_WordPress_Extended to get me an idea on how to upgrade to the latest version of wordpress, and to patch my custom made theme. I was anxious but I could upgrade in less than 15min.
I logged into the google webmaster tool’s thingy and sent google a nice message, I just hope they’ll reconsider the removal :(

So people moral of the story is, upgrade your open source software often!