API: Make output containing private or user-specific data uncacheable for logged...
[lhc/web/wiklou.git] / includes / api / ApiQueryAllmessages.php
1 <?php
2
3 /**
4 * Created on Dec 1, 2007
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 action to return messages from site message cache
33 *
34 * @ingroup API
35 */
36 class ApiQueryAllmessages extends ApiQueryBase {
37
38 public function __construct( $query, $moduleName ) {
39 parent::__construct( $query, $moduleName, 'am' );
40 }
41
42 public function execute() {
43 $params = $this->extractRequestParams();
44
45 global $wgLang;
46
47 $oldLang = null;
48 if ( !is_null( $params['lang'] ) && $params['lang'] != $wgLang->getCode() ) {
49 $oldLang = $wgLang; // Keep $wgLang for restore later
50 $wgLang = Language::factory( $params['lang'] );
51 } else if ( is_null( $params['lang'] ) ) {
52 // Language not determined by URL but by user preferences, so don't cache
53 $this->getMain()->setVaryCookie();
54 }
55
56 $prop = array_flip( (array)$params['prop'] );
57
58 // Determine which messages should we print
59 $messages_target = array();
60 if ( in_array( '*', $params['messages'] ) ) {
61 $message_names = array_keys( Language::getMessagesFor( 'en' ) );
62 sort( $message_names );
63 $messages_target = $message_names;
64 } else {
65 $messages_target = $params['messages'];
66 }
67
68 // Filter messages
69 if ( isset( $params['filter'] ) ) {
70 $messages_filtered = array();
71 foreach ( $messages_target as $message ) {
72 // !== is used because filter can be at the beginning of the string
73 if ( strpos( $message, $params['filter'] ) !== false ) {
74 $messages_filtered[] = $message;
75 }
76 }
77 $messages_target = $messages_filtered;
78 }
79
80 // Get all requested messages and print the result
81 $messages = array();
82 $skip = !is_null( $params['from'] );
83 $result = $this->getResult();
84 foreach ( $messages_target as $message ) {
85 // Skip all messages up to $params['from']
86 if ( $skip && $message === $params['from'] ) {
87 $skip = false;
88 }
89
90 if ( !$skip ) {
91 $a = array( 'name' => $message );
92 $args = null;
93 if ( isset( $params['args'] ) && count( $params['args'] ) != 0 ) {
94 $args = $params['args'];
95 }
96 // Check if the parser is enabled:
97 if ( $params['enableparser'] ) {
98 $msg = wfMsgExt( $message, array( 'parsemag' ), $args );
99 } elseif ( $args ) {
100 $msgString = wfMsgGetKey( $message, true, false, false );
101 $msg = wfMsgReplaceArgs( $msgString, $args );
102 } else {
103 $msg = wfMsgGetKey( $message, true, false, false );
104 }
105
106 if ( wfEmptyMsg( $message, $msg ) ) {
107 $a['missing'] = '';
108 } else {
109 ApiResult::setContent( $a, $msg );
110 if ( isset( $prop['default'] ) ) {
111 $default = wfMsgGetKey( $message, false, false, false );
112 if ( $default !== $msg ) {
113 if ( wfEmptyMsg( $message, $default ) ) {
114 $a['defaultmissing'] = '';
115 } else {
116 $a['default'] = $default;
117 }
118 }
119 }
120 }
121 $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $a );
122 if ( !$fit ) {
123 $this->setContinueEnumParameter( 'from', $name );
124 break;
125 }
126 }
127 }
128 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'message' );
129
130 if ( !is_null( $oldLang ) ) {
131 $wgLang = $oldLang; // Restore $oldLang
132 }
133 }
134
135 public function getAllowedParams() {
136 return array(
137 'messages' => array(
138 ApiBase::PARAM_DFLT => '*',
139 ApiBase::PARAM_ISMULTI => true,
140 ),
141 'prop' => array(
142 ApiBase::PARAM_ISMULTI => true,
143 ApiBase::PARAM_TYPE => array(
144 'default'
145 )
146 ),
147 'enableparser' => false,
148 'args' => array(
149 ApiBase::PARAM_ISMULTI => true
150 ),
151 'filter' => array(),
152 'lang' => null,
153 'from' => null,
154 );
155 }
156
157 public function getParamDescription() {
158 return array(
159 'messages' => 'Which messages to output. "*" means all messages',
160 'prop' => 'Which properties to get',
161 'enableparser' => array( 'Set to enable parser, will preprocess the wikitext of message',
162 'Will substitute magic words, handle templates etc.' ),
163 'args' => 'Arguments to be substituted into message',
164 'filter' => 'Return only messages that contain this string',
165 'lang' => 'Return messages in this language',
166 'from' => 'Return messages starting at this message',
167 );
168 }
169
170 public function getDescription() {
171 return 'Return messages from this site';
172 }
173
174 protected function getExamples() {
175 return array(
176 'api.php?action=query&meta=allmessages&amfilter=ipb-',
177 'api.php?action=query&meta=allmessages&ammessages=august|mainpage&amlang=de',
178 );
179 }
180
181 public function getVersion() {
182 return __CLASS__ . ': $Id$';
183 }
184 }