-(bug 24484) Add prop=pageprops module
[mediawiki.git] / includes / api / ApiQueryPageProps.php
blob04d568703a50217bec5676c6ab6305def1c1a2cc
1 <?php
3 /**
4 * Created on Sep 25, 2006
6 * API for MediaWiki 1.8+
8 * Copyright © 2006 Yuri Astrakhan <Firstname><Lastname>@gmail.com
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23 * http://www.gnu.org/copyleft/gpl.html
26 if ( !defined( 'MEDIAWIKI' ) ) {
27 // Eclipse helper - will be ignored in production
28 require_once( 'ApiQueryBase.php' );
31 /**
32 * A query module to show basic page information.
34 * @ingroup API
36 class ApiQueryPageProps extends ApiQueryBase {
38 public function __construct( $query, $moduleName ) {
39 parent::__construct( $query, $moduleName, 'pp' );
42 public function execute() {
43 $this->params = $this->extractRequestParams();
45 $pageSet = $this->getPageSet();
46 $this->titles = $pageSet->getGoodTitles();
47 $this->missing = $pageSet->getMissingTitles();
48 $this->everything = $this->titles + $this->missing;
49 $result = $this->getResult();
51 uasort( $this->everything, array( 'Title', 'compare' ) );
52 if ( !is_null( $this->params['continue'] ) ) {
53 // Throw away any titles we're gonna skip so they don't
54 // clutter queries
55 $cont = explode( '|', $this->params['continue'] );
56 if ( count( $cont ) != 2 ) {
57 $this->dieUsage( 'Invalid continue param. You should pass the original ' .
58 'value returned by the previous query', '_badcontinue' );
60 $conttitle = Title::makeTitleSafe( $cont[0], $cont[1] );
61 foreach ( $this->everything as $pageid => $title ) {
62 if ( Title::compare( $title, $conttitle ) >= 0 ) {
63 break;
65 unset( $this->titles[$pageid] );
66 unset( $this->missing[$pageid] );
67 unset( $this->everything[$pageid] );
73 foreach ( $this->everything as $pageid => $title ) {
74 $pageInfo = $this->extractPageInfo( $pageid, $title );
75 $fit = $result->addValue( array(
76 'query',
77 'pages'
78 ), $pageid, $pageInfo );
79 if ( !$fit ) {
80 $this->setContinueEnumParameter( 'continue',
81 $title->getNamespace() . '|' .
82 $title->getText() );
83 break;
88 /**
89 * Get a result array with information about a title
90 * @param $pageid int Page ID (negative for missing titles)
91 * @param $title Title object
92 * @return array
94 private function extractPageInfo( $pageid, $title ) {
95 global $wgPageProps;
97 $pageInfo = array();
98 if ( $title->exists() ) {
100 $dbr = wfGetDB( DB_SLAVE );
102 $res = $dbr->select(
103 'page_props',
104 array( 'pp_propname', 'pp_value' ),
105 array( 'pp_page' => $pageid ),
106 __METHOD__
109 foreach( $res as $row ) {
110 if( isset( $wgPageProps[$row->pp_propname] ) ) {
111 if( !is_null( $prop ) && !in_array( $row->pp_propname, $prop ) ) {
112 continue;
114 $pageInfo[$row->pp_propname] = $row->pp_value;
120 return $pageInfo;
123 public function getCacheMode( $params ) {
124 return 'public';
127 public function getAllowedParams() {
128 global $wgPageProps;
130 return array(
131 'prop' => array(
132 ApiBase::PARAM_DFLT => null,
133 ApiBase::PARAM_ISMULTI => true,
134 ApiBase::PARAM_TYPE => array_keys( $wgPageProps )
136 'continue' => null,
140 public function getParamDescription() {
141 global $wgPageProps;
143 $ret = array(
144 'prop' => array(
145 'Which additional properties to get:',
147 'continue' => 'When more results are available, use this to continue',
150 $maxLen = max( array_map( 'strlen', array_keys( $wgPageProps ) ) );
151 $matchLen = $maxLen + 2;
152 if( $maxLen < 12 ) {
153 $matchLen = 14;
156 foreach( $wgPageProps as $propName => $desc ) {
157 $pretext = " $propName" . str_repeat( ' ', $matchLen - strlen( $propName ) );
159 $ret['prop'][] = "$pretext- $desc";
162 return $ret;
165 public function getDescription() {
166 return 'Get various properties about a page...';
169 public function getPossibleErrors() {
170 return array_merge( parent::getPossibleErrors(), array(
171 array( 'code' => '_badcontinue', 'info' => 'Invalid continue param. You should pass the original value returned by the previous query' ),
172 ) );
175 protected function getExamples() {
176 return array(
177 'api.php?action=query&prop=pageprops&titles=Category:Foo',
181 public function getVersion() {
182 return __CLASS__ . ': $Id$';