* (bug 26480) add a pppageprops param to prop=pageprops
[lhc/web/wiklou.git] / includes / api / ApiQueryPageProps.php
1 <?php
2 /**
3 *
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;
40
41 public function __construct( $query, $moduleName ) {
42 parent::__construct( $query, $moduleName, 'pp' );
43 }
44
45 public function execute() {
46 # Only operate on existing pages
47 $pages = $this->getPageSet()->getGoodTitles();
48 if ( !count( $pages ) ) {
49 # Nothing to do
50 return;
51 }
52
53 $this->params = $this->extractRequestParams();
54
55 $this->addTables( 'page_props' );
56 $this->addFields( array( 'pp_page', 'pp_propname', 'pp_value' ) );
57 $this->addWhereFld( 'pp_page', array_keys( $pages ) );
58
59 if ( $this->params['continue'] ) {
60 $this->addWhere( 'pp_page >=' . intval( $this->params['continue'] ) );
61 }
62
63 if ( $this->params['prop'] ) {
64 $this->addWhereFld( 'pp_propname', $this->params['prop'] );
65 }
66
67 # Force a sort order to ensure that properties are grouped by page
68 $this->addOption( 'ORDER BY', 'pp_page' );
69
70 $res = $this->select( __METHOD__ );
71 $currentPage = 0; # Id of the page currently processed
72 $props = array();
73 $result = $this->getResult();
74
75 foreach ( $res as $row ) {
76 if ( $currentPage != $row->pp_page ) {
77 # Different page than previous row, so add the properties to
78 # the result and save the new page id
79
80 if ( $currentPage ) {
81 if ( !$this->addPageProps( $result, $currentPage, $props ) ) {
82 # addPageProps() indicated that the result did not fit
83 # so stop adding data. Reset props so that it doesn't
84 # get added again after loop exit
85
86 $props = array();
87 break;
88 }
89
90 $props = array();
91 }
92
93 $currentPage = $row->pp_page;
94 }
95
96 $props[$row->pp_propname] = $row->pp_value;
97 }
98
99 if ( count( $props ) ) {
100 # Add any remaining properties to the results
101 $this->addPageProps( $result, $currentPage, $props );
102 }
103 }
104
105 /**
106 * Add page properties to an ApiResult, adding a continue
107 * parameter if it doesn't fit.
108 *
109 * @param $result ApiResult
110 * @param $page int
111 * @param $props array
112 * @return bool True if it fits in the result
113 */
114 private function addPageProps( $result, $page, $props ) {
115 $fit = $result->addValue( array( 'query', 'pages' ), $page, $props );
116
117 if ( !$fit ) {
118 $this->setContinueEnumParameter( 'continue', $page );
119 }
120 return $fit;
121 }
122
123 public function getCacheMode( $params ) {
124 return 'public';
125 }
126
127 public function getAllowedParams() {
128 return array(
129 'continue' => null,
130 'prop' => null,
131 );
132 }
133
134 public function getParamDescription() {
135 return array(
136 'continue' => 'When more results are available, use this to continue',
137 'prop' => 'Page prop to look on the page for. Useful for checking whether a certain page uses a certain page prop.'
138 );
139 }
140
141 public function getDescription() {
142 return 'Get various properties defined in the page content';
143 }
144
145 protected function getExamples() {
146 return array(
147 'api.php?action=query&prop=pageprops&titles=Category:Foo',
148 );
149 }
150
151 public function getVersion() {
152 return __CLASS__ . ': $Id$';
153 }
154 }