Followup r83862, add notation that page count will be added if appropriate
[lhc/web/wiklou.git] / includes / api / ApiQueryAllmessages.php
index 25fb1bf..2801fa4 100644 (file)
@@ -1,11 +1,10 @@
 <?php
-
-/*
- * Created on Dec 1, 2007
+/**
  *
- * API for MediaWiki 1.8+
  *
- * Copyright (C) 2006 Yuri Astrakhan <Firstname><Lastname>@gmail.com
+ * Created on Dec 1, 2007
+ *
+ * Copyright © 2006 Yuri Astrakhan <Firstname><Lastname>@gmail.com
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
  *
  * You should have received a copy of the GNU General Public License along
  * with this program; if not, write to the Free Software Foundation, Inc.,
- * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  * http://www.gnu.org/copyleft/gpl.html
+ *
+ * @file
  */
 
 if ( !defined( 'MEDIAWIKI' ) ) {
        // Eclipse helper - will be ignored in production
-       require_once ( 'ApiQueryBase.php' );
+       require_once( 'ApiQueryBase.php' );
 }
 
 /**
@@ -36,22 +37,33 @@ if ( !defined( 'MEDIAWIKI' ) ) {
 class ApiQueryAllmessages extends ApiQueryBase {
 
        public function __construct( $query, $moduleName ) {
-               parent :: __construct( $query, $moduleName, 'am' );
+               parent::__construct( $query, $moduleName, 'am' );
        }
 
        public function execute() {
                $params = $this->extractRequestParams();
 
-               if ( !is_null( $params['lang'] ) )
-               {
+               if ( is_null( $params['lang'] ) ) {
                        global $wgLang;
-                       $wgLang = Language::factory( $params['lang'] );
+                       $langObj = $wgLang;
+               } else {
+                       $langObj = Language::factory( $params['lang'] );
                }
-               
+
+               if ( $params['enableparser'] ) {
+                       if ( !is_null( $params['title'] ) ) {
+                               $title = Title::newFromText( $params['title'] );
+                               if ( !$title ) {
+                                       $this->dieUsageMsg( array( 'invalidtitle', $params['title'] ) );
+                               }
+                       } else {
+                               $title = Title::newFromText( 'API' );
+                       }
+               }
+
                $prop = array_flip( (array)$params['prop'] );
 
                // Determine which messages should we print
-               $messages_target = array();
                if ( in_array( '*', $params['messages'] ) ) {
                        $message_names = array_keys( Language::getMessagesFor( 'en' ) );
                        sort( $message_names );
@@ -60,45 +72,105 @@ class ApiQueryAllmessages extends ApiQueryBase {
                        $messages_target = $params['messages'];
                }
 
-               // Filter messages
+               // Filter messages that have the specified prefix
+               // Because we sorted the message array earlier, they will appear in a clump:
+               if ( isset( $params['prefix'] ) ) {
+                       $skip = false;
+                       $messages_filtered = array();
+                       foreach ( $messages_target as $message ) {
+                               // === 0: must be at beginning of string (position 0)
+                               if ( strpos( $message, $params['prefix'] ) === 0 ) {
+                                       if( !$skip ) {
+                                               $skip = true;
+                                       }
+                                       $messages_filtered[] = $message;
+                               } else if ( $skip ) {
+                                       break;
+                               }
+                       }
+                       $messages_target = $messages_filtered;
+               }
+
+               // Filter messages that contain specified string
                if ( isset( $params['filter'] ) ) {
                        $messages_filtered = array();
                        foreach ( $messages_target as $message ) {
-                               if ( strpos( $message, $params['filter'] ) !== false ) {        // !== is used because filter can be at the beginnig of the string
+                               // !== is used because filter can be at the beginning of the string
+                               if ( strpos( $message, $params['filter'] ) !== false ) {
                                        $messages_filtered[] = $message;
                                }
                        }
                        $messages_target = $messages_filtered;
                }
 
+               // Whether we have any sort of message customisation filtering
+               $customiseFilterEnabled = $params['customised'] !== 'all';
+               if ( $customiseFilterEnabled ) {
+                       global $wgContLang;
+                       $lang = $langObj->getCode();
+
+                       $customisedMessages = AllmessagesTablePager::getCustomisedStatuses(
+                               array_map( array( $langObj, 'ucfirst'), $messages_target ), $lang, $lang != $wgContLang->getCode() );
+
+                       $customised = $params['customised'] === 'modified';
+               }
+
                // Get all requested messages and print the result
-               $messages = array();
                $skip = !is_null( $params['from'] );
+               $useto = !is_null( $params['to'] );
                $result = $this->getResult();
                foreach ( $messages_target as $message ) {
                        // Skip all messages up to $params['from']
-                       if ( $skip && $message === $params['from'] )
+                       if ( $skip && $message === $params['from'] ) {
                                $skip = false;
+                       }
+
+                       if ( $useto && $message > $params['to'] ) {
+                               break;
+                       }
+
                        if ( !$skip ) {
                                $a = array( 'name' => $message );
-                               $msg = wfMsgGetKey( $message, true, false, false );
-                               if ( wfEmptyMsg( $message, $msg ) )
+                               $args = array();
+                               if ( isset( $params['args'] ) && count( $params['args'] ) != 0 ) {
+                                       $args = $params['args'];
+                               }
+
+                               if ( $customiseFilterEnabled ) {
+                                       $messageIsCustomised = isset( $customisedMessages['pages'][ $langObj->ucfirst( $message ) ] );
+                                       if ( $customised === $messageIsCustomised ) {
+                                               if ( $customised ) {
+                                                       $a['customised'] = '';
+                                               }
+                                       } else {
+                                               continue;
+                                       }
+                               }
+
+                               $msg = wfMessage( $message, $args )->inLanguage( $langObj );
+
+                               if ( !$msg->exists() ) {
                                        $a['missing'] = '';
-                               else {
-                                       ApiResult::setContent( $a, $msg );
+                               } else {
+                                       // Check if the parser is enabled:
+                                       if ( $params['enableparser'] ) {
+                                               $msgString = $msg->title( $title )->text();
+                                       } else {
+                                               $msgString = $msg->plain();
+                                       }
+                                       ApiResult::setContent( $a, $msgString );
                                        if ( isset( $prop['default'] ) ) {
-                                               $default = wfMsgGetKey( $message, false, false, false );
-                                               if ( $default !== $msg ) {
-                                                       if ( wfEmptyMsg( $message, $default ) )
-                                                               $a['defaultmissing'] = '';
-                                                       else
-                                                               $a['default'] = $default;
+                                               $default = wfMessage( $message )->inLanguage( $langObj )->useDatabase( false );
+                                               if ( !$default->exists() ) {
+                                                       $a['defaultmissing'] = '';
+                                               } elseif ( $default->plain() != $msgString ) {
+                                                       $a['default'] = $default->plain();
                                                }
                                        }
                                }
                                $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $a );
                                if ( !$fit ) {
-                                       $this->setContinueEnumParameter( 'from', $name );
+                                       $this->setContinueEnumParameter( 'from', $message );
                                        break;
                                }
                        }
@@ -106,46 +178,81 @@ class ApiQueryAllmessages extends ApiQueryBase {
                $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'message' );
        }
 
+       public function getCacheMode( $params ) {
+               if ( is_null( $params['lang'] ) ) {
+                       // Language not specified, will be fetched from preferences
+                       return 'anon-public-user-private';
+               } elseif ( $params['enableparser'] ) {
+                       // User-specific parser options will be used
+                       return 'anon-public-user-private';
+               } else {
+                       // OK to cache
+                       return 'public';
+               }
+       }
+
        public function getAllowedParams() {
-               return array (
-                       'messages' => array (
-                               ApiBase :: PARAM_DFLT => '*',
-                               ApiBase :: PARAM_ISMULTI => true,
+               return array(
+                       'messages' => array(
+                               ApiBase::PARAM_DFLT => '*',
+                               ApiBase::PARAM_ISMULTI => true,
                        ),
                        'prop' => array(
-                               ApiBase :: PARAM_ISMULTI => true,
-                               ApiBase :: PARAM_TYPE => array(
+                               ApiBase::PARAM_ISMULTI => true,
+                               ApiBase::PARAM_TYPE => array(
                                        'default'
                                )
                        ),
+                       'enableparser' => false,
+                       'args' => array(
+                               ApiBase::PARAM_ISMULTI => true
+                       ),
                        'filter' => array(),
+                       'customised' => array(
+                               ApiBase::PARAM_DFLT => 'all',
+                               ApiBase::PARAM_TYPE => array(
+                                       'all',
+                                       'modified',
+                                       'unmodified'
+                               )
+                       ),
                        'lang' => null,
                        'from' => null,
+                       'to' => null,
+                       'title' => null,
+                       'prefix' => null,
                );
        }
 
        public function getParamDescription() {
-               return array (
-                       'messages' => 'Which messages to output. "*" means all messages',
+               return array(
+                       'messages' => 'Which messages to output. "*" (default) means all messages',
                        'prop' => 'Which properties to get',
-                       'filter' => 'Return only messages that contain this string',
+                       'enableparser' => array( 'Set to enable parser, will preprocess the wikitext of message',
+                                                         'Will substitute magic words, handle templates etc.' ),
+                       'title' => 'Page name to use as context when parsing message (for enableparser option)',
+                       'args' => 'Arguments to be substituted into message',
+                       'prefix' => 'Return messages with this prefix',
+                       'filter' => 'Return only messages with names that contain this string',
+                       'customised' => 'Return only messages in this customisation state',
                        'lang' => 'Return messages in this language',
                        'from' => 'Return messages starting at this message',
+                       'to' => 'Return messages ending at this message',
                );
        }
 
        public function getDescription() {
-               return 'Return messages from this site.';
+               return 'Return messages from this site';
        }
 
        protected function getExamples() {
                return array(
-                       'api.php?action=query&meta=allmessages&amfilter=ipb-',
+                       'api.php?action=query&meta=allmessages&amprefix=ipb-',
                        'api.php?action=query&meta=allmessages&ammessages=august|mainpage&amlang=de',
-                       );
+               );
        }
 
        public function getVersion() {
                return __CLASS__ . ': $Id$';
        }
-}
\ No newline at end of file
+}