Kill "* @return void"
[lhc/web/wiklou.git] / includes / api / ApiQueryRandom.php
1 <?php
2
3 /**
4 *
5 *
6 * Created on Monday, January 28, 2008
7 *
8 * Copyright © 2008 Brent Garber
9 *
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.
14 *
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.
19 *
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
24 *
25 * @file
26 */
27
28 /**
29 * Query module to get list of random pages
30 *
31 * @ingroup API
32 */
33
34 class ApiQueryRandom extends ApiQueryGeneratorBase {
35
36 public function __construct( $query, $moduleName ) {
37 parent::__construct( $query, $moduleName, 'rn' );
38 }
39
40 public function execute() {
41 $this->run();
42 }
43
44 public function executeGenerator( $resultPageSet ) {
45 $this->run( $resultPageSet );
46 }
47
48 /**
49 * @param $randstr
50 * @param $limit
51 * @param $namespace
52 * @param $resultPageSet ApiPageSet
53 * @param $redirect
54 */
55 protected function prepareQuery( $randstr, $limit, $namespace, &$resultPageSet, $redirect ) {
56 $this->resetQueryParams();
57 $this->addTables( 'page' );
58 $this->addOption( 'LIMIT', $limit );
59 $this->addWhereFld( 'page_namespace', $namespace );
60 $this->addWhereRange( 'page_random', 'newer', $randstr, null );
61 $this->addWhereFld( 'page_is_redirect', $redirect );
62 $this->addOption( 'USE INDEX', 'page_random' );
63 if ( is_null( $resultPageSet ) ) {
64 $this->addFields( array( 'page_id', 'page_title', 'page_namespace' ) );
65 } else {
66 $this->addFields( $resultPageSet->getPageTableFields() );
67 }
68 }
69
70 /**
71 * @param $resultPageSet ApiPageSet
72 * @return int
73 */
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;
90 }
91 $this->pageIDs[] = $row->page_id;
92 }
93 } else {
94 $resultPageSet->processDbRow( $row );
95 }
96 }
97
98 return $count;
99 }
100
101 /**
102 * @param $resultPageSet ApiPageSet
103 */
104 public function run( $resultPageSet = null ) {
105 $params = $this->extractRequestParams();
106 $result = $this->getResult();
107 $this->pageIDs = array();
108
109 $this->prepareQuery( wfRandom(), $params['limit'], $params['namespace'], $resultPageSet, $params['redirect'] );
110 $count = $this->runQuery( $resultPageSet );
111 if ( $count < $params['limit'] ) {
112 /* We got too few pages, we probably picked a high value
113 * for page_random. We'll just take the lowest ones, see
114 * also the comment in Title::getRandomTitle()
115 */
116 $this->prepareQuery( 0, $params['limit'] - $count, $params['namespace'], $resultPageSet, $params['redirect'] );
117 $this->runQuery( $resultPageSet );
118 }
119
120 if ( is_null( $resultPageSet ) ) {
121 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'page' );
122 }
123 }
124
125 private function extractRowInfo( $row ) {
126 $title = Title::makeTitle( $row->page_namespace, $row->page_title );
127 $vals = array();
128 $vals['id'] = intval( $row->page_id );
129 ApiQueryBase::addTitleInfo( $vals, $title );
130 return $vals;
131 }
132
133 public function getCacheMode( $params ) {
134 return 'public';
135 }
136
137 public function getAllowedParams() {
138 return array(
139 'namespace' => array(
140 ApiBase::PARAM_TYPE => 'namespace',
141 ApiBase::PARAM_ISMULTI => true
142 ),
143 'limit' => array(
144 ApiBase::PARAM_TYPE => 'limit',
145 ApiBase::PARAM_DFLT => 1,
146 ApiBase::PARAM_MIN => 1,
147 ApiBase::PARAM_MAX => 10,
148 ApiBase::PARAM_MAX2 => 20
149 ),
150 'redirect' => false,
151 );
152 }
153
154 public function getParamDescription() {
155 return array(
156 'namespace' => 'Return pages in these namespaces only',
157 'limit' => 'Limit how many random pages will be returned',
158 'redirect' => 'Load a random redirect instead of a random page'
159 );
160 }
161
162 public function getDescription() {
163 return array(
164 'Get a set of random pages',
165 'NOTE: Pages are listed in a fixed sequence, only the starting point is random. This means that if, for example, "Main Page" is the first ',
166 ' random page on your list, "List of fictional monkeys" will *always* be second, "List of people on stamps of Vanuatu" third, etc',
167 'NOTE: If the number of pages in the namespace is lower than rnlimit, you will get fewer pages. You will not get the same page twice'
168 );
169 }
170
171 public function getExamples() {
172 return 'api.php?action=query&list=random&rnnamespace=0&rnlimit=2';
173 }
174
175 public function getVersion() {
176 return __CLASS__ . ': $Id: ApiQueryRandom.php overlordq$';
177 }
178 }