6 * Created on Monday, January 28, 2008
8 * Copyright © 2008 Brent Garber
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23 * http://www.gnu.org/copyleft/gpl.html
29 * Query module to get list of random pages
33 class ApiQueryRandom
extends ApiQueryGeneratorBase
{
36 public function __construct( ApiQuery
$query, $moduleName ) {
37 parent
::__construct( $query, $moduleName, 'rn' );
40 public function execute() {
44 public function executeGenerator( $resultPageSet ) {
45 $this->run( $resultPageSet );
49 * @param string $randstr
51 * @param int $namespace
52 * @param ApiPageSet $resultPageSet
53 * @param bool $redirect
56 protected function prepareQuery( $randstr, $limit, $namespace, &$resultPageSet, $redirect ) {
57 $this->resetQueryParams();
58 $this->addTables( 'page' );
59 $this->addOption( 'LIMIT', $limit );
60 $this->addWhereFld( 'page_namespace', $namespace );
61 $this->addWhereRange( 'page_random', 'newer', $randstr, null );
62 $this->addWhereFld( 'page_is_redirect', $redirect );
63 if ( is_null( $resultPageSet ) ) {
64 $this->addFields( array( 'page_id', 'page_title', 'page_namespace' ) );
66 $this->addFields( $resultPageSet->getPageTableFields() );
71 * @param ApiPageSet $resultPageSet
74 protected function runQuery( $resultPageSet = null ) {
75 $res = $this->select( __METHOD__
);
77 foreach ( $res as $row ) {
79 if ( is_null( $resultPageSet ) ) {
81 if ( !in_array( $row->page_id
, $this->pageIDs
) ) {
82 $fit = $this->getResult()->addValue(
83 array( 'query', $this->getModuleName() ),
84 null, $this->extractRowInfo( $row ) );
86 // We can't really query-continue a random list.
87 // Return an insanely high value so
88 // $count < $limit is false
91 $this->pageIDs
[] = $row->page_id
;
94 $resultPageSet->processDbRow( $row );
102 * @param ApiPageSet $resultPageSet
105 public function run( $resultPageSet = null ) {
106 $params = $this->extractRequestParams();
107 $result = $this->getResult();
108 $this->pageIDs
= array();
113 $params['namespace'],
117 $count = $this->runQuery( $resultPageSet );
118 if ( $count < $params['limit'] ) {
119 /* We got too few pages, we probably picked a high value
120 * for page_random. We'll just take the lowest ones, see
121 * also the comment in Title::getRandomTitle()
125 $params['limit'] - $count,
126 $params['namespace'],
130 $this->runQuery( $resultPageSet );
133 if ( is_null( $resultPageSet ) ) {
134 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'page' );
138 private function extractRowInfo( $row ) {
139 $title = Title
::makeTitle( $row->page_namespace
, $row->page_title
);
141 $vals['id'] = intval( $row->page_id
);
142 ApiQueryBase
::addTitleInfo( $vals, $title );
147 public function getCacheMode( $params ) {
151 public function getAllowedParams() {
153 'namespace' => array(
154 ApiBase
::PARAM_TYPE
=> 'namespace',
155 ApiBase
::PARAM_ISMULTI
=> true
158 ApiBase
::PARAM_TYPE
=> 'limit',
159 ApiBase
::PARAM_DFLT
=> 1,
160 ApiBase
::PARAM_MIN
=> 1,
161 ApiBase
::PARAM_MAX
=> 10,
162 ApiBase
::PARAM_MAX2
=> 20
168 protected function getExamplesMessages() {
170 'action=query&list=random&rnnamespace=0&rnlimit=2'
171 => 'apihelp-query+random-example-simple',
172 'action=query&generator=random&grnnamespace=0&grnlimit=2&prop=info'
173 => 'apihelp-query+random-example-generator',
177 public function getHelpUrls() {
178 return 'https://www.mediawiki.org/wiki/API:Random';