Make WordPress Core

Opened 15 years ago

Closed 15 years ago

#12362 closed defect (bug) (fixed)

wpdb code improvements

Reported by: hakre's profile hakre Owned by: nacin's profile nacin
Milestone: 3.0 Priority: normal
Severity: normal Version: 3.0
Component: Database Keywords:
Focuses: Cc:

Description

In Ticket #11644 we started to improve the general code qualitity of wpdb in multiple iterations. We continue here to not clutter the main ticket up any longer.

Attachments (2)

11644.code_cleanup_wpdb-3nd_iteration.patch (10.1 KB) - added by nacin 15 years ago.
12362_4th-iteration.patch (6.0 KB) - added by hakre 15 years ago.
Patch updated because of missing whitespace fixes.

Download all attachments as: .zip

Change History (11)

#1 follow-up: @nacin
15 years ago

  • Owner set to nacin
  • Status changed from new to accepted

The title's fine, though I don't really need the self aggrandizing. Anyway, I'm uploading your most recent patch. Here is your most recent comment:


Thanks for the feedback. For the version number lookup, that's a job I'll do, no need for you to care. I thought for the same for sorting it, I just wanted to tell you about the Idea. But that's fine anyway now, so I do not need to do it.

Here is a new numbered list, some older unfixed are still on it, just ignore if you do not want to care, I keep them for reference. There are three or four more major points left over, for other just give me a hint what you prefer, sometimes I asked questions, would be great if you can answer them so I know better for upcomming patches we do together.

  1. UNFIXED destruct() breaks with PHP5 defition of the return value (should be void, is true)
  2. UNFIXED: in the variable definitions, the meta tables sometimes precede their main table. Also tables are not sorted by their names. Let me know if you would like to see that fixed.
  3. UNFIXED: I'm not sure wether unsetting members of an array while iterating it is save with all the PHP version we aim to unset( $tables[ $k ] );.
  4. QUERSTION: you've set access to wpdb::$blogid to public. why? should other parts of the program overwrite the blogid for a certain functionality? Same for wpdb::$siteid. A public setter for both is wpdb::set_blog_id(). Maybe a get_blog_id() function with an optional parameter so that it's a getter for siteid as well?
  5. QUESTION: you skip the note about the problem of registering the wpdb instance with the register shutdown function call prevents it from unloading as expected. What's the problem with leaving a note there? Same for the hints given in wpdb::desctruct(). I found those thoughtfull so we at least have the discrepancies documented.
  6. QUESTION: What's the problem with the scope 'now+old'? I mean there are at least two points in wpdb that will benefit from it.
  7. QEUSTION: What's the problem with listing the scope values inside the docblock. I think that's easier to read than inside the @param description. And easier to maintain.
  8. wpdb::tables() did not return an array as specified for certain scope values (the undefined ones were forgotten). Patched, will prevent notices/warnings inside the function as well.
  9. There is no need to cast an array as array. wpdb::tables() always returns an array, you know that - well okay, not it is.
  1. Since you did not want to have the merge inside wpdb::tables() (for a reason I do not understand), array_merge still actually works. I prefer to encapsulate that inside tables(), really.
  2. All parameters in tables() are optional. @param should reflect that.
  3. Scopes can be sorted by alphabet in the switch construct in wpdb::table()
  4. @uses does not fit here, please use @see as already suggested in wpdb::_weak_escape().
  5. INFO: for the same class you do not need to prefix wpdb:: for @see and @uses. Additionally PHP function names can be used with @see as well.
  6. QUESTION: Sometimes @-tags are aligning their values, sometimes not. Any Idea? Normally alignment is missing in core code, so with these iteration I removed those where I saw them.
  7. MINOR: If in for single quotes on 'foo', should be in for single quotes on 'SELECT... as well, right?
  8. There is a problem with the preg_replace call in wpdb::prepare() reported on another ticket. I suggest to keep the old behaviour.
  9. QUESTION: Since when is $this always a reference? Since the beginning or has that changed later?
  10. SKIPPED: I did not care about error_log now. I'll do that later. I saw you changed something there, my Idea was to check against the base reference. I suggest to keep that the old code until we have fully clarified that to not break behaviour.
  11. It's toally clear that some queries are made prior to load the plugin API. No need to comment, right? Or if so, why not comment the other parts I find totally clear as well (e.g. preventing the unload)
  12. INFO: wpdb::insert( 'table', array( 'column' => 'foo', 'field' => 1337 ) ) was the original example. I just added one, I just saw no need to edit it (FYI).
  13. INFO: I still think that constants are normally written uppercase, but I dropped that for this patch to reduce the FUZZ. This needs to be clarified elsewere anyway I assume.
  14. There still was a parameter documented that does not exists in wpdb::has_cap(), you've forgotten a line to remove or was that by intention?
  15. You made a mistake in changes applied to wpdb::get_caller(), $caller is not filled with values any longer. A line was missing. You can expand it if you like using a temporary variable like $function, but I just patched it.
  16. Additionally I moved the join next to return (as always suggested) in wpdb::get_caller().
  17. Another parameter that does not exists documented in wpdb::db_version() (see 23.)
  18. QUESTION: Include files to not need control characters after the closing ?> at the end, right?

#2 in reply to: ↑ 1 ; follow-up: @nacin
15 years ago

Replying to hakre:

  1. __destruct() return value (should be void, is true)

Doesn't really matter though.

  1. in the variable definitions, the meta tables sometimes precede their main table.

Sorted by names (which means "commentmeta" comes before "comments"), but I pushed multisite to the end. I think we're good.

  1. I'm not sure wether unsetting members of an array while iterating it is save with all the PHP version we aim to unset( $tables[ $k ] );.

This should be no problem as-is.

  1. you've set access to wpdb::$blogid to public.

We use it publicly during the MS loading process. I don't think it's necessary to create setters.

  1. you skip the note about the problem of registering the wpdb instance with the register shutdown function call prevents it from unloading as expected. What's the problem with leaving a note there? Same for the hints given in wpdb::desctruct(). I found those thoughtfull so we at least have the discrepancies documented.

Not sure I really follow or see the need. We register it for PHP4, I imagine. No reason to document this.

  1. QUESTION: What's the problem with the scope 'now+old'? I mean there are at least two points in wpdb that will benefit from it.

It's bulky and less clear at what is going on. The alternative, array_merge() on this->tables(blog) and this->tables(old) before looping through, seems unnecessary and also less clear at what is going on. (It's also slower, technically.) Currently it is very clear that we're setting up global tables, then blog tables, then the old tables.

  1. What's the problem with listing the scope values for table inside the docblock.

We can move it into the docblock.

  1. wpdb::tables() did not return an array as specified for certain scope values (the undefined ones were forgotten). Patched, will prevent notices/warnings inside the function as well.

Ok, I think we can just add default: return array(); . It only accesses private properties otherwise.

  1. There is no need to cast an array as array. wpdb::tables() always returns an array, you know that - well okay, not it is.

Sure, we can pull those out.

  1. Since you did not want to have the merge inside wpdb::tables() (for a reason I do not understand), array_merge still actually works. I prefer to encapsulate that inside tables(), really.

Ok, we don't have any merges outside of wpdb::tables() (see point 6).

  1. All parameters in tables() are optional.

Ok.

  1. Scopes can be sorted by alphabet in the switch construct in wpdb::table()

Sure.

  1. @uses does not fit here, please use @see as already suggested in wpdb::_weak_escape().

Ok.

  1. for the same class you do not need to prefix wpdb:: for @see and @uses.

Sounds about right.

  1. Normally alignment is missing in core code, so with these iteration I removed those where I saw them.

Sure.

  1. If in for single quotes on 'foo', should be in for single quotes on 'SELECT... as well, right?

I would rather not put queries in single quotes, because when a plugin author does that and uses a $wpdb->{table} reference, it doesn't work.

  1. There is a problem with the preg_replace call in wpdb::prepare() reported on another ticket. I suggest to keep the old behaviour.

[13357]

  1. Since when is $this always a reference? Since the beginning or has that changed later?

From the PHP 4 manual: "In PHP4, it was necessary to use a reference to create a callback that points to the actual object, and not a copy of it."

  1. I did not care about error_log now. I'll do that later. I saw you changed something there, my Idea was to check against the base reference. I suggest to keep that the old code until we have fully clarified that to not break behaviour.

I went over the logic step by step -- my head hurt when I was done, as did westi's -- and I'm pretty sure I cleaned it up while keeping the same behavior.

  1. It's toally clear that some queries are made prior to load the plugin API. No need to comment, right?

It's clear to you and me, but not to a run-of-the-mill plugin author. We only check whether the plugin API is loaded four times total.

  1. INFO: wpdb::insert( 'table', array( 'column' => 'foo', 'field' => 1337 ) ) was the original example.

I know, I decided to change it to clarify how field types are handled.

  1. INFO: I still think that constants are normally written uppercase, but I dropped that for this patch to reduce the FUZZ. This needs to be clarified elsewere anyway I assume.

Across core we consistently use true|false|null, not TRUE|FALSE|NULL, especially in inline docs. It reads better anyway.

  1. There still was a parameter documented that does not exists in wpdb::has_cap(), you've forgotten a line to remove or was that by intention?

Ok, we can remove.

  1. You made a mistake in changes applied to wpdb::get_caller(), $caller is not filled with values any longer. A line was missing. You can expand it if you like using a temporary variable like $function, but I just patched it.

Looks like I fudged the merge there. Will fix.

  1. Additionally I moved the join next to return (as always suggested) in wpdb::get_caller().

See point 24.

  1. Another parameter that does not exists documented in wpdb::db_version() (see 23.)

Ok.

  1. QUESTION: Include files to not need control characters after the closing ?> at the end, right?

Pretty sure the correct way is the way it is. (We see editors trying to munge the end-of-file line endings all the time, hence why it's in the patch.)

#3 @automattor
15 years ago

(In [13386]) update wpdb blogid using setter, See #12362

#4 @hakre
15 years ago

Another patch now containing the missing version numbers. This time w/o a list, should be pretty straight-forward to apply. After this is comitted I'll create a last list as summary so unfixed stuff / related tickets won't get lost documentation wise.

@hakre
15 years ago

Patch updated because of missing whitespace fixes.

#5 in reply to: ↑ 2 @miqrogroove
15 years ago

  1. QUESTION: Include files to not need control characters after the closing ?> at the end, right?

Pretty sure the correct way is the way it is. (We see editors trying to munge the end-of-file line endings all the time, hence why it's in the patch.)

When necessary:

return;
?>

I never leave PHP tags open.

#6 @nacin
15 years ago

This looks good, though we need to leave $blog_id == 0 there. Thanks a lot for the version numbers, I know how annoying those are to do.

#7 @nacin
15 years ago

(In [13426]) @since version numbers and other docs for wp-db. props hakre see #12362

#8 @nacin
15 years ago

(In [13578]) Fix notice. See #12362

#9 @nacin
15 years ago

  • Component changed from General to Database
  • Keywords featured removed
  • Resolution set to fixed
  • Status changed from accepted to closed
  • Summary changed from wpdb code improvements - nacin / hakre iterations to wpdb code improvements
Note: See TracTickets for help on using tickets.