Standardised file description headers, added @file
[lhc/web/wiklou.git] / includes / api / ApiQueryPageProps.php
1 <?php
2 /**
3 * API for MediaWiki 1.8+
4 *
5 * Created on Sep 25, 2006
6 *
7 * Copyright © 2010 Yuri Astrakhan <Firstname><Lastname>@gmail.com
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 */
26
27 if ( !defined( 'MEDIAWIKI' ) ) {
28 // Eclipse helper - will be ignored in production
29 require_once( 'ApiQueryBase.php' );
30 }
31
32 /**
33 * A query module to show basic page information.
34 *
35 * @ingroup API
36 */
37 class ApiQueryPageProps extends ApiQueryBase {
38
39 public function __construct( $query, $moduleName ) {
40 parent::__construct( $query, $moduleName, 'pp' );
41 }
42
43 public function execute() {
44 $this->params = $this->extractRequestParams();
45
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();
51
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
55 // clutter queries
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' );
60 }
61 $conttitle = Title::makeTitleSafe( $cont[0], $cont[1] );
62 foreach ( $this->everything as $pageid => $title ) {
63 if ( Title::compare( $title, $conttitle ) >= 0 ) {
64 break;
65 }
66 unset( $this->titles[$pageid] );
67 unset( $this->missing[$pageid] );
68 unset( $this->everything[$pageid] );
69 }
70 }
71
72
73
74 foreach ( $this->everything as $pageid => $title ) {
75 $pageInfo = $this->extractPageInfo( $pageid, $title );
76 $fit = $result->addValue( array(
77 'query',
78 'pages'
79 ), $pageid, $pageInfo );
80 if ( !$fit ) {
81 $this->setContinueEnumParameter( 'continue',
82 $title->getNamespace() . '|' .
83 $title->getText() );
84 break;
85 }
86 }
87 }
88
89 /**
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
93 * @return array
94 */
95 private function extractPageInfo( $pageid, $title ) {
96 global $wgPageProps;
97
98 $pageInfo = array();
99 if ( $title->exists() ) {
100
101 $dbr = wfGetDB( DB_SLAVE );
102
103 $res = $dbr->select(
104 'page_props',
105 array( 'pp_propname', 'pp_value' ),
106 array( 'pp_page' => $pageid ),
107 __METHOD__
108 );
109
110 foreach( $res as $row ) {
111 if( isset( $wgPageProps[$row->pp_propname] ) ) {
112 if( !is_null( $prop ) && !in_array( $row->pp_propname, $prop ) ) {
113 continue;
114 }
115 $pageInfo[$row->pp_propname] = $row->pp_value;
116 }
117 }
118
119 }
120
121 return $pageInfo;
122 }
123
124 public function getCacheMode( $params ) {
125 return 'public';
126 }
127
128 public function getAllowedParams() {
129 global $wgPageProps;
130
131 return array(
132 'prop' => array(
133 ApiBase::PARAM_DFLT => null,
134 ApiBase::PARAM_ISMULTI => true,
135 ApiBase::PARAM_TYPE => array_keys( $wgPageProps )
136 ),
137 'continue' => null,
138 );
139 }
140
141 public function getParamDescription() {
142 global $wgPageProps;
143
144 $ret = array(
145 'prop' => array(
146 'Which additional properties to get:',
147 ),
148 'continue' => 'When more results are available, use this to continue',
149 );
150
151 $maxLen = max( array_map( 'strlen', array_keys( $wgPageProps ) ) );
152 $matchLen = $maxLen + 2;
153 if( $maxLen < 12 ) {
154 $matchLen = 14;
155 }
156
157 foreach( $wgPageProps as $propName => $desc ) {
158 $pretext = " $propName" . str_repeat( ' ', $matchLen - strlen( $propName ) );
159
160 $ret['prop'][] = "$pretext- $desc";
161 }
162
163 return $ret;
164 }
165
166 public function getDescription() {
167 return 'Get various properties about a page...';
168 }
169
170 public function getPossibleErrors() {
171 return array_merge( parent::getPossibleErrors(), array(
172 array( 'code' => '_badcontinue', 'info' => 'Invalid continue param. You should pass the original value returned by the previous query' ),
173 ) );
174 }
175
176 protected function getExamples() {
177 return array(
178 'api.php?action=query&prop=pageprops&titles=Category:Foo',
179 );
180 }
181
182 public function getVersion() {
183 return __CLASS__ . ': $Id$';
184 }
185 }