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