Follow-up to r75282: don't query if there are no pages to query
[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 $this->params = $this->extractRequestParams();
47
48 # Only operate on existing pages
49 $pages = $this->getPageSet()->getGoodTitles();
50 if ( !count( $pages ) ) {
51 # Nothing to do
52 return;
53 }
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 # Force a sort order to ensure that properties are grouped by page
64 $this->addOption( 'ORDER BY', 'pp_page' );
65
66 $res = $this->select( __METHOD__ );
67 $currentPage = 0; # Id of the page currently processed
68 $props = array();
69 $result = $this->getResult();
70
71 foreach ( $res as $row ) {
72 if ( $currentPage != $row->pp_page ) {
73 # Different page than previous row, so add the properties to
74 # the result and save the new page id
75
76 if ( $currentPage ) {
77 if ( !$this->addPageProps( $result, $currentPage, $props ) ) {
78 # addPageProps() indicated that the result did not fit
79 # so stop adding data. Reset props so that it doesn't
80 # get added again after loop exit
81
82 $props = array();
83 break;
84 }
85
86 $props = array();
87 }
88
89 $currentPage = $row->pp_page;
90 }
91
92 $props[$row->pp_propname] = $row->pp_value;
93 }
94
95 if ( count( $props ) ) {
96 # Add any remaining properties to the results
97 $this->addPageProps( $result, $currentPage, $props );
98 }
99 }
100
101 /**
102 * Add page properties to an ApiResult, adding a continue
103 * parameter if it doesn't fit.
104 *
105 * @param $result ApiResult
106 * @param $page int
107 * @param $props array
108 * @return bool True if it fits in the result
109 */
110 private function addPageProps( $result, $page, $props ) {
111 $fit = $result->addValue( array( 'query', 'pages' ), $page, $props );
112
113 if ( !$fit ) {
114 $this->setContinueEnumParameter( 'continue', $page );
115 }
116 return $fit;
117 }
118
119 public function getCacheMode( $params ) {
120 return 'public';
121 }
122
123 public function getAllowedParams() {
124 return array( 'continue' => null );
125 }
126
127 public function getParamDescription() {
128 return array( 'continue' => 'When more results are available, use this to continue' );
129 }
130
131 public function getDescription() {
132 return 'Get various properties defined in the page content';
133 }
134
135 public function getPossibleErrors() {
136 return array_merge( parent::getPossibleErrors(), array(
137 array( 'code' => '_badcontinue', 'info' => 'Invalid continue param. You should pass the original value returned by the previous query' ),
138 ) );
139 }
140
141 protected function getExamples() {
142 return array(
143 'api.php?action=query&prop=pageprops&titles=Category:Foo',
144 );
145 }
146
147 public function getVersion() {
148 return __CLASS__ . ': $Id$';
149 }
150 }