+Add BrokenRedirects and DoubleRedirects API script for bot development.
[lhc/web/wiklou.git] / includes / api / ApiQueryDoubleRedirects.php
1 <?php
2
3 /*
4 * Created on Sep 25, 2006
5 *
6 * API for MediaWiki 1.8+
7 *
8 * Copyright (C) 2006 Yuri Astrakhan <Firstname><Lastname>@gmail.com
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 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 * http://www.gnu.org/copyleft/gpl.html
24 */
25
26 if (!defined('MEDIAWIKI')) {
27 // Eclipse helper - will be ignored in production
28 require_once ('ApiQueryBase.php');
29 }
30
31 /**
32 * Query module to enumerate all available pages.
33 *
34 * @ingroup API
35 */
36 class ApiQueryDoubleRedirects extends ApiQueryGeneratorBase {
37 public function __construct($query, $moduleName) {
38 parent :: __construct($query, $moduleName, 'do');
39 }
40
41 public function execute() {
42 $this->run();
43 }
44
45 public function executeGenerator($resultPageSet) {
46 if ($resultPageSet->isResolvingRedirects())
47 $this->dieUsage('Use "gapfilterredir=nonredirects" option instead of "redirects" when using allpages as a generator', 'params');
48
49 $this->run($resultPageSet);
50 }
51
52 private function run($resultPageSet = null) {
53 $db = $this->getDB();
54 $params = $this->extractRequestParams();
55
56 list( $page, $redirect ) = $db->tableNamesN( 'page', 'redirect' );
57 $this->addFields( array(
58 "pa.page_namespace as namespace",
59 "pa.page_title as title",
60 "pa.page_id as pageid",
61 "pb.page_namespace as nsb",
62 "pb.page_title as tb",
63 "pb.page_id as idb",
64 "pc.page_namespace as nsc",
65 "pc.page_title as tc",
66 "pc.page_id as idc",
67 )
68 );
69 $this->addTables($redirect, 'ra');
70 $this->addTables($redirect, 'rb');
71 $this->addTables($page, 'pa');
72 $this->addTables($page, 'pb');
73 $this->addTables($page, 'pc');
74 $this->addWhere(array(
75 "ra.rd_from=pa.page_id",
76 "ra.rd_namespace=pb.page_namespace",
77 "ra.rd_title=pb.page_title",
78 "rb.rd_from=pb.page_id",
79 "rb.rd_namespace=pc.page_namespace",
80 "rb.rd_title=pc.page_title"
81 )
82 );
83 $limit = $params['limit'];
84 $this->addOption('LIMIT', $limit+1);
85 if(!is_null($params['offset']))
86 $this->addOption('OFFSET', $params['offset']);
87 $res = $this->select(__METHOD__);
88 $result = $this->getResult();
89 $count = 0;
90 while ($row = $db->fetchObject($res)) {
91 if (++ $count > $limit) {
92 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
93 // TODO: Security issue - if the user has no right to view next title, it will still be shown
94 $this->setContinueEnumParameter('offset', @$params['offset'] + $params['limit']);
95 break;
96 }
97 if (is_null($resultPageSet)) {
98 $title = Title :: makeTitle($row->page_namespace, $row->title);
99 $titleB = Title :: makeTitle($row->page_namespace, $row->tb);
100 $titleC = Title :: makeTitle($row->page_namespace, $row->tc);
101 $vals = array(
102 'pageid' => $row->pageid,
103 'ns' => intval($row->namespace),
104 'title' => $title->getPrefixedText(),
105 'idb' => intval($row->idb),
106 'nsb' => intval($row->nsb),
107 'tb' => $titleB->getPrefixedText(),
108 'idc' => intval($row->idc),
109 'nsc' => intval($row->nsc),
110 'tc' => $titleC->getPrefixedText(),
111 );
112 $fit = $result->addValue(array('query', $this->getModuleName()), null, $vals);
113 if(!$fit)
114 {
115 $this->setContinueEnumParameter('offset', @$params['offset'] + $count - 1);
116 break;
117 }
118 } else {
119 $resultPageSet->processDbRow($row);
120 }
121 }
122 $db->freeResult($res);
123
124 if (is_null($resultPageSet)) {
125 $result->setIndexedTagName_internal(array('query', $this->getModuleName()), 'p');
126 }
127 }
128
129 public function getAllowedParams() {
130 return array (
131 'limit' => array(
132 ApiBase :: PARAM_DFLT => 10,
133 ApiBase :: PARAM_TYPE => 'limit',
134 ApiBase :: PARAM_MIN => 1,
135 ApiBase :: PARAM_MAX => ApiBase :: LIMIT_BIG1,
136 ApiBase :: PARAM_MAX2 => ApiBase :: LIMIT_BIG2
137 ),
138 'offset' => null,
139 );
140 }
141
142 public function getParamDescription() {
143 return array(
144 'limit' => 'How many links to return',
145 'offset' => 'When more results are available, use this to continue',
146 );
147 }
148
149 public function getDescription() {
150 return 'Enumerate all double redirects';
151 }
152
153 protected function getExamples() {
154 return array (
155 'api.php?action=query&list=doubleredirects',
156 );
157 }
158
159 public function getVersion() {
160 return __CLASS__ . ': $Id: ApiQueryDoubleRedirects.php 46845 2009-07-24 14:00:00Z alexsh $';
161 }
162
163 }