3 * API for MediaWiki 1.8+
5 * Created on Sep 25, 2006
7 * Copyright © 2010 Yuri Astrakhan <Firstname><Lastname>@gmail.com
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.
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.
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
27 if ( !defined( 'MEDIAWIKI' ) ) {
28 // Eclipse helper - will be ignored in production
29 require_once( 'ApiQueryBase.php' );
33 * A query module to show basic page information.
37 class ApiQueryPageProps
extends ApiQueryBase
{
39 public function __construct( $query, $moduleName ) {
40 parent
::__construct( $query, $moduleName, 'pp' );
43 public function execute() {
44 $this->params
= $this->extractRequestParams();
46 $pageSet = $this->getPageSet();
47 $this->titles
= $pageSet->getGoodTitles();
48 $this->missing
= $pageSet->getMissingTitles();
49 $this->everything
= $this->titles +
$this->missing
;
50 $result = $this->getResult();
52 uasort( $this->everything
, array( 'Title', 'compare' ) );
53 if ( !is_null( $this->params
['continue'] ) ) {
54 // Throw away any titles we're gonna skip so they don't
56 $cont = explode( '|', $this->params
['continue'] );
57 if ( count( $cont ) != 2 ) {
58 $this->dieUsage( 'Invalid continue param. You should pass the original ' .
59 'value returned by the previous query', '_badcontinue' );
61 $conttitle = Title
::makeTitleSafe( $cont[0], $cont[1] );
62 foreach ( $this->everything
as $pageid => $title ) {
63 if ( Title
::compare( $title, $conttitle ) >= 0 ) {
66 unset( $this->titles
[$pageid] );
67 unset( $this->missing
[$pageid] );
68 unset( $this->everything
[$pageid] );
74 foreach ( $this->everything
as $pageid => $title ) {
75 $pageInfo = $this->extractPageInfo( $pageid, $title, $this->params
['prop'] );
76 $fit = $result->addValue( array(
79 ), $pageid, $pageInfo );
81 $this->setContinueEnumParameter( 'continue',
82 $title->getNamespace() . '|' .
90 * Get a result array with information about a title
91 * @param $pageid int Page ID (negative for missing titles)
92 * @param $title Title object
95 private function extractPageInfo( $pageid, $title, $prop ) {
99 if ( $title->exists() ) {
101 $dbr = wfGetDB( DB_SLAVE
);
105 array( 'pp_propname', 'pp_value' ),
106 array( 'pp_page' => $pageid ),
110 foreach( $res as $row ) {
111 if( isset( $wgPageProps[$row->pp_propname
] ) ) {
112 if( !is_null( $prop ) && !in_array( $row->pp_propname
, $prop ) ) {
115 $pageInfo[$row->pp_propname
] = $row->pp_value
;
124 public function getCacheMode( $params ) {
128 public function getAllowedParams() {
133 ApiBase
::PARAM_DFLT
=> null,
134 ApiBase
::PARAM_ISMULTI
=> true,
135 ApiBase
::PARAM_TYPE
=> array_keys( $wgPageProps )
141 public function getParamDescription() {
146 'Which additional properties to get:',
148 'continue' => 'When more results are available, use this to continue',
151 //This mess of code first gets the length of the biggest propname, and adds two onto it to make
152 //the number of characters should be used before the dash. If the biggest propname is shorter than 12 characters,
153 //the number of characters before the dash become 14.
154 $maxLen = max( array_map( 'strlen', array_keys( $wgPageProps ) ) );
155 $matchLen = $maxLen +
2;
160 foreach( $wgPageProps as $propName => $desc ) {
161 $pretext = " $propName" . str_repeat( ' ', $matchLen - strlen( $propName ) );
163 $ret['prop'][] = "$pretext- $desc";
169 public function getDescription() {
170 return 'Get various properties about a page...';
173 public function getPossibleErrors() {
174 return array_merge( parent
::getPossibleErrors(), array(
175 array( 'code' => '_badcontinue', 'info' => 'Invalid continue param. You should pass the original value returned by the previous query' ),
179 protected function getExamples() {
181 'api.php?action=query&prop=pageprops&titles=Category:Foo',
185 public function getVersion() {
186 return __CLASS__
. ': $Id$';