* Refactoring ApiQueryImageInfo to use new File::loadHistory() interface. No change...
[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 (C) 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 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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 * @addtogroup 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 global $wgMessageCache;
44 $params = $this->extractRequestParams();
45
46 //Determine which messages should we print
47 $messages_target = array();
48 if( $params['messages'] == '*' ) {
49 $wgMessageCache->loadAllMessages();
50 $message_names = array_keys( array_merge( Language::getMessagesFor( 'en' ), $wgMessageCache->getExtensionMessagesFor( 'en' ) ) );
51 sort( $message_names );
52 $messages_target = $message_names;
53 } else {
54 $messages_target = explode( '|', $params['messages'] );
55 }
56
57 //Filter messages
58 if( isset( $params['filter'] ) ) {
59 $messages_filtered = array();
60 foreach( $messages_target as $message ) {
61 if( strpos( $message, $params['filter'] ) !== false ) { //!== is used because filter can be at the beginnig of the string
62 $messages_filtered[] = $message;
63 }
64 }
65 $messages_target = $messages_filtered;
66 }
67
68 $wgMessageCache->disableTransform();
69
70 //Get all requested messages
71 $messages = array();
72 foreach( $messages_target as $message ) {
73 $message = trim( $message ); //Message list can be formatted like "msg1 | msg2 | msg3", so let's trim() it
74 $messages[$message] = wfMsg( $message );
75 }
76
77 //Print the result
78 $result = $this->getResult();
79 $messages_out = array();
80 foreach( $messages as $name => $value ) {
81 $message = array();
82 $message['name'] = $name;
83 $result->setContent( $message, $value );
84 $messages_out[] = $message;
85 }
86 $result->setIndexedTagName( $messages_out, 'message' );
87 $result->addValue( 'query', $this->getModuleName(), $messages_out );
88 }
89
90 protected function getAllowedParams() {
91 return array (
92 'messages' => array (
93 ApiBase :: PARAM_DFLT => '*',
94 ),
95 'filter' => array(),
96 );
97 }
98
99 protected function getParamDescription() {
100 return array (
101 'messages' => 'Which messages to output. "*" means all messages',
102 'filter' => 'Return only messages that contains specified string',
103 );
104 }
105
106 protected function getDescription() {
107 return 'Return messages from this site.';
108 }
109
110 protected function getExamples() {
111 return array(
112 'api.php?action=query&meta=allmessages&amfilter=ipb-',
113 'api.php?action=query&meta=allmessages&ammessages=august|mainpage',
114 );
115 }
116
117 public function getVersion() {
118 return __CLASS__ . ': $Id$';
119 }
120 }