Followup to r69776: cache result of extractRequestParams() because it gets called...
[mediawiki.git] / includes / api / ApiQueryAllmessages.php
blob39b736b4ac8e42db82bced000dc64c4cbfeba06f
1 <?php
3 /**
4 * Created on Dec 1, 2007
6 * API for MediaWiki 1.8+
8 * Copyright © 2006 Yuri Astrakhan <Firstname><Lastname>@gmail.com
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.
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.
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
26 if ( !defined( 'MEDIAWIKI' ) ) {
27 // Eclipse helper - will be ignored in production
28 require_once( 'ApiQueryBase.php' );
31 /**
32 * A query action to return messages from site message cache
34 * @ingroup API
36 class ApiQueryAllmessages extends ApiQueryBase {
38 public function __construct( $query, $moduleName ) {
39 parent::__construct( $query, $moduleName, 'am' );
42 public function execute() {
43 $params = $this->extractRequestParams();
45 global $wgLang;
47 $oldLang = null;
48 if ( !is_null( $params['lang'] ) ) {
49 $oldLang = $wgLang; // Keep $wgLang for restore later
50 $wgLang = Language::factory( $params['lang'] );
53 $prop = array_flip( (array)$params['prop'] );
55 // Determine which messages should we print
56 $messages_target = array();
57 if ( in_array( '*', $params['messages'] ) ) {
58 $message_names = array_keys( Language::getMessagesFor( 'en' ) );
59 sort( $message_names );
60 $messages_target = $message_names;
61 } else {
62 $messages_target = $params['messages'];
65 // Filter messages
66 if ( isset( $params['filter'] ) ) {
67 $messages_filtered = array();
68 foreach ( $messages_target as $message ) {
69 // !== is used because filter can be at the beginning of the string
70 if ( strpos( $message, $params['filter'] ) !== false ) {
71 $messages_filtered[] = $message;
74 $messages_target = $messages_filtered;
77 // Get all requested messages and print the result
78 $skip = !is_null( $params['from'] );
79 $result = $this->getResult();
80 foreach ( $messages_target as $message ) {
81 // Skip all messages up to $params['from']
82 if ( $skip && $message === $params['from'] ) {
83 $skip = false;
86 if ( !$skip ) {
87 $a = array( 'name' => $message );
88 $args = null;
89 if ( isset( $params['args'] ) && count( $params['args'] ) != 0 ) {
90 $args = $params['args'];
92 // Check if the parser is enabled:
93 if ( $params['enableparser'] ) {
94 $msg = wfMsgExt( $message, array( 'parsemag' ), $args );
95 } elseif ( $args ) {
96 $msgString = wfMsgGetKey( $message, true, false, false );
97 $msg = wfMsgReplaceArgs( $msgString, $args );
98 } else {
99 $msg = wfMsgGetKey( $message, true, false, false );
102 if ( wfEmptyMsg( $message, $msg ) ) {
103 $a['missing'] = '';
104 } else {
105 ApiResult::setContent( $a, $msg );
106 if ( isset( $prop['default'] ) ) {
107 $default = wfMsgGetKey( $message, false, false, false );
108 if ( $default !== $msg ) {
109 if ( wfEmptyMsg( $message, $default ) ) {
110 $a['defaultmissing'] = '';
111 } else {
112 $a['default'] = $default;
117 $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $a );
118 if ( !$fit ) {
119 $this->setContinueEnumParameter( 'from', $name );
120 break;
124 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'message' );
126 if ( !is_null( $oldLang ) ) {
127 $wgLang = $oldLang; // Restore $oldLang
131 public function getCacheMode( $params ) {
132 if ( is_null( $params['lang'] ) ) {
133 // Language not specified, will be fetched from preferences
134 return 'anon-public-user-private';
135 } elseif ( $params['enableparser'] ) {
136 // User-specific parser options will be used
137 return 'anon-public-user-private';
138 } else {
139 // OK to cache
140 return 'public';
144 public function getAllowedParams() {
145 return array(
146 'messages' => array(
147 ApiBase::PARAM_DFLT => '*',
148 ApiBase::PARAM_ISMULTI => true,
150 'prop' => array(
151 ApiBase::PARAM_ISMULTI => true,
152 ApiBase::PARAM_TYPE => array(
153 'default'
156 'enableparser' => false,
157 'args' => array(
158 ApiBase::PARAM_ISMULTI => true
160 'filter' => array(),
161 'lang' => null,
162 'from' => null,
166 public function getParamDescription() {
167 return array(
168 'messages' => 'Which messages to output. "*" means all messages',
169 'prop' => 'Which properties to get',
170 'enableparser' => array( 'Set to enable parser, will preprocess the wikitext of message',
171 'Will substitute magic words, handle templates etc.' ),
172 'args' => 'Arguments to be substituted into message',
173 'filter' => 'Return only messages that contain this string',
174 'lang' => 'Return messages in this language',
175 'from' => 'Return messages starting at this message',
179 public function getDescription() {
180 return 'Return messages from this site';
183 protected function getExamples() {
184 return array(
185 'api.php?action=query&meta=allmessages&amfilter=ipb-',
186 'api.php?action=query&meta=allmessages&ammessages=august|mainpage&amlang=de',
190 public function getVersion() {
191 return __CLASS__ . ': $Id$';