r53678 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r53677‎ | r53678 | r53679 >
Date:16:25, 23 July 2009
Author:alexsh
Status:reverted (Comments)
Tags:
Comment:
+Add BrokenRedirects and DoubleRedirects API script for bot development.
Modified paths:
  • /trunk/phase3/includes/AutoLoader.php (modified) (history)
  • /trunk/phase3/includes/api/ApiQuery.php (modified) (history)
  • /trunk/phase3/includes/api/ApiQueryBrokenRedirects.php (added) (history)
  • /trunk/phase3/includes/api/ApiQueryDoubleRedirects.php (added) (history)

Diff [purge]

Index: trunk/phase3/includes/AutoLoader.php
@@ -295,12 +295,14 @@
296296 'ApiQueryBacklinks' => 'includes/api/ApiQueryBacklinks.php',
297297 'ApiQueryBase' => 'includes/api/ApiQueryBase.php',
298298 'ApiQueryBlocks' => 'includes/api/ApiQueryBlocks.php',
 299+ 'ApiQueryBrokenRedirects' => 'includes/api/ApiQueryBrokenRedirects.php',
299300 'ApiQueryCategories' => 'includes/api/ApiQueryCategories.php',
300301 'ApiQueryCategoryInfo' => 'includes/api/ApiQueryCategoryInfo.php',
301302 'ApiQueryCategoryMembers' => 'includes/api/ApiQueryCategoryMembers.php',
302303 'ApiQueryContributions' => 'includes/api/ApiQueryUserContributions.php',
303304 'ApiQueryDeletedrevs' => 'includes/api/ApiQueryDeletedrevs.php',
304305 'ApiQueryDisabled' => 'includes/api/ApiQueryDisabled.php',
 306+ 'ApiQueryDoubleRedirects' => 'includes/api/ApiQueryDoubleRedirects.php',
305307 'ApiQueryDuplicateFiles' => 'includes/api/ApiQueryDuplicateFiles.php',
306308 'ApiQueryExtLinksUsage' => 'includes/api/ApiQueryExtLinksUsage.php',
307309 'ApiQueryExternalLinks' => 'includes/api/ApiQueryExternalLinks.php',
Index: trunk/phase3/includes/api/ApiQueryDoubleRedirects.php
@@ -0,0 +1,163 @@
 2+<?php
 3+
 4+/*
 5+ * Created on Sep 25, 2006
 6+ *
 7+ * API for MediaWiki 1.8+
 8+ *
 9+ * Copyright (C) 2006 Yuri Astrakhan <Firstname><Lastname>@gmail.com
 10+ *
 11+ * This program is free software; you can redistribute it and/or modify
 12+ * it under the terms of the GNU General Public License as published by
 13+ * the Free Software Foundation; either version 2 of the License, or
 14+ * (at your option) any later version.
 15+ *
 16+ * This program is distributed in the hope that it will be useful,
 17+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
 18+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 19+ * GNU General Public License for more details.
 20+ *
 21+ * You should have received a copy of the GNU General Public License along
 22+ * with this program; if not, write to the Free Software Foundation, Inc.,
 23+ * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 24+ * http://www.gnu.org/copyleft/gpl.html
 25+ */
 26+
 27+if (!defined('MEDIAWIKI')) {
 28+ // Eclipse helper - will be ignored in production
 29+ require_once ('ApiQueryBase.php');
 30+}
 31+
 32+/**
 33+ * Query module to enumerate all available pages.
 34+ *
 35+ * @ingroup API
 36+ */
 37+class ApiQueryDoubleRedirects extends ApiQueryGeneratorBase {
 38+ public function __construct($query, $moduleName) {
 39+ parent :: __construct($query, $moduleName, 'do');
 40+ }
 41+
 42+ public function execute() {
 43+ $this->run();
 44+ }
 45+
 46+ public function executeGenerator($resultPageSet) {
 47+ if ($resultPageSet->isResolvingRedirects())
 48+ $this->dieUsage('Use "gapfilterredir=nonredirects" option instead of "redirects" when using allpages as a generator', 'params');
 49+
 50+ $this->run($resultPageSet);
 51+ }
 52+
 53+ private function run($resultPageSet = null) {
 54+ $db = $this->getDB();
 55+ $params = $this->extractRequestParams();
 56+
 57+ list( $page, $redirect ) = $db->tableNamesN( 'page', 'redirect' );
 58+ $this->addFields( array(
 59+ "pa.page_namespace as namespace",
 60+ "pa.page_title as title",
 61+ "pa.page_id as pageid",
 62+ "pb.page_namespace as nsb",
 63+ "pb.page_title as tb",
 64+ "pb.page_id as idb",
 65+ "pc.page_namespace as nsc",
 66+ "pc.page_title as tc",
 67+ "pc.page_id as idc",
 68+ )
 69+ );
 70+ $this->addTables($redirect, 'ra');
 71+ $this->addTables($redirect, 'rb');
 72+ $this->addTables($page, 'pa');
 73+ $this->addTables($page, 'pb');
 74+ $this->addTables($page, 'pc');
 75+ $this->addWhere(array(
 76+ "ra.rd_from=pa.page_id",
 77+ "ra.rd_namespace=pb.page_namespace",
 78+ "ra.rd_title=pb.page_title",
 79+ "rb.rd_from=pb.page_id",
 80+ "rb.rd_namespace=pc.page_namespace",
 81+ "rb.rd_title=pc.page_title"
 82+ )
 83+ );
 84+ $limit = $params['limit'];
 85+ $this->addOption('LIMIT', $limit+1);
 86+ if(!is_null($params['offset']))
 87+ $this->addOption('OFFSET', $params['offset']);
 88+ $res = $this->select(__METHOD__);
 89+ $result = $this->getResult();
 90+ $count = 0;
 91+ while ($row = $db->fetchObject($res)) {
 92+ if (++ $count > $limit) {
 93+ // We've reached the one extra which shows that there are additional pages to be had. Stop here...
 94+ // TODO: Security issue - if the user has no right to view next title, it will still be shown
 95+ $this->setContinueEnumParameter('offset', @$params['offset'] + $params['limit']);
 96+ break;
 97+ }
 98+ if (is_null($resultPageSet)) {
 99+ $title = Title :: makeTitle($row->page_namespace, $row->title);
 100+ $titleB = Title :: makeTitle($row->page_namespace, $row->tb);
 101+ $titleC = Title :: makeTitle($row->page_namespace, $row->tc);
 102+ $vals = array(
 103+ 'pageid' => $row->pageid,
 104+ 'ns' => intval($row->namespace),
 105+ 'title' => $title->getPrefixedText(),
 106+ 'idb' => intval($row->idb),
 107+ 'nsb' => intval($row->nsb),
 108+ 'tb' => $titleB->getPrefixedText(),
 109+ 'idc' => intval($row->idc),
 110+ 'nsc' => intval($row->nsc),
 111+ 'tc' => $titleC->getPrefixedText(),
 112+ );
 113+ $fit = $result->addValue(array('query', $this->getModuleName()), null, $vals);
 114+ if(!$fit)
 115+ {
 116+ $this->setContinueEnumParameter('offset', @$params['offset'] + $count - 1);
 117+ break;
 118+ }
 119+ } else {
 120+ $resultPageSet->processDbRow($row);
 121+ }
 122+ }
 123+ $db->freeResult($res);
 124+
 125+ if (is_null($resultPageSet)) {
 126+ $result->setIndexedTagName_internal(array('query', $this->getModuleName()), 'p');
 127+ }
 128+ }
 129+
 130+ public function getAllowedParams() {
 131+ return array (
 132+ 'limit' => array(
 133+ ApiBase :: PARAM_DFLT => 10,
 134+ ApiBase :: PARAM_TYPE => 'limit',
 135+ ApiBase :: PARAM_MIN => 1,
 136+ ApiBase :: PARAM_MAX => ApiBase :: LIMIT_BIG1,
 137+ ApiBase :: PARAM_MAX2 => ApiBase :: LIMIT_BIG2
 138+ ),
 139+ 'offset' => null,
 140+ );
 141+ }
 142+
 143+ public function getParamDescription() {
 144+ return array(
 145+ 'limit' => 'How many links to return',
 146+ 'offset' => 'When more results are available, use this to continue',
 147+ );
 148+ }
 149+
 150+ public function getDescription() {
 151+ return 'Enumerate all double redirects';
 152+ }
 153+
 154+ protected function getExamples() {
 155+ return array (
 156+ 'api.php?action=query&list=doubleredirects',
 157+ );
 158+ }
 159+
 160+ public function getVersion() {
 161+ return __CLASS__ . ': $Id: ApiQueryDoubleRedirects.php 46845 2009-07-24 14:00:00Z alexsh $';
 162+ }
 163+
 164+}
\ No newline at end of file
Index: trunk/phase3/includes/api/ApiQuery.php
@@ -67,7 +67,9 @@
6868 'allusers' => 'ApiQueryAllUsers',
6969 'backlinks' => 'ApiQueryBacklinks',
7070 'blocks' => 'ApiQueryBlocks',
 71+ 'brokenredirects' => 'ApiQueryBrokenRedirects',
7172 'categorymembers' => 'ApiQueryCategoryMembers',
 73+ 'doubleredirects' => 'ApiQueryDoubleRedirects',
7274 'deletedrevs' => 'ApiQueryDeletedrevs',
7375 'embeddedin' => 'ApiQueryBacklinks',
7476 'imageusage' => 'ApiQueryBacklinks',
Index: trunk/phase3/includes/api/ApiQueryBrokenRedirects.php
@@ -0,0 +1,149 @@
 2+<?php
 3+
 4+/*
 5+ * Created on Sep 25, 2006
 6+ *
 7+ * API for MediaWiki 1.8+
 8+ *
 9+ * Copyright (C) 2006 Yuri Astrakhan <Firstname><Lastname>@gmail.com
 10+ *
 11+ * This program is free software; you can redistribute it and/or modify
 12+ * it under the terms of the GNU General Public License as published by
 13+ * the Free Software Foundation; either version 2 of the License, or
 14+ * (at your option) any later version.
 15+ *
 16+ * This program is distributed in the hope that it will be useful,
 17+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
 18+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 19+ * GNU General Public License for more details.
 20+ *
 21+ * You should have received a copy of the GNU General Public License along
 22+ * with this program; if not, write to the Free Software Foundation, Inc.,
 23+ * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 24+ * http://www.gnu.org/copyleft/gpl.html
 25+ */
 26+
 27+if (!defined('MEDIAWIKI')) {
 28+ // Eclipse helper - will be ignored in production
 29+ require_once ('ApiQueryBase.php');
 30+}
 31+
 32+/**
 33+ * Query module to enumerate all available pages.
 34+ *
 35+ * @ingroup API
 36+ */
 37+class ApiQueryBrokenRedirects extends ApiQueryGeneratorBase {
 38+ public function __construct($query, $moduleName) {
 39+ parent :: __construct($query, $moduleName, 'br');
 40+ }
 41+
 42+ public function execute() {
 43+ $this->run();
 44+ }
 45+
 46+ public function executeGenerator($resultPageSet) {
 47+ if ($resultPageSet->isResolvingRedirects())
 48+ $this->dieUsage('Use "gapfilterredir=nonredirects" option instead of "redirects" when using allpages as a generator', 'params');
 49+
 50+ $this->run($resultPageSet);
 51+ }
 52+
 53+ private function run($resultPageSet = null) {
 54+ $db = $this->getDB();
 55+ $params = $this->extractRequestParams();
 56+ list( $page, $redirect ) = $db->tableNamesN( 'page', 'redirect' );
 57+
 58+ $this->addFields( array(
 59+ "'BrokenRedirects' AS type",
 60+ "p1.page_namespace AS namespace",
 61+ "p1.page_title AS title",
 62+ "p1.page_id AS pageid",
 63+ "rd_namespace",
 64+ "rd_title",
 65+ ));
 66+ $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 )");
 67+ # I don't know why these two not work ~~Alexsh
 68+ #$this->addJoinConds(array("$page AS p1" => array('JOIN', 'rd.rd_from=p1.page_id')));
 69+ #$this->addJoinConds(array("$page AS p2" => array('LEFT JOIN', 'rd_namespace=p2.page_namespace AND rd_title=p2.page_title')));
 70+ $this->addWhere( array(
 71+ "rd_namespace >= 0",
 72+ "p2.page_namespace IS NULL",
 73+ ));
 74+
 75+ $limit = $params['limit'];
 76+ $this->addOption('LIMIT', $limit+1);
 77+ if(!is_null($params['offset']))
 78+ $this->addOption('OFFSET', $params['offset']);
 79+
 80+ $res = $this->select(__METHOD__);
 81+ $result = $this->getResult();
 82+ $count = 0;
 83+ while ($row = $db->fetchObject($res)) {
 84+ if (++ $count > $limit) {
 85+ // We've reached the one extra which shows that there are additional pages to be had. Stop here...
 86+ // TODO: Security issue - if the user has no right to view next title, it will still be shown
 87+ $this->setContinueEnumParameter('offset', @$params['offset'] + $params['limit']);
 88+ break;
 89+ }
 90+ if (is_null($resultPageSet)) {
 91+ $title = Title :: makeTitle($row->page_namespace, $row->title);
 92+ $rdtitle = Title :: makeTitle($row->page_namespace, $row->rd_title);
 93+ $vals = array(
 94+ 'pageid' => intval($row->pageid),
 95+ 'ns' => intval($row->namespace),
 96+ 'title' => $title->getPrefixedText(),
 97+ 'target' => $rdtitle->getPrefixedText()
 98+ );
 99+ $fit = $result->addValue(array('query', $this->getModuleName()), null, $vals);
 100+ if(!$fit)
 101+ {
 102+ $this->setContinueEnumParameter('offset', @$params['offset'] + $count - 1);
 103+ break;
 104+ }
 105+ } else {
 106+ $resultPageSet->processDbRow($row);
 107+ }
 108+ }
 109+ $db->freeResult($res);
 110+
 111+ if (is_null($resultPageSet)) {
 112+ $result->setIndexedTagName_internal(array('query', $this->getModuleName()), 'p');
 113+ }
 114+ }
 115+
 116+ public function getAllowedParams() {
 117+ return array (
 118+ 'limit' => array(
 119+ ApiBase :: PARAM_DFLT => 10,
 120+ ApiBase :: PARAM_TYPE => 'limit',
 121+ ApiBase :: PARAM_MIN => 1,
 122+ ApiBase :: PARAM_MAX => ApiBase :: LIMIT_BIG1,
 123+ ApiBase :: PARAM_MAX2 => ApiBase :: LIMIT_BIG2
 124+ ),
 125+ 'offset' => null,
 126+ );
 127+ }
 128+
 129+ public function getParamDescription() {
 130+ return array(
 131+ 'limit' => 'How many links to return',
 132+ 'offset' => 'When more results are available, use this to continue',
 133+ );
 134+ }
 135+
 136+ public function getDescription() {
 137+ return 'Enumerate all broken redirects';
 138+ }
 139+
 140+ protected function getExamples() {
 141+ return array (
 142+ 'api.php?action=query&list=brokenredirects',
 143+ );
 144+ }
 145+
 146+ public function getVersion() {
 147+ return __CLASS__ . ': $Id: ApiQueryBrokenRedirects.php 46845 2009-07-23 14:00:00Z alexsh $';
 148+ }
 149+
 150+}
\ No newline at end of file

Follow-up revisions

RevisionCommit summaryAuthorDate
r54265Revert r53678, r53704, r53705, r53773, r53930, r54199, r54201: Addition of Ap...btongminh11:06, 3 August 2009

Comments

#Comment by OverlordQ (talk | contribs)   10:23, 31 July 2009

Aren't these very expensive queries?

Making them available via API in a non-cached form is just asking to kill your database.

#Comment by Bryan (talk | contribs)   11:07, 3 August 2009

Reverted in r54265.

Status & tagging log