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 $prop = array_flip( (array)$params['prop'] );
55 // Determine which messages should we print
56 if ( in_array( '*', $params['messages'] ) ) {
57 $message_names = array_keys( Language
::getMessagesFor( 'en' ) );
58 sort( $message_names );
59 $messages_target = $message_names;
61 $messages_target = $params['messages'];
65 if ( isset( $params['filter'] ) ) {
66 $messages_filtered = array();
67 foreach ( $messages_target as $message ) {
68 // !== is used because filter can be at the beginning of the string
69 if ( strpos( $message, $params['filter'] ) !== false ) {
70 $messages_filtered[] = $message;
73 $messages_target = $messages_filtered;
76 // Get all requested messages and print the result
77 $skip = !is_null( $params['from'] );
78 $useto = !is_null( $params['to'] );
79 $result = $this->getResult();
80 foreach ( $messages_target as $message ) {
81 // Skip all messages up to $params['from']
82 if ( $skip && $message === $params['from'] ) {
86 if ( $useto && $message > $params['to'] ) {
91 $a = array( 'name' => $message );
93 if ( isset( $params['args'] ) && count( $params['args'] ) != 0 ) {
94 $args = $params['args'];
97 $msg = wfMessage( $message, $args )->inLanguage( $langObj );
99 if ( !$msg->exists() ) {
102 // Check if the parser is enabled:
103 if ( $params['enableparser'] ) {
104 $msgString = $msg->text();
106 $msgString = $msg->plain();
108 ApiResult
::setContent( $a, $msgString );
109 if ( isset( $prop['default'] ) ) {
110 $default = wfMessage( $message )->inLanguage( $langObj )->useDatabase( false );
111 if ( !$default->exists() ) {
112 $a['defaultmissing'] = '';
113 } elseif ( $default->plain() != $msgString ) {
114 $a['default'] = $default->plain();
118 $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $a );
120 $this->setContinueEnumParameter( 'from', $message );
125 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'message' );
128 public function getCacheMode( $params ) {
129 if ( is_null( $params['lang'] ) ) {
130 // Language not specified, will be fetched from preferences
131 return 'anon-public-user-private';
132 } elseif ( $params['enableparser'] ) {
133 // User-specific parser options will be used
134 return 'anon-public-user-private';
141 public function getAllowedParams() {
144 ApiBase
::PARAM_DFLT
=> '*',
145 ApiBase
::PARAM_ISMULTI
=> true,
148 ApiBase
::PARAM_ISMULTI
=> true,
149 ApiBase
::PARAM_TYPE
=> array(
153 'enableparser' => false,
155 ApiBase
::PARAM_ISMULTI
=> true
164 public function getParamDescription() {
166 'messages' => 'Which messages to output. "*" means all messages',
167 'prop' => 'Which properties to get',
168 'enableparser' => array( 'Set to enable parser, will preprocess the wikitext of message',
169 'Will substitute magic words, handle templates etc.' ),
170 'args' => 'Arguments to be substituted into message',
171 'filter' => 'Return only messages that contain this string',
172 'lang' => 'Return messages in this language',
173 'from' => 'Return messages starting at this message',
174 'to' => 'Return messages ending at this message',
178 public function getDescription() {
179 return 'Return messages from this site';
182 protected function getExamples() {
184 'api.php?action=query&meta=allmessages&amfilter=ipb-',
185 'api.php?action=query&meta=allmessages&ammessages=august|mainpage&amlang=de',
189 public function getVersion() {
190 return __CLASS__
. ': $Id$';