r108246 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r108245‎ | r108246 | r108247 >
Date:16:09, 6 January 2012
Author:maxsem
Status:ok
Tags:
Comment:
prop=coordinates module, notices
Modified paths:
  • /trunk/extensions/GeoData/ApiQueryCoordinates.php (added) (history)
  • /trunk/extensions/GeoData/ApiQueryGeoSearch.php (modified) (history)
  • /trunk/extensions/GeoData/GeoData.body.php (modified) (history)

Diff [purge]

Index: trunk/extensions/GeoData/ApiQueryCoordinates.php
@@ -0,0 +1,131 @@
 2+<?php
 3+/**
 4+ * This query adds an <coordinates> subelement to all pages with the list of coordinated present on those pages.
 5+ */
 6+class ApiQueryCoordinates extends ApiQueryBase {
 7+
 8+ public function __construct( $query, $moduleName ) {
 9+ parent::__construct( $query, $moduleName, 'co' );
 10+ }
 11+
 12+ public function execute() {
 13+ $titles = $this->getPageSet()->getGoodTitles();
 14+ if ( count( $titles ) == 0 ) {
 15+ return;
 16+ }
 17+
 18+ $params = $this->extractRequestParams();
 19+ $this->addTables( 'geo_tags' );
 20+ $this->addFields( array( 'gt_id', 'gt_page_id', 'gt_lat', 'gt_lon', 'gt_primary' ) );
 21+ foreach( $params['prop'] as $prop ) {
 22+ if ( isset( Coord::$fieldMapping[$prop] ) ) {
 23+ $this->addFields( Coord::$fieldMapping[$prop] );
 24+ }
 25+ }
 26+ $this->addWhereFld( 'gt_page_id', array_keys( $titles ) );
 27+ $primary = array_flip( $params['primary'] );
 28+ $this->addWhereIf( array( 'gt_primary' => 1 ), isset( $primary['yes'] ) && !isset( $primary['no'] ) );
 29+ $this->addWhereIf( array( 'gt_primary' => 0 ), !isset( $primary['yes'] ) && isset( $primary['no'] ) );
 30+
 31+ if ( isset( $params['continue'] ) ) {
 32+ $parts = explode( '|', $params['continue'] );
 33+ if ( count( $parts ) != 2 || !is_numeric( $parts[0] ) || !is_numeric( $parts[0] ) ) {
 34+ $this->dieUsage( "Invalid continue parameter. You should pass the " .
 35+ "original value returned by the previous query", "_badcontinue" );
 36+ }
 37+ $parts[0] = intval( $parts[0] );
 38+ $parts[1] = intval( $parts[1] );
 39+ $this->addWhere( "gt_page_id > {$parts[0]} OR ( gt_page_id = {$parts[0]} AND gt_id > {$parts[1]} )" );
 40+ }
 41+
 42+ $this->addOption( 'ORDER BY', array( 'gt_page_id', 'gt_id' ) );
 43+ $this->addOption( 'LIMIT', $params['limit'] + 1 );
 44+
 45+ $res = $this->select( __METHOD__ );
 46+
 47+ $count = 0;
 48+ foreach ( $res as $row ) {
 49+ if ( ++$count > $params['limit'] ) {
 50+ $this->setContinueEnumParameter( 'continue', $row->gt_page_id . '|' . $row->gt_id );
 51+ break;
 52+ }
 53+ $vals = array(
 54+ 'lat' => floatval( $row->gt_lat ),
 55+ 'lon' => floatval( $row->gt_lon ),
 56+ );
 57+ if ( $row->gt_primary ) {
 58+ $vals['primary'] = '';
 59+ }
 60+ foreach( $params['prop'] as $prop ) {
 61+ if ( isset( Coord::$fieldMapping[$prop] ) ) {
 62+ $field = Coord::$fieldMapping[$prop];
 63+ $vals[$prop] = $row->$field;
 64+ }
 65+ }
 66+ $fit = $this->addPageSubItem( $row->gt_page_id, $vals );
 67+ if ( !$fit ) {
 68+ $this->setContinueEnumParameter( 'continue', $row->gt_page_id . '|' . $row->gt_id );
 69+ }
 70+ }
 71+ }
 72+
 73+ public function getCacheMode( $params ) {
 74+ return 'public';
 75+ }
 76+
 77+ public function getAllowedParams() {
 78+ return array(
 79+ 'limit' => array(
 80+ ApiBase::PARAM_DFLT => 10,
 81+ ApiBase::PARAM_TYPE => 'limit',
 82+ ApiBase::PARAM_MIN => 1,
 83+ ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
 84+ ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
 85+ ),
 86+ 'continue' => array(
 87+ ApiBase::PARAM_TYPE => 'string',
 88+ ),
 89+ 'prop' => array(
 90+ ApiBase::PARAM_TYPE => array( 'type', 'name', 'country', 'region' ),
 91+ ApiBase::PARAM_DFLT => '',
 92+ ApiBase::PARAM_ISMULTI => true,
 93+ ),
 94+ 'primary' => array(
 95+ ApiBase::PARAM_TYPE => array( 'yes', 'no' ),
 96+ ApiBase::PARAM_ISMULTI => true,
 97+ ApiBase::PARAM_DFLT => 'yes',
 98+ ),
 99+ );
 100+ }
 101+
 102+ public function getParamDescription() {
 103+ return array(
 104+ 'limit' => 'How many coordinates to return',
 105+ 'continue' => 'When more results are available, use this to continue',
 106+ 'prop' => 'What additional coordinate properties to return',
 107+ 'primary' => "Whether to return only primary coordinates (`yes'), secondary (`no') or both (`yes|no')",
 108+ );
 109+ }
 110+
 111+ public function getDescription() {
 112+ return 'Returns coordinates of the given page(s)';
 113+ }
 114+
 115+ public function getPossibleErrors() {
 116+ return array_merge( parent::getPossibleErrors(), array(
 117+ array( 'code' => '_badcontinue', 'info' => 'Invalid continue param. You should pass the original value returned by the previous query' ),
 118+ ) );
 119+ }
 120+
 121+ public function getExamples() {
 122+ return array(
 123+ 'Get a list of coordinates of the [[Main Page]]:',
 124+ ' api.php?action=query&prop=coordinates&titles=Main%20Page',
 125+ );
 126+ }
 127+
 128+
 129+ public function getVersion() {
 130+ return __CLASS__ . ': $Id$';
 131+ }
 132+}
Property changes on: trunk/extensions/GeoData/ApiQueryCoordinates.php
___________________________________________________________________
Added: svn:eol-style
1133 + native
Added: svn:keywords
2134 + Id
Index: trunk/extensions/GeoData/ApiQueryGeoSearch.php
@@ -165,6 +165,7 @@
166166 ),
167167 'prop' => array(
168168 ApiBase::PARAM_TYPE => array( 'type', 'name', 'country', 'region' ),
 169+ ApiBase::PARAM_DFLT => '',
169170 ApiBase::PARAM_ISMULTI => true,
170171 ),
171172 'primary' => array(
Index: trunk/extensions/GeoData/GeoData.body.php
@@ -230,7 +230,7 @@
231231
232232 public static function newFromRow( $row ) {
233233 global $wgDefaultGlobe;
234 - $c = new Coord();
 234+ $c = new Coord( $row->lat, $row->lon );
235235 foreach ( self::$fieldMapping as $field => $column ) {
236236 if ( isset( $row->$column ) ) {
237237 $c->$field = $row->$column;

Status & tagging log