r23842 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r23841‎ | r23842 | r23843 >
Date:09:35, 7 July 2007
Author:yurik
Status:old
Tags:
Comment:
API: Added list=exturlusage - allows url searches within wiki
Modified paths:
  • /trunk/phase3/RELEASE-NOTES (modified) (history)
  • /trunk/phase3/includes/AutoLoader.php (modified) (history)
  • /trunk/phase3/includes/api/ApiQuery.php (modified) (history)
  • /trunk/phase3/includes/api/ApiQueryExtLinksUsage.php (added) (history)

Diff [purge]

Index: trunk/phase3/includes/api/ApiQuery.php
@@ -68,6 +68,7 @@
6969 'usercontribs' => 'ApiQueryContributions',
7070 'watchlist' => 'ApiQueryWatchlist',
7171 // 'users' => 'ApiQueryUsers',
 72+ 'exturlusage' => 'ApiQueryExtLinksUsage',
7273 );
7374
7475 private $mQueryMetaModules = array (
Index: trunk/phase3/includes/api/ApiQueryExtLinksUsage.php
@@ -0,0 +1,203 @@
 2+<?php
 3+
 4+/*
 5+ * Created on July 7, 2007
 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+ * @addtogroup API
 34+ */
 35+class ApiQueryExtLinksUsage extends ApiQueryGeneratorBase {
 36+
 37+ public function __construct($query, $moduleName) {
 38+ parent :: __construct($query, $moduleName, 'eu');
 39+ }
 40+
 41+ public function execute() {
 42+ $this->run();
 43+ }
 44+
 45+ public function executeGenerator($resultPageSet) {
 46+ $this->run($resultPageSet);
 47+ }
 48+
 49+ private function run($resultPageSet = null) {
 50+
 51+ $params = $this->extractRequestParams();
 52+
 53+ $protocol = $params['protocol'];
 54+ $query = $params['query'];
 55+ if (is_null($query))
 56+ $this->dieUsage('Missing required query parameter', 'params');
 57+
 58+ // Find the right prefix
 59+ global $wgUrlProtocols;
 60+ foreach ($wgUrlProtocols as $p) {
 61+ if( substr( $p, 0, strlen( $protocol ) ) === $protocol ) {
 62+ $protocol = $p;
 63+ break;
 64+ }
 65+ }
 66+
 67+ $likeQuery = LinkFilter::makeLike($query , $protocol);
 68+ if (!$likeQuery)
 69+ $this->dieUsage('Invalid query', 'bad_query');
 70+ $likeQuery = substr($likeQuery, 0, strpos($likeQuery,'%')+1);
 71+
 72+ $this->addTables(array('page','externallinks')); // must be in this order for 'USE INDEX'
 73+ $this->addOption('USE INDEX', 'el_index');
 74+
 75+ $db = $this->getDB();
 76+ $this->addWhere('page_id=el_from');
 77+ $this->addWhere('el_index LIKE ' . $db->addQuotes( $likeQuery ));
 78+ $this->addWhereFld('page_namespace', $params['namespace']);
 79+
 80+ $prop = array_flip($params['prop']);
 81+ $fld_ids = isset($prop['ids']);
 82+ $fld_title = isset($prop['title']);
 83+ $fld_url = isset($prop['url']);
 84+
 85+ if (is_null($resultPageSet)) {
 86+ $this->addFields(array (
 87+ 'page_id',
 88+ 'page_namespace',
 89+ 'page_title'
 90+ ));
 91+ $this->addFieldsIf('el_to', $fld_url);
 92+ } else {
 93+ $this->addFields($resultPageSet->getPageTableFields());
 94+ }
 95+
 96+ $limit = $params['limit'];
 97+ $offset = $params['offset'];
 98+ $this->addOption('LIMIT', $limit +1);
 99+ if (isset ($offset))
 100+ $this->addOption('OFFSET', $offset);
 101+
 102+ $res = $this->select(__METHOD__);
 103+
 104+ $data = array ();
 105+ $count = 0;
 106+ while ($row = $db->fetchObject($res)) {
 107+ if (++ $count > $limit) {
 108+ // We've reached the one extra which shows that there are additional pages to be had. Stop here...
 109+ $this->setContinueEnumParameter('offset', $offset+$limit+1);
 110+ break;
 111+ }
 112+
 113+ if (is_null($resultPageSet)) {
 114+ $title = Title :: makeTitle($row->page_namespace, $row->page_title);
 115+ if ($title->userCanRead()) {
 116+ $vals = array();
 117+ if ($fld_ids)
 118+ $vals['pageid'] = intval($row->page_id);
 119+ if ($fld_title) {
 120+ $vals['ns'] = intval($title->getNamespace());
 121+ $vals['title'] = $title->getPrefixedText();
 122+ }
 123+ if ($fld_url)
 124+ $vals['url'] = $row->el_to;
 125+ $data[] = $vals;
 126+ }
 127+ } else {
 128+ $resultPageSet->processDbRow($row);
 129+ }
 130+ }
 131+ $db->freeResult($res);
 132+
 133+ if (is_null($resultPageSet)) {
 134+ $result = $this->getResult();
 135+ $result->setIndexedTagName($data, 'p');
 136+ $result->addValue('query', $this->getModuleName(), $data);
 137+ }
 138+ }
 139+
 140+ protected function getAllowedParams() {
 141+ global $wgUrlProtocols;
 142+ $protocols = array();
 143+ foreach ($wgUrlProtocols as $p) {
 144+ $protocols[] = substr($p, 0, strpos($p,':'));
 145+ }
 146+
 147+ return array (
 148+ 'prop' => array (
 149+ ApiBase :: PARAM_ISMULTI => true,
 150+ ApiBase :: PARAM_DFLT => 'ids|title|url',
 151+ ApiBase :: PARAM_TYPE => array (
 152+ 'ids',
 153+ 'title',
 154+ 'url'
 155+ )
 156+ ),
 157+ 'offset' => array (
 158+ ApiBase :: PARAM_TYPE => 'integer'
 159+ ),
 160+ 'protocol' => array (
 161+ ApiBase :: PARAM_TYPE => $protocols,
 162+ ApiBase :: PARAM_DFLT => 'http',
 163+ ),
 164+ 'query' => null,
 165+ 'namespace' => array (
 166+ ApiBase :: PARAM_ISMULTI => true,
 167+ ApiBase :: PARAM_TYPE => 'namespace'
 168+ ),
 169+ 'limit' => array (
 170+ ApiBase :: PARAM_DFLT => 10,
 171+ ApiBase :: PARAM_TYPE => 'limit',
 172+ ApiBase :: PARAM_MIN => 1,
 173+ ApiBase :: PARAM_MAX => ApiBase :: LIMIT_BIG1,
 174+ ApiBase :: PARAM_MAX2 => ApiBase :: LIMIT_BIG2
 175+ )
 176+ );
 177+ }
 178+
 179+ protected function getParamDescription() {
 180+ return array (
 181+ 'prop' => 'What pieces of information to include',
 182+ 'offset' => 'Used for paging. Use the value returned for "continue"',
 183+ 'protocol' => 'Protocol of the url',
 184+ 'query' => 'Search string without protocol. See [[Special:LinkSearch]]',
 185+ 'namespace' => 'The page namespace(s) to enumerate.',
 186+ 'limit' => 'How many entries to return.'
 187+ );
 188+ }
 189+
 190+ protected function getDescription() {
 191+ return 'Enumerate pages that contain a given URL';
 192+ }
 193+
 194+ protected function getExamples() {
 195+ return array (
 196+ 'api.php?action=query&list=exturlusage&euquery=www.mediawiki.org'
 197+ );
 198+ }
 199+
 200+ public function getVersion() {
 201+ return __CLASS__ . ': $Id:$';
 202+ }
 203+}
 204+?>
Property changes on: trunk/phase3/includes/api/ApiQueryExtLinksUsage.php
___________________________________________________________________
Added: svn:eol-style
1205 + native
Added: svn:keywords
2206 + Id
Index: trunk/phase3/includes/AutoLoader.php
@@ -313,6 +313,7 @@
314314 'ApiQueryCategoryMembers' => 'includes/api/ApiQueryCategoryMembers.php',
315315 'ApiQueryContributions' => 'includes/api/ApiQueryUserContributions.php',
316316 'ApiQueryExternalLinks' => 'includes/api/ApiQueryExternalLinks.php',
 317+ 'ApiQueryExtLinksUsage' => 'includes/api/ApiQueryExtLinksUsage.php',
317318 'ApiQueryImages' => 'includes/api/ApiQueryImages.php',
318319 'ApiQueryImageInfo' => 'includes/api/ApiQueryImageInfo.php',
319320 'ApiQueryInfo' => 'includes/api/ApiQueryInfo.php',
Index: trunk/phase3/RELEASE-NOTES
@@ -307,6 +307,7 @@
308308 instead of titles. Titles for these lists is obsolete and might stop working soon.
309309 * Added prop=imageinfo - gets image properties and upload history
310310 * (bug 10211) Added db server replication lag information in meta=siteinfo
 311+* Added external url search within wiki pages (list=exturlusage)
311312
312313 == Maintenance script changes since 1.10 ==
313314

Follow-up revisions

RevisionCommit summaryAuthorDate
r23912Merged revisions 23662-23909 via svnmerge from...david18:11, 9 July 2007

Status & tagging log