4 * Created on Sep 25, 2006
6 * API for MediaWiki 1.8+
8 * Copyright (C) 2006 Yuri Astrakhan <Firstname><Lastname>@gmail.com
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 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 * http://www.gnu.org/copyleft/gpl.html
26 if (!defined('MEDIAWIKI')) {
27 // Eclipse helper - will be ignored in production
28 require_once ('ApiQueryBase.php');
32 * Query module to enumerate broken redirect pages.
36 class ApiQueryBrokenRedirects
extends ApiQueryGeneratorBase
{
37 public function __construct($query, $moduleName) {
38 parent
:: __construct($query, $moduleName, 'br');
41 public function execute() {
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');
49 $this->run($resultPageSet);
52 private function run($resultPageSet = null) {
54 $params = $this->extractRequestParams();
55 list( $page, $redirect ) = $db->tableNamesN( 'page', 'redirect' );
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",
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(
71 "p2.page_namespace IS NULL",
74 $limit = $params['limit'];
75 $this->addOption('LIMIT', $limit+
1);
76 if(!is_null($params['offset']))
77 $this->addOption('OFFSET', $params['offset']);
79 $res = $this->select(__METHOD__
);
80 $result = $this->getResult();
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']);
89 if (is_null($resultPageSet)) {
90 $title = Title
:: makeTitle($row->page_namespace
, $row->title
);
91 $rdtitle = Title
:: makeTitle($row->rd_namespace
, $row->rd_title
);
93 'pageid' => intval($row->pageid
),
94 'ns' => intval($row->namespace),
95 'title' => $title->getPrefixedText(),
96 'target' => $rdtitle->getPrefixedText()
98 $fit = $result->addValue(array('query', $this->getModuleName()), null, $vals);
101 $this->setContinueEnumParameter('offset', @$params['offset'] +
$count - 1);
105 $resultPageSet->processDbRow($row);
108 $db->freeResult($res);
110 if (is_null($resultPageSet)) {
111 $result->setIndexedTagName_internal(array('query', $this->getModuleName()), 'p');
115 public function getAllowedParams() {
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
128 public function getParamDescription() {
130 'limit' => 'How many links to return',
131 'offset' => 'When more results are available, use this to continue',
135 public function getDescription() {
136 return 'Enumerate all broken redirects';
139 protected function getExamples() {
141 'api.php?action=query&list=brokenredirects',
145 public function getVersion() {
146 return __CLASS__
. ': $Id: ApiQueryBrokenRedirects.php 46845 2009-07-23 14:00:00Z alexsh $';