1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775
|
<?php
/**
* @defgroup FileBackend File backend
*
* File backend is used to interact with file storage systems,
* such as the local file system, NFS, or cloud storage systems.
* See [the architecture doc](@ref filebackendarch) for more information.
*/
/**
* Base class for all file backends.
*
* 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
* @ingroup FileBackend
*/
namespace Wikimedia\FileBackend;
use InvalidArgumentException;
use LockManager;
use MediaWiki\FileBackend\FSFile\TempFSFileFactory;
use NullLockManager;
use Psr\Log\LoggerAwareInterface;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
use ScopedLock;
use Shellbox\Command\BoxedCommand;
use StatusValue;
use Wikimedia\FileBackend\FSFile\FSFile;
use Wikimedia\FileBackend\FSFile\TempFSFile;
use Wikimedia\ScopedCallback;
/**
* @brief Base class for all file backend classes (including multi-write backends).
*
* This class defines the methods as abstract that subclasses must implement.
* Outside callers can assume that all backends will have these functions.
*
* All "storage paths" are of the format "mwstore://<backend>/<container>/<path>".
* The "backend" portion is unique name for the application to refer to a backend, while
* the "container" portion is a top-level directory of the backend. The "path" portion
* is a relative path that uses UNIX file system (FS) notation, though any particular
* backend may not actually be using a local filesystem. Therefore, the relative paths
* are only virtual.
*
* Backend contents are stored under "domain"-specific container names by default.
* A domain is simply a logical umbrella for entities, such as those belonging to a certain
* application or portion of a website, for example. A domain can be local or global.
* Global (qualified) backends are achieved by configuring the "domain ID" to a constant.
* Global domains are simpler, but local domains can be used by choosing a domain ID based on
* the current context, such as which language of a website is being used.
*
* For legacy reasons, the FSFileBackend class allows manually setting the paths of
* containers to ones that do not respect the "domain ID".
*
* In key/value (object) stores, containers are the only hierarchy (the rest is emulated).
* FS-based backends are somewhat more restrictive due to the existence of real
* directory files; a regular file cannot have the same name as a directory. Other
* backends with virtual directories may not have this limitation. Callers should
* store files in such a way that no files and directories are under the same path.
*
* In general, this class allows for callers to access storage through the same
* interface, without regard to the underlying storage system. However, calling code
* must follow certain patterns and be aware of certain things to ensure compatibility:
* - a) Always call prepare() on the parent directory before trying to put a file there;
* key/value stores only need the container to exist first, but filesystems need
* all the parent directories to exist first (prepare() is aware of all this)
* - b) Always call clean() on a directory when it might become empty to avoid empty
* directory buildup on filesystems; key/value stores never have empty directories,
* so doing this helps preserve consistency in both cases
* - c) Likewise, do not rely on the existence of empty directories for anything;
* calling directoryExists() on a path that prepare() was previously called on
* will return false for key/value stores if there are no files under that path
* - d) Never alter the resulting FSFile returned from getLocalReference(), as it could
* either be a copy of the source file in /tmp or the original source file itself
* - e) Use a file layout that results in never attempting to store files over directories
* or directories over files; key/value stores allow this but filesystems do not
* - f) Use ASCII file names (e.g. base32, IDs, hashes) to avoid Unicode issues in Windows
* - g) Do not assume that move operations are atomic (difficult with key/value stores)
* - h) Do not assume that file stat or read operations always have immediate consistency;
* various methods have a "latest" flag that should always be used if up-to-date
* information is required (this trades performance for correctness as needed)
* - i) Do not assume that directory listings have immediate consistency
*
* Methods of subclasses should avoid throwing exceptions at all costs.
* As a corollary, external dependencies should be kept to a minimum.
*
* See [the architecture doc](@ref filebackendarch) for more information.
*
* @stable to extend
*
* @ingroup FileBackend
* @since 1.19
*/
abstract class FileBackend implements LoggerAwareInterface {
/** @var string Unique backend name */
protected $name;
/** @var string Unique domain name */
protected $domainId;
/** @var string Read-only explanation message */
protected $readOnly;
/** @var string When to do operations in parallel */
protected $parallelize;
/** @var int How many operations can be done in parallel */
protected $concurrency;
/** @var TempFSFileFactory */
protected $tmpFileFactory;
/** @var LockManager */
protected $lockManager;
/** @var LoggerInterface */
protected $logger;
/** @var callable|null */
protected $profiler;
/** @var callable */
private $obResetFunc;
/** @var callable */
private $headerFunc;
/** @var array Option map for use with HTTPFileStreamer */
protected $streamerOptions;
/** @var callable|null */
protected $statusWrapper;
/** Bitfield flags for supported features */
public const ATTR_HEADERS = 1; // files can be tagged with standard HTTP headers
public const ATTR_METADATA = 2; // files can be stored with metadata key/values
public const ATTR_UNICODE_PATHS = 4; // files can have Unicode paths (not just ASCII)
/** @var false Idiom for "no info; non-existant file" (since 1.34) */
protected const STAT_ABSENT = false;
/** @var null Idiom for "no info; I/O errors" (since 1.34) */
public const STAT_ERROR = null;
/** @var null Idiom for "no file/directory list; I/O errors" (since 1.34) */
public const LIST_ERROR = null;
/** @var null Idiom for "no temp URL; not supported or I/O errors" (since 1.34) */
public const TEMPURL_ERROR = null;
/** @var null Idiom for "existence unknown; I/O errors" (since 1.34) */
public const EXISTENCE_ERROR = null;
/** @var false Idiom for "no timestamp; missing file or I/O errors" (since 1.34) */
public const TIMESTAMP_FAIL = false;
/** @var false Idiom for "no content; missing file or I/O errors" (since 1.34) */
public const CONTENT_FAIL = false;
/** @var false Idiom for "no metadata; missing file or I/O errors" (since 1.34) */
public const XATTRS_FAIL = false;
/** @var false Idiom for "no size; missing file or I/O errors" (since 1.34) */
public const SIZE_FAIL = false;
/** @var false Idiom for "no SHA1 hash; missing file or I/O errors" (since 1.34) */
public const SHA1_FAIL = false;
/**
* Create a new backend instance from configuration.
* This should only be called from within FileBackendGroup.
* @stable to call
*
* @param array $config Parameters include:
* - name : The unique name of this backend.
* This should consist of alphanumberic, '-', and '_' characters.
* This name should not be changed after use.
* Note that the name is *not* used in actual container names.
* - domainId : Prefix to container names that is unique to this backend.
* It should only consist of alphanumberic, '-', and '_' characters.
* This ID is what avoids collisions if multiple logical backends
* use the same storage system, so this should be set carefully.
* - lockManager : LockManager object to use for any file locking.
* If not provided, then no file locking will be enforced.
* - readOnly : Write operations are disallowed if this is a non-empty string.
* It should be an explanation for the backend being read-only.
* - parallelize : When to do file operations in parallel (when possible).
* Allowed values are "implicit", "explicit" and "off".
* - concurrency : How many file operations can be done in parallel.
* - tmpDirectory : Directory to use for temporary files.
* - tmpFileFactory : Optional TempFSFileFactory object. Only has an effect if
* tmpDirectory is not set. If both are unset or null, then the backend will
* try to discover a usable temporary directory.
* - obResetFunc : alternative callback to clear the output buffer
* - streamMimeFunc : alternative method to determine the content type from the path
* - headerFunc : alternative callback for sending response headers
* - logger : Optional PSR logger object.
* - profiler : Optional callback that takes a section name argument and returns
* a ScopedCallback instance that ends the profile section in its destructor.
* - statusWrapper : Optional callback that is used to wrap returned StatusValues
*/
public function __construct( array $config ) {
if ( !array_key_exists( 'name', $config ) ) {
throw new InvalidArgumentException( 'Backend name not specified.' );
}
$this->name = $config['name'];
$this->domainId = $config['domainId'] // e.g. "my_wiki-en_"
?? $config['wikiId'] // b/c alias
?? null;
if ( !is_string( $this->name ) || !preg_match( '!^[a-zA-Z0-9-_]{1,255}$!', $this->name ) ) {
throw new InvalidArgumentException( "Backend name '{$this->name}' is invalid." );
}
if ( !is_string( $this->domainId ) ) {
throw new InvalidArgumentException(
"Backend domain ID not provided for '{$this->name}'." );
}
$this->lockManager = $config['lockManager'] ?? new NullLockManager( [] );
$this->readOnly = isset( $config['readOnly'] )
? (string)$config['readOnly']
: '';
$this->parallelize = isset( $config['parallelize'] )
? (string)$config['parallelize']
: 'off';
$this->concurrency = isset( $config['concurrency'] )
? (int)$config['concurrency']
: 50;
$this->obResetFunc = $config['obResetFunc']
?? [ self::class, 'resetOutputBufferTheDefaultWay' ];
$this->headerFunc = $config['headerFunc'] ?? 'header';
$this->streamerOptions = [
'obResetFunc' => $this->obResetFunc,
'headerFunc' => $this->headerFunc,
'streamMimeFunc' => $config['streamMimeFunc'] ?? null,
];
$this->profiler = $config['profiler'] ?? null;
if ( !is_callable( $this->profiler ) ) {
$this->profiler = null;
}
$this->logger = $config['logger'] ?? new NullLogger();
$this->statusWrapper = $config['statusWrapper'] ?? null;
// tmpDirectory gets precedence for backward compatibility
if ( isset( $config['tmpDirectory'] ) ) {
$this->tmpFileFactory = new TempFSFileFactory( $config['tmpDirectory'] );
} else {
$this->tmpFileFactory = $config['tmpFileFactory'] ?? new TempFSFileFactory();
}
}
protected function header( $header ) {
( $this->headerFunc )( $header );
}
protected function resetOutputBuffer() {
// By default, this ends up calling $this->defaultOutputBufferReset
( $this->obResetFunc )();
}
public function setLogger( LoggerInterface $logger ) {
$this->logger = $logger;
}
/**
* Get the unique backend name
*
* We may have multiple different backends of the same type.
* For example, we can have two Swift backends using different proxies.
*
* @return string
*/
final public function getName() {
return $this->name;
}
/**
* Get the domain identifier used for this backend (possibly empty).
*
* @return string
* @since 1.28
*/
final public function getDomainId() {
return $this->domainId;
}
/**
* Alias to getDomainId()
*
* @return string
* @since 1.20
* @deprecated Since 1.34 Use getDomainId()
*/
final public function getWikiId() {
return $this->getDomainId();
}
/**
* Check if this backend is read-only
*
* @return bool
*/
final public function isReadOnly() {
return ( $this->readOnly != '' );
}
/**
* Get an explanatory message if this backend is read-only
*
* @return string|bool Returns false if the backend is not read-only
*/
final public function getReadOnlyReason() {
return ( $this->readOnly != '' ) ? $this->readOnly : false;
}
/**
* Get the a bitfield of extra features supported by the backend medium
* @stable to override
*
* @return int Bitfield of FileBackend::ATTR_* flags
* @since 1.23
*/
public function getFeatures() {
return self::ATTR_UNICODE_PATHS;
}
/**
* Check if the backend medium supports a field of extra features
*
* @param int $bitfield Bitfield of FileBackend::ATTR_* flags
* @return bool
* @since 1.23
*/
final public function hasFeatures( $bitfield ) {
return ( $this->getFeatures() & $bitfield ) === $bitfield;
}
/**
* This is the main entry point into the backend for write operations.
* Callers supply an ordered list of operations to perform as a transaction.
* Files will be locked, the stat cache cleared, and then the operations attempted.
* If any serious errors occur, all attempted operations will be rolled back.
*
* $ops is an array of arrays. The outer array holds a list of operations.
* Each inner array is a set of key value pairs that specify an operation.
*
* Supported operations and their parameters. The supported actions are:
* - create
* - store
* - copy
* - move
* - delete
* - describe (since 1.21)
* - null
*
* FSFile/TempFSFile object support was added in 1.27.
*
* a) Create a new file in storage with the contents of a string
* @code
* [
* 'op' => 'create',
* 'dst' => <storage path>,
* 'content' => <string of new file contents>,
* 'overwrite' => <boolean>,
* 'overwriteSame' => <boolean>,
* 'headers' => <HTTP header name/value map> # since 1.21
* ]
* @endcode
*
* b) Copy a file system file into storage
* @code
* [
* 'op' => 'store',
* 'src' => <file system path, FSFile, or TempFSFile>,
* 'dst' => <storage path>,
* 'overwrite' => <boolean>,
* 'overwriteSame' => <boolean>,
* 'headers' => <HTTP header name/value map> # since 1.21
* ]
* @endcode
*
* c) Copy a file within storage
* @code
* [
* 'op' => 'copy',
* 'src' => <storage path>,
* 'dst' => <storage path>,
* 'overwrite' => <boolean>,
* 'overwriteSame' => <boolean>,
* 'ignoreMissingSource' => <boolean>, # since 1.21
* 'headers' => <HTTP header name/value map> # since 1.21
* ]
* @endcode
*
* d) Move a file within storage
* @code
* [
* 'op' => 'move',
* 'src' => <storage path>,
* 'dst' => <storage path>,
* 'overwrite' => <boolean>,
* 'overwriteSame' => <boolean>,
* 'ignoreMissingSource' => <boolean>, # since 1.21
* 'headers' => <HTTP header name/value map> # since 1.21
* ]
* @endcode
*
* e) Delete a file within storage
* @code
* [
* 'op' => 'delete',
* 'src' => <storage path>,
* 'ignoreMissingSource' => <boolean>
* ]
* @endcode
*
* f) Update metadata for a file within storage
* @code
* [
* 'op' => 'describe',
* 'src' => <storage path>,
* 'headers' => <HTTP header name/value map>
* ]
* @endcode
*
* g) Do nothing (no-op)
* @code
* [
* 'op' => 'null',
* ]
* @endcode
*
* Boolean flags for operations (operation-specific):
* - ignoreMissingSource : The operation will simply succeed and do
* nothing if the source file does not exist.
* - overwrite : Any destination file will be overwritten.
* - overwriteSame : If a file already exists at the destination with the
* same contents, then do nothing to the destination file
* instead of giving an error. This does not compare headers.
* This option is ignored if 'overwrite' is already provided.
* - headers : If supplied, the result of merging these headers with any
* existing source file headers (replacing conflicting ones)
* will be set as the destination file headers. Headers are
* deleted if their value is set to the empty string. When a
* file has headers they are included in responses to GET and
* HEAD requests to the backing store for that file.
* Header values should be no larger than 255 bytes, except for
* Content-Disposition. The system might ignore or truncate any
* headers that are too long to store (exact limits will vary).
* Backends that don't support metadata ignore this. (since 1.21)
*
* $opts is an associative of boolean flags, including:
* - force : Operation precondition errors no longer trigger an abort.
* Any remaining operations are still attempted. Unexpected
* failures may still cause remaining operations to be aborted.
* - nonLocking : No locks are acquired for the operations.
* This can increase performance for non-critical writes.
* This has no effect unless the 'force' flag is set.
* - parallelize : Try to do operations in parallel when possible.
* - bypassReadOnly : Allow writes in read-only mode. (since 1.20)
* - preserveCache : Don't clear the process cache before checking files.
* This should only be used if all entries in the process
* cache were added after the files were already locked. (since 1.20)
*
* @note Remarks on locking:
* File system paths given to operations should refer to files that are
* already locked or otherwise safe from modification from other processes.
* Normally these files will be new temp files, which should be adequate.
*
* @par Return value:
*
* This returns a Status, which contains all warnings and fatals that occurred
* during the operation. The 'failCount', 'successCount', and 'success' members
* will reflect each operation attempted.
*
* The StatusValue will be "OK" unless:
* - a) unexpected operation errors occurred (network partitions, disk full...)
* - b) predicted operation errors occurred and 'force' was not set
*
* @param array[] $ops List of operations to execute in order
* @phan-param array<int,array{ignoreMissingSource?:bool,overwrite?:bool,overwriteSame?:bool,headers?:bool}> $ops
* @param array $opts Batch operation options
* @phpcs:ignore Generic.Files.LineLength
* @phan-param array{force?:bool,nonLocking?:bool,parallelize?:bool,bypassReadOnly?:bool,preserveCache?:bool} $opts
* @return StatusValue
*/
final public function doOperations( array $ops, array $opts = [] ) {
if ( empty( $opts['bypassReadOnly'] ) && $this->isReadOnly() ) {
return $this->newStatus( 'backend-fail-readonly', $this->name, $this->readOnly );
}
if ( $ops === [] ) {
return $this->newStatus(); // nothing to do
}
$ops = $this->resolveFSFileObjects( $ops );
if ( empty( $opts['force'] ) ) {
unset( $opts['nonLocking'] );
}
/** @noinspection PhpUnusedLocalVariableInspection */
$scope = ScopedCallback::newScopedIgnoreUserAbort(); // try to ignore client aborts
return $this->doOperationsInternal( $ops, $opts );
}
/**
* @see FileBackend::doOperations()
* @param array $ops
* @param array $opts
* @return StatusValue
*/
abstract protected function doOperationsInternal( array $ops, array $opts );
/**
* Same as doOperations() except it takes a single operation.
* If you are doing a batch of operations that should either
* all succeed or all fail, then use that function instead.
*
* @see FileBackend::doOperations()
*
* @param array $op Operation
* @param array $opts Operation options
* @return StatusValue
*/
final public function doOperation( array $op, array $opts = [] ) {
return $this->doOperations( [ $op ], $opts );
}
/**
* Performs a single create operation.
* This sets $params['op'] to 'create' and passes it to doOperation().
*
* @see FileBackend::doOperation()
*
* @param array $params Operation parameters
* @param array $opts Operation options
* @return StatusValue
*/
final public function create( array $params, array $opts = [] ) {
return $this->doOperation( [ 'op' => 'create' ] + $params, $opts );
}
/**
* Performs a single store operation.
* This sets $params['op'] to 'store' and passes it to doOperation().
*
* @see FileBackend::doOperation()
*
* @param array $params Operation parameters
* @param array $opts Operation options
* @return StatusValue
*/
final public function store( array $params, array $opts = [] ) {
return $this->doOperation( [ 'op' => 'store' ] + $params, $opts );
}
/**
* Performs a single copy operation.
* This sets $params['op'] to 'copy' and passes it to doOperation().
*
* @see FileBackend::doOperation()
*
* @param array $params Operation parameters
* @param array $opts Operation options
* @return StatusValue
*/
final public function copy( array $params, array $opts = [] ) {
return $this->doOperation( [ 'op' => 'copy' ] + $params, $opts );
}
/**
* Performs a single move operation.
* This sets $params['op'] to 'move' and passes it to doOperation().
*
* @see FileBackend::doOperation()
*
* @param array $params Operation parameters
* @param array $opts Operation options
* @return StatusValue
*/
final public function move( array $params, array $opts = [] ) {
return $this->doOperation( [ 'op' => 'move' ] + $params, $opts );
}
/**
* Performs a single delete operation.
* This sets $params['op'] to 'delete' and passes it to doOperation().
*
* @see FileBackend::doOperation()
*
* @param array $params Operation parameters
* @param array $opts Operation options
* @return StatusValue
*/
final public function delete( array $params, array $opts = [] ) {
return $this->doOperation( [ 'op' => 'delete' ] + $params, $opts );
}
/**
* Performs a single describe operation.
* This sets $params['op'] to 'describe' and passes it to doOperation().
*
* @see FileBackend::doOperation()
*
* @param array $params Operation parameters
* @param array $opts Operation options
* @return StatusValue
* @since 1.21
*/
final public function describe( array $params, array $opts = [] ) {
return $this->doOperation( [ 'op' => 'describe' ] + $params, $opts );
}
/**
* Perform a set of independent file operations on some files.
*
* This does no locking, and possibly no stat calls.
* Any destination files that already exist will be overwritten.
* This should *only* be used on non-original files, like cache files.
*
* Supported operations and their parameters:
* - create
* - store
* - copy
* - move
* - delete
* - describe (since 1.21)
* - null
*
* FSFile/TempFSFile object support was added in 1.27.
*
* a) Create a new file in storage with the contents of a string
* @code
* [
* 'op' => 'create',
* 'dst' => <storage path>,
* 'content' => <string of new file contents>,
* 'headers' => <HTTP header name/value map> # since 1.21
* ]
* @endcode
*
* b) Copy a file system file into storage
* @code
* [
* 'op' => 'store',
* 'src' => <file system path, FSFile, or TempFSFile>,
* 'dst' => <storage path>,
* 'headers' => <HTTP header name/value map> # since 1.21
* ]
* @endcode
*
* c) Copy a file within storage
* @code
* [
* 'op' => 'copy',
* 'src' => <storage path>,
* 'dst' => <storage path>,
* 'ignoreMissingSource' => <boolean>, # since 1.21
* 'headers' => <HTTP header name/value map> # since 1.21
* ]
* @endcode
*
* d) Move a file within storage
* @code
* [
* 'op' => 'move',
* 'src' => <storage path>,
* 'dst' => <storage path>,
* 'ignoreMissingSource' => <boolean>, # since 1.21
* 'headers' => <HTTP header name/value map> # since 1.21
* ]
* @endcode
*
* e) Delete a file within storage
* @code
* [
* 'op' => 'delete',
* 'src' => <storage path>,
* 'ignoreMissingSource' => <boolean>
* ]
* @endcode
*
* f) Update metadata for a file within storage
* @code
* [
* 'op' => 'describe',
* 'src' => <storage path>,
* 'headers' => <HTTP header name/value map>
* ]
* @endcode
*
* g) Do nothing (no-op)
* @code
* [
* 'op' => 'null',
* ]
* @endcode
*
* @par Boolean flags for operations (operation-specific):
* - ignoreMissingSource : The operation will simply succeed and do
* nothing if the source file does not exist.
* - headers : If supplied with a header name/value map, the backend will
* reply with these headers when GETs/HEADs of the destination
* file are made. Header values should be smaller than 256 bytes.
* Content-Disposition headers can be longer, though the system
* might ignore or truncate ones that are too long to store.
* Existing headers will remain, but these will replace any
* conflicting previous headers, and headers will be removed
* if they are set to an empty string.
* Backends that don't support metadata ignore this. (since 1.21)
*
* $opts is an associative of boolean flags, including:
* - bypassReadOnly : Allow writes in read-only mode (since 1.20)
*
* @par Return value:
* This returns a Status, which contains all warnings and fatals that occurred
* during the operation. The 'failCount', 'successCount', and 'success' members
* will reflect each operation attempted for the given files. The StatusValue will be
* considered "OK" as long as no fatal errors occurred.
*
* @param array $ops Set of operations to execute
* @phan-param list<array{op:?string,src?:string,dst?:string,ignoreMissingSource?:bool,headers?:array}> $ops
* @param array $opts Batch operation options
* @phan-param array{bypassReadOnly?:bool} $opts
* @return StatusValue
* @since 1.20
*/
final public function doQuickOperations( array $ops, array $opts = [] ) {
if ( empty( $opts['bypassReadOnly'] ) && $this->isReadOnly() ) {
return $this->newStatus( 'backend-fail-readonly', $this->name, $this->readOnly );
}
if ( $ops === [] ) {
return $this->newStatus(); // nothing to do
}
$ops = $this->resolveFSFileObjects( $ops );
foreach ( $ops as &$op ) {
$op['overwrite'] = true; // avoids RTTs in key/value stores
}
/** @noinspection PhpUnusedLocalVariableInspection */
$scope = ScopedCallback::newScopedIgnoreUserAbort(); // try to ignore client aborts
return $this->doQuickOperationsInternal( $ops, $opts );
}
/**
* @see FileBackend::doQuickOperations()
* @param array $ops
* @param array $opts
* @return StatusValue
* @since 1.20
*/
abstract protected function doQuickOperationsInternal( array $ops, array $opts );
/**
* Same as doQuickOperations() except it takes a single operation.
* If you are doing a batch of operations, then use that function instead.
*
* @see FileBackend::doQuickOperations()
*
* @param array $op Operation
* @param array $opts Batch operation options
* @return StatusValue
* @since 1.20
*/
final public function doQuickOperation( array $op, array $opts = [] ) {
return $this->doQuickOperations( [ $op ], $opts );
}
/**
* Performs a single quick create operation.
* This sets $params['op'] to 'create' and passes it to doQuickOperation().
*
* @see FileBackend::doQuickOperation()
*
* @param array $params Operation parameters
* @param array $opts Operation options
* @return StatusValue
* @since 1.20
*/
final public function quickCreate( array $params, array $opts = [] ) {
return $this->doQuickOperation( [ 'op' => 'create' ] + $params, $opts );
}
/**
* Performs a single quick store operation.
* This sets $params['op'] to 'store' and passes it to doQuickOperation().
*
* @see FileBackend::doQuickOperation()
*
* @param array $params Operation parameters
* @param array $opts Operation options
* @return StatusValue
* @since 1.20
*/
final public function quickStore( array $params, array $opts = [] ) {
return $this->doQuickOperation( [ 'op' => 'store' ] + $params, $opts );
}
/**
* Performs a single quick copy operation.
* This sets $params['op'] to 'copy' and passes it to doQuickOperation().
*
* @see FileBackend::doQuickOperation()
*
* @param array $params Operation parameters
* @param array $opts Operation options
* @return StatusValue
* @since 1.20
*/
final public function quickCopy( array $params, array $opts = [] ) {
return $this->doQuickOperation( [ 'op' => 'copy' ] + $params, $opts );
}
/**
* Performs a single quick move operation.
* This sets $params['op'] to 'move' and passes it to doQuickOperation().
*
* @see FileBackend::doQuickOperation()
*
* @param array $params Operation parameters
* @param array $opts Operation options
* @return StatusValue
* @since 1.20
*/
final public function quickMove( array $params, array $opts = [] ) {
return $this->doQuickOperation( [ 'op' => 'move' ] + $params, $opts );
}
/**
* Performs a single quick delete operation.
* This sets $params['op'] to 'delete' and passes it to doQuickOperation().
*
* @see FileBackend::doQuickOperation()
*
* @param array $params Operation parameters
* @param array $opts Operation options
* @return StatusValue
* @since 1.20
*/
final public function quickDelete( array $params, array $opts = [] ) {
return $this->doQuickOperation( [ 'op' => 'delete' ] + $params, $opts );
}
/**
* Performs a single quick describe operation.
* This sets $params['op'] to 'describe' and passes it to doQuickOperation().
*
* @see FileBackend::doQuickOperation()
*
* @param array $params Operation parameters
* @param array $opts Operation options
* @return StatusValue
* @since 1.21
*/
final public function quickDescribe( array $params, array $opts = [] ) {
return $this->doQuickOperation( [ 'op' => 'describe' ] + $params, $opts );
}
/**
* Concatenate a list of storage files into a single file system file.
* The target path should refer to a file that is already locked or
* otherwise safe from modification from other processes. Normally,
* the file will be a new temp file, which should be adequate.
*
* @param array $params Operation parameters, include:
* - srcs : ordered source storage paths (e.g. chunk1, chunk2, ...)
* - dst : file system path to 0-byte temp file
* - parallelize : try to do operations in parallel when possible
* @return StatusValue
*/
abstract public function concatenate( array $params );
/**
* Prepare a storage directory for usage.
* This will create any required containers and parent directories.
* Backends using key/value stores only need to create the container.
*
* The 'noAccess' and 'noListing' parameters works the same as in secure(),
* except they are only applied *if* the directory/container had to be created.
* These flags should always be set for directories that have private files.
* However, setting them is not guaranteed to actually do anything.
* Additional server configuration may be needed to achieve the desired effect.
*
* @param array $params Parameters include:
* - dir : storage directory
* - noAccess : try to deny file access (since 1.20)
* - noListing : try to deny file listing (since 1.20)
* - bypassReadOnly : allow writes in read-only mode (since 1.20)
* @return StatusValue Good status without value for success, fatal otherwise.
*/
final public function prepare( array $params ) {
if ( empty( $params['bypassReadOnly'] ) && $this->isReadOnly() ) {
return $this->newStatus( 'backend-fail-readonly', $this->name, $this->readOnly );
}
/** @noinspection PhpUnusedLocalVariableInspection */
$scope = ScopedCallback::newScopedIgnoreUserAbort(); // try to ignore client aborts
return $this->doPrepare( $params );
}
/**
* @see FileBackend::prepare()
* @param array $params
* @return StatusValue Good status without value for success, fatal otherwise.
*/
abstract protected function doPrepare( array $params );
/**
* Take measures to block web access to a storage directory and
* the container it belongs to. FS backends might add .htaccess
* files whereas key/value store backends might revoke container
* access to the storage user representing end-users in web requests.
*
* This is not guaranteed to actually make files or listings publicly hidden.
* Additional server configuration may be needed to achieve the desired effect.
*
* @param array $params Parameters include:
* - dir : storage directory
* - noAccess : try to deny file access
* - noListing : try to deny file listing
* - bypassReadOnly : allow writes in read-only mode (since 1.20)
* @return StatusValue
*/
final public function secure( array $params ) {
if ( empty( $params['bypassReadOnly'] ) && $this->isReadOnly() ) {
return $this->newStatus( 'backend-fail-readonly', $this->name, $this->readOnly );
}
/** @noinspection PhpUnusedLocalVariableInspection */
$scope = ScopedCallback::newScopedIgnoreUserAbort(); // try to ignore client aborts
return $this->doSecure( $params );
}
/**
* @see FileBackend::secure()
* @param array $params
* @return StatusValue
*/
abstract protected function doSecure( array $params );
/**
* Remove measures to block web access to a storage directory and
* the container it belongs to. FS backends might remove .htaccess
* files whereas key/value store backends might grant container
* access to the storage user representing end-users in web requests.
* This essentially can undo the result of secure() calls.
*
* This is not guaranteed to actually make files or listings publicly viewable.
* Additional server configuration may be needed to achieve the desired effect.
*
* @param array $params Parameters include:
* - dir : storage directory
* - access : try to allow file access
* - listing : try to allow file listing
* - bypassReadOnly : allow writes in read-only mode (since 1.20)
* @return StatusValue
* @since 1.20
*/
final public function publish( array $params ) {
if ( empty( $params['bypassReadOnly'] ) && $this->isReadOnly() ) {
return $this->newStatus( 'backend-fail-readonly', $this->name, $this->readOnly );
}
/** @noinspection PhpUnusedLocalVariableInspection */
$scope = ScopedCallback::newScopedIgnoreUserAbort(); // try to ignore client aborts
return $this->doPublish( $params );
}
/**
* @see FileBackend::publish()
* @param array $params
* @return StatusValue
*/
abstract protected function doPublish( array $params );
/**
* Delete a storage directory if it is empty.
* Backends using key/value stores may do nothing unless the directory
* is that of an empty container, in which case it will be deleted.
*
* @param array $params Parameters include:
* - dir : storage directory
* - recursive : recursively delete empty subdirectories first (since 1.20)
* - bypassReadOnly : allow writes in read-only mode (since 1.20)
* @return StatusValue
*/
final public function clean( array $params ) {
if ( empty( $params['bypassReadOnly'] ) && $this->isReadOnly() ) {
return $this->newStatus( 'backend-fail-readonly', $this->name, $this->readOnly );
}
/** @noinspection PhpUnusedLocalVariableInspection */
$scope = ScopedCallback::newScopedIgnoreUserAbort(); // try to ignore client aborts
return $this->doClean( $params );
}
/**
* @see FileBackend::clean()
* @param array $params
* @return StatusValue
*/
abstract protected function doClean( array $params );
/**
* Check if a file exists at a storage path in the backend.
* This returns false if only a directory exists at the path.
*
* Callers that only care if a file is readily accessible can use non-strict
* comparisons on the result. If "does not exist" and "existence is unknown"
* must be distinguished, then strict comparisons to true/null should be used.
*
* @see FileBackend::EXISTENCE_ERROR
* @see FileBackend::directoryExists()
*
* @param array $params Parameters include:
* - src : source storage path
* - latest : use the latest available data
* @return bool|null Whether the file exists or null (I/O error)
*/
abstract public function fileExists( array $params );
/**
* Get the last-modified timestamp of the file at a storage path.
*
* @see FileBackend::TIMESTAMP_FAIL
*
* @param array $params Parameters include:
* - src : source storage path
* - latest : use the latest available data
* @return string|false TS_MW timestamp or false (missing file or I/O error)
*/
abstract public function getFileTimestamp( array $params );
/**
* Get the contents of a file at a storage path in the backend.
* This should be avoided for potentially large files.
*
* @see FileBackend::CONTENT_FAIL
*
* @param array $params Parameters include:
* - src : source storage path
* - latest : use the latest available data
* @return string|false Content string or false (missing file or I/O error)
*/
final public function getFileContents( array $params ) {
$contents = $this->getFileContentsMulti( [ 'srcs' => [ $params['src'] ] ] + $params );
return $contents[$params['src']];
}
/**
* Like getFileContents() except it takes an array of storage paths
* and returns an order preserved map of storage paths to their content.
*
* @see FileBackend::getFileContents()
*
* @param array $params Parameters include:
* - srcs : list of source storage paths
* - latest : use the latest available data
* - parallelize : try to do operations in parallel when possible
* @return string[]|false[] Map of (path name => file content or false on failure)
* @since 1.20
*/
abstract public function getFileContentsMulti( array $params );
/**
* Get metadata about a file at a storage path in the backend.
* If the file does not exist, then this returns false.
* Otherwise, the result is an associative array that includes:
* - headers : map of HTTP headers used for GET/HEAD requests (name => value)
* - metadata : map of file metadata (name => value)
* Metadata keys and headers names will be returned in all lower-case.
* Additional values may be included for internal use only.
*
* Use FileBackend::hasFeatures() to check how well this is supported.
*
* @see FileBackend::XATTRS_FAIL
*
* @param array $params
* $params include:
* - src : source storage path
* - latest : use the latest available data
* @return array|false File metadata array or false (missing file or I/O error)
* @since 1.23
*/
abstract public function getFileXAttributes( array $params );
/**
* Get the size (bytes) of a file at a storage path in the backend.
*
* @see FileBackend::SIZE_FAIL
*
* @param array $params Parameters include:
* - src : source storage path
* - latest : use the latest available data
* @return int|false File size in bytes or false (missing file or I/O error)
*/
abstract public function getFileSize( array $params );
/**
* Get quick information about a file at a storage path in the backend.
* If the file does not exist, then this returns false.
* Otherwise, the result is an associative array that includes:
* - mtime : the last-modified timestamp (TS_MW)
* - size : the file size (bytes)
* Additional values may be included for internal use only.
*
* @see FileBackend::STAT_ABSENT
* @see FileBackend::STAT_ERROR
*
* @param array $params Parameters include:
* - src : source storage path
* - latest : use the latest available data
* @return array|false|null Attribute map, false (missing file), or null (I/O error)
*/
abstract public function getFileStat( array $params );
/**
* Get a SHA-1 hash of the content of the file at a storage path in the backend.
*
* @see FileBackend::SHA1_FAIL
*
* @param array $params Parameters include:
* - src : source storage path
* - latest : use the latest available data
* @return string|false Hash string or false (missing file or I/O error)
*/
abstract public function getFileSha1Base36( array $params );
/**
* Get the properties of the content of the file at a storage path in the backend.
* This gives the result of FSFile::getProps() on a local copy of the file.
*
* @param array $params Parameters include:
* - src : source storage path
* - latest : use the latest available data
* @return array Properties map; FSFile::placeholderProps() if file missing or on I/O error
*/
abstract public function getFileProps( array $params );
/**
* Stream the content of the file at a storage path in the backend.
*
* If the file does not exists, an HTTP 404 error will be given.
* Appropriate HTTP headers (Status, Content-Type, Content-Length)
* will be sent if streaming began, while none will be sent otherwise.
* Implementations should flush the output buffer before sending data.
*
* @param array $params Parameters include:
* - src : source storage path
* - headers : list of additional HTTP headers to send if the file exists
* - options : HTTP request header map with lower case keys (since 1.28). Supports:
* range : format is "bytes=(\d*-\d*)"
* if-modified-since : format is an HTTP date
* - headless : do not send HTTP headers (including those of "headers") (since 1.28)
* - latest : use the latest available data
* - allowOB : preserve any output buffers (since 1.28)
* @return StatusValue
*/
abstract public function streamFile( array $params );
/**
* Returns a file system file, identical in content to the file at a storage path.
* The file returned is either:
* - a) A TempFSFile local copy of the file at a storage path in the backend.
* The temporary copy will have the same extension as the source.
* Temporary files may be purged when the file object falls out of scope.
* - b) An FSFile pointing to the original file at a storage path in the backend.
* This is applicable for backends layered directly on top of file systems.
*
* Never modify the returned file since it might be the original, it might be shared
* among multiple callers of this method, or the backend might internally keep FSFile
* references for deferred operations.
*
* @param array $params Parameters include:
* - src : source storage path
* - latest : use the latest available data
* @return FSFile|null|false Local file copy or false (missing) or null (error)
*/
final public function getLocalReference( array $params ) {
$fsFiles = $this->getLocalReferenceMulti( [ 'srcs' => [ $params['src'] ] ] + $params );
return $fsFiles[$params['src']];
}
/**
* Like getLocalReference() except it takes an array of storage paths and
* yields an order-preserved map of storage paths to temporary local file copies.
*
* Never modify the returned files since they might be originals, they might be shared
* among multiple callers of this method, or the backend might internally keep FSFile
* references for deferred operations.
*
* @see FileBackend::getLocalReference()
*
* @param array $params Parameters include:
* - srcs : list of source storage paths
* - latest : use the latest available data
* - parallelize : try to do operations in parallel when possible
* @return array Map of (path name => FSFile or false (missing) or null (error))
* @since 1.20
*/
abstract public function getLocalReferenceMulti( array $params );
/**
* Get a local copy on disk of the file at a storage path in the backend.
* The temporary copy will have the same file extension as the source.
* Temporary files may be purged when the file object falls out of scope.
*
* Multiple calls to this method for the same path will create new copies.
*
* @param array $params Parameters include:
* - src : source storage path
* - latest : use the latest available data
* @return TempFSFile|null|false Temporary local file copy or false (missing) or null (error)
*/
final public function getLocalCopy( array $params ) {
$tmpFiles = $this->getLocalCopyMulti( [ 'srcs' => [ $params['src'] ] ] + $params );
return $tmpFiles[$params['src']];
}
/**
* Like getLocalCopy() except it takes an array of storage paths and yields
* an order preserved-map of storage paths to temporary local file copies.
*
* Multiple calls to this method for the same path will create new copies.
*
* @see FileBackend::getLocalCopy()
*
* @param array $params Parameters include:
* - srcs : list of source storage paths
* - latest : use the latest available data
* - parallelize : try to do operations in parallel when possible
* @return array Map of (path name => TempFSFile or false (missing) or null (error))
* @since 1.20
*/
abstract public function getLocalCopyMulti( array $params );
/**
* Return an HTTP URL to a given file that requires no authentication to use.
* The URL may be pre-authenticated (via some token in the URL) and temporary.
* This will return null if the backend cannot make an HTTP URL for the file.
*
* This is useful for key/value stores when using scripts that seek around
* large files and those scripts (and the backend) support HTTP Range headers.
* Otherwise, one would need to use getLocalReference(), which involves loading
* the entire file on to local disk.
*
* @see FileBackend::TEMPURL_ERROR
*
* @param array $params Parameters include:
* - src : source storage path
* - ttl : lifetime (seconds) if pre-authenticated; default is 1 day
* - latest : use the latest available data
* - method : the allowed method; default GET
* - ipRange : the allowed IP range; default unlimited
* @return string|null URL or null (not supported or I/O error)
* @since 1.21
*/
abstract public function getFileHttpUrl( array $params );
/**
* Add a file to a Shellbox command as an input file.
*
* @param BoxedCommand $command
* @param string $boxedName
* @param array $params Parameters include:
* - src : source storage path
* - latest : use the latest available data
* @return StatusValue
* @since 1.43
*/
abstract public function addShellboxInputFile( BoxedCommand $command, string $boxedName,
array $params );
/**
* Check if a directory exists at a given storage path
*
* For backends using key/value stores, a directory is said to exist whenever
* there exist any files with paths using the given directory path as a prefix
* followed by a forward slash. For example, if there is a file called
* "mwstore://backend/container/dir/path.svg" then directories are said to exist
* at "mwstore://backend/container" and "mwstore://backend/container/dir". These
* can be thought of as "virtual" directories.
*
* Backends that directly use a filesystem layer might enumerate empty directories.
* The clean() method should always be used when files are deleted or moved if this
* is a concern. This is a trade-off to avoid write amplication/contention on file
* changes or read amplification when calling this method.
*
* Storage backends with eventual consistency might return stale data.
*
* @see FileBackend::EXISTENCE_ERROR
* @see FileBackend::clean()
*
* @param array $params Parameters include:
* - dir : storage directory
* @return bool|null Whether a directory exists or null (I/O error)
* @since 1.20
*/
abstract public function directoryExists( array $params );
/**
* Get an iterator to list *all* directories under a storage directory
*
* If the directory is of the form "mwstore://backend/container",
* then all directories in the container will be listed.
* If the directory is of form "mwstore://backend/container/dir",
* then all directories directly under that directory will be listed.
* Results will be storage directories relative to the given directory.
*
* Storage backends with eventual consistency might return stale data.
*
* Failures during iteration can result in FileBackendError exceptions (since 1.22).
*
* @see FileBackend::LIST_ERROR
* @see FileBackend::directoryExists()
*
* @param array $params Parameters include:
* - dir : storage directory
* - topOnly : only return direct child dirs of the directory
* @return \Traversable|array|null Directory list enumerator or null (initial I/O error)
* @since 1.20
*/
abstract public function getDirectoryList( array $params );
/**
* Same as FileBackend::getDirectoryList() except only lists
* directories that are immediately under the given directory.
*
* Storage backends with eventual consistency might return stale data.
*
* Failures during iteration can result in FileBackendError exceptions (since 1.22).
*
* @see FileBackend::LIST_ERROR
* @see FileBackend::directoryExists()
*
* @param array $params Parameters include:
* - dir : storage directory
* @return \Traversable|array|null Directory list enumerator or null (initial I/O error)
* @since 1.20
*/
final public function getTopDirectoryList( array $params ) {
return $this->getDirectoryList( [ 'topOnly' => true ] + $params );
}
/**
* Get an iterator to list *all* stored files under a storage directory
*
* If the directory is of the form "mwstore://backend/container", then all
* files in the container will be listed. If the directory is of form
* "mwstore://backend/container/dir", then all files under that directory will
* be listed. Results will be storage paths relative to the given directory.
*
* Storage backends with eventual consistency might return stale data.
*
* Failures during iteration can result in FileBackendError exceptions (since 1.22).
*
* @see FileBackend::LIST_ERROR
*
* @param array $params Parameters include:
* - dir : storage directory
* - topOnly : only return direct child files of the directory (since 1.20)
* - adviseStat : set to true if stat requests will be made on the files (since 1.22)
* - forWrite : true if the list will inform a write operations (since 1.41)
* @return \Traversable|array|null File list enumerator or null (initial I/O error)
*/
abstract public function getFileList( array $params );
/**
* Same as FileBackend::getFileList() except only lists
* files that are immediately under the given directory.
*
* Storage backends with eventual consistency might return stale data.
*
* Failures during iteration can result in FileBackendError exceptions (since 1.22).
*
* @see FileBackend::LIST_ERROR
*
* @param array $params Parameters include:
* - dir : storage directory
* - adviseStat : set to true if stat requests will be made on the files (since 1.22)
* @return \Traversable|array|null File list enumerator or null on failure
* @since 1.20
*/
final public function getTopFileList( array $params ) {
return $this->getFileList( [ 'topOnly' => true ] + $params );
}
/**
* Preload persistent file stat cache and property cache into in-process cache.
* This should be used when stat calls will be made on a known list of a many files.
*
* @see FileBackend::getFileStat()
*
* @param array $paths Storage paths
*/
abstract public function preloadCache( array $paths );
/**
* Invalidate any in-process file stat and property cache.
* If $paths is given, then only the cache for those files will be cleared.
*
* @see FileBackend::getFileStat()
*
* @param array|null $paths Storage paths (optional)
*/
abstract public function clearCache( ?array $paths = null );
/**
* Preload file stat information (concurrently if possible) into in-process cache.
*
* This should be used when stat calls will be made on a known list of a many files.
* This does not make use of the persistent file stat cache.
*
* @see FileBackend::getFileStat()
*
* @param array $params Parameters include:
* - srcs : list of source storage paths
* - latest : use the latest available data
* @return bool Whether all requests proceeded without I/O errors (since 1.24)
* @since 1.23
*/
abstract public function preloadFileStat( array $params );
/**
* Lock the files at the given storage paths in the backend.
* This will either lock all the files or none (on failure).
*
* Callers should consider using getScopedFileLocks() instead.
*
* @param array $paths Storage paths
* @param int $type LockManager::LOCK_* constant
* @param int $timeout Timeout in seconds (0 means non-blocking) (since 1.24)
* @return StatusValue
*/
final public function lockFiles( array $paths, $type, $timeout = 0 ) {
$paths = array_map( [ __CLASS__, 'normalizeStoragePath' ], $paths );
return $this->wrapStatus( $this->lockManager->lock( $paths, $type, $timeout ) );
}
/**
* Unlock the files at the given storage paths in the backend.
*
* @param array $paths Storage paths
* @param int $type LockManager::LOCK_* constant
* @return StatusValue
*/
final public function unlockFiles( array $paths, $type ) {
$paths = array_map( [ __CLASS__, 'normalizeStoragePath' ], $paths );
return $this->wrapStatus( $this->lockManager->unlock( $paths, $type ) );
}
/**
* Lock the files at the given storage paths in the backend.
* This will either lock all the files or none (on failure).
* On failure, the StatusValue object will be updated with errors.
*
* Once the return value goes out scope, the locks will be released and
* the StatusValue updated. Unlock fatals will not change the StatusValue "OK" value.
*
* @see ScopedLock::factory()
*
* @param array $paths List of storage paths or map of lock types to path lists
* @param int|string $type LockManager::LOCK_* constant or "mixed"
* @param StatusValue $status StatusValue to update on lock/unlock
* @param int $timeout Timeout in seconds (0 means non-blocking) (since 1.24)
* @return ScopedLock|null RAII-style self-unlocking lock or null on failure
*/
final public function getScopedFileLocks(
array $paths, $type, StatusValue $status, $timeout = 0
) {
if ( $type === 'mixed' ) {
foreach ( $paths as &$typePaths ) {
$typePaths = array_map( [ __CLASS__, 'normalizeStoragePath' ], $typePaths );
}
} else {
$paths = array_map( [ __CLASS__, 'normalizeStoragePath' ], $paths );
}
return ScopedLock::factory( $this->lockManager, $paths, $type, $status, $timeout );
}
/**
* Get an array of scoped locks needed for a batch of file operations.
*
* Normally, FileBackend::doOperations() handles locking, unless
* the 'nonLocking' param is passed in. This function is useful if you
* want the files to be locked for a broader scope than just when the
* files are changing. For example, if you need to update DB metadata,
* you may want to keep the files locked until finished.
*
* @see FileBackend::doOperations()
*
* @param array $ops List of file operations to FileBackend::doOperations()
* @param StatusValue $status StatusValue to update on lock/unlock
* @return ScopedLock|null RAII-style self-unlocking lock or null on failure
* @since 1.20
*/
abstract public function getScopedLocksForOps( array $ops, StatusValue $status );
/**
* Get the root storage path of this backend.
* All container paths are "subdirectories" of this path.
*
* @return string Storage path
* @since 1.20
*/
final public function getRootStoragePath() {
return "mwstore://{$this->name}";
}
/**
* Get the storage path for the given container for this backend
*
* @param string $container Container name
* @return string Storage path
* @since 1.21
*/
final public function getContainerStoragePath( $container ) {
return $this->getRootStoragePath() . "/{$container}";
}
/**
* Convert FSFile 'src' paths to string paths (with an 'srcRef' field set to the FSFile)
*
* The 'srcRef' field keeps any TempFSFile objects in scope for the backend to have it
* around as long it needs (which may vary greatly depending on configuration)
*
* @param array $ops File operation batch for FileBaclend::doOperations()
* @return array File operation batch
*/
protected function resolveFSFileObjects( array $ops ) {
foreach ( $ops as &$op ) {
$src = $op['src'] ?? null;
if ( $src instanceof FSFile ) {
$op['srcRef'] = $src;
$op['src'] = $src->getPath();
}
}
unset( $op );
return $ops;
}
/**
* Check if a given path is a "mwstore://" path.
* This does not do any further validation or any existence checks.
*
* @param string|null $path
* @return bool
*/
final public static function isStoragePath( $path ) {
return ( strpos( $path ?? '', 'mwstore://' ) === 0 );
}
/**
* Split a storage path into a backend name, a container name,
* and a relative file path. The relative path may be the empty string.
* This does not do any path normalization or traversal checks.
*
* @param string $storagePath
* @return array (backend, container, rel object) or (null, null, null)
*/
final public static function splitStoragePath( $storagePath ) {
if ( self::isStoragePath( $storagePath ) ) {
// Remove the "mwstore://" prefix and split the path
$parts = explode( '/', substr( $storagePath, 10 ), 3 );
if ( count( $parts ) >= 2 && $parts[0] != '' && $parts[1] != '' ) {
if ( count( $parts ) == 3 ) {
return $parts; // e.g. "backend/container/path"
} else {
return [ $parts[0], $parts[1], '' ]; // e.g. "backend/container"
}
}
}
return [ null, null, null ];
}
/**
* Normalize a storage path by cleaning up directory separators.
* Returns null if the path is not of the format of a valid storage path.
*
* @param string $storagePath
* @return string|null Normalized storage path or null on failure
*/
final public static function normalizeStoragePath( $storagePath ) {
[ $backend, $container, $relPath ] = self::splitStoragePath( $storagePath );
if ( $relPath !== null ) { // must be for this backend
$relPath = self::normalizeContainerPath( $relPath );
if ( $relPath !== null ) {
return ( $relPath != '' )
? "mwstore://{$backend}/{$container}/{$relPath}"
: "mwstore://{$backend}/{$container}";
}
}
return null;
}
/**
* Get the parent storage directory of a storage path.
* This returns a path like "mwstore://backend/container",
* "mwstore://backend/container/...", or null if there is no parent.
*
* @param string $storagePath
* @return string|null Parent storage path or null on failure
*/
final public static function parentStoragePath( $storagePath ) {
// XXX dirname() depends on platform and locale! If nothing enforces that the storage path
// doesn't contain characters like '\', behavior can vary by platform. We should use
// explode() instead.
$storagePath = dirname( $storagePath );
[ , , $rel ] = self::splitStoragePath( $storagePath );
return ( $rel === null ) ? null : $storagePath;
}
/**
* Get the final extension from a storage or FS path
*
* @param string $path
* @param string $case One of (rawcase, uppercase, lowercase) (since 1.24)
* @return string
*/
final public static function extensionFromPath( $path, $case = 'lowercase' ) {
// This will treat a string starting with . as not having an extension, but store paths have
// to start with 'mwstore://', so "garbage in, garbage out".
$i = strrpos( $path ?? '', '.' );
$ext = $i ? substr( $path, $i + 1 ) : '';
if ( $case === 'lowercase' ) {
$ext = strtolower( $ext );
} elseif ( $case === 'uppercase' ) {
$ext = strtoupper( $ext );
}
return $ext;
}
/**
* Check if a relative path has no directory traversals
*
* @param string $path
* @return bool
* @since 1.20
*/
final public static function isPathTraversalFree( $path ) {
return ( self::normalizeContainerPath( $path ) !== null );
}
/**
* Build a Content-Disposition header value per RFC 6266.
*
* @param string $type One of (attachment, inline)
* @param string $filename Suggested file name (should not contain slashes)
* @return string
* @since 1.20
*/
final public static function makeContentDisposition( $type, $filename = '' ) {
$parts = [];
$type = strtolower( $type );
if ( !in_array( $type, [ 'inline', 'attachment' ] ) ) {
throw new InvalidArgumentException( "Invalid Content-Disposition type '$type'." );
}
$parts[] = $type;
if ( strlen( $filename ) ) {
$parts[] = "filename*=UTF-8''" . rawurlencode( basename( $filename ) );
}
return implode( ';', $parts );
}
/**
* Validate and normalize a relative storage path.
* Null is returned if the path involves directory traversal.
* Traversal is insecure for FS backends and broken for others.
*
* This uses the same traversal protection as Title::secureAndSplit().
*
* @param string $path Storage path relative to a container
* @return string|null Normalized container path or null on failure
*/
final protected static function normalizeContainerPath( $path ) {
// Normalize directory separators
$path = strtr( $path, '\\', '/' );
// Collapse any consecutive directory separators
$path = preg_replace( '![/]{2,}!', '/', $path );
// Remove any leading directory separator
$path = ltrim( $path, '/' );
// Use the same traversal protection as Title::secureAndSplit()
if ( strpos( $path, '.' ) !== false ) {
if (
$path === '.' ||
$path === '..' ||
strpos( $path, './' ) === 0 ||
strpos( $path, '../' ) === 0 ||
strpos( $path, '/./' ) !== false ||
strpos( $path, '/../' ) !== false
) {
return null;
}
}
return $path;
}
/**
* Yields the result of the status wrapper callback on either:
* - StatusValue::newGood() if this method is called without parameters
* - StatusValue::newFatal() with all parameters to this method if passed in
*
* @param mixed ...$args
* @return StatusValue
*/
final protected function newStatus( ...$args ) {
if ( count( $args ) ) {
$sv = StatusValue::newFatal( ...$args );
} else {
$sv = StatusValue::newGood();
}
return $this->wrapStatus( $sv );
}
/**
* @param StatusValue $sv
* @return StatusValue Modified status or StatusValue subclass
*/
final protected function wrapStatus( StatusValue $sv ) {
return $this->statusWrapper ? call_user_func( $this->statusWrapper, $sv ) : $sv;
}
/**
* @param string $section
* @return ScopedCallback|null
*/
protected function scopedProfileSection( $section ) {
return $this->profiler ? ( $this->profiler )( $section ) : null;
}
/**
* Default behavior of resetOutputBuffer().
* @codeCoverageIgnore
* @internal
*/
public static function resetOutputBufferTheDefaultWay() {
// XXX According to documentation, ob_get_status() always returns a non-empty array and this
// condition will always be true
while ( ob_get_status() ) {
if ( !ob_end_clean() ) {
// Could not remove output buffer handler; abort now
// to avoid getting in some kind of infinite loop.
break;
}
}
}
/**
* Return options for use with HTTPFileStreamer.
*
* @internal
*/
public function getStreamerOptions(): array {
return $this->streamerOptions;
}
}
/** @deprecated class alias since 1.43 */
class_alias( FileBackend::class, 'FileBackend' );
|