5 * Created on Dec 1, 2007
7 * Copyright © 2006 Yuri Astrakhan <Firstname><Lastname>@gmail.com
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.
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.
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
27 if ( !defined( 'MEDIAWIKI' ) ) {
28 // Eclipse helper - will be ignored in production
29 require_once( 'ApiQueryBase.php' );
33 * A query action to return messages from site message cache
37 class ApiQueryAllmessages
extends ApiQueryBase
{
39 public function __construct( $query, $moduleName ) {
40 parent
::__construct( $query, $moduleName, 'am' );
43 public function execute() {
44 $params = $this->extractRequestParams();
46 if ( is_null( $params['lang'] ) ) {
50 $langObj = Language
::factory( $params['lang'] );
53 if ( $params['enableparser'] ) {
54 if ( !is_null( $params['title'] ) ) {
55 $title = Title
::newFromText( $params['title'] );
57 $this->dieUsageMsg( array( 'invalidtitle', $params['title'] ) );
60 $title = Title
::newFromText( 'API' );
64 $prop = array_flip( (array)$params['prop'] );
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;
72 $messages_target = $params['messages'];
75 // Filter messages that have the specified prefix
76 // Because we sorted the message array earlier, they will appear in a clump:
77 if ( isset( $params['prefix'] ) ) {
79 $messages_filtered = array();
80 foreach ( $messages_target as $message ) {
81 // === 0: must be at beginning of string (position 0)
82 if ( strpos( $message, $params['prefix'] ) === 0 ) {
86 $messages_filtered[] = $message;
91 $messages_target = $messages_filtered;
94 // Filter messages that contain specified string
95 if ( isset( $params['filter'] ) ) {
96 $messages_filtered = array();
97 foreach ( $messages_target as $message ) {
98 // !== is used because filter can be at the beginning of the string
99 if ( strpos( $message, $params['filter'] ) !== false ) {
100 $messages_filtered[] = $message;
103 $messages_target = $messages_filtered;
106 // Whether we have any sort of message customisation filtering
107 $customiseFilterEnabled = $params['customised'] !== 'all';
108 if ( $customiseFilterEnabled ) {
110 $lang = $langObj->getCode();
112 $customisedMessages = AllmessagesTablePager
::getCustomisedStatuses(
113 array_map( array( $langObj, 'ucfirst'), $messages_target ), $lang, $lang != $wgContLang->getCode() );
115 $customised = $params['customised'] === 'modified';
118 // Get all requested messages and print the result
119 $skip = !is_null( $params['from'] );
120 $useto = !is_null( $params['to'] );
121 $result = $this->getResult();
122 foreach ( $messages_target as $message ) {
123 // Skip all messages up to $params['from']
124 if ( $skip && $message === $params['from'] ) {
128 if ( $useto && $message > $params['to'] ) {
133 $a = array( 'name' => $message );
135 if ( isset( $params['args'] ) && count( $params['args'] ) != 0 ) {
136 $args = $params['args'];
139 if ( $customiseFilterEnabled ) {
140 $messageIsCustomised = isset( $customisedMessages['pages'][ $langObj->ucfirst( $message ) ] );
141 if ( $customised === $messageIsCustomised ) {
143 $a['customised'] = '';
150 $msg = wfMessage( $message, $args )->inLanguage( $langObj );
152 if ( !$msg->exists() ) {
155 // Check if the parser is enabled:
156 if ( $params['enableparser'] ) {
157 $msgString = $msg->title( $title )->text();
159 $msgString = $msg->plain();
161 ApiResult
::setContent( $a, $msgString );
162 if ( isset( $prop['default'] ) ) {
163 $default = wfMessage( $message )->inLanguage( $langObj )->useDatabase( false );
164 if ( !$default->exists() ) {
165 $a['defaultmissing'] = '';
166 } elseif ( $default->plain() != $msgString ) {
167 $a['default'] = $default->plain();
171 $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $a );
173 $this->setContinueEnumParameter( 'from', $message );
178 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'message' );
181 public function getCacheMode( $params ) {
182 if ( is_null( $params['lang'] ) ) {
183 // Language not specified, will be fetched from preferences
184 return 'anon-public-user-private';
185 } elseif ( $params['enableparser'] ) {
186 // User-specific parser options will be used
187 return 'anon-public-user-private';
194 public function getAllowedParams() {
197 ApiBase
::PARAM_DFLT
=> '*',
198 ApiBase
::PARAM_ISMULTI
=> true,
201 ApiBase
::PARAM_ISMULTI
=> true,
202 ApiBase
::PARAM_TYPE
=> array(
206 'enableparser' => false,
208 ApiBase
::PARAM_ISMULTI
=> true,
209 ApiBase
::PARAM_ALLOW_DUPLICATES
=> true,
212 'customised' => array(
213 ApiBase
::PARAM_DFLT
=> 'all',
214 ApiBase
::PARAM_TYPE
=> array(
228 public function getParamDescription() {
230 'messages' => 'Which messages to output. "*" (default) means all messages',
231 'prop' => 'Which properties to get',
232 'enableparser' => array( 'Set to enable parser, will preprocess the wikitext of message',
233 'Will substitute magic words, handle templates etc.' ),
234 'title' => 'Page name to use as context when parsing message (for enableparser option)',
235 'args' => 'Arguments to be substituted into message',
236 'prefix' => 'Return messages with this prefix',
237 'filter' => 'Return only messages with names that contain this string',
238 'customised' => 'Return only messages in this customisation state',
239 'lang' => 'Return messages in this language',
240 'from' => 'Return messages starting at this message',
241 'to' => 'Return messages ending at this message',
245 public function getDescription() {
246 return 'Return messages from this site';
249 protected function getExamples() {
251 'api.php?action=query&meta=allmessages&refix=ipb-',
252 'api.php?action=query&meta=allmessages&ammessages=august|mainpage&amlang=de',
256 public function getVersion() {
257 return __CLASS__
. ': $Id$';