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