X-Git-Url: https://git.heureux-cyclage.org/?a=blobdiff_plain;f=includes%2Fapi%2FApiQueryPageProps.php;h=2de57106e9f2f8884f2b4ca99100463f1f59e5ef;hb=871a9ac5ff83b5298606a564ff0d1e0c1df4e987;hp=4629333beb31738654cc0fe4e1ab2adbc6a8d93f;hpb=b528bcdf89c9c3d65e08508e01b3eaa488e959c8;p=lhc%2Fweb%2Fwiklou.git diff --git a/includes/api/ApiQueryPageProps.php b/includes/api/ApiQueryPageProps.php index 4629333beb..2de57106e9 100644 --- a/includes/api/ApiQueryPageProps.php +++ b/includes/api/ApiQueryPageProps.php @@ -1,10 +1,10 @@ @gmail.com + * Created on Aug 7, 2010 + * + * Copyright © 2010 soxred93, Bryan Tong Minh * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -24,11 +24,6 @@ * @file */ -if ( !defined( 'MEDIAWIKI' ) ) { - // Eclipse helper - will be ignored in production - require_once( 'ApiQueryBase.php' ); -} - /** * A query module to show basic page information. * @@ -36,153 +31,124 @@ if ( !defined( 'MEDIAWIKI' ) ) { */ class ApiQueryPageProps extends ApiQueryBase { + private $params; + public function __construct( $query, $moduleName ) { parent::__construct( $query, $moduleName, 'pp' ); } public function execute() { + # Only operate on existing pages + $pages = $this->getPageSet()->getGoodTitles(); + if ( !count( $pages ) ) { + # Nothing to do + return; + } + $this->params = $this->extractRequestParams(); - $pageSet = $this->getPageSet(); - $this->titles = $pageSet->getGoodTitles(); - $this->missing = $pageSet->getMissingTitles(); - $this->everything = $this->titles + $this->missing; + $this->addTables( 'page_props' ); + $this->addFields( array( 'pp_page', 'pp_propname', 'pp_value' ) ); + $this->addWhereFld( 'pp_page', array_keys( $pages ) ); + + if ( $this->params['continue'] ) { + $this->addWhere( 'pp_page >=' . intval( $this->params['continue'] ) ); + } + + if ( $this->params['prop'] ) { + $this->addWhereFld( 'pp_propname', $this->params['prop'] ); + } + + # Force a sort order to ensure that properties are grouped by page + # But only if pp_page is not constant in the WHERE clause. + if ( count( $pages ) > 1 ) { + $this->addOption( 'ORDER BY', 'pp_page' ); + } + + $res = $this->select( __METHOD__ ); + $currentPage = 0; # Id of the page currently processed + $props = array(); $result = $this->getResult(); - uasort( $this->everything, array( 'Title', 'compare' ) ); - if ( !is_null( $this->params['continue'] ) ) { - // Throw away any titles we're gonna skip so they don't - // clutter queries - $cont = explode( '|', $this->params['continue'] ); - if ( count( $cont ) != 2 ) { - $this->dieUsage( 'Invalid continue param. You should pass the original ' . - 'value returned by the previous query', '_badcontinue' ); - } - $conttitle = Title::makeTitleSafe( $cont[0], $cont[1] ); - foreach ( $this->everything as $pageid => $title ) { - if ( Title::compare( $title, $conttitle ) >= 0 ) { - break; + foreach ( $res as $row ) { + if ( $currentPage != $row->pp_page ) { + # Different page than previous row, so add the properties to + # the result and save the new page id + + if ( $currentPage ) { + if ( !$this->addPageProps( $result, $currentPage, $props ) ) { + # addPageProps() indicated that the result did not fit + # so stop adding data. Reset props so that it doesn't + # get added again after loop exit + + $props = array(); + break; + } + + $props = array(); } - unset( $this->titles[$pageid] ); - unset( $this->missing[$pageid] ); - unset( $this->everything[$pageid] ); + + $currentPage = $row->pp_page; } + + $props[$row->pp_propname] = $row->pp_value; } - - - foreach ( $this->everything as $pageid => $title ) { - $pageInfo = $this->extractPageInfo( $pageid, $title, $prop ); - $fit = $result->addValue( array( - 'query', - 'pages' - ), $pageid, $pageInfo ); - if ( !$fit ) { - $this->setContinueEnumParameter( 'continue', - $title->getNamespace() . '|' . - $title->getText() ); - break; - } + if ( count( $props ) ) { + # Add any remaining properties to the results + $this->addPageProps( $result, $currentPage, $props ); } } /** - * Get a result array with information about a title - * @param $pageid int Page ID (negative for missing titles) - * @param $title Title object - * @return array + * Add page properties to an ApiResult, adding a continue + * parameter if it doesn't fit. + * + * @param $result ApiResult + * @param $page int + * @param $props array + * @return bool True if it fits in the result */ - private function extractPageInfo( $pageid, $title, $prop ) { - global $wgPageProps; - - $pageInfo = array(); - if ( $title->exists() ) { - - $dbr = wfGetDB( DB_SLAVE ); - - $res = $dbr->select( - 'page_props', - array( 'pp_propname', 'pp_value' ), - array( 'pp_page' => $pageid ), - __METHOD__ - ); - - foreach( $res as $row ) { - if( isset( $wgPageProps[$row->pp_propname] ) ) { - if( !is_null( $prop ) && !in_array( $row->pp_propname, $prop ) ) { - continue; - } - $pageInfo[$row->pp_propname] = $row->pp_value; - } - } - - } + private function addPageProps( $result, $page, $props ) { + $fit = $result->addValue( array( 'query', 'pages', $page ), 'pageprops', $props ); - return $pageInfo; + if ( !$fit ) { + $this->setContinueEnumParameter( 'continue', $page ); + } + return $fit; } - public function getCacheMode() { + public function getCacheMode( $params ) { return 'public'; } public function getAllowedParams() { - global $wgPageProps; - return array( + 'continue' => null, 'prop' => array( - ApiBase::PARAM_DFLT => null, ApiBase::PARAM_ISMULTI => true, - ApiBase::PARAM_TYPE => array_keys( $wgPageProps ) ), - 'continue' => null, ); } public function getParamDescription() { - global $wgPageProps; - - $ret = array( - 'prop' => array( - 'Which additional properties to get:', - ), + return array( 'continue' => 'When more results are available, use this to continue', + 'prop' => 'Only list these props. Useful for checking whether a certain page uses a certain page prop', ); - - //This mess of code first gets the length of the biggest propname, and adds two onto it to make - //the number of characters should be used before the dash. If the biggest propname is shorter than 12 characters, - //the number of characters before the dash become 14. - $maxLen = max( array_map( 'strlen', array_keys( $wgPageProps ) ) ); - $matchLen = $maxLen + 2; - if( $maxLen < 12 ) { - $matchLen = 14; - } - - foreach( $wgPageProps as $propName => $desc ) { - $pretext = " $propName" . str_repeat( ' ', $matchLen - strlen( $propName ) ); - - $ret['prop'][] = "$pretext- $desc"; - } - - return $ret; } public function getDescription() { - return 'Get various properties about a page...'; - } - - public function getPossibleErrors() { - return array_merge( parent::getPossibleErrors(), array( - array( 'code' => '_badcontinue', 'info' => 'Invalid continue param. You should pass the original value returned by the previous query' ), - ) ); + return 'Get various properties defined in the page content'; } - protected function getExamples() { + public function getExamples() { return array( 'api.php?action=query&prop=pageprops&titles=Category:Foo', ); } - public function getVersion() { - return __CLASS__ . ': $Id$'; + public function getHelpUrls() { + return 'https://www.mediawiki.org/wiki/API:Properties#pageprops_.2F_pp'; } }