Removed the 'eclipse helper' bit on top of every API module
[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 * @return void
55 */
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 $this->addOption( 'USE INDEX', 'page_random' );
64 if ( is_null( $resultPageSet ) ) {
65 $this->addFields( array( 'page_id', 'page_title', 'page_namespace' ) );
66 } else {
67 $this->addFields( $resultPageSet->getPageTableFields() );
68 }
69 }
70
71 /**
72 * @param $resultPageSet ApiPageSet
73 * @return int
74 */
75 protected function runQuery( $resultPageSet = null ) {
76 $res = $this->select( __METHOD__ );
77 $count = 0;
78 foreach ( $res as $row ) {
79 $count++;
80 if ( is_null( $resultPageSet ) ) {
81 // Prevent duplicates
82 if ( !in_array( $row->page_id, $this->pageIDs ) ) {
83 $fit = $this->getResult()->addValue(
84 array( 'query', $this->getModuleName() ),
85 null, $this->extractRowInfo( $row ) );
86 if ( !$fit ) {
87 // We can't really query-continue a random list.
88 // Return an insanely high value so
89 // $count < $limit is false
90 return 1E9;
91 }
92 $this->pageIDs[] = $row->page_id;
93 }
94 } else {
95 $resultPageSet->processDbRow( $row );
96 }
97 }
98
99 return $count;
100 }
101
102 /**
103 * @param $resultPageSet ApiPageSet
104 * @return void
105 */
106 public function run( $resultPageSet = null ) {
107 $params = $this->extractRequestParams();
108 $result = $this->getResult();
109 $this->pageIDs = array();
110
111 $this->prepareQuery( wfRandom(), $params['limit'], $params['namespace'], $resultPageSet, $params['redirect'] );
112 $count = $this->runQuery( $resultPageSet );
113 if ( $count < $params['limit'] ) {
114 /* We got too few pages, we probably picked a high value
115 * for page_random. We'll just take the lowest ones, see
116 * also the comment in Title::getRandomTitle()
117 */
118 $this->prepareQuery( 0, $params['limit'] - $count, $params['namespace'], $resultPageSet, $params['redirect'] );
119 $this->runQuery( $resultPageSet );
120 }
121
122 if ( is_null( $resultPageSet ) ) {
123 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'page' );
124 }
125 }
126
127 private function extractRowInfo( $row ) {
128 $title = Title::makeTitle( $row->page_namespace, $row->page_title );
129 $vals = array();
130 $vals['id'] = intval( $row->page_id );
131 ApiQueryBase::addTitleInfo( $vals, $title );
132 return $vals;
133 }
134
135 public function getCacheMode( $params ) {
136 return 'public';
137 }
138
139 public function getAllowedParams() {
140 return array(
141 'namespace' => array(
142 ApiBase::PARAM_TYPE => 'namespace',
143 ApiBase::PARAM_ISMULTI => true
144 ),
145 'limit' => array(
146 ApiBase::PARAM_TYPE => 'limit',
147 ApiBase::PARAM_DFLT => 1,
148 ApiBase::PARAM_MIN => 1,
149 ApiBase::PARAM_MAX => 10,
150 ApiBase::PARAM_MAX2 => 20
151 ),
152 'redirect' => false,
153 );
154 }
155
156 public function getParamDescription() {
157 return array(
158 'namespace' => 'Return pages in these namespaces only',
159 'limit' => 'Limit how many random pages will be returned',
160 'redirect' => 'Load a random redirect instead of a random page'
161 );
162 }
163
164 public function getDescription() {
165 return array(
166 'Get a set of random pages',
167 '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 ',
168 ' random page on your list, "List of fictional monkeys" will *always* be second, "List of people on stamps of Vanuatu" third, etc',
169 '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'
170 );
171 }
172
173 public function getExamples() {
174 return 'api.php?action=query&list=random&rnnamespace=0&rnlimit=2';
175 }
176
177 public function getVersion() {
178 return __CLASS__ . ': $Id: ApiQueryRandom.php overlordq$';
179 }
180 }