+ /**
+ * Set the 'BlockID' cookie to this block's ID and expiry time. The cookie's expiry will be
+ * the same as the block's, to a maximum of 24 hours.
+ *
+ * An empty value can also be set, in order to retain the cookie but remove the block ID
+ * (e.g. as used in User::getBlockedStatus).
+ *
+ * @param WebResponse $response The response on which to set the cookie.
+ * @param boolean $setEmpty Whether to set the cookie's value to the empty string.
+ */
+ public function setCookie( WebResponse $response, $setEmpty = false ) {
+ // Calculate the default expiry time.
+ $maxExpiryTime = wfTimestamp( TS_MW, wfTimestamp() + ( 24 * 60 * 60 ) );
+
+ // Use the Block's expiry time only if it's less than the default.
+ $expiryTime = $this->getExpiry();
+ if ( $expiryTime === 'infinity' || $expiryTime > $maxExpiryTime ) {
+ $expiryTime = $maxExpiryTime;
+ }
+
+ // Set the cookie. Reformat the MediaWiki datetime as a Unix timestamp for the cookie.
+ $cookieValue = $setEmpty ? '' : $this->getId();
+ $expiryValue = DateTime::createFromFormat( "YmdHis", $expiryTime );
+ $response->setCookie( 'BlockID', $cookieValue, $expiryValue->format( "U" ) );
+ }
+