Commit stuff from my w/c
[lhc/web/wiklou.git] / includes / api / ApiQueryAllmessages.php
1 <?php
2 /**
3 *
4 *
5 * Created on Dec 1, 2007
6 *
7 * Copyright © 2006 Yuri Astrakhan <Firstname><Lastname>@gmail.com
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 action to return messages from site message cache
34 *
35 * @ingroup API
36 */
37 class ApiQueryAllmessages extends ApiQueryBase {
38
39 public function __construct( $query, $moduleName ) {
40 parent::__construct( $query, $moduleName, 'am' );
41 }
42
43 public function execute() {
44 $params = $this->extractRequestParams();
45
46 if ( is_null( $params['lang'] ) ) {
47 global $wgLang;
48 $langObj = $wgLang;
49 } else {
50 $langObj = Language::factory( $params['lang'] );
51 }
52
53 if ( $params['enableparser'] ) {
54 if ( !is_null( $params['title'] ) ) {
55 $title = Title::newFromText( $params['title'] );
56 if ( !$title ) {
57 $this->dieUsageMsg( array( 'invalidtitle', $params['title'] ) );
58 }
59 } else {
60 $title = Title::newFromText( 'API' );
61 }
62 }
63
64 $prop = array_flip( (array)$params['prop'] );
65
66 // Determine which messages should we print
67 if ( in_array( '*', $params['messages'] ) ) {
68 $message_names = array_keys( Language::getMessagesFor( 'en' ) );
69 sort( $message_names );
70 $messages_target = $message_names;
71 } else {
72 $messages_target = $params['messages'];
73 }
74
75 // Filter messages
76 if ( isset( $params['filter'] ) ) {
77 $messages_filtered = array();
78 foreach ( $messages_target as $message ) {
79 // !== is used because filter can be at the beginning of the string
80 if ( strpos( $message, $params['filter'] ) !== false ) {
81 $messages_filtered[] = $message;
82 }
83 }
84 $messages_target = $messages_filtered;
85 }
86
87 // Get all requested messages and print the result
88 $skip = !is_null( $params['from'] );
89 $useto = !is_null( $params['to'] );
90 $result = $this->getResult();
91 foreach ( $messages_target as $message ) {
92 // Skip all messages up to $params['from']
93 if ( $skip && $message === $params['from'] ) {
94 $skip = false;
95 }
96
97 if ( $useto && $message > $params['to'] ) {
98 break;
99 }
100
101 if ( !$skip ) {
102 $a = array( 'name' => $message );
103 $args = array();
104 if ( isset( $params['args'] ) && count( $params['args'] ) != 0 ) {
105 $args = $params['args'];
106 }
107
108 $msg = wfMessage( $message, $args )->inLanguage( $langObj );
109
110 if ( !$msg->exists() ) {
111 $a['missing'] = '';
112 } else {
113 // Check if the parser is enabled:
114 if ( $params['enableparser'] ) {
115 $msgString = $msg->title( $title )->text();
116 } else {
117 $msgString = $msg->plain();
118 }
119 ApiResult::setContent( $a, $msgString );
120 if ( isset( $prop['default'] ) ) {
121 $default = wfMessage( $message )->inLanguage( $langObj )->useDatabase( false );
122 if ( !$default->exists() ) {
123 $a['defaultmissing'] = '';
124 } elseif ( $default->plain() != $msgString ) {
125 $a['default'] = $default->plain();
126 }
127 }
128 }
129 $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $a );
130 if ( !$fit ) {
131 $this->setContinueEnumParameter( 'from', $message );
132 break;
133 }
134 }
135 }
136 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'message' );
137 }
138
139 public function getCacheMode( $params ) {
140 if ( is_null( $params['lang'] ) ) {
141 // Language not specified, will be fetched from preferences
142 return 'anon-public-user-private';
143 } elseif ( $params['enableparser'] ) {
144 // User-specific parser options will be used
145 return 'anon-public-user-private';
146 } else {
147 // OK to cache
148 return 'public';
149 }
150 }
151
152 public function getAllowedParams() {
153 return array(
154 'messages' => array(
155 ApiBase::PARAM_DFLT => '*',
156 ApiBase::PARAM_ISMULTI => true,
157 ),
158 'prop' => array(
159 ApiBase::PARAM_ISMULTI => true,
160 ApiBase::PARAM_TYPE => array(
161 'default'
162 )
163 ),
164 'enableparser' => false,
165 'args' => array(
166 ApiBase::PARAM_ISMULTI => true
167 ),
168 'filter' => array(),
169 'lang' => null,
170 'from' => null,
171 'to' => null,
172 'title' => null,
173 );
174 }
175
176 public function getParamDescription() {
177 return array(
178 'messages' => 'Which messages to output. "*" means all messages',
179 'prop' => 'Which properties to get',
180 'enableparser' => array( 'Set to enable parser, will preprocess the wikitext of message',
181 'Will substitute magic words, handle templates etc.' ),
182 'title' => 'Page name to use as context when parsing message (for enableparser option)',
183 'args' => 'Arguments to be substituted into message',
184 'filter' => 'Return only messages that contain this string',
185 'lang' => 'Return messages in this language',
186 'from' => 'Return messages starting at this message',
187 'to' => 'Return messages ending at this message',
188 );
189 }
190
191 public function getDescription() {
192 return 'Return messages from this site';
193 }
194
195 protected function getExamples() {
196 return array(
197 'api.php?action=query&meta=allmessages&amfilter=ipb-',
198 'api.php?action=query&meta=allmessages&ammessages=august|mainpage&amlang=de',
199 );
200 }
201
202 public function getVersion() {
203 return __CLASS__ . ': $Id$';
204 }
205 }