Add namespace restrictions to Special:Block and API
[lhc/web/wiklou.git] / includes / api / ApiBase.php
index 32156d8..1efd747 100644 (file)
@@ -268,6 +268,9 @@ abstract class ApiBase extends ContextSource {
        /** @var array Maps extension paths to info arrays */
        private static $extensionInfo = null;
 
+       /** @var int[][][] Cache for self::filterIDs() */
+       private static $filterIDsCache = [];
+
        /** @var ApiMain */
        private $mMainModule;
        /** @var string */
@@ -1832,6 +1835,41 @@ abstract class ApiBase extends ContextSource {
                }
        }
 
+       /**
+        * Filter out-of-range values from a list of positive integer IDs
+        * @since 1.33
+        * @param array $fields Array of pairs of table and field to check
+        * @param (string|int)[] $ids IDs to filter. Strings in the array are
+        *  expected to be stringified ints.
+        * @return (string|int)[] Filtered IDs.
+        */
+       protected function filterIDs( $fields, array $ids ) {
+               $min = INF;
+               $max = 0;
+               foreach ( $fields as list( $table, $field ) ) {
+                       if ( isset( self::$filterIDsCache[$table][$field] ) ) {
+                               $row = self::$filterIDsCache[$table][$field];
+                       } else {
+                               $row = $this->getDB()->selectRow(
+                                       $table,
+                                       [
+                                               'min_id' => "MIN($field)",
+                                               'max_id' => "MAX($field)",
+                                       ],
+                                       '',
+                                       __METHOD__
+                               );
+                               self::$filterIDsCache[$table][$field] = $row;
+                       }
+                       $min = min( $min, $row->min_id );
+                       $max = max( $max, $row->max_id );
+               }
+               return array_filter( $ids, function ( $id ) use ( $min, $max ) {
+                       return ( is_int( $id ) && $id >= 0 || ctype_digit( $id ) )
+                               && $id >= $min && $id <= $max;
+               } );
+       }
+
        /**@}*/
 
        /************************************************************************//**
@@ -1911,9 +1949,14 @@ abstract class ApiBase extends ContextSource {
         * @since 1.29
         * @param StatusValue $status
         * @param string[] $types 'warning' and/or 'error'
+        * @param string[] $filter Message keys to filter out (since 1.33)
         */
-       public function addMessagesFromStatus( StatusValue $status, $types = [ 'warning', 'error' ] ) {
-               $this->getErrorFormatter()->addMessagesFromStatus( $this->getModulePath(), $status, $types );
+       public function addMessagesFromStatus(
+               StatusValue $status, $types = [ 'warning', 'error' ], array $filter = []
+       ) {
+               $this->getErrorFormatter()->addMessagesFromStatus(
+                       $this->getModulePath(), $status, $types, $filter
+               );
        }
 
        /**