fix classes description.
[lhc/web/wiklou.git] / includes / api / ApiQueryBrokenRedirects.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 broken redirect pages.
33 *
34 * @ingroup API
35 */
36 class ApiQueryBrokenRedirects extends ApiQueryGeneratorBase {
37 public function __construct($query, $moduleName) {
38 parent :: __construct($query, $moduleName, 'br');
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 list( $page, $redirect ) = $db->tableNamesN( 'page', 'redirect' );
56
57 $this->addFields( array(
58 "'BrokenRedirects' AS type",
59 "p1.page_namespace AS namespace",
60 "p1.page_title AS title",
61 "p1.page_id AS pageid",
62 "rd_namespace",
63 "rd_title",
64 ));
65 $this->addTables("redirect AS rd JOIN page p1 ON (rd.rd_from=p1.page_id) LEFT JOIN page AS p2 ON (rd_namespace=p2.page_namespace AND rd_title=p2.page_title )");
66 # I don't know why these two not work ~~Alexsh
67 #$this->addJoinConds(array("$page AS p1" => array('JOIN', 'rd.rd_from=p1.page_id')));
68 #$this->addJoinConds(array("$page AS p2" => array('LEFT JOIN', 'rd_namespace=p2.page_namespace AND rd_title=p2.page_title')));
69 $this->addWhere( array(
70 "rd_namespace >= 0",
71 "p2.page_namespace IS NULL",
72 ));
73
74 $limit = $params['limit'];
75 $this->addOption('LIMIT', $limit+1);
76 if(!is_null($params['offset']))
77 $this->addOption('OFFSET', $params['offset']);
78
79 $res = $this->select(__METHOD__);
80 $result = $this->getResult();
81 $count = 0;
82 while ($row = $db->fetchObject($res)) {
83 if (++ $count > $limit) {
84 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
85 // TODO: Security issue - if the user has no right to view next title, it will still be shown
86 $this->setContinueEnumParameter('offset', @$params['offset'] + $params['limit']);
87 break;
88 }
89 if (is_null($resultPageSet)) {
90 $title = Title :: makeTitle($row->page_namespace, $row->title);
91 $rdtitle = Title :: makeTitle($row->rd_namespace, $row->rd_title);
92 $vals = array(
93 'pageid' => intval($row->pageid),
94 'ns' => intval($row->namespace),
95 'title' => $title->getPrefixedText(),
96 'target' => $rdtitle->getPrefixedText()
97 );
98 $fit = $result->addValue(array('query', $this->getModuleName()), null, $vals);
99 if(!$fit)
100 {
101 $this->setContinueEnumParameter('offset', @$params['offset'] + $count - 1);
102 break;
103 }
104 } else {
105 $resultPageSet->processDbRow($row);
106 }
107 }
108 $db->freeResult($res);
109
110 if (is_null($resultPageSet)) {
111 $result->setIndexedTagName_internal(array('query', $this->getModuleName()), 'p');
112 }
113 }
114
115 public function getAllowedParams() {
116 return array (
117 'limit' => array(
118 ApiBase :: PARAM_DFLT => 10,
119 ApiBase :: PARAM_TYPE => 'limit',
120 ApiBase :: PARAM_MIN => 1,
121 ApiBase :: PARAM_MAX => ApiBase :: LIMIT_BIG1,
122 ApiBase :: PARAM_MAX2 => ApiBase :: LIMIT_BIG2
123 ),
124 'offset' => null,
125 );
126 }
127
128 public function getParamDescription() {
129 return array(
130 'limit' => 'How many links to return',
131 'offset' => 'When more results are available, use this to continue',
132 );
133 }
134
135 public function getDescription() {
136 return 'Enumerate all broken redirects';
137 }
138
139 protected function getExamples() {
140 return array (
141 'api.php?action=query&list=brokenredirects',
142 );
143 }
144
145 public function getVersion() {
146 return __CLASS__ . ': $Id: ApiQueryBrokenRedirects.php 46845 2009-07-23 14:00:00Z alexsh $';
147 }
148
149 }