Merge "New hook 'ParserTestGlobals'"
authorDemon <chadh@wikimedia.org>
Tue, 14 Aug 2012 17:53:58 +0000 (17:53 +0000)
committerGerrit Code Review <gerrit@wikimedia.org>
Tue, 14 Aug 2012 17:53:58 +0000 (17:53 +0000)
RELEASE-NOTES-1.20
includes/AutoLoader.php
includes/Linker.php
includes/db/Database.php
includes/json/FormatJson.php
includes/libs/GenericArrayObject.php [new file with mode: 0644]
includes/specials/SpecialLog.php
includes/specials/SpecialRecentchanges.php
tests/phpunit/MediaWikiTestCase.php
tests/phpunit/includes/db/DatabaseTest.php
tests/phpunit/includes/libs/GenericArrayObjectTest.php [new file with mode: 0644]

index a53d8ee..6f45cf0 100644 (file)
@@ -108,10 +108,10 @@ upgrade PHP if you have not done so prior to upgrading MediaWiki.
 * Added support in jquery.localize for placeholder attributes.
 * (bug 38151) Implemented mw.user.getRights for getting and caching the current
   user's user rights.
-* Session storage can now configured independently of general object cache 
-  storage, by using $wgSessionCacheType. $wgSessionsInMemcached has been 
+* Session storage can now configured independently of general object cache
+  storage, by using $wgSessionCacheType. $wgSessionsInMemcached has been
   renamed to $wgSessionsInObjectCache, with the old name retained for backwards
-  compatibility. When this feature is enabled, the expiry time can now be 
+  compatibility. When this feature is enabled, the expiry time can now be
   configured with $wgObjectCacheSessionExpiry.
 * Added a Redis client for object caching.
 * Implemented mw.user.getGroups for getting and caching user groups.
@@ -202,6 +202,8 @@ upgrade PHP if you have not done so prior to upgrading MediaWiki.
 * (bug 38953) --memory-limit switch not working for runJobs.php.
 * (bug 33037) Make subpage of Special:newfiles control how many files
   are returned, like in previous versions.
+* (bug 36524) "Show" options on Special:RecentChanges and Special:RecentChangesLinked
+  are now remembered between successive clicks.
 
 === API changes in 1.20 ===
 * (bug 34316) Add ability to retrieve maximum upload size from MediaWiki API.
index a3d26fe..2ea025e 100644 (file)
@@ -651,6 +651,7 @@ $wgAutoloadLocalClasses = array(
        'CSSJanus' => 'includes/libs/CSSJanus.php',
        'CSSJanus_Tokenizer' => 'includes/libs/CSSJanus.php',
        'CSSMin' => 'includes/libs/CSSMin.php',
+       'GenericArrayObject' => 'includes/libs/GenericArrayObject.php',
        'HttpStatus' => 'includes/libs/HttpStatus.php',
        'IEContentAnalyzer' => 'includes/libs/IEContentAnalyzer.php',
        'IEUrlExtension' => 'includes/libs/IEUrlExtension.php',
@@ -1052,6 +1053,9 @@ $wgAutoloadLocalClasses = array(
        'TestFileIterator' => 'tests/testHelpers.inc',
        'TestRecorder' => 'tests/testHelpers.inc',
 
+       # tests/phpunit/includes
+       'GenericArrayObjectTest' => 'tests/phpunit/includes/libs/GenericArrayObjectTest.php',
+
        # tests/phpunit/includes/db
        'ORMRowTest' => 'tests/phpunit/includes/db/ORMRowTest.php',
 
index 9949aa8..5b1f0ff 100644 (file)
@@ -202,6 +202,7 @@ class Linker {
                if( is_string( $query ) ) {
                        // some functions withing core using this still hand over query strings
                        wfDeprecated( __METHOD__ . ' with parameter $query as string (should be array)', '1.20' );
+                       $query = wfCgiToArray( $query );
                }
                $options = (array)$options;
 
index 4771659..3bf0588 100644 (file)
@@ -1951,7 +1951,9 @@ abstract class DatabaseBase implements DatabaseType {
 
                # Quote the $database and $table and apply the prefix if not quoted.
                if ( isset( $database ) ) {
-                       $database = ( $format == 'quoted' || $this->isQuotedIdentifier( $database ) ? $database : $this->addIdentifierQuotes( $database ) );
+                       if ( $format == 'quoted' && !$this->isQuotedIdentifier( $database ) ) {
+                               $database = $this->addIdentifierQuotes( $database );
+                       }
                }
 
                $table = "{$prefix}{$table}";
index bbcc185..aa60fbd 100644 (file)
@@ -41,11 +41,11 @@ class FormatJson {
         * @return string
         */
        public static function encode( $value, $isHtml = false ) {
-               if ( !function_exists( 'json_encode' ) || $isHtml ) {
+               if ( !function_exists( 'json_encode' ) || ( $isHtml && version_compare( PHP_VERSION, '5.4.0', '<' ) ) ) {
                        $json = new Services_JSON();
                        return $json->encode( $value, $isHtml );
                } else {
-                       return json_encode( $value );
+                       return json_encode( $value, $isHtml ? JSON_PRETTY_PRINT : 0 );
                }
        }
 
diff --git a/includes/libs/GenericArrayObject.php b/includes/libs/GenericArrayObject.php
new file mode 100644 (file)
index 0000000..d4cc525
--- /dev/null
@@ -0,0 +1,244 @@
+<?php
+
+/**
+ * Extends ArrayObject and does two things:
+ *
+ * Allows for deriving classes to easily intercept additions
+ * and deletions for purposes such as additional indexing.
+ *
+ * Enforces the objects to be of a certain type, so this
+ * can be replied upon, much like if this had true support
+ * for generics, which sadly enough is not possible in PHP.
+ *
+ * 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
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * 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.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ *
+ * @since 1.20
+ *
+ * @file
+ * @ingroup Diff
+ *
+ * @licence GNU GPL v2+
+ * @author Jeroen De Dauw < jeroendedauw@gmail.com >
+ */
+abstract class GenericArrayObject extends ArrayObject {
+
+       /**
+        * Returns the name of an interface/class that the element should implement/extend.
+        *
+        * @since 1.20
+        *
+        * @return string
+        */
+       public abstract function getObjectType();
+
+       /**
+        * @see SiteList::getNewOffset()
+        * @since 1.20
+        * @var integer
+        */
+       protected $indexOffset = 0;
+
+       /**
+        * Finds a new offset for when appending an element.
+        * The base class does this, so it would be better to integrate,
+        * but there does not appear to be any way to do this...
+        *
+        * @since 1.20
+        *
+        * @return integer
+        */
+       protected function getNewOffset() {
+               while ( true ) {
+                       if ( !$this->offsetExists( $this->indexOffset ) ) {
+                               return $this->indexOffset;
+                       }
+
+                       $this->indexOffset++;
+               }
+       }
+
+       /**
+        * Constructor.
+        * @see ArrayObject::__construct
+        *
+        * @since 1.20
+        *
+        * @param null|array $input
+        * @param int $flags
+        * @param string $iterator_class
+        */
+       public function __construct( $input = null, $flags = 0, $iterator_class = 'ArrayIterator' ) {
+               parent::__construct( array(), $flags, $iterator_class );
+
+               if ( !is_null( $input ) ) {
+                       foreach ( $input as $offset => $value ) {
+                               $this->offsetSet( $offset, $value );
+                       }
+               }
+       }
+
+       /**
+        * @see ArrayObject::append
+        *
+        * @since 1.20
+        *
+        * @param mixed $value
+        */
+       public function append( $value ) {
+               $this->setElement( null, $value );
+       }
+
+       /**
+        * @see ArrayObject::offsetSet()
+        *
+        * @since 1.20
+        *
+        * @param mixed $index
+        * @param mixed $value
+        */
+       public function offsetSet( $index, $value ) {
+               $this->setElement( $index, $value );
+       }
+
+       /**
+        * Returns if the provided value has the same type as the elements
+        * that can be added to this ArrayObject.
+        *
+        * @since 1.20
+        *
+        * @param mixed $value
+        *
+        * @return boolean
+        */
+       protected function hasValidType( $value ) {
+               $class = $this->getObjectType();
+               return $value instanceof $class;
+       }
+
+       /**
+        * Method that actually sets the element and holds
+        * all common code needed for set operations, including
+        * type checking and offset resolving.
+        *
+        * If you want to do additional indexing or have code that
+        * otherwise needs to be executed whenever an element is added,
+        * you can overload @see preSetElement.
+        *
+        * @since 1.20
+        *
+        * @param mixed $index
+        * @param mixed $value
+        *
+        * @throws Exception
+        */
+       protected function setElement( $index, $value ) {
+               if ( !$this->hasValidType( $value ) ) {
+                       throw new Exception(
+                               'Can only add ' . $this->getObjectType() . ' implementing objects to ' . get_called_class() . '.'
+                       );
+               }
+
+               if ( is_null( $index ) ) {
+                       $index = $this->getNewOffset();
+               }
+
+               if ( $this->preSetElement( $index, $value ) ) {
+                       parent::offsetSet( $index, $value );
+               }
+       }
+
+       /**
+        * Gets called before a new element is added to the ArrayObject.
+        *
+        * At this point the index is always set (ie not null) and the
+        * value is always of the type returned by @see getObjectType.
+        *
+        * Should return a boolean. When false is returned the element
+        * does not get added to the ArrayObject.
+        *
+        * @since 1.20
+        *
+        * @param integer|string $index
+        * @param mixed $value
+        *
+        * @return boolean
+        */
+       protected function preSetElement( $index, $value ) {
+               return true;
+       }
+
+       /**
+        * @see Serializable::serialize
+        *
+        * @since 1.20
+        *
+        * @return string
+        */
+       public function serialize() {
+               return serialize( $this->getSerializationData() );
+       }
+
+       /**
+        * Returns an array holding all the data that should go into serialization calls.
+        * This is intended to allow overloading without having to reimplement the
+        * behaviour of this base class.
+        *
+        * @since 1.20
+        *
+        * @return array
+        */
+       protected function getSerializationData() {
+               return array(
+                       'data' => $this->getArrayCopy(),
+                       'index' => $this->indexOffset,
+               );
+       }
+
+       /**
+        * @see Serializable::unserialize
+        *
+        * @since 1.20
+        *
+        * @param string $serialization
+        *
+        * @return array
+        */
+       public function unserialize( $serialization ) {
+               $serializationData = unserialize( $serialization );
+
+               foreach ( $serializationData['data'] as $offset => $value ) {
+                       // Just set the element, bypassing checks and offset resolving,
+                       // as these elements have already gone through this.
+                       parent::offsetSet( $offset, $value );
+               }
+
+               $this->indexOffset = $serializationData['index'];
+
+               return $serializationData;
+       }
+
+       /**
+        * Returns if the ArrayObject has no elements.
+        *
+        * @since 1.20
+        *
+        * @return boolean
+        */
+       public function isEmpty() {
+               return $this->count() === 0;
+       }
+
+}
index 8e4205c..8ab0976 100644 (file)
@@ -162,7 +162,7 @@ class SpecialLog extends SpecialPage {
 
        private function getRevisionButton( $formcontents ) {
                # If the user doesn't have the ability to delete log entries, don't bother showing him/her the button.
-               if ( !$this->getUser()->isAllowed( 'deletelogentry' ) ) {
+               if ( !$this->getUser()->isAllowedAll( 'deletedhistory', 'deletelogentry' ) ) {
                        return $formcontents;
                }
 
index bb384d4..ec8f93b 100644 (file)
@@ -772,6 +772,16 @@ class SpecialRecentChanges extends IncludableSpecialPage {
         */
        function makeOptionsLink( $title, $override, $options, $active = false ) {
                $params = $override + $options;
+
+               // Bug 36524: false values have be converted to "0" otherwise
+               // wfArrayToCgi() will omit it them.
+               foreach ( $params as &$value ) {
+                       if ( $value === false ) {
+                               $value = '0';
+                       }
+               }
+               unset( $value );
+
                $text = htmlspecialchars( $title );
                if ( $active ) {
                        $text = '<strong>' . $text . '</strong>';
index a7e3954..c873c51 100644 (file)
@@ -385,6 +385,26 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase {
                $this->assertFalse( $r, "found extra row (after #$i)" );
        }
 
+       /**
+        * Utility method taking an array of elements and wrapping
+        * each element in it's own array. Useful for data providers
+        * that only return a single argument.
+        *
+        * @since 1.20
+        *
+        * @param array $elements
+        *
+        * @return array
+        */
+       protected function arrayWrap( array $elements ) {
+               return array_map(
+                       function( $element ) {
+                               return array( $element );
+                       },
+                       $elements
+               );
+       }
+
        /**
         * Assert that two arrays are equal. By default this means that both arrays need to hold
         * the same set of values. Using additional arguments, order and associated key can also
index 672e664..379ffb1 100644 (file)
@@ -57,6 +57,98 @@ class DatabaseTest extends MediaWikiTestCase {
                        $this->db->addQuotes( "string's cause trouble" ) );
        }
 
+       private function getSharedTableName( $table, $database, $prefix, $format = 'quoted' ) {
+               global $wgSharedDB, $wgSharedTables, $wgSharedPrefix;
+
+               $oldName = $wgSharedDB;
+               $oldTables = $wgSharedTables;
+               $oldPrefix = $wgSharedPrefix;
+
+               $wgSharedDB = $database;
+               $wgSharedTables = array( $table );
+               $wgSharedPrefix = $prefix;
+
+               $ret = $this->db->tableName( $table, $format );
+
+               $wgSharedDB = $oldName;
+               $wgSharedTables = $oldTables;
+               $wgSharedPrefix = $oldPrefix;
+
+               return $ret;
+       }
+
+       private function prefixAndQuote( $table, $database = null, $prefix = null, $format = 'quoted' ) {
+               if ( $this->db->getType() === 'sqlite' || $format !== 'quoted' ) {
+                       $quote = '';
+               } elseif ( $this->db->getType() === 'mysql' ) {
+                       $quote = '`';
+               } else {
+                       $quote = '"';
+               }
+
+               if ( $database !== null ) {
+                       $database = $quote . $database . $quote . '.';
+               }
+
+               if ( $prefix === null ) {
+                       $prefix = $this->dbPrefix();
+               }
+
+               return $database . $quote . $prefix . $table . $quote;
+       }
+
+       function testTableNameLocal() {
+               $this->assertEquals(
+                       $this->prefixAndQuote( 'tablename' ),
+                       $this->db->tableName( 'tablename' )
+               );
+       }
+
+       function testTableNameRawLocal() {
+               $this->assertEquals(
+                       $this->prefixAndQuote( 'tablename', null, null, 'raw' ),
+                       $this->db->tableName( 'tablename', 'raw' )
+               );
+       }
+
+       function testTableNameShared() {
+               $this->assertEquals(
+                       $this->prefixAndQuote( 'tablename', 'sharedatabase', 'sh_' ),
+                       $this->getSharedTableName( 'tablename', 'sharedatabase', 'sh_' )
+               );
+
+               $this->assertEquals(
+                       $this->prefixAndQuote( 'tablename', 'sharedatabase', null ),
+                       $this->getSharedTableName( 'tablename', 'sharedatabase', null )
+               );
+       }
+
+       function testTableNameRawShared() {
+               $this->assertEquals(
+                       $this->prefixAndQuote( 'tablename', 'sharedatabase', 'sh_', 'raw' ),
+                       $this->getSharedTableName( 'tablename', 'sharedatabase', 'sh_', 'raw' )
+               );
+
+               $this->assertEquals(
+                       $this->prefixAndQuote( 'tablename', 'sharedatabase', null, 'raw' ),
+                       $this->getSharedTableName( 'tablename', 'sharedatabase', null, 'raw' )
+               );
+       }
+
+       function testTableNameForeign() {
+               $this->assertEquals(
+                       $this->prefixAndQuote( 'tablename', 'databasename', '' ),
+                       $this->db->tableName( 'databasename.tablename' )
+               );
+       }
+
+       function testTableNameRawForeign() {
+               $this->assertEquals(
+                       $this->prefixAndQuote( 'tablename', 'databasename', '', 'raw' ),
+                       $this->db->tableName( 'databasename.tablename', 'raw' )
+               );
+       }
+
        function testFillPreparedEmpty() {
                $sql = $this->db->fillPrepared(
                        'SELECT * FROM interwiki', array() );
diff --git a/tests/phpunit/includes/libs/GenericArrayObjectTest.php b/tests/phpunit/includes/libs/GenericArrayObjectTest.php
new file mode 100644 (file)
index 0000000..5e4cda1
--- /dev/null
@@ -0,0 +1,238 @@
+<?php
+
+
+/**
+ * Tests for the GenericArrayObject and deriving classes.
+ *
+ * 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
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * 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.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ *
+ * @file
+ * @since 1.20
+ *
+ * @ingroup Test
+ * @group GenericArrayObject
+ *
+ * @licence GNU GPL v2+
+ * @author Jeroen De Dauw < jeroendedauw@gmail.com >
+ */
+abstract class GenericArrayObjectTest extends MediaWikiTestCase {
+
+       /**
+        * Returns objects that can serve as elements in the concrete GenericArrayObject deriving class being tested.
+        *
+        * @since 1.20
+        *
+        * @return array
+        */
+       public abstract function elementInstancesProvider();
+
+       /**
+        * Provides instances of the concrete class being tested.
+        *
+        * @since 1.20
+        *
+        * @return array
+        */
+       public abstract function instanceProvider();
+
+       /**
+        * Returns the name of the concrete class being tested.
+        *
+        * @since 1.20
+        *
+        * @return string
+        */
+       public abstract function getInstanceClass();
+
+       /**
+        * @since 1.20
+        *
+        * @param array $elements
+        *
+        * @return GenericArrayObject
+        */
+       protected function getNew( array $elements = array() ) {
+               $class = $this->getInstanceClass();
+               return new $class( $elements );
+       }
+
+       /**
+        * @dataProvider elementInstancesProvider
+        *
+        * @since 1.20
+        *
+        * @param array $elements
+        */
+       public function testConstructor( array $elements ) {
+               $arrayObject = $this->getNew( $elements );
+
+               $this->assertEquals( count( $elements ), $arrayObject->count() );
+       }
+
+       /**
+        * @dataProvider elementInstancesProvider
+        *
+        * @since 1.20
+        *
+        * @param array $elements
+        */
+       public function testIsEmpty( array $elements ) {
+               $arrayObject = $this->getNew( $elements );
+
+               $this->assertEquals( $elements === array(), $arrayObject->isEmpty() );
+       }
+
+       /**
+        * @dataProvider instanceProvider
+        *
+        * @since 1.20
+        *
+        * @param GenericArrayObject $list
+        */
+       public function testUnset( GenericArrayObject $list ) {
+               if ( !$list->isEmpty() ) {
+                       $offset = $list->getIterator()->key();
+                       $count = $list->count();
+                       $list->offsetUnset( $offset );
+                       $this->assertEquals( $count - 1, $list->count() );
+               }
+
+               if ( !$list->isEmpty() ) {
+                       $offset = $list->getIterator()->key();
+                       $count = $list->count();
+                       unset( $list[$offset] );
+                       $this->assertEquals( $count - 1, $list->count() );
+               }
+
+               $exception = null;
+               try { $list->offsetUnset( 'sdfsedtgsrdysftu' ); } catch ( \Exception $exception ){}
+               $this->assertInstanceOf( '\Exception', $exception );
+       }
+
+       /**
+        * @dataProvider elementInstancesProvider
+        *
+        * @since 1.20
+        *
+        * @param array $elements
+        */
+       public function testAppend( array $elements ) {
+               $list = $this->getNew();
+
+               $listSize = count( $elements );
+
+               foreach ( $elements as $element ) {
+                       $list->append( $element );
+               }
+
+               $this->assertEquals( $listSize, $list->count() );
+
+               $list = $this->getNew();
+
+               foreach ( $elements as $element ) {
+                       $list[] = $element;
+               }
+
+               $this->assertEquals( $listSize, $list->count() );
+
+               $this->checkTypeChecks( function( GenericArrayObject $list, $element ) {
+                       $list->append( $element );
+               } );
+       }
+
+       /**
+        * @since 1.20
+        *
+        * @param callback $function
+        */
+       protected function checkTypeChecks( $function ) {
+               $excption = null;
+               $list = $this->getNew();
+
+               $elementClass = $list->getObjectType();
+
+               foreach ( array( 42, 'foo', array(), new \stdClass(), 4.2 ) as $element ) {
+                       $validValid = $element instanceof $elementClass;
+
+                       try{
+                               call_user_func( $function, $list, $element );
+                               $valid = true;
+                       }
+                       catch ( \MWException $exception ) {
+                               $valid = false;
+                       }
+
+                       $this->assertEquals(
+                               $validValid,
+                               $valid,
+                               'Object of invalid type got successfully added to a GenericArrayObject'
+                       );
+               }
+       }
+
+       /**
+        * @dataProvider elementInstancesProvider
+        *
+        * @since 1.20
+        *
+        * @param array $elements
+        */
+       public function testOffsetSet( array $elements ) {
+               if ( $elements === array() ) {
+                       $this->assertTrue( true );
+                       return;
+               }
+
+               $list = $this->getNew();
+
+               $element = reset( $elements );
+               $list->offsetSet( 42, $element );
+               $this->assertEquals( $element, $list->offsetGet( 42 ) );
+
+               $list = $this->getNew();
+
+               $element = reset( $elements );
+               $list['oHai'] = $element;
+               $this->assertEquals( $element, $list['oHai'] );
+
+               $list = $this->getNew();
+
+               $element = reset( $elements );
+               $list->offsetSet( 9001, $element );
+               $this->assertEquals( $element, $list[9001] );
+
+               $list = $this->getNew();
+
+               $element = reset( $elements );
+               $list->offsetSet( null, $element );
+               $this->assertEquals( $element, $list[0] );
+
+               $list = $this->getNew();
+               $offset = 0;
+
+               foreach ( $elements as $element ) {
+                       $list->offsetSet( null, $element );
+                       $this->assertEquals( $element, $list[$offset++] );
+               }
+
+               $this->assertEquals( count( $elements ), $list->count() );
+
+               $this->checkTypeChecks( function( GenericArrayObject $list, $element ) {
+                       $list->offsetSet( mt_rand(), $element );
+               } );
+       }
+
+}