[ Index ] |
PHP Cross Reference of MediaWiki-1.24.0 |
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * API module to handle links table back-queries 4 * 5 * Created on Aug 19, 2014 6 * 7 * Copyright © 2014 Brad Jorsch <[email protected]> 8 * 9 * This program is free software; you can redistribute it and/or modify 10 * it under the terms of the GNU General Public License as published by 11 * the Free Software Foundation; either version 2 of the License, or 12 * (at your option) any later version. 13 * 14 * This program is distributed in the hope that it will be useful, 15 * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 * GNU General Public License for more details. 18 * 19 * You should have received a copy of the GNU General Public License along 20 * with this program; if not, write to the Free Software Foundation, Inc., 21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 22 * http://www.gnu.org/copyleft/gpl.html 23 * 24 * @file 25 * @since 1.24 26 */ 27 28 /** 29 * This implements prop=redirects, prop=linkshere, prop=catmembers, 30 * prop=transcludedin, and prop=fileusage 31 * 32 * @ingroup API 33 * @since 1.24 34 */ 35 class ApiQueryBacklinksprop extends ApiQueryGeneratorBase { 36 37 // Data for the various modules implemented by this class 38 private static $settings = array( 39 'redirects' => array( 40 'code' => 'rd', 41 'prefix' => 'rd', 42 'linktable' => 'redirect', 43 'what' => 'redirects to', 44 'description' => 'Returns all redirects to the given pages.', 45 'props' => array( 46 'fragment' => 'Fragment of each redirect, if any', 47 ), 48 'showredirects' => false, 49 'show' => array( 50 'fragment' => 'Only show redirects with a fragment', 51 '!fragment' => 'Only show redirects without a fragment', 52 ), 53 ), 54 'linkshere' => array( 55 'code' => 'lh', 56 'prefix' => 'pl', 57 'linktable' => 'pagelinks', 58 'from_namespace' => true, 59 'what' => 'pages linking to', 60 'description' => 'Find all pages that link to the given pages.', 61 'showredirects' => true, 62 ), 63 'transcludedin' => array( 64 'code' => 'ti', 65 'prefix' => 'tl', 66 'linktable' => 'templatelinks', 67 'from_namespace' => true, 68 'what' => 'pages transcluding', 69 'description' => 'Find all pages that transclude the given pages.', 70 'showredirects' => true, 71 ), 72 'fileusage' => array( 73 'code' => 'fu', 74 'prefix' => 'il', 75 'linktable' => 'imagelinks', 76 'from_namespace' => true, 77 'to_namespace' => NS_FILE, 78 'what' => 'pages using', 79 'exampletitle' => 'File:Example.jpg', 80 'description' => 'Find all pages that use the given files.', 81 'showredirects' => true, 82 ), 83 ); 84 85 public function __construct( ApiQuery $query, $moduleName ) { 86 parent::__construct( $query, $moduleName, self::$settings[$moduleName]['code'] ); 87 } 88 89 public function execute() { 90 $this->run(); 91 } 92 93 public function executeGenerator( $resultPageSet ) { 94 $this->run( $resultPageSet ); 95 } 96 97 /** 98 * @param ApiPageSet $resultPageSet 99 */ 100 private function run( ApiPageSet $resultPageSet = null ) { 101 $settings = self::$settings[$this->getModuleName()]; 102 103 $db = $this->getDB(); 104 $params = $this->extractRequestParams(); 105 $prop = array_flip( $params['prop'] ); 106 $emptyString = $db->addQuotes( '' ); 107 108 $pageSet = $this->getPageSet(); 109 $titles = $pageSet->getGoodTitles() + $pageSet->getMissingTitles(); 110 $map = $pageSet->getAllTitlesByNamespace(); 111 112 // Determine our fields to query on 113 $p = $settings['prefix']; 114 $hasNS = !isset( $settings['to_namespace'] ); 115 if ( $hasNS ) { 116 $bl_namespace = "{$p}_namespace"; 117 $bl_title = "{$p}_title"; 118 } else { 119 $bl_namespace = $settings['to_namespace']; 120 $bl_title = "{$p}_to"; 121 122 $titles = array_filter( $titles, function ( $t ) use ( $bl_namespace ) { 123 return $t->getNamespace() === $bl_namespace; 124 } ); 125 $map = array_intersect_key( $map, array( $bl_namespace => true ) ); 126 } 127 $bl_from = "{$p}_from"; 128 129 if ( !$titles ) { 130 return; // nothing to do 131 } 132 133 // Figure out what we're sorting by, and add associated WHERE clauses. 134 // MySQL's query planner screws up if we include a field in ORDER BY 135 // when it's constant in WHERE, so we have to test that for each field. 136 $sortby = array(); 137 if ( $hasNS && count( $map ) > 1 ) { 138 $sortby[$bl_namespace] = 'ns'; 139 } 140 $theTitle = null; 141 foreach ( $map as $nsTitles ) { 142 reset( $nsTitles ); 143 $key = key( $nsTitles ); 144 if ( $theTitle === null ) { 145 $theTitle = $key; 146 } 147 if ( count( $nsTitles ) > 1 || $key !== $theTitle ) { 148 $sortby[$bl_title] = 'title'; 149 break; 150 } 151 } 152 $miser_ns = null; 153 if ( $params['namespace'] !== null ) { 154 if ( empty( $settings['from_namespace'] ) && $this->getConfig()->get( 'MiserMode' ) ) { 155 $miser_ns = $params['namespace']; 156 } else { 157 $this->addWhereFld( "{$p}_from_namespace", $params['namespace'] ); 158 if ( !empty( $settings['from_namespace'] ) && count( $params['namespace'] ) > 1 ) { 159 $sortby["{$p}_from_namespace"] = 'int'; 160 } 161 } 162 } 163 $sortby[$bl_from] = 'int'; 164 165 // Now use the $sortby to figure out the continuation 166 if ( !is_null( $params['continue'] ) ) { 167 $cont = explode( '|', $params['continue'] ); 168 $this->dieContinueUsageIf( count( $cont ) != count( $sortby ) ); 169 $where = ''; 170 $i = count( $sortby ) - 1; 171 $cont_ns = 0; 172 $cont_title = ''; 173 foreach ( array_reverse( $sortby, true ) as $field => $type ) { 174 $v = $cont[$i]; 175 switch ( $type ) { 176 case 'ns': 177 $cont_ns = (int)$v; 178 /* fall through */ 179 case 'int': 180 $v = (int)$v; 181 $this->dieContinueUsageIf( $v != $cont[$i] ); 182 break; 183 184 case 'title': 185 $cont_title = $v; 186 /* fall through */ 187 default: 188 $v = $db->addQuotes( $v ); 189 break; 190 } 191 192 if ( $where === '' ) { 193 $where = "$field >= $v"; 194 } else { 195 $where = "$field > $v OR ($field = $v AND ($where))"; 196 } 197 198 $i--; 199 } 200 $this->addWhere( $where ); 201 } 202 203 // Populate the rest of the query 204 $this->addTables( array( $settings['linktable'], 'page' ) ); 205 $this->addWhere( "$bl_from = page_id" ); 206 207 if ( $this->getModuleName() === 'redirects' ) { 208 $this->addWhere( "rd_interwiki = $emptyString OR rd_interwiki IS NULL" ); 209 } 210 211 $this->addFields( array_keys( $sortby ) ); 212 $this->addFields( array( 'bl_namespace' => $bl_namespace, 'bl_title' => $bl_title ) ); 213 if ( is_null( $resultPageSet ) ) { 214 $fld_pageid = isset( $prop['pageid'] ); 215 $fld_title = isset( $prop['title'] ); 216 $fld_redirect = isset( $prop['redirect'] ); 217 218 $this->addFieldsIf( 'page_id', $fld_pageid ); 219 $this->addFieldsIf( array( 'page_title', 'page_namespace' ), $fld_title ); 220 $this->addFieldsIf( 'page_is_redirect', $fld_redirect ); 221 222 // prop=redirects 223 $fld_fragment = isset( $prop['fragment'] ); 224 $this->addFieldsIf( 'rd_fragment', $fld_fragment ); 225 } else { 226 $this->addFields( $resultPageSet->getPageTableFields() ); 227 } 228 229 $this->addFieldsIf( 'page_namespace', $miser_ns !== null ); 230 231 if ( $hasNS ) { 232 $lb = new LinkBatch( $titles ); 233 $this->addWhere( $lb->constructSet( $p, $db ) ); 234 } else { 235 $where = array(); 236 foreach ( $titles as $t ) { 237 if ( $t->getNamespace() == $bl_namespace ) { 238 $where[] = "$bl_title = " . $db->addQuotes( $t->getDBkey() ); 239 } 240 } 241 $this->addWhere( $db->makeList( $where, LIST_OR ) ); 242 } 243 244 if ( $params['show'] !== null ) { 245 // prop=redirects only 246 $show = array_flip( $params['show'] ); 247 if ( isset( $show['fragment'] ) && isset( $show['!fragment'] ) || 248 isset( $show['redirect'] ) && isset( $show['!redirect'] ) 249 ) { 250 $this->dieUsageMsg( 'show' ); 251 } 252 $this->addWhereIf( "rd_fragment != $emptyString", isset( $show['fragment'] ) ); 253 $this->addWhereIf( 254 "rd_fragment = $emptyString OR rd_fragment IS NULL", 255 isset( $show['!fragment'] ) 256 ); 257 $this->addWhereIf( array( 'page_is_redirect' => 1 ), isset( $show['redirect'] ) ); 258 $this->addWhereIf( array( 'page_is_redirect' => 0 ), isset( $show['!redirect'] ) ); 259 } 260 261 // Override any ORDER BY from above with what we calculated earlier. 262 $this->addOption( 'ORDER BY', array_keys( $sortby ) ); 263 264 $this->addOption( 'LIMIT', $params['limit'] + 1 ); 265 266 $res = $this->select( __METHOD__ ); 267 268 if ( is_null( $resultPageSet ) ) { 269 $count = 0; 270 foreach ( $res as $row ) { 271 if ( ++$count > $params['limit'] ) { 272 // We've reached the one extra which shows that 273 // there are additional pages to be had. Stop here... 274 $this->setContinue( $row, $sortby ); 275 break; 276 } 277 278 if ( $miser_ns !== null && !in_array( $row->page_namespace, $miser_ns ) ) { 279 // Miser mode namespace check 280 continue; 281 } 282 283 // Get the ID of the current page 284 $id = $map[$row->bl_namespace][$row->bl_title]; 285 286 $vals = array(); 287 if ( $fld_pageid ) { 288 $vals['pageid'] = $row->page_id; 289 } 290 if ( $fld_title ) { 291 ApiQueryBase::addTitleInfo( $vals, 292 Title::makeTitle( $row->page_namespace, $row->page_title ) 293 ); 294 } 295 if ( $fld_fragment && $row->rd_fragment !== null && $row->rd_fragment !== '' ) { 296 $vals['fragment'] = $row->rd_fragment; 297 } 298 if ( $fld_redirect && $row->page_is_redirect ) { 299 $vals['redirect'] = ''; 300 } 301 $fit = $this->addPageSubItem( $id, $vals ); 302 if ( !$fit ) { 303 $this->setContinue( $row, $sortby ); 304 break; 305 } 306 } 307 } else { 308 $titles = array(); 309 $count = 0; 310 foreach ( $res as $row ) { 311 if ( ++$count > $params['limit'] ) { 312 // We've reached the one extra which shows that 313 // there are additional pages to be had. Stop here... 314 $this->setContinue( $row, $sortby ); 315 break; 316 } 317 $titles[] = Title::makeTitle( $row->page_namespace, $row->page_title ); 318 } 319 $resultPageSet->populateFromTitles( $titles ); 320 } 321 } 322 323 private function setContinue( $row, $sortby ) { 324 $cont = array(); 325 foreach ( $sortby as $field => $v ) { 326 $cont[] = $row->$field; 327 } 328 $this->setContinueEnumParameter( 'continue', join( '|', $cont ) ); 329 } 330 331 public function getCacheMode( $params ) { 332 return 'public'; 333 } 334 335 public function getAllowedParams() { 336 $settings = self::$settings[$this->getModuleName()]; 337 338 $ret = array( 339 'prop' => array( 340 ApiBase::PARAM_TYPE => array( 341 'pageid', 342 'title', 343 ), 344 ApiBase::PARAM_ISMULTI => true, 345 ApiBase::PARAM_DFLT => 'pageid|title', 346 ), 347 'namespace' => array( 348 ApiBase::PARAM_ISMULTI => true, 349 ApiBase::PARAM_TYPE => 'namespace', 350 ), 351 'limit' => array( 352 ApiBase::PARAM_DFLT => 10, 353 ApiBase::PARAM_TYPE => 'limit', 354 ApiBase::PARAM_MIN => 1, 355 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1, 356 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2 357 ), 358 'continue' => null, 359 ); 360 361 if ( !empty( $settings['showredirects'] ) ) { 362 $ret['prop'][ApiBase::PARAM_TYPE][] = 'redirect'; 363 $ret['prop'][ApiBase::PARAM_DFLT] .= '|redirect'; 364 } 365 if ( isset( $settings['props'] ) ) { 366 $ret['prop'][ApiBase::PARAM_TYPE] = array_merge( 367 $ret['prop'][ApiBase::PARAM_TYPE], array_keys( $settings['props'] ) 368 ); 369 } 370 371 $show = array(); 372 if ( !empty( $settings['showredirects'] ) ) { 373 $show[] = 'redirect'; 374 $show[] = '!redirect'; 375 } 376 if ( isset( $settings['show'] ) ) { 377 $show = array_merge( $show, array_keys( $settings['show'] ) ); 378 } 379 if ( $show ) { 380 $ret['show'] = array( 381 ApiBase::PARAM_TYPE => $show, 382 ApiBase::PARAM_ISMULTI => true, 383 ); 384 } 385 386 return $ret; 387 } 388 389 public function getParamDescription() { 390 $settings = self::$settings[$this->getModuleName()]; 391 $p = $this->getModulePrefix(); 392 393 $ret = array( 394 'prop' => array( 395 'Which properties to get:', 396 ), 397 'show' => array( 398 'Show only items that meet this criteria.', 399 ), 400 'namespace' => 'Only include pages in these namespaces', 401 'limit' => 'How many to return', 402 'continue' => 'When more results are available, use this to continue', 403 ); 404 405 if ( empty( $settings['from_namespace'] ) && $this->getConfig()->get( 'MiserMode' ) ) { 406 $ret['namespace'] = array( 407 $ret['namespace'], 408 "NOTE: Due to \$wgMiserMode, using this may result in fewer than \"{$p}limit\" results", 409 'returned before continuing; in extreme cases, zero results may be returned.', 410 ); 411 if ( isset( $ret['type'] ) ) { 412 $ret['namespace'][] = "Note that you can use {$p}type=subcat or {$p}type=file " . 413 "instead of {$p}namespace=14 or 6."; 414 } 415 } 416 417 $props = array( 418 'pageid' => 'Adds the ID of page', 419 'title' => 'Adds the title and namespace ID of the page', 420 ); 421 if ( !empty( $settings['showredirects'] ) ) { 422 $props['redirect'] = 'Indicate if the page is a redirect'; 423 } 424 if ( isset( $settings['props'] ) ) { 425 $props += $settings['props']; 426 } 427 foreach ( $props as $k => $v ) { 428 $ret['props'][] = sprintf( "%-9s - %s", $k, $v ); 429 } 430 431 $show = array(); 432 if ( !empty( $settings['showredirects'] ) ) { 433 $show += array( 434 'redirect' => 'Only show redirects', 435 '!redirect' => 'Only show non-redirects', 436 ); 437 } 438 if ( isset( $settings['show'] ) ) { 439 $show += $settings['show']; 440 } 441 foreach ( $show as $k => $v ) { 442 $ret['show'][] = sprintf( "%-9s - %s", $k, $v ); 443 } 444 445 return $ret; 446 } 447 448 public function getDescription() { 449 return self::$settings[$this->getModuleName()]['description']; 450 } 451 452 public function getExamples() { 453 $settings = self::$settings[$this->getModuleName()]; 454 $name = $this->getModuleName(); 455 $what = $settings['what']; 456 $title = isset( $settings['exampletitle'] ) ? $settings['exampletitle'] : 'Main Page'; 457 $etitle = rawurlencode( $title ); 458 459 return array( 460 "api.php?action=query&prop={$name}&titles={$etitle}" 461 => "Get a list of $what [[$title]]", 462 "api.php?action=query&generator={$name}&titles={$etitle}&prop=info" 463 => "Get information about $what [[$title]]", 464 ); 465 } 466 467 public function getHelpUrls() { 468 $name = $this->getModuleName(); 469 $prefix = $this->getModulePrefix(); 470 return "https://www.mediawiki.org/wiki/API:Properties#{$name}_.2F_{$prefix}"; 471 } 472 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Fri Nov 28 14:03:12 2014 | Cross-referenced by PHPXref 0.7.1 |