From 1b691f504b7de16547bdf909de509f1e2a688d73 Mon Sep 17 00:00:00 2001 From: Roan Kattouw Date: Mon, 28 Jan 2008 13:56:20 +0000 Subject: [PATCH] (bug 12816) Adding list=random module which fetches a list of random pages. Based on a patch by Brent G. --- RELEASE-NOTES | 1 + includes/AutoLoader.php | 1 + includes/api/ApiQuery.php | 1 + includes/api/ApiQueryRandom.php | 129 ++++++++++++++++++++++++++++++++ 4 files changed, 132 insertions(+) create mode 100644 includes/api/ApiQueryRandom.php diff --git a/RELEASE-NOTES b/RELEASE-NOTES index 4488c69a14..86ad332fca 100644 --- a/RELEASE-NOTES +++ b/RELEASE-NOTES @@ -473,6 +473,7 @@ Full API documentation is available at http://www.mediawiki.org/wiki/API * Added format=txt and format=dbg, imported from query.php * Added uiprop=editcount to meta=userinfo * Added list=users which fetches user information +* Added list=random which fetches a list of random pages === Languages updated in 1.12 === diff --git a/includes/AutoLoader.php b/includes/AutoLoader.php index e7c9f1bfd2..3d34909bce 100644 --- a/includes/AutoLoader.php +++ b/includes/AutoLoader.php @@ -351,6 +351,7 @@ function __autoload($className) { 'ApiQueryLangLinks' => 'includes/api/ApiQueryLangLinks.php', 'ApiQueryLinks' => 'includes/api/ApiQueryLinks.php', 'ApiQueryLogEvents' => 'includes/api/ApiQueryLogEvents.php', + 'ApiQueryRandom' => 'includes/api/ApiQueryRandom.php', 'ApiQueryRecentChanges'=> 'includes/api/ApiQueryRecentChanges.php', 'ApiQueryRevisions' => 'includes/api/ApiQueryRevisions.php', 'ApiQuerySearch' => 'includes/api/ApiQuerySearch.php', diff --git a/includes/api/ApiQuery.php b/includes/api/ApiQuery.php index 5f6c88bc80..57d29bf768 100644 --- a/includes/api/ApiQuery.php +++ b/includes/api/ApiQuery.php @@ -75,6 +75,7 @@ class ApiQuery extends ApiBase { 'watchlist' => 'ApiQueryWatchlist', 'exturlusage' => 'ApiQueryExtLinksUsage', 'users' => 'ApiQueryUsers', + 'random' => 'ApiQueryRandom', ); private $mQueryMetaModules = array ( diff --git a/includes/api/ApiQueryRandom.php b/includes/api/ApiQueryRandom.php new file mode 100644 index 0000000000..5741ed53a4 --- /dev/null +++ b/includes/api/ApiQueryRandom.php @@ -0,0 +1,129 @@ +run(); + } + + public function executeGenerator($resultPageSet) { + $this->run($resultPageSet); + } + + public function run($resultPageSet = null) { + $params = $this->extractRequestParams(); + $result = $this->getResult(); + $randstr = wfRandom(); + $data = array(); + + $this->addTables('page'); + $this->addOption('LIMIT', $params['limit']); + $this->addWhereFld('page_namespace', $params['namespace']); + $this->addWhereRange('page_random', 'newer', $randstr, null); + $this->addWhere(array('page_is_redirect' => 0)); + $this->addOption('USE INDEX', 'page_random'); + if(is_null($resultPageSet)) + $this->addFields(array('page_id', 'page_title', 'page_namespace')); + else + $this->addFields($resultPageSet->getPageTableFields()); + + $db = $this->getDB(); + $res = $this->select(__METHOD__); + while($row = $db->fetchObject($res)) { + if(is_null($resultPageSet)) + $data[] = $this->extractRowInfo($row); + else + $resultPageSet->processDbRow($row); + } + $db->freeResult($res); + + if(is_null($resultPageSet)) { + $result->setIndexedTagName($data, 'page'); + $result->addValue('query', $this->getModuleName(), $data); + } + } + + private function extractRowInfo($row) { + $title = Title::makeTitle($row->page_namespace, $row->page_title); + $vals = array(); + $vals['title'] = $title->getPrefixedText(); + $vals['ns'] = $row->page_namespace; + $vals['id'] = $row->page_id; + return $vals; + } + + protected function getAllowedParams() { + return array ( + 'namespace' => array( + ApiBase :: PARAM_TYPE => 'namespace', + ApiBase :: PARAM_ISMULTI => true + ), + 'limit' => array ( + ApiBase :: PARAM_TYPE => 'limit', + ApiBase :: PARAM_DFLT => 1, + ApiBase :: PARAM_MIN => 1, + ApiBase :: PARAM_MAX => 10, + ApiBase :: PARAM_MAX2 => 20 + ), + ); + } + + protected function getParamDescription() { + return array ( + 'namespace' => 'Return pages in these namespaces only', + 'limit' => 'Limit how many random pages will be returned' + ); + } + + protected function getDescription() { + return array( 'Get a set of random pages', + 'NOTE: When using a namespace filter, this module may return an empty result. In that case, retry the request' + ); + } + + protected function getExamples() { + return 'api.php?action=query&list=random&rnnamespace=0&rnlimit=2'; + } + + public function getVersion() { + return __CLASS__ . ': $Id: ApiQueryRandom.php overlordq$'; + } +} -- 2.20.1