Follow-up r70638:
[lhc/web/wiklou.git] / includes / api / ApiQueryPageProps.php
1 <?php
2 /**
3 * API for MediaWiki 1.8+
4 *
5 * Created on Aug 7, 2010
6 *
7 * Copyright © 2010 soxred93, Bryan Tong Minh
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 private $params, $titles, $missing, $everything;
40
41 public function __construct( $query, $moduleName ) {
42 parent::__construct( $query, $moduleName, 'pp' );
43 }
44
45 public function execute() {
46 $this->params = $this->extractRequestParams();
47
48 $pages = $this->getPageSet()->getGoodTitles();
49
50 $this->addTables( 'page_props' );
51 $this->addFields( array( 'pp_page', 'pp_propname', 'pp_value' ) );
52 $this->addWhereFld( 'pp_page', array_keys( $pages ) );
53
54 if ( $this->params['continue'] ) {
55 $this->addWhereFld( 'pp_page >=' . intval( $this->params['continue'] ) );
56 }
57
58 $this->addOption( 'ORDER BY', 'pp_page' );
59
60 $res = $this->select( __METHOD__ );
61 $currentPage = 0;
62 $props = array();
63 $result = $this->getResult();
64 foreach ( $res as $row ) {
65 if ( $currentPage != $row->pp_page ) {
66 if ( $currentPage ) {
67 if ( !$this->addPageProps( $result, $currentPage, $props ) ) {
68 break;
69 }
70
71 $props = array();
72 } else {
73 $currentPage = $row->pp_page;
74 }
75 }
76
77 $props[$row->pp_propname] = $row->pp_value;
78 }
79
80 if ( count( $props ) ) {
81 $this->addPageProps( $result, $currentPage, $props );
82 }
83 }
84
85 /**
86 * Add page properties to an ApiResult, adding a continue
87 * parameter if it doesn't fit.
88 *
89 * @param $result ApiResult
90 * @param $page int
91 * @param $props array
92 * @return bool True if it fits in the result
93 */
94 private function addPageProps( $result, $page, $props ) {
95 $fit = $result->addValue( array( 'query', 'pages' ), $page, $props );
96
97 if ( !$fit ) {
98 $this->setContinueEnumParameter( 'continue', $page );
99 }
100 return $fit;
101 }
102
103 public function getCacheMode( $params ) {
104 return 'public';
105 }
106
107 public function getAllowedParams() {
108 return array( 'continue' => null );
109 }
110
111 public function getParamDescription() {
112 return array( 'continue' => 'When more results are available, use this to continue' );
113 }
114
115 public function getDescription() {
116 return 'Get various properties defined in the page content';
117 }
118
119 public function getPossibleErrors() {
120 return array_merge( parent::getPossibleErrors(), array(
121 array( 'code' => '_badcontinue', 'info' => 'Invalid continue param. You should pass the original value returned by the previous query' ),
122 ) );
123 }
124
125 protected function getExamples() {
126 return array(
127 'api.php?action=query&prop=pageprops&titles=Category:Foo',
128 );
129 }
130
131 public function getVersion() {
132 return __CLASS__ . ': $Id$';
133 }
134 }