Make WordPress Core

Opened 13 months ago

Last modified 8 weeks ago

#40510 assigned enhancement

REST API: Post Revisions: Adding support for pagination

Reported by: benoitperson Owned by: flixos90
Milestone: 5.0 Priority: normal
Severity: normal Version: 4.7
Component: REST API Keywords: has-patch needs-unit-tests
Focuses: rest-api Cc:

Description (last modified by flixos90)

The post revisions REST endpoint currently has no support for pagination. While it works well for most usages with fairly low counts of revisions (WordPress.com limits the number at 25 for instance), on posts with a revision count in the hundreds (or even thousands), this can quickly yield unreasonably large (Mb+ of pure text) and long (several seconds on my "test" instances) responses.

The API already supports pagination for a vast majority of ressources (posts, pages, categories, tags, users) and is normalised around using 2 integer parameters: page (default: 1)and per_page (default: 10).

I wonder if there is any reason why pagination was never implemented on that endpoint? Is it something potentially worth working on? Considering some of the past issues around high memory usage for the revisions page of wp-admin [0][1], it seems like getting a way to only load (for instance) the most recent revisions would be an interesting win to provide a smoother experience with revisions.

[0] https://core.trac.wordpress.org/ticket/34560 [1] https://core.trac.wordpress.org/ticket/24958

PS: To provide some (more) context, there is an ongoing effort to support revisions in Calypso: https://github.com/Automattic/wp-calypso/pull/12733 from which this enhancement request emerged.

Attachments (3)

40510.diff (6.9 KB) - added by flixos90 3 months ago.
40510.2.diff (13.8 KB) - added by adamsilverstein 8 weeks ago.
40510.3.diff (26.8 KB) - added by birgire 8 weeks ago.

Download all attachments as: .zip

Change History (9)

#1 @danielbachhuber
13 months ago

I wonder if there is any reason why pagination was never implemented on that endpoint?

Probably an oversight, to be honest.

Is it something potentially worth working on?

Go for it!

#2 @rmccue
13 months ago

  • Keywords needs-patch added
  • Milestone changed from Awaiting Review to Future Release

Seems to just be something that was missed. Let's add it. :)

3 months ago

#3 @flixos90
3 months ago

  • Description modified (diff)
  • Keywords has-patch needs-unit-tests added; needs-patch removed
  • Milestone changed from Future Release to 5.0
  • Owner set to flixos90
  • Status changed from new to assigned

Per today's REST API chat, supporting pagination and generally allowing a bit more flexibility with the revisions controller would also come in handy for Gutenberg.

40510.diff adds support for the following query parameters for revisions:

  • exclude
  • include
  • offset
  • order (default 'desc')
  • orderby (default 'date')
  • page (default 1)
  • per_page (default not provided, falling back to using -1 with WP_Query, meaning no limit, for BC with current behavior)
  • search

The implementation aligns closer with how the posts controller works. WP_Query is directly used instead of calling wp_get_post_revisions(), to be able to run a SELECT FOUND_ROWS() query and get the result. Since the function is only a simple wrapper for a query object anyway which sets default arguments, this can easily be handled in the controller itself.

The revisions controller will now return X-WP-Total and X-WP-TotalPages headers and pagination link headers (if applicable) for collection requests, just how the posts controller does.

This ticket was mentioned in Slack in #core-restapi by flixos90. View the logs.

3 months ago

#5 @adamsilverstein
8 weeks ago

This looks good @flixos90! Tested with the JS client and I see all of the expected headers and pagination functionality.

In 40510.2.diff I added a cast to (int) for $request['page'];use and update the fixtures. (phpunit --group=restapi-jsclient or grunt precommit generates this file). Some tests to validate all of the new features would be nice.

Last edited 8 weeks ago by adamsilverstein (previous) (diff)

#6 @birgire
8 weeks ago

This seems to be in a good progress.

The posts rest controller handles out-of-bounds cases for the page parameter. I think it's originally from here: https://github.com/WP-API/WP-API/commit/789de83a3c5b4f8d6c6cf5fd63408702833c0cb5

The comments and users rest controllers handles the number and offset in a similar way.

I played around with the offset and page parameters in 40510.2.diff and noticed that for out-of-bound values, we loose the information for X-WP-Total and X-WP-TotalPages as they only show 0.

Here's an example for a post with two revisions and per_page as 1:

    X-WP-Total 2
    X-WP-TotalPages 2
    Link <http://wp.localhost/index.php?rest_route=%2Fwp%2Fv2%2Fposts%2F3%2Frevisions&per_page=1&page=2>; rel="next"

    X-WP-Total 2
    X-WP-TotalPages 2
    Link <http://wp.localhost/index.php?rest_route=%2Fwp%2Fv2%2Fposts%2F3%2Frevisions&per_page=1&page=1>; rel="prev"

    X-WP-Total 0
    X-WP-TotalPages 0
    Link <http://wp.localhost/index.php?rest_route=%2Fwp%2Fv2%2Fposts%2F3%2Frevisions&per_page=1&page=0>; rel="prev"

Here we see what happens with the out-of-bound value 3 for page.

The reason is that the found_posts and max_num_pages properties of WP_Query are not calculated for empty results set. I'm not sure why that has to happen, because it's useful information.

We can see that here:


as there is no $this->set_found_posts( $q, $limits ); call, but even if it were, there's an empty posts check within that method too.

Then I started to look at the corresponding tests and 40510.3.diff is a suggestion that:

  • Handles out-of-bound values for offset and page in the same way as the posts, comments and users rest controllers do.
  • Adds the rest_revision_invalid_offset_number error, similar to the existing rest_revision_invalid_page_number error.
  • Introduces tests for the revision query parameters:
    • offset
    • page
    • per_page
    • search

The tests for the posts rest controller where really helpful, but I tried to break them up into single test methods.

The patch also adds another revision and the total revision count to the current fixtures, to make it more usable by the newly added tests. I was hesitated to do this, but I ended up doing it to reduce the new code lines ;-) Using data providers might also be considered.

Last edited 8 weeks ago by birgire (previous) (diff)

8 weeks ago

Note: See TracTickets for help on using tickets.