From: Brion Vibber <brion(a)pobox.com>
Date: February 21, 2007 5:52:38 PM EST
To: MediaWiki announcements and site admin list <mediawiki-
l(a)lists.wikimedia.org>
Subject: Re: [Mediawiki-l] DB query: Articles of a given Category
Reply-To: MediaWiki announcements and site admin list <mediawiki-
l(a)lists.wikimedia.org>
Vernon Thommeret wrote:
I'm trying to come up with code that can tell
me whether or not a
given category has articles or not. Naturally, I borrowed some code
from CategoryPage.php. This is what I got up to:
$res2 = $dbr->select(
array( 'page', 'categorylinks' ),
array( 'page_title' ),
array( 'cl_from = page_id', 'cl_to' => $title->getArticleID()),
Ok, your first problem is that cl_to contains the target _name_,
not ID.
So you'll want to use $title->getDbKey() here.
$fname,
array('LIMIT' => 1 ));
echo $res2;
echo $dbr->fetchObject($res2);
The "echo $res2" spouts off a "Resource #12" or whatever, but the
second echo doesn't leave anything.
fetchObject() returns false when there are no more rows to fetch. The
boolean false value when coerced to string (as when echo'ing) turns
into
a nice empty "", so you see nothing.
If you got a result here you'd get an object ID or something dumped to
output.
Also, consider using $dbr->selectRow() here, so you won't have to call
fetchObject() and freeResult() manually for your single-row result.
- -- brion vibber (brion @
pobox.com / brion @
wikimedia.org)
Thanks for your help. I got the code working perfectly. One thing,
however, is that the category will never update on their own. I have
to modify LocalSettings.php, and then it reparses the page. Is this
something I have to live with?
Thanks again,
Vernon