Requesting 'sections' for nonexistent page does not give error
Closed, ResolvedPublic

Description

Author: brian.mcneil

Description:
I'm writing a set of PHP classes to replace botclasses.php for automated use of the API. I've come across what I feel is a bug, but the API does not return an error for it.

If I request the sections (TOC) for "Does-not-exist", I get an 'empty' result. If I request the sections (TOC) for "Special:Does-not-exist", I get a real error thrown.

This means I've had to code my function such that when an empty list of sections is returned, it checks if the page exists, thus:

$q  = '&prop=sections&page='.urlencode($page);
if ( $revid !== null )
    $q  .= '&rvstartid='.$revid;

$r  = $this->query_content( $q );
if ( isset($r['error']) ) { // Error getting any page data
    return self::ERR_ret( self::ERR_error, "API error, info:"
        .$result['error']['info']." Result:".$result['error']['code'] );
}
$toc_elem   = $r['parse']['sections'];
if ( empty($toc_elem) ) { // Empty, does that mean page doesn't exist?
    if ( $this->get_page( $page ) == false) {
        return self::ERR_ret( self::ERR_warn, "Requested TOC for nonexistent page" );
    }
}

Because the API is not returning an error straight away that indicates the page does not exist, a second API call is needed to pull the entire page or throw an error because a nonexistent page was requested.

This can be seen with
http://en.wikinews.org/w/api.php?action=parse&format=xml&prop=sections&page=doesnotexist
versus
http://en.wikinews.org/w/api.php?action=parse&format=xml&prop=sections&page=Special:doesnotexist

Only the latter throws an error.


Version: unspecified
Severity: normal

Details

Reference
bz41042
bzimport raised the priority of this task from to High.
bzimport set Reference to bz41042.
bzimport added a subscriber: Unknown Object (MLST).
bzimport created this task.Oct 15 2012, 6:04 PM

The error returned by an API call against a special page doesn't mean the page doesn't exist but you can't create a WikiPage object from some namespaces (in default install, media and special).

If you read the http://en.wikinews.org/w/api.php?action=parse&format=xml&prop=sections&page=Special:doesnotexist error text, you'll see:

<error code="internal_api_error_MWException" info="Exception Caught: Invalid or virtual namespace -1 given." xml:space="preserve"/>

This is an exception thrown by WikiPage::Factory method.

Note the same error will be thrown by API calls requesting sections from *existing* special pages:
https://en.wikinews.org/w/api.php?action=parse&format=xml&prop=sections&page=Special:Version

wmf.amgine3691 wrote:

(In reply to comment #1)

The error returned by an API call against a special page doesn't mean the page
doesn't exist but you can't create a WikiPage object from some namespaces (in
default install, media and special).

The point, Dereckson, is that no error is returned by API action=parse when a page does not exist, despite documentation stating it will.

(In reply to comment #2)

(In reply to comment #1)
> The error returned by an API call against a special page doesn't mean the page
> doesn't exist but you can't create a WikiPage object from some namespaces (in
> default install, media and special).

The point, Dereckson, is that no error is returned by API action=parse when a
page does not exist, despite documentation stating it will.

My comment explains why there is a difference between the two API calls and to stress on the fact the error returned by the second API call weren't related to the page non existence. This helps to clarify the bug and allows to focus on to the point you've just stressed.

(In reply to comment #2)

The point, Dereckson, is that no error is returned by API action=parse when a
page does not exist, despite documentation stating it will.

Where does the documentation state this? The only thing I see at [[mw:API:Parsing wikitext#parse]] that is even close is a note that an error will be returned for an ''invalid'' title. Or are we referring to the misleading message quoted for the "missingtitle" error?

It seems to me that a page that does not exist has no sections, so the returned result is technically correct. As noted above, Special-namespace pages cannot be parsed at all.

Note you can check if the page existed by including 'revid' in the props and checking for 0, e.g. https://en.wikinews.org/w/api.php?action=parse&format=xml&prop=sections|revid&page=doesnotexist. This is noted in the documentation at [[mw:API:Parsing wikitext#parse]].

wmf.amgine3691 wrote:

I interpreted

  • code: missingtitle
    • info: The page you specified doesn't exist.

from https://www.mediawiki.org/wiki/API:Parsing_wikitext#Possible_errors_2 as not at all misleading, but directly and simply stating an error is generated when a page specified does not exist. The same language is used at https://www.mediawiki.org/wiki/API:Parse#Errors_Codes, and it is listed as a standard error at https://www.mediawiki.org/wiki/API:Errors_and_warnings#Standard_error_messages. This would be the least surprising interpretation, in my opinion.

It is *not* the same behavior as API displays in some other actions, however. In &action=query a missing page is simply reported as missing, not an error code.

https://en.wikinews.org/w/api.php?action=query&titles=DoesntExist&prop=info&format=xml

According to [[mw:API:Errors_and_warnings]]

"If something goes wrong in an API request, an error or a warning will be thrown. Warnings are thrown for non-fatal conditions such as invalid parameters, whereas errors are only thrown for fatal conditions."

My personal opinion is there should be an explicit report for both no sections && missing page; the former should throw a warning (setting sections property count=0), the latter might throw either a warning (setting page property missing=';') or an error. A section count property could resolve the former as an enhancement.

I've reviewed the matter, and it seems that the behavior here has recently changed accidentally due to Gerrit change #18973. I've submitted Gerrit change #28225 to revert to the old behavior.

(In reply to comment #6)

I've reviewed the matter, and it seems that the behavior here has recently
changed accidentally due to Gerrit change #18973. I've submitted Gerrit change
#28225 to revert to the old behavior.

Does the API support PHP unit tests? If so, I think it would be nice to add a test for this to prevent this from happening in the future, if possible.

You can use http://en.wikipedia.org/w/api.php?action=query&titles=Not%20Exist|In%7Bvlid|Special:Not-exist to validate one or more titles with the api.

You will get:

<?xml version="1.0"?>
<api>

<query>
  <pages>
    <page ns="0" title="Not Exist" missing="" />
    <page title="In{vlid" invalid="" />
    <page ns="-1" title="Special:Not-exist" special="" missing="" />
  </pages>
</query>

</api>

Special pages also supported and some more stuff (like converting of titles and redirects)

TheDJ added a comment.Oct 30 2012, 7:53 PM

The change was merged, marking as fixed.