Fix hook situation for Skin::doEditSectionLink
[mediawiki.git] / includes / api / ApiQueryRandom.php
blob282f49865605ffde1f25238450f15727eb01df35
1 <?php
3 /**
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
25 * @file
28 /**
29 * Query module to get list of random pages
31 * @ingroup API
33 class ApiQueryRandom extends ApiQueryGeneratorBase {
34 private $pageIDs;
36 public function __construct( ApiQuery $query, $moduleName ) {
37 parent::__construct( $query, $moduleName, 'rn' );
40 public function execute() {
41 $this->run();
44 public function executeGenerator( $resultPageSet ) {
45 $this->run( $resultPageSet );
48 /**
49 * @param string $randstr
50 * @param int $limit
51 * @param int $namespace
52 * @param ApiPageSet $resultPageSet
53 * @param bool $redirect
54 * @return void
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' ) );
65 } else {
66 $this->addFields( $resultPageSet->getPageTableFields() );
70 /**
71 * @param ApiPageSet $resultPageSet
72 * @return int
74 protected function runQuery( $resultPageSet = null ) {
75 $res = $this->select( __METHOD__ );
76 $count = 0;
77 foreach ( $res as $row ) {
78 $count++;
79 if ( is_null( $resultPageSet ) ) {
80 // Prevent duplicates
81 if ( !in_array( $row->page_id, $this->pageIDs ) ) {
82 $fit = $this->getResult()->addValue(
83 array( 'query', $this->getModuleName() ),
84 null, $this->extractRowInfo( $row ) );
85 if ( !$fit ) {
86 // We can't really query-continue a random list.
87 // Return an insanely high value so
88 // $count < $limit is false
89 return 1E9;
91 $this->pageIDs[] = $row->page_id;
93 } else {
94 $resultPageSet->processDbRow( $row );
98 return $count;
102 * @param ApiPageSet $resultPageSet
103 * @return void
105 public function run( $resultPageSet = null ) {
106 $params = $this->extractRequestParams();
107 $result = $this->getResult();
108 $this->pageIDs = array();
110 $this->prepareQuery(
111 wfRandom(),
112 $params['limit'],
113 $params['namespace'],
114 $resultPageSet,
115 $params['redirect']
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()
123 $this->prepareQuery(
125 $params['limit'] - $count,
126 $params['namespace'],
127 $resultPageSet,
128 $params['redirect']
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 );
140 $vals = array();
141 $vals['id'] = intval( $row->page_id );
142 ApiQueryBase::addTitleInfo( $vals, $title );
144 return $vals;
147 public function getCacheMode( $params ) {
148 return 'public';
151 public function getAllowedParams() {
152 return array(
153 'namespace' => array(
154 ApiBase::PARAM_TYPE => 'namespace',
155 ApiBase::PARAM_ISMULTI => true
157 'limit' => array(
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
164 'redirect' => false,
168 protected function getExamplesMessages() {
169 return array(
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';