101 parent::__construct( $config );
103 $this->swiftAuthUrl = $config[
'swiftAuthUrl'];
104 $this->swiftUser = $config[
'swiftUser'];
105 $this->swiftKey = $config[
'swiftKey'];
107 $this->authTTL = isset( $config[
'swiftAuthTTL'] )
108 ? $config[
'swiftAuthTTL']
110 $this->swiftTempUrlKey = isset( $config[
'swiftTempUrlKey'] )
111 ? $config[
'swiftTempUrlKey']
113 $this->swiftStorageUrl = isset( $config[
'swiftStorageUrl'] )
114 ? $config[
'swiftStorageUrl']
116 $this->shardViaHashLevels = isset( $config[
'shardViaHashLevels'] )
117 ? $config[
'shardViaHashLevels']
119 $this->rgwS3AccessKey = isset( $config[
'rgwS3AccessKey'] )
120 ? $config[
'rgwS3AccessKey']
122 $this->rgwS3SecretKey = isset( $config[
'rgwS3SecretKey'] )
123 ? $config[
'rgwS3SecretKey']
128 if ( isset( $config[
'wanCache'] ) && $config[
'wanCache'] instanceof
WANObjectCache ) {
129 $this->memCache = $config[
'wanCache'];
134 if ( !empty( $config[
'cacheAuthInfo'] ) && isset( $config[
'srvCache'] ) ) {
135 $this->srvCache = $config[
'srvCache'];
147 if ( !mb_check_encoding( $relStoragePath,
'UTF-8' ) ) {
149 } elseif ( strlen( urlencode( $relStoragePath ) ) > 1024 ) {
153 return $relStoragePath;
158 if ( $rel ===
null ) {
173 return isset(
$params[
'headers'] )
186 foreach ( $rawHeaders as $name =>
$value ) {
187 $name = strtolower( $name );
188 if ( preg_match(
'/^content-(type|length)$/', $name ) ) {
190 } elseif ( preg_match(
'/^(x-)?content-/', $name ) ) {
192 } elseif ( preg_match(
'/^content-(disposition)/', $name ) ) {
197 if ( isset( $headers[
'content-disposition'] ) ) {
200 foreach ( explode(
';', $headers[
'content-disposition'] ) as $part ) {
201 $part = trim( $part );
202 $new = ( $disposition ===
'' ) ? $part :
"{$disposition};{$part}";
203 if ( strlen( $new ) <= 255 ) {
209 $headers[
'content-disposition'] = $disposition;
221 foreach ( $rawHeaders as $name =>
$value ) {
222 $name = strtolower( $name );
223 if ( strpos( $name,
'x-object-meta-' ) === 0 ) {
238 $metadata[substr( $name, strlen(
'x-object-meta-' ) )] =
$value;
248 if ( $dstRel ===
null ) {
254 $sha1Hash = Wikimedia\base_convert( sha1(
$params[
'content'] ), 16, 36, 31 );
255 $contentType = isset(
$params[
'headers'][
'content-type'] )
256 ?
$params[
'headers'][
'content-type']
261 'url' => [ $dstCont, $dstRel ],
263 'content-length' => strlen(
$params[
'content'] ),
264 'etag' => md5(
$params[
'content'] ),
265 'content-type' => $contentType,
266 'x-object-meta-sha1base36' => $sha1Hash
271 $method = __METHOD__;
273 list( $rcode, $rdesc, $rhdrs, $rbody, $rerr ) =
$request[
'response'];
274 if ( $rcode === 201 ) {
276 } elseif ( $rcode === 412 ) {
284 if ( !empty(
$params[
'async'] ) ) {
297 if ( $dstRel ===
null ) {
303 MediaWiki\suppressWarnings();
304 $sha1Hash = sha1_file(
$params[
'src'] );
305 MediaWiki\restoreWarnings();
306 if ( $sha1Hash ===
false ) {
311 $sha1Hash = Wikimedia\base_convert( $sha1Hash, 16, 36, 31 );
312 $contentType = isset(
$params[
'headers'][
'content-type'] )
313 ?
$params[
'headers'][
'content-type']
316 $handle = fopen(
$params[
'src'],
'rb' );
317 if ( $handle ===
false ) {
325 'url' => [ $dstCont, $dstRel ],
327 'content-length' => filesize(
$params[
'src'] ),
328 'etag' => md5_file(
$params[
'src'] ),
329 'content-type' => $contentType,
330 'x-object-meta-sha1base36' => $sha1Hash
335 $method = __METHOD__;
337 list( $rcode, $rdesc, $rhdrs, $rbody, $rerr ) =
$request[
'response'];
338 if ( $rcode === 201 ) {
340 } elseif ( $rcode === 412 ) {
348 $opHandle->resourcesToClose[] = $handle;
350 if ( !empty(
$params[
'async'] ) ) {
363 if ( $srcRel ===
null ) {
370 if ( $dstRel ===
null ) {
378 'url' => [ $dstCont, $dstRel ],
380 'x-copy-from' =>
'/' . rawurlencode( $srcCont ) .
381 '/' . str_replace(
"%2F",
"/", rawurlencode( $srcRel ) )
385 $method = __METHOD__;
387 list( $rcode, $rdesc, $rhdrs, $rbody, $rerr ) =
$request[
'response'];
388 if ( $rcode === 201 ) {
390 } elseif ( $rcode === 404 ) {
398 if ( !empty(
$params[
'async'] ) ) {
411 if ( $srcRel ===
null ) {
418 if ( $dstRel ===
null ) {
427 'url' => [ $dstCont, $dstRel ],
429 'x-copy-from' =>
'/' . rawurlencode( $srcCont ) .
430 '/' . str_replace(
"%2F",
"/", rawurlencode( $srcRel ) )
434 if (
"{$srcCont}/{$srcRel}" !==
"{$dstCont}/{$dstRel}" ) {
436 'method' =>
'DELETE',
437 'url' => [ $srcCont, $srcRel ],
442 $method = __METHOD__;
444 list( $rcode, $rdesc, $rhdrs, $rbody, $rerr ) =
$request[
'response'];
445 if (
$request[
'method'] ===
'PUT' && $rcode === 201 ) {
447 } elseif (
$request[
'method'] ===
'DELETE' && $rcode === 204 ) {
449 } elseif ( $rcode === 404 ) {
457 if ( !empty(
$params[
'async'] ) ) {
470 if ( $srcRel ===
null ) {
477 'method' =>
'DELETE',
478 'url' => [ $srcCont, $srcRel ],
482 $method = __METHOD__;
484 list( $rcode, $rdesc, $rhdrs, $rbody, $rerr ) =
$request[
'response'];
485 if ( $rcode === 204 ) {
487 } elseif ( $rcode === 404 ) {
488 if ( empty(
$params[
'ignoreMissingSource'] ) ) {
497 if ( !empty(
$params[
'async'] ) ) {
510 if ( $srcRel ===
null ) {
517 $stat = $this->
getFileStat( [
'src' => $params[
'src'],
'latest' => 1 ] );
518 if ( $stat && !isset( $stat[
'xattr'] ) ) {
519 $stat = $this->
doGetFileStat( [
'src' => $params[
'src'],
'latest' => 1 ] );
529 foreach ( $stat[
'xattr'][
'metadata'] as $name =>
$value ) {
530 $metaHdrs[
"x-object-meta-$name"] =
$value;
532 $customHdrs = $this->
sanitizeHdrs( $params ) + $stat[
'xattr'][
'headers'];
536 'url' => [ $srcCont, $srcRel ],
537 'headers' => $metaHdrs + $customHdrs
540 $method = __METHOD__;
542 list( $rcode, $rdesc, $rhdrs, $rbody, $rerr ) =
$request[
'response'];
543 if ( $rcode === 202 ) {
545 } elseif ( $rcode === 404 ) {
553 if ( !empty(
$params[
'async'] ) ) {
567 if ( is_array( $stat ) ) {
569 } elseif ( $stat ===
null ) {
570 $status->fatal(
'backend-fail-internal', $this->name );
571 $this->logger->error( __METHOD__ .
': cannot get container stat' );
577 if ( $stat ===
false ) {
587 if ( empty(
$params[
'noAccess'] ) ) {
592 if ( is_array( $stat ) ) {
596 [ $this->swiftUser ],
599 } elseif ( $stat ===
false ) {
602 $status->fatal(
'backend-fail-internal', $this->name );
603 $this->logger->error( __METHOD__ .
': cannot get container stat' );
613 if ( is_array( $stat ) ) {
617 [ $this->swiftUser,
'.r:*' ],
620 } elseif ( $stat ===
false ) {
623 $status->fatal(
'backend-fail-internal', $this->name );
624 $this->logger->error( __METHOD__ .
': cannot get container stat' );
640 if ( $stat ===
false ) {
642 } elseif ( !is_array( $stat ) ) {
643 $status->fatal(
'backend-fail-internal', $this->name );
644 $this->logger->error( __METHOD__ .
': cannot get container stat' );
650 if ( $stat[
'count'] == 0 ) {
663 return reset( $stats );
680 return $timestamp->getTimestamp( $format );
681 }
catch ( Exception
$e ) {
694 if ( isset( $objHdrs[
'x-object-meta-sha1base36'] ) ) {
700 $this->logger->error( __METHOD__ .
": $path was not stored with SHA-1 metadata." );
702 $objHdrs[
'x-object-meta-sha1base36'] =
false;
718 $tmpFile = $this->
getLocalCopy( [
'src' => $path,
'latest' => 1 ] );
720 $hash = $tmpFile->getSha1Base36();
721 if ( $hash !==
false ) {
722 $objHdrs[
'x-object-meta-sha1base36'] = $hash;
724 $postHeaders[
'x-object-meta-sha1base36'] = $hash;
726 list( $rcode ) = $this->
http->run( [
728 'url' => $this->
storageUrl( $auth, $srcCont, $srcRel ),
731 if ( $rcode >= 200 && $rcode <= 299 ) {
740 $this->logger->error( __METHOD__ .
": unable to set SHA-1 metadata for $path" );
750 $ep = array_diff_key(
$params, [
'srcs' => 1 ] );
755 foreach (
$params[
'srcs'] as $path ) {
757 if ( $srcRel ===
null || !$auth ) {
758 $contents[
$path] =
false;
762 $handle = fopen(
'php://temp',
'wb' );
766 'url' => $this->
storageUrl( $auth, $srcCont, $srcRel ),
772 $contents[
$path] =
false;
775 $opts = [
'maxConnsPerHost' =>
$params[
'concurrency'] ];
776 $reqs = $this->
http->runMulti( $reqs, $opts );
777 foreach ( $reqs as $path => $op ) {
778 list( $rcode, $rdesc, $rhdrs, $rbody, $rerr ) = $op[
'response'];
779 if ( $rcode >= 200 && $rcode <= 299 ) {
780 rewind( $op[
'stream'] );
781 $contents[
$path] = stream_get_contents( $op[
'stream'] );
782 } elseif ( $rcode === 404 ) {
783 $contents[
$path] =
false;
785 $this->
onError(
null, __METHOD__,
786 [
'src' => $path ] + $ep, $rerr, $rcode, $rdesc );
788 fclose( $op[
'stream'] );
795 $prefix = (
$dir ==
'' ) ?
null :
"{$dir}/";
798 return ( count(
$status->value ) ) > 0;
839 if ( $after === INF ) {
845 $prefix = (
$dir ==
'' ) ?
null :
"{$dir}/";
847 if ( !empty(
$params[
'topOnly'] ) ) {
853 foreach ( $objects as $object ) {
854 if ( substr( $object, -1 ) ===
'/' ) {
860 $getParentDir =
function (
$path ) {
861 return ( strpos( $path,
'/' ) !==
false ) ? dirname( $path ) :
false;
865 $lastDir = $getParentDir( $after );
874 foreach ( $objects as $object ) {
875 $objectDir = $getParentDir( $object );
877 if ( $objectDir !==
false && $objectDir !==
$dir ) {
882 if ( strcmp( $objectDir, $lastDir ) > 0 ) {
885 $dirs[] =
"{$pDir}/";
886 $pDir = $getParentDir( $pDir );
887 }
while ( $pDir !==
false
888 && strcmp( $pDir, $lastDir ) > 0
889 && strlen( $pDir ) > strlen(
$dir )
892 $lastDir = $objectDir;
897 if ( count( $objects ) < $limit ) {
900 $after = end( $objects );
919 if ( $after === INF ) {
925 $prefix = (
$dir ==
'' ) ?
null :
"{$dir}/";
928 if ( !empty(
$params[
'topOnly'] ) ) {
929 if ( !empty(
$params[
'adviseStat'] ) ) {
936 if ( !empty(
$params[
'adviseStat'] ) ) {
952 if ( count( $objects ) < $limit ) {
955 $after = end( $objects );
956 $after = is_object( $after ) ? $after->name : $after;
973 foreach ( $objects as $object ) {
974 if ( is_object( $object ) ) {
975 if ( isset( $object->subdir ) || !isset( $object->name ) ) {
981 'size' => (int)$object->bytes,
984 'md5' => ctype_xdigit( $object->hash ) ? $object->hash :
null,
987 $names[] = [ $object->name, $stat ];
988 } elseif ( substr( $object, -1 ) !==
'/' ) {
990 $names[] = [ $object, null ];
1004 $this->cheapCache->set( $path,
'stat', $val );
1010 if ( !isset( $stat[
'xattr'] ) ) {
1016 return $stat[
'xattr'];
1025 if ( !isset( $stat[
'sha1'] ) ) {
1031 return $stat[
'sha1'];
1043 if ( $srcRel ===
null ) {
1072 if ( empty(
$params[
'allowOB'] ) ) {
1074 call_user_func( $this->obResetFunc );
1077 $handle = fopen(
'php://output',
'wb' );
1078 list( $rcode, $rdesc, $rhdrs, $rbody, $rerr ) = $this->
http->run( [
1080 'url' => $this->
storageUrl( $auth, $srcCont, $srcRel ),
1083 'stream' => $handle,
1084 'flags' => [
'relayResponseHeaders' => empty(
$params[
'headless'] ) ]
1087 if ( $rcode >= 200 && $rcode <= 299 ) {
1089 } elseif ( $rcode === 404 ) {
1109 $ep = array_diff_key(
$params, [
'srcs' => 1 ] );
1114 foreach (
$params[
'srcs'] as $path ) {
1116 if ( $srcRel ===
null || !$auth ) {
1117 $tmpFiles[
$path] =
null;
1125 $handle = fopen( $tmpFile->getPath(),
'wb' );
1129 'url' => $this->
storageUrl( $auth, $srcCont, $srcRel ),
1132 'stream' => $handle,
1138 $tmpFiles[
$path] = $tmpFile;
1141 $isLatest = ( $this->isRGW || !empty(
$params[
'latest'] ) );
1142 $opts = [
'maxConnsPerHost' =>
$params[
'concurrency'] ];
1143 $reqs = $this->
http->runMulti( $reqs, $opts );
1144 foreach ( $reqs as $path => $op ) {
1145 list( $rcode, $rdesc, $rhdrs, $rbody, $rerr ) = $op[
'response'];
1146 fclose( $op[
'stream'] );
1147 if ( $rcode >= 200 && $rcode <= 299 ) {
1148 $size = $tmpFiles[
$path] ? $tmpFiles[
$path]->getSize() : 0;
1150 if ( $size != $rhdrs[
'content-length'] ) {
1151 $tmpFiles[
$path] =
null;
1152 $rerr =
"Got {$size}/{$rhdrs['content-length']} bytes";
1153 $this->
onError(
null, __METHOD__,
1154 [
'src' => $path ] + $ep, $rerr, $rcode, $rdesc );
1158 $stat[
'latest'] = $isLatest;
1159 $this->cheapCache->set( $path,
'stat', $stat );
1160 } elseif ( $rcode === 404 ) {
1161 $tmpFiles[
$path] =
false;
1163 $tmpFiles[
$path] =
null;
1164 $this->
onError(
null, __METHOD__,
1165 [
'src' => $path ] + $ep, $rerr, $rcode, $rdesc );
1173 if ( $this->swiftTempUrlKey !=
'' ||
1174 ( $this->rgwS3AccessKey !=
'' && $this->rgwS3SecretKey !=
'' )
1177 if ( $srcRel ===
null ) {
1187 $expires = time() + $ttl;
1189 if ( $this->swiftTempUrlKey !=
'' ) {
1190 $url = $this->
storageUrl( $auth, $srcCont, $srcRel );
1192 $contPath = parse_url( $this->
storageUrl( $auth, $srcCont ), PHP_URL_PATH );
1193 $signature = hash_hmac(
'sha1',
1194 "GET\n{$expires}\n{$contPath}/{$srcRel}",
1195 $this->swiftTempUrlKey
1198 return "{$url}?temp_url_sig={$signature}&temp_url_expires={$expires}";
1201 $spath =
'/' . rawurlencode( $srcCont ) .
'/' .
1202 str_replace(
'%2F',
'/', rawurlencode( $srcRel ) );
1204 $signature = base64_encode( hash_hmac(
1206 "GET\n\n\n{$expires}\n{$spath}",
1207 $this->rgwS3SecretKey,
1213 return str_replace(
'/swift/v1',
'', $this->
storageUrl( $auth ) . $spath ) .
1216 'Signature' => $signature,
1217 'Expires' => $expires,
1218 'AWSAccessKeyId' => $this->rgwS3AccessKey
1240 if ( !empty(
$params[
'latest'] ) ) {
1241 $hdrs[
'x-newest'] =
'true';
1258 foreach ( $fileOpHandles as $index => $fileOpHandle ) {
1259 $statuses[$index] = $this->
newStatus(
'backend-fail-connect', $this->name );
1266 $httpReqsByStage = [];
1267 foreach ( $fileOpHandles as $index => $fileOpHandle ) {
1269 $reqs = $fileOpHandle->httpOp;
1271 foreach ( $reqs as $stage => &
$req ) {
1272 list( $container, $relPath ) =
$req[
'url'];
1274 $req[
'headers'] = isset(
$req[
'headers'] ) ?
$req[
'headers'] : [];
1276 $httpReqsByStage[$stage][$index] =
$req;
1282 $reqCount = count( $httpReqsByStage );
1283 for ( $stage = 0; $stage < $reqCount; ++$stage ) {
1284 $httpReqs = $this->
http->runMulti( $httpReqsByStage[$stage] );
1285 foreach ( $httpReqs as $index => $httpReq ) {
1287 $callback = $fileOpHandles[$index]->callback;
1288 call_user_func_array( $callback, [ $httpReq, $statuses[$index] ] );
1291 if ( !$statuses[$index]->isOK() ) {
1292 $stages = count( $fileOpHandles[$index]->httpOp );
1293 for (
$s = ( $stage + 1 );
$s < $stages; ++
$s ) {
1294 unset( $httpReqsByStage[
$s][$index] );
1330 $status->fatal(
'backend-fail-connect', $this->name );
1335 list( $rcode, $rdesc, $rhdrs, $rbody, $rerr ) = $this->
http->run( [
1337 'url' => $this->
storageUrl( $auth, $container ),
1339 'x-container-read' => implode(
',', $readGrps ),
1340 'x-container-write' => implode(
',', $writeGrps )
1344 if ( $rcode != 204 && $rcode !== 202 ) {
1345 $status->fatal(
'backend-fail-internal', $this->name );
1346 $this->logger->error( __METHOD__ .
': unexpected rcode value (' . $rcode .
')' );
1363 if ( $bypassCache ) {
1364 $this->containerStatCache->clear( $container );
1365 } elseif ( !$this->containerStatCache->has( $container,
'stat' ) ) {
1368 if ( !$this->containerStatCache->has( $container,
'stat' ) ) {
1374 list( $rcode, $rdesc, $rhdrs, $rbody, $rerr ) = $this->
http->run( [
1376 'url' => $this->
storageUrl( $auth, $container ),
1380 if ( $rcode === 204 ) {
1382 'count' => $rhdrs[
'x-container-object-count'],
1383 'bytes' => $rhdrs[
'x-container-bytes-used']
1385 if ( $bypassCache ) {
1388 $this->containerStatCache->set( $container,
'stat', $stat );
1391 } elseif ( $rcode === 404 ) {
1394 $this->
onError(
null, __METHOD__,
1395 [
'cont' => $container ], $rerr, $rcode, $rdesc );
1401 return $this->containerStatCache->get( $container,
'stat' );
1416 $status->fatal(
'backend-fail-connect', $this->name );
1422 if ( empty(
$params[
'noAccess'] ) ) {
1429 list( $rcode, $rdesc, $rhdrs, $rbody, $rerr ) = $this->
http->run( [
1431 'url' => $this->
storageUrl( $auth, $container ),
1433 'x-container-read' => implode(
',', $readGrps ),
1434 'x-container-write' => implode(
',', $writeGrps )
1438 if ( $rcode === 201 ) {
1440 } elseif ( $rcode === 202 ) {
1461 $status->fatal(
'backend-fail-connect', $this->name );
1466 list( $rcode, $rdesc, $rhdrs, $rbody, $rerr ) = $this->
http->run( [
1467 'method' =>
'DELETE',
1468 'url' => $this->
storageUrl( $auth, $container ),
1472 if ( $rcode >= 200 && $rcode <= 299 ) {
1473 $this->containerStatCache->clear( $container );
1474 } elseif ( $rcode === 404 ) {
1476 } elseif ( $rcode === 409 ) {
1498 $fullCont,
$type, $limit, $after =
null, $prefix =
null, $delim =
null
1504 $status->fatal(
'backend-fail-connect', $this->name );
1509 $query = [
'limit' => $limit ];
1510 if (
$type ===
'info' ) {
1511 $query[
'format'] =
'json';
1513 if ( $after !==
null ) {
1514 $query[
'marker'] = $after;
1516 if ( $prefix !==
null ) {
1517 $query[
'prefix'] = $prefix;
1519 if ( $delim !==
null ) {
1520 $query[
'delimiter'] = $delim;
1523 list( $rcode, $rdesc, $rhdrs, $rbody, $rerr ) = $this->
http->run( [
1525 'url' => $this->
storageUrl( $auth, $fullCont ),
1530 $params = [
'cont' => $fullCont,
'prefix' => $prefix,
'delim' => $delim ];
1531 if ( $rcode === 200 ) {
1532 if (
$type ===
'info' ) {
1533 $status->value = FormatJson::decode( trim( $rbody ) );
1535 $status->value = explode(
"\n", trim( $rbody ) );
1537 } elseif ( $rcode === 204 ) {
1539 } elseif ( $rcode === 404 ) {
1549 foreach ( $containerInfo as $container => $info ) {
1550 $this->containerStatCache->set( $container,
'stat', $info );
1560 foreach (
$params[
'srcs'] as $path ) {
1562 if ( $srcRel ===
null ) {
1563 $stats[
$path] =
false;
1565 } elseif ( !$auth ) {
1566 $stats[
$path] =
null;
1572 if ( $cstat ===
false ) {
1573 $stats[
$path] =
false;
1575 } elseif ( !is_array( $cstat ) ) {
1576 $stats[
$path] =
null;
1582 'url' => $this->
storageUrl( $auth, $srcCont, $srcRel ),
1587 $opts = [
'maxConnsPerHost' =>
$params[
'concurrency'] ];
1588 $reqs = $this->
http->runMulti( $reqs, $opts );
1590 foreach (
$params[
'srcs'] as $path ) {
1591 if ( array_key_exists( $path, $stats ) ) {
1595 list( $rcode, $rdesc, $rhdrs, $rbody, $rerr ) = $reqs[
$path][
'response'];
1596 if ( $rcode === 200 || $rcode === 204 ) {
1601 if ( $this->isRGW ) {
1602 $stat[
'latest'] =
true;
1604 } elseif ( $rcode === 404 ) {
1608 $this->
onError(
null, __METHOD__,
$params, $rerr, $rcode, $rdesc );
1610 $stats[
$path] = $stat;
1624 $headers = $this->
sanitizeHdrs( [
'headers' => $rhdrs ] );
1630 'size' => isset( $rhdrs[
'content-length'] ) ? (int)$rhdrs[
'content-length'] : 0,
1631 'sha1' => isset( $metadata[
'sha1base36'] ) ? $metadata[
'sha1base36'] :
null,
1633 'md5' => ctype_xdigit( $rhdrs[
'etag'] ) ? $rhdrs[
'etag'] :
null,
1634 'xattr' => [
'metadata' => $metadata,
'headers' => $headers ]
1642 if ( $this->authErrorTimestamp !==
null ) {
1643 if ( ( time() - $this->authErrorTimestamp ) < 60 ) {
1646 $this->authErrorTimestamp =
null;
1652 if ( !$this->authCreds || $reAuth ) {
1653 $this->authSessionTimestamp = 0;
1655 $creds = $this->srvCache->get( $cacheKey );
1657 if ( isset( $creds[
'auth_token'] ) && isset( $creds[
'storage_url'] ) ) {
1658 $this->authCreds = $creds;
1660 $this->authSessionTimestamp = time() - ceil( $this->authTTL / 2 );
1662 list( $rcode, $rdesc, $rhdrs, $rbody, $rerr ) = $this->
http->run( [
1664 'url' =>
"{$this->swiftAuthUrl}/v1.0",
1666 'x-auth-user' => $this->swiftUser,
1667 'x-auth-key' => $this->swiftKey
1671 if ( $rcode >= 200 && $rcode <= 299 ) {
1672 $this->authCreds = [
1673 'auth_token' => $rhdrs[
'x-auth-token'],
1674 'storage_url' => ( $this->swiftStorageUrl !== null )
1675 ? $this->swiftStorageUrl
1676 : $rhdrs[
'x-storage-url']
1679 $this->srvCache->set( $cacheKey, $this->authCreds, ceil( $this->authTTL / 2 ) );
1680 $this->authSessionTimestamp = time();
1681 } elseif ( $rcode === 401 ) {
1682 $this->
onError(
null, __METHOD__, [],
"Authentication failed.", $rcode );
1683 $this->authErrorTimestamp = time();
1687 $this->
onError(
null, __METHOD__, [],
"HTTP return code: $rcode", $rcode );
1688 $this->authErrorTimestamp = time();
1694 if ( substr( $this->authCreds[
'storage_url'], -3 ) ===
'/v1' ) {
1695 $this->isRGW =
true;
1708 protected function storageUrl( array $creds, $container =
null, $object =
null ) {
1709 $parts = [ $creds[
'storage_url'] ];
1710 if ( strlen( $container ) ) {
1711 $parts[] = rawurlencode( $container );
1713 if ( strlen( $object ) ) {
1714 $parts[] = str_replace(
"%2F",
"/", rawurlencode( $object ) );
1717 return implode(
'/', $parts );
1725 return [
'x-auth-token' => $creds[
'auth_token'] ];
1735 return 'swiftcredentials:' . md5(
$username .
':' . $this->swiftAuthUrl );
1751 $status->fatal(
'backend-fail-internal', $this->name );
1753 if (
$code == 401 ) {
1756 $this->logger->error(
1757 "HTTP $code ($desc) in '{$func}' (given '" . FormatJson::encode(
$params ) .
"')" .
1758 ( $err ?
": $err" :
"" )
1826 $this->container = $fullCont;
1828 if ( substr( $this->dir, -1 ) ===
'/' ) {
1829 $this->dir = substr( $this->dir, 0, -1 );
1831 if ( $this->dir ==
'' ) {
1832 $this->suffixStart = 0;
1834 $this->suffixStart = strlen( $this->dir ) + 1;
1852 next( $this->bufferIter );
1856 if ( !$this->
valid() && count( $this->bufferIter ) ) {
1858 $this->container, $this->dir, $this->bufferAfter, self::PAGE_SIZE, $this->params
1868 $this->bufferAfter =
null;
1870 $this->container, $this->dir, $this->bufferAfter, self::PAGE_SIZE, $this->params
1879 if ( $this->bufferIter ===
null ) {
1882 return ( current( $this->bufferIter ) !==
false );
1908 return substr(
current( $this->bufferIter ), $this->suffixStart, -1 );
1925 list( $path, $stat ) =
current( $this->bufferIter );
1926 $relPath = substr( $path, $this->suffixStart );
1927 if ( is_array( $stat ) ) {
1928 $storageDir = rtrim( $this->params[
'dir'],
'/' );
1929 $this->backend->loadListingStatInternal(
"$storageDir/$relPath", $stat );
Apache License January http
interface is intended to be more or less compatible with the PHP memcached client.
A BagOStuff object with no objects in it.
File backend exception for checked exceptions (e.g.
FileBackendStore helper class for performing asynchronous file operations.
FileBackendStore $backend
Base class for all backends using particular storage medium.
setContainerCache( $container, array $val)
Set the cached info for a container.
executeOpHandlesInternal(array $fileOpHandles)
Execute a list of FileBackendStoreOpHandle handles in parallel.
getFileStat(array $params)
Get quick information about a file at a storage path in the backend.
resolveStoragePathReal( $storagePath)
Like resolveStoragePath() except null values are returned if the container is sharded and the shard c...
clearCache(array $paths=null)
Invalidate any in-process file stat and property cache.
primeContainerCache(array $items)
Do a batch lookup from cache for container stats for all containers used in a list of container names...
deleteFileCache( $path)
Delete the cached stat info for a file path.
getContentType( $storagePath, $content, $fsPath)
Get the content type to use in HEAD/GET requests for a file.
fileExists(array $params)
Check if a file exists at a storage path in the backend.
getLocalCopy(array $params)
Get a local copy on disk of the file at a storage path in the backend.
string $name
Unique backend name.
static extensionFromPath( $path, $case='lowercase')
Get the final extension from a storage or FS path.
getScopedFileLocks(array $paths, $type, StatusValue $status, $timeout=0)
Lock the files at the given storage paths in the backend.
newStatus()
Yields the result of the status wrapper callback on either:
scopedProfileSection( $section)
const ATTR_HEADERS
Bitfield flags for supported features.
Library for creating and parsing MW-style timestamps.
Class to handle concurrent HTTP requests.
Handles per process caching of items.
Generic operation result class Has warning/error list, boolean status and arbitrary value.
static send404Message( $fname, $flags=0)
Send out a standard 404 message for a file.
Iterator for listing directories.
pageFromList( $container, $dir, &$after, $limit, array $params)
Get the given list portion (page)
Iterator for listing regular files.
pageFromList( $container, $dir, &$after, $limit, array $params)
Get the given list portion (page)
SwiftFileBackend helper class to page through listings.
string $dir
Storage directory.
string $container
Container name.
__construct(SwiftFileBackend $backend, $fullCont, $dir, array $params)
string $bufferAfter
List items after this path.
pageFromList( $container, $dir, &$after, $limit, array $params)
Get the given list portion (page)
array $bufferIter
List of path or (path,stat array) entries.
SwiftFileBackend $backend
Class for an OpenStack Swift (or Ceph RGW) based file backend.
string $swiftUser
Swift user (account:user) to authenticate as.
sanitizeHdrs(array $params)
Sanitize and filter the custom headers from a $params array.
string $swiftAuthUrl
Authentication base URL (without version)
string $swiftTempUrlKey
Shared secret value for making temp URLs.
isPathUsableInternal( $storagePath)
Check if a file can be created or changed at a given storage path.
getDirListPageInternal( $fullCont, $dir, &$after, $limit, array $params)
Do not call this function outside of SwiftFileBackendFileList.
doPublishInternal( $fullCont, $dir, array $params)
doCreateInternal(array $params)
doGetFileStatMulti(array $params)
Get file stat information (concurrently if possible) for several files.
doGetFileSha1base36(array $params)
__construct(array $config)
doGetFileXAttributes(array $params)
onError( $status, $func, array $params, $err='', $code=0, $desc='')
Log an unexpected exception for this backend.
buildFileObjectListing(array $params, $dir, array $objects)
Build a list of file objects, filtering out any directories and extracting any stat info if provided ...
authTokenHeaders(array $creds)
getStatFromHeaders(array $rhdrs)
string $swiftStorageUrl
Override of storage base URL.
createContainer( $container, array $params)
Create a Swift container.
doCopyInternal(array $params)
getCredsCacheKey( $username)
Get the cache key for a container.
getDirectoryListInternal( $fullCont, $dir, array $params)
string $rgwS3AccessKey
S3 access key (RADOS Gateway)
getFileHttpUrl(array $params)
getCustomHeaders(array $rawHeaders)
int $authTTL
TTL in seconds.
headersFromParams(array $params)
Get headers to send to Swift when reading a file based on a FileBackend params array,...
getMetadata(array $rawHeaders)
bool $isRGW
Whether the server is an Ceph RGW.
addMissingMetadata(array $objHdrs, $path)
Fill in any missing object metadata and save it to Swift.
doStoreInternal(array $params)
int $authErrorTimestamp
UNIX timestamp.
getMetadataHeaders(array $rawHeaders)
int $authSessionTimestamp
UNIX timestamp.
loadListingStatInternal( $path, array $val)
Do not call this function outside of SwiftFileBackendFileList.
doPrepareInternal( $fullCont, $dir, array $params)
doSecureInternal( $fullCont, $dir, array $params)
getFileListInternal( $fullCont, $dir, array $params)
doMoveInternal(array $params)
getFeatures()
Get the a bitfield of extra features supported by the backend medium.
deleteContainer( $container, array $params)
Delete a Swift container.
setContainerAccess( $container, array $readGrps, array $writeGrps)
Set read/write permissions for a Swift container.
doGetFileStat(array $params)
doGetLocalCopyMulti(array $params)
string $rgwS3SecretKey
S3 authentication key (RADOS Gateway)
doGetFileContentsMulti(array $params)
storageUrl(array $creds, $container=null, $object=null)
convertSwiftDate( $ts, $format=TS_MW)
Convert dates like "Tue, 03 Jan 2012 22:01:04 GMT"/"2013-05-11T07:37:27.678360Z".
doStreamFile(array $params)
doPrimeContainerCache(array $containerInfo)
Fill the backend-specific process cache given an array of resolved container names and their correspo...
resolveContainerPath( $container, $relStoragePath)
Resolve a relative storage path, checking if it's allowed by the backend.
doCleanInternal( $fullCont, $dir, array $params)
ProcessCacheLRU $containerStatCache
Container stat cache.
getFileListPageInternal( $fullCont, $dir, &$after, $limit, array $params)
Do not call this function outside of SwiftFileBackendFileList.
string $swiftKey
Secret key for user.
doDirectoryExists( $fullCont, $dir, array $params)
objectListing( $fullCont, $type, $limit, $after=null, $prefix=null, $delim=null)
Get a list of objects under a container.
directoriesAreVirtual()
Is this a key/value store where directories are just virtual? Virtual directories exists in so much a...
doExecuteOpHandlesInternal(array $fileOpHandles)
doDeleteInternal(array $params)
doDescribeInternal(array $params)
getContainerStat( $container, $bypassCache=false)
Get a Swift container stat array, possibly from process cache.
array $httpOp
List of Requests for MultiHttpClient.
__construct(SwiftFileBackend $backend, Closure $callback, array $httpOp)
static factory( $prefix, $extension='', $tmpDirectory=null)
Make a new temporary file on the file system.
Multi-datacenter aware caching interface.
deferred txt A few of the database updates required by various functions here can be deferred until after the result page is displayed to the user For updating the view updating the linked to tables after a etc PHP does not yet have any way to tell the server to actually return and disconnect while still running these but it might have such a feature in the future We handle these by creating a deferred update object and putting those objects on a global list
this hook is for auditing only $req
do that in ParserLimitReportFormat instead use this to modify the parameters of the image all existing parser cache entries will be invalid To avoid you ll need to handle that somehow(e.g. with the RejectParserCacheValue hook) because MediaWiki won 't do it for you. & $defaults also a ContextSource after deleting those rows but within the same transaction you ll probably need to make sure the header is varied on $request
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that probably a stub it is not rendered in wiki pages or galleries in category pages allow injecting custom HTML after the section Any uses of the hook need to handle escaping see BaseTemplate::getToolbox and BaseTemplate::makeListItem for details on the format of individual items inside of this array or by returning and letting standard HTTP rendering take place modifiable or by returning false and taking over the output modifiable & $code
it s the revision text itself In either if gzip is the revision text is gzipped $flags
this hook is for auditing only or null if authentication failed before getting that far $username
Status::newGood()` to allow deletion, and then `return false` from the hook function. Ensure you consume the 'ChangeTagAfterDelete' hook to carry out custom deletion actions. $tag:name of the tag $user:user initiating the action & $status:Status object. See above. 'ChangeTagsListActive':Allows you to nominate which of the tags your extension uses are in active use. & $tags:list of all active tags. Append to this array. 'ChangeTagsAfterUpdateTags':Called after tags have been updated with the ChangeTags::updateTags function. Params:$addedTags:tags effectively added in the update $removedTags:tags effectively removed in the update $prevTags:tags that were present prior to the update $rc_id:recentchanges table id $rev_id:revision table id $log_id:logging table id $params:tag params $rc:RecentChange being tagged when the tagging accompanies the action or null $user:User who performed the tagging when the tagging is subsequent to the action or null 'ChangeTagsAllowedAdd':Called when checking if a user can add tags to a change. & $allowedTags:List of all the tags the user is allowed to add. Any tags the user wants to add( $addTags) that are not in this array will cause it to fail. You may add or remove tags to this array as required. $addTags:List of tags user intends to add. $user:User who is adding the tags. 'ChangeUserGroups':Called before user groups are changed. $performer:The User who will perform the change $user:The User whose groups will be changed & $add:The groups that will be added & $remove:The groups that will be removed 'Collation::factory':Called if $wgCategoryCollation is an unknown collation. $collationName:Name of the collation in question & $collationObject:Null. Replace with a subclass of the Collation class that implements the collation given in $collationName. 'ConfirmEmailComplete':Called after a user 's email has been confirmed successfully. $user:user(object) whose email is being confirmed 'ContentAlterParserOutput':Modify parser output for a given content object. Called by Content::getParserOutput after parsing has finished. Can be used for changes that depend on the result of the parsing but have to be done before LinksUpdate is called(such as adding tracking categories based on the rendered HTML). $content:The Content to render $title:Title of the page, as context $parserOutput:ParserOutput to manipulate 'ContentGetParserOutput':Customize parser output for a given content object, called by AbstractContent::getParserOutput. May be used to override the normal model-specific rendering of page content. $content:The Content to render $title:Title of the page, as context $revId:The revision ID, as context $options:ParserOptions for rendering. To avoid confusing the parser cache, the output can only depend on parameters provided to this hook function, not on global state. $generateHtml:boolean, indicating whether full HTML should be generated. If false, generation of HTML may be skipped, but other information should still be present in the ParserOutput object. & $output:ParserOutput, to manipulate or replace 'ContentHandlerDefaultModelFor':Called when the default content model is determined for a given title. May be used to assign a different model for that title. $title:the Title in question & $model:the model name. Use with CONTENT_MODEL_XXX constants. 'ContentHandlerForModelID':Called when a ContentHandler is requested for a given content model name, but no entry for that model exists in $wgContentHandlers. Note:if your extension implements additional models via this hook, please use GetContentModels hook to make them known to core. $modeName:the requested content model name & $handler:set this to a ContentHandler object, if desired. 'ContentModelCanBeUsedOn':Called to determine whether that content model can be used on a given page. This is especially useful to prevent some content models to be used in some special location. $contentModel:ID of the content model in question $title:the Title in question. & $ok:Output parameter, whether it is OK to use $contentModel on $title. Handler functions that modify $ok should generally return false to prevent further hooks from further modifying $ok. 'ContribsPager::getQueryInfo':Before the contributions query is about to run & $pager:Pager object for contributions & $queryInfo:The query for the contribs Pager 'ContribsPager::reallyDoQuery':Called before really executing the query for My Contributions & $data:an array of results of all contribs queries $pager:The ContribsPager object hooked into $offset:Index offset, inclusive $limit:Exact query limit $descending:Query direction, false for ascending, true for descending 'ContributionsLineEnding':Called before a contributions HTML line is finished $page:SpecialPage object for contributions & $ret:the HTML line $row:the DB row for this line & $classes:the classes to add to the surrounding< li > & $attribs:associative array of other HTML attributes for the< li > element. Currently only data attributes reserved to MediaWiki are allowed(see Sanitizer::isReservedDataAttribute). 'ContributionsToolLinks':Change tool links above Special:Contributions $id:User identifier $title:User page title & $tools:Array of tool links $specialPage:SpecialPage instance for context and services. Can be either SpecialContributions or DeletedContributionsPage. Extensions should type hint against a generic SpecialPage though. 'ConvertContent':Called by AbstractContent::convert when a conversion to another content model is requested. Handler functions that modify $result should generally return false to disable further attempts at conversion. $content:The Content object to be converted. $toModel:The ID of the content model to convert to. $lossy: boolean indicating whether lossy conversion is allowed. & $result:Output parameter, in case the handler function wants to provide a converted Content object. Note that $result->getContentModel() must return $toModel. 'CustomEditor':When invoking the page editor Return true to allow the normal editor to be used, or false if implementing a custom editor, e.g. for a special namespace, etc. $article:Article being edited $user:User performing the edit 'DatabaseOraclePostInit':Called after initialising an Oracle database $db:the DatabaseOracle object 'DeletedContribsPager::reallyDoQuery':Called before really executing the query for Special:DeletedContributions Similar to ContribsPager::reallyDoQuery & $data:an array of results of all contribs queries $pager:The DeletedContribsPager object hooked into $offset:Index offset, inclusive $limit:Exact query limit $descending:Query direction, false for ascending, true for descending 'DeletedContributionsLineEnding':Called before a DeletedContributions HTML line is finished. Similar to ContributionsLineEnding $page:SpecialPage object for DeletedContributions & $ret:the HTML line $row:the DB row for this line & $classes:the classes to add to the surrounding< li > & $attribs:associative array of other HTML attributes for the< li > element. Currently only data attributes reserved to MediaWiki are allowed(see Sanitizer::isReservedDataAttribute). 'DifferenceEngineAfterLoadNewText':called in DifferenceEngine::loadNewText() after the new revision 's content has been loaded into the class member variable $differenceEngine->mNewContent but before returning true from this function. $differenceEngine:DifferenceEngine object 'DifferenceEngineLoadTextAfterNewContentIsLoaded':called in DifferenceEngine::loadText() after the new revision 's content has been loaded into the class member variable $differenceEngine->mNewContent but before checking if the variable 's value is null. This hook can be used to inject content into said class member variable. $differenceEngine:DifferenceEngine object 'DifferenceEngineMarkPatrolledLink':Allows extensions to change the "mark as patrolled" link which is shown both on the diff header as well as on the bottom of a page, usually wrapped in a span element which has class="patrollink". $differenceEngine:DifferenceEngine object & $markAsPatrolledLink:The "mark as patrolled" link HTML(string) $rcid:Recent change ID(rc_id) for this change(int) 'DifferenceEngineMarkPatrolledRCID':Allows extensions to possibly change the rcid parameter. For example the rcid might be set to zero due to the user being the same as the performer of the change but an extension might still want to show it under certain conditions. & $rcid:rc_id(int) of the change or 0 $differenceEngine:DifferenceEngine object $change:RecentChange object $user:User object representing the current user 'DifferenceEngineNewHeader':Allows extensions to change the $newHeader variable, which contains information about the new revision, such as the revision 's author, whether the revision was marked as a minor edit or not, etc. $differenceEngine:DifferenceEngine object & $newHeader:The string containing the various #mw-diff-otitle[1-5] divs, which include things like revision author info, revision comment, RevisionDelete link and more $formattedRevisionTools:Array containing revision tools, some of which may have been injected with the DiffRevisionTools hook $nextlink:String containing the link to the next revision(if any) $status
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that probably a stub it is not rendered in wiki pages or galleries in category pages allow injecting custom HTML after the section Any uses of the hook need to handle escaping see BaseTemplate::getToolbox and BaseTemplate::makeListItem for details on the format of individual items inside of this array or by returning and letting standard HTTP rendering take place modifiable or by returning false and taking over the output modifiable modifiable after all normalizations have been except for the $wgMaxImageArea check set to true or false to override the $wgMaxImageArea check result gives extension the possibility to transform it themselves $handler
null for the local wiki Added should default to null in handler for backwards compatibility add a value to it if you want to add a cookie that have to vary cache options can modify $query
processing should stop and the error should be shown to the user * false
returning false will NOT prevent logging $e