00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 if (!defined('MEDIAWIKI')) {
00027
00028 require_once ('ApiQueryBase.php');
00029 }
00030
00034 class ApiQueryExtLinksUsage extends ApiQueryGeneratorBase {
00035
00036 public function __construct($query, $moduleName) {
00037 parent :: __construct($query, $moduleName, 'eu');
00038 }
00039
00040 public function execute() {
00041 $this->run();
00042 }
00043
00044 public function executeGenerator($resultPageSet) {
00045 $this->run($resultPageSet);
00046 }
00047
00048 private function run($resultPageSet = null) {
00049
00050 $params = $this->extractRequestParams();
00051
00052 $protocol = $params['protocol'];
00053 $query = $params['query'];
00054
00055
00056 global $wgUrlProtocols;
00057 if($protocol && !in_array($protocol, $wgUrlProtocols))
00058 {
00059 foreach ($wgUrlProtocols as $p) {
00060 if( substr( $p, 0, strlen( $protocol ) ) === $protocol ) {
00061 $protocol = $p;
00062 break;
00063 }
00064 }
00065 }
00066 else
00067 $protocol = null;
00068
00069 $db = $this->getDB();
00070 $this->addTables(array('page','externallinks'));
00071 $this->addOption('USE INDEX', 'el_index');
00072 $this->addWhere('page_id=el_from');
00073 $this->addWhereFld('page_namespace', $params['namespace']);
00074
00075 if(!is_null($query) || $query != '')
00076 {
00077 if(is_null($protocol))
00078 $protocol = 'http://';
00079
00080 $likeQuery = LinkFilter::makeLike($query, $protocol);
00081 if (!$likeQuery)
00082 $this->dieUsage('Invalid query', 'bad_query');
00083 $likeQuery = substr($likeQuery, 0, strpos($likeQuery,'%')+1);
00084 $this->addWhere('el_index LIKE ' . $db->addQuotes( $likeQuery ));
00085 }
00086 else if(!is_null($protocol))
00087 $this->addWhere('el_index LIKE ' . $db->addQuotes( "$protocol%" ));
00088
00089 $prop = array_flip($params['prop']);
00090 $fld_ids = isset($prop['ids']);
00091 $fld_title = isset($prop['title']);
00092 $fld_url = isset($prop['url']);
00093
00094 if (is_null($resultPageSet)) {
00095 $this->addFields(array (
00096 'page_id',
00097 'page_namespace',
00098 'page_title'
00099 ));
00100 $this->addFieldsIf('el_to', $fld_url);
00101 } else {
00102 $this->addFields($resultPageSet->getPageTableFields());
00103 }
00104
00105 $limit = $params['limit'];
00106 $offset = $params['offset'];
00107 $this->addOption('LIMIT', $limit +1);
00108 if (isset ($offset))
00109 $this->addOption('OFFSET', $offset);
00110
00111 $res = $this->select(__METHOD__);
00112
00113 $result = $this->getResult();
00114 $count = 0;
00115 while ($row = $db->fetchObject($res)) {
00116 if (++ $count > $limit) {
00117
00118 $this->setContinueEnumParameter('offset', $offset+$limit);
00119 break;
00120 }
00121
00122 if (is_null($resultPageSet)) {
00123 $vals = array();
00124 if ($fld_ids)
00125 $vals['pageid'] = intval($row->page_id);
00126 if ($fld_title) {
00127 $title = Title :: makeTitle($row->page_namespace, $row->page_title);
00128 ApiQueryBase::addTitleInfo($vals, $title);
00129 }
00130 if ($fld_url)
00131 $vals['url'] = $row->el_to;
00132 $fit = $result->addValue(array('query', $this->getModuleName()), null, $vals);
00133 if(!$fit)
00134 {
00135 $this->setContinueEnumParameter('offset', $offset + $count - 1);
00136 break;
00137 }
00138 } else {
00139 $resultPageSet->processDbRow($row);
00140 }
00141 }
00142 $db->freeResult($res);
00143
00144 if (is_null($resultPageSet)) {
00145 $result->setIndexedTagName_internal(array('query', $this->getModuleName()),
00146 $this->getModulePrefix());
00147 }
00148 }
00149
00150 public function getAllowedParams() {
00151 global $wgUrlProtocols;
00152 $protocols = array('');
00153 foreach ($wgUrlProtocols as $p) {
00154 $protocols[] = substr($p, 0, strpos($p,':'));
00155 }
00156
00157 return array (
00158 'prop' => array (
00159 ApiBase :: PARAM_ISMULTI => true,
00160 ApiBase :: PARAM_DFLT => 'ids|title|url',
00161 ApiBase :: PARAM_TYPE => array (
00162 'ids',
00163 'title',
00164 'url'
00165 )
00166 ),
00167 'offset' => array (
00168 ApiBase :: PARAM_TYPE => 'integer'
00169 ),
00170 'protocol' => array (
00171 ApiBase :: PARAM_TYPE => $protocols,
00172 ApiBase :: PARAM_DFLT => '',
00173 ),
00174 'query' => null,
00175 'namespace' => array (
00176 ApiBase :: PARAM_ISMULTI => true,
00177 ApiBase :: PARAM_TYPE => 'namespace'
00178 ),
00179 'limit' => array (
00180 ApiBase :: PARAM_DFLT => 10,
00181 ApiBase :: PARAM_TYPE => 'limit',
00182 ApiBase :: PARAM_MIN => 1,
00183 ApiBase :: PARAM_MAX => ApiBase :: LIMIT_BIG1,
00184 ApiBase :: PARAM_MAX2 => ApiBase :: LIMIT_BIG2
00185 )
00186 );
00187 }
00188
00189 public function getParamDescription() {
00190 return array (
00191 'prop' => 'What pieces of information to include',
00192 'offset' => 'Used for paging. Use the value returned for "continue"',
00193 'protocol' => array( 'Protocol of the url. If empty and euquery set, the protocol is http.',
00194 'Leave both this and euquery empty to list all external links'),
00195 'query' => 'Search string without protocol. See [[Special:LinkSearch]]. Leave empty to list all external links',
00196 'namespace' => 'The page namespace(s) to enumerate.',
00197 'limit' => 'How many pages to return.'
00198 );
00199 }
00200
00201 public function getDescription() {
00202 return 'Enumerate pages that contain a given URL';
00203 }
00204
00205 protected function getExamples() {
00206 return array (
00207 'api.php?action=query&list=exturlusage&euquery=www.mediawiki.org'
00208 );
00209 }
00210
00211 public function getVersion() {
00212 return __CLASS__ . ': $Id: ApiQueryExtLinksUsage.php 47865 2009-02-27 16:03:01Z catrope $';
00213 }
00214 }