Steps to reproduce
- Take a wiki that has pages in more than one namespace.
- Open a PHP shell (e.g. maintenance/shell.php).
- Issue the following command: MediaWiki\MediaWikiServices::getInstance()->getDBLoadBalancer()->getConnection( DB_REPLICA )->newSelectQueryBuilder()->from( 'page' )->select( 'page_namespace' )->distinct()->fetchFieldValues();
- Notice that it returns an array with multiple values.
- Now issue the following command: MediaWiki\MediaWikiServices::getInstance()->getDBLoadBalancer()->getConnection( DB_REPLICA )->newSelectQueryBuilder()->from( 'page' )->select( 'page_namespace' )->distinct()->fetchRowCount();
Actual result
- Notice that ->fetchRowCount() always returns 1, however many namespaces you have pages in.
Expected result
- Notice that ->fetchRowCount() returns the length of the array returned by ->fetchFieldValues().
Software version
MediaWiki 1.41.0-alpha (47f5a23)
Debugging results
->fetchFieldValues() runs the following SQL command:
SELECT DISTINCT page_namespace AS `value` FROM `page`;
All good.
->fetchRowCount() runs the following SQL command:
SELECT COUNT(*) AS `rowcount` FROM (SELECT DISTINCT 1 FROM `page` WHERE (page_namespace IS NOT NULL)) `tmp_count`;
Of course, there’s only one distinct value of 1…
Why doesn’t it do the following?
SELECT COUNT(*) AS `rowcount` FROM (SELECT DISTINCT page_namespace FROM `page`) `tmp_count`;