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 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037
|
<?php
/**
* @defgroup FileRepo File Repository
*
* @brief This module handles how MediaWiki interacts with filesystems.
*
* @details
*/
use MediaWiki\Context\RequestContext;
use MediaWiki\Linker\LinkTarget;
use MediaWiki\MainConfigNames;
use MediaWiki\MediaWikiServices;
use MediaWiki\Page\PageIdentity;
use MediaWiki\Permissions\Authority;
use MediaWiki\Status\Status;
use MediaWiki\Title\Title;
use MediaWiki\User\UserIdentity;
use MediaWiki\Utils\MWTimestamp;
use Shellbox\Command\BoxedCommand;
use Wikimedia\AtEase\AtEase;
use Wikimedia\FileBackend\FileBackend;
use Wikimedia\FileBackend\FSFile\FSFile;
use Wikimedia\FileBackend\FSFile\TempFSFile;
use Wikimedia\ObjectCache\WANObjectCache;
use Wikimedia\Rdbms\IDBAccessObject;
/**
* Base code for file repositories.
*
* 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 FileRepo
*/
/**
* Base class for file repositories
*
* See [the architecture doc](@ref filerepoarch) for more information.
*
* @ingroup FileRepo
*/
class FileRepo {
public const DELETE_SOURCE = 1;
public const OVERWRITE = 2;
public const OVERWRITE_SAME = 4;
public const SKIP_LOCKING = 8;
public const NAME_AND_TIME_ONLY = 1;
/** @var bool Whether to fetch commons image description pages and display
* them on the local wiki
*/
public $fetchDescription;
/** @var int */
public $descriptionCacheExpiry;
/** @var bool */
protected $hasSha1Storage = false;
/** @var bool */
protected $supportsSha1URLs = false;
/** @var FileBackend */
protected $backend;
/** @var array Map of zones to config */
protected $zones = [];
/** @var string URL of thumb.php */
protected $thumbScriptUrl;
/** @var bool Whether to skip media file transformation on parse and rely
* on a 404 handler instead.
*/
protected $transformVia404;
/** @var string URL of image description pages, e.g.
* https://en.wikipedia.org/wiki/File:
*/
protected $descBaseUrl;
/** @var string URL of the MediaWiki installation, equivalent to
* $wgScriptPath, e.g. https://en.wikipedia.org/w
*/
protected $scriptDirUrl;
/** @var string Equivalent to $wgArticlePath, e.g. https://en.wikipedia.org/wiki/$1 */
protected $articleUrl;
/** @var bool Equivalent to $wgCapitalLinks (or $wgCapitalLinkOverrides[NS_FILE],
* determines whether filenames implicitly start with a capital letter.
* The current implementation may give incorrect description page links
* when the local $wgCapitalLinks and initialCapital are mismatched.
*/
protected $initialCapital;
/** @var string May be 'paranoid' to remove all parameters from error
* messages, 'none' to leave the paths in unchanged, or 'simple' to
* replace paths with placeholders. Default for LocalRepo is
* 'simple'.
*/
protected $pathDisclosureProtection = 'simple';
/** @var string|false Public zone URL. */
protected $url;
/** @var string|false The base thumbnail URL. Defaults to "<url>/thumb". */
protected $thumbUrl;
/** @var int The number of directory levels for hash-based division of files */
protected $hashLevels;
/** @var int The number of directory levels for hash-based division of deleted files */
protected $deletedHashLevels;
/** @var int File names over this size will use the short form of thumbnail
* names. Short thumbnail names only have the width, parameters, and the
* extension.
*/
protected $abbrvThreshold;
/** @var null|string The URL to a favicon (optional, may be a server-local path URL). */
protected $favicon = null;
/** @var bool Whether all zones should be private (e.g. private wiki repo) */
protected $isPrivate;
/** @var callable Override these in the base class */
protected $fileFactory = [ UnregisteredLocalFile::class, 'newFromTitle' ];
/** @var callable|false Override these in the base class */
protected $oldFileFactory = false;
/** @var callable|false Override these in the base class */
protected $fileFactoryKey = false;
/** @var callable|false Override these in the base class */
protected $oldFileFactoryKey = false;
/** @var string URL of where to proxy thumb.php requests to.
* Example: http://127.0.0.1:8888/wiki/dev/thumb/
*/
protected $thumbProxyUrl;
/** @var string Secret key to pass as an X-Swift-Secret header to the proxied thumb service */
protected $thumbProxySecret;
/** @var bool Disable local image scaling */
protected $disableLocalTransform = false;
/** @var WANObjectCache */
protected $wanCache;
/**
* @var string
* @note Use $this->getName(). Public for back-compat only
* @todo make protected
*/
public $name;
/**
* @see Documentation of info options at $wgLocalFileRepo
* @param array|null $info
* @phan-assert array $info
*/
public function __construct( ?array $info = null ) {
// Verify required settings presence
if (
$info === null
|| !array_key_exists( 'name', $info )
|| !array_key_exists( 'backend', $info )
) {
throw new InvalidArgumentException( __CLASS__ .
" requires an array of options having both 'name' and 'backend' keys.\n" );
}
// Required settings
$this->name = $info['name'];
if ( $info['backend'] instanceof FileBackend ) {
$this->backend = $info['backend']; // useful for testing
} else {
$this->backend =
MediaWikiServices::getInstance()->getFileBackendGroup()->get( $info['backend'] );
}
// Optional settings that can have no value
$optionalSettings = [
'descBaseUrl', 'scriptDirUrl', 'articleUrl', 'fetchDescription',
'thumbScriptUrl', 'pathDisclosureProtection', 'descriptionCacheExpiry',
'favicon', 'thumbProxyUrl', 'thumbProxySecret', 'disableLocalTransform'
];
foreach ( $optionalSettings as $var ) {
if ( isset( $info[$var] ) ) {
$this->$var = $info[$var];
}
}
// Optional settings that have a default
$localCapitalLinks =
MediaWikiServices::getInstance()->getNamespaceInfo()->isCapitalized( NS_FILE );
$this->initialCapital = $info['initialCapital'] ?? $localCapitalLinks;
if ( $localCapitalLinks && !$this->initialCapital ) {
// If the local wiki's file namespace requires an initial capital, but a foreign file
// repo doesn't, complications will result. Linker code will want to auto-capitalize the
// first letter of links to files, but those links might actually point to files on
// foreign wikis with initial-lowercase names. This combination is not likely to be
// used by anyone anyway, so we just outlaw it to save ourselves the bugs. If you want
// to include a foreign file repo with initialCapital false, set your local file
// namespace to not be capitalized either.
throw new InvalidArgumentException(
'File repos with initial capital false are not allowed on wikis where the File ' .
'namespace has initial capital true' );
}
$this->url = $info['url'] ?? false; // a subclass may set the URL (e.g. ForeignAPIRepo)
$defaultThumbUrl = $this->url ? $this->url . '/thumb' : false;
$this->thumbUrl = $info['thumbUrl'] ?? $defaultThumbUrl;
$this->hashLevels = $info['hashLevels'] ?? 2;
$this->deletedHashLevels = $info['deletedHashLevels'] ?? $this->hashLevels;
$this->transformVia404 = !empty( $info['transformVia404'] );
$this->abbrvThreshold = $info['abbrvThreshold'] ?? 255;
$this->isPrivate = !empty( $info['isPrivate'] );
// Give defaults for the basic zones...
$this->zones = $info['zones'] ?? [];
foreach ( [ 'public', 'thumb', 'transcoded', 'temp', 'deleted' ] as $zone ) {
if ( !isset( $this->zones[$zone]['container'] ) ) {
$this->zones[$zone]['container'] = "{$this->name}-{$zone}";
}
if ( !isset( $this->zones[$zone]['directory'] ) ) {
$this->zones[$zone]['directory'] = '';
}
if ( !isset( $this->zones[$zone]['urlsByExt'] ) ) {
$this->zones[$zone]['urlsByExt'] = [];
}
}
$this->supportsSha1URLs = !empty( $info['supportsSha1URLs'] );
$this->wanCache = $info['wanCache'] ?? WANObjectCache::newEmpty();
}
/**
* Get the file backend instance. Use this function wisely.
*
* @return FileBackend
*/
public function getBackend() {
return $this->backend;
}
/**
* Get an explanatory message if this repo is read-only.
* This checks if an administrator disabled writes to the backend.
*
* @return string|false Returns false if the repo is not read-only
*/
public function getReadOnlyReason() {
return $this->backend->getReadOnlyReason();
}
/**
* Ensure that a single zone or list of zones is defined for usage
*
* @param string[]|string $doZones Only do a particular zones
*/
protected function initZones( $doZones = [] ): void {
foreach ( (array)$doZones as $zone ) {
$root = $this->getZonePath( $zone );
if ( $root === null ) {
throw new RuntimeException( "No '$zone' zone defined in the {$this->name} repo." );
}
}
}
/**
* Determine if a string is an mwrepo:// URL
*
* @param string $url
* @return bool
*/
public static function isVirtualUrl( $url ) {
return str_starts_with( $url, 'mwrepo://' );
}
/**
* Get a URL referring to this repository, with the private mwrepo protocol.
* The suffix, if supplied, is considered to be unencoded, and will be
* URL-encoded before being returned.
*
* @param string|false $suffix
* @return string
*/
public function getVirtualUrl( $suffix = false ) {
$path = 'mwrepo://' . $this->name;
if ( $suffix !== false ) {
$path .= '/' . rawurlencode( $suffix );
}
return $path;
}
/**
* Get the URL corresponding to one of the four basic zones
*
* @param string $zone One of: public, deleted, temp, thumb
* @param string|null $ext Optional file extension
* @return string|false
*/
public function getZoneUrl( $zone, $ext = null ) {
if ( in_array( $zone, [ 'public', 'thumb', 'transcoded' ] ) ) {
// standard public zones
if ( $ext !== null && isset( $this->zones[$zone]['urlsByExt'][$ext] ) ) {
// custom URL for extension/zone
// @phan-suppress-next-line PhanTypeArraySuspiciousNullable
return $this->zones[$zone]['urlsByExt'][$ext];
} elseif ( isset( $this->zones[$zone]['url'] ) ) {
// custom URL for zone
return $this->zones[$zone]['url'];
}
}
switch ( $zone ) {
case 'public':
return $this->url;
case 'temp':
case 'deleted':
return false; // no public URL
case 'thumb':
return $this->thumbUrl;
case 'transcoded':
return "{$this->url}/transcoded";
default:
return false;
}
}
/**
* @return bool Whether non-ASCII path characters are allowed
*/
public function backendSupportsUnicodePaths() {
return (bool)( $this->getBackend()->getFeatures() & FileBackend::ATTR_UNICODE_PATHS );
}
/**
* Get the backend storage path corresponding to a virtual URL. Callers are responsible of
* verifying that $url is a valid virtual URL.
* Use this function wisely.
*
* @param string $url
* @return string
*/
public function resolveVirtualUrl( $url ) {
if ( !str_starts_with( $url, 'mwrepo://' ) ) {
throw new InvalidArgumentException( __METHOD__ . ': unknown protocol' );
}
$bits = explode( '/', substr( $url, 9 ), 3 );
if ( count( $bits ) != 3 ) {
throw new InvalidArgumentException( __METHOD__ . ": invalid mwrepo URL: $url" );
}
[ $repo, $zone, $rel ] = $bits;
if ( $repo !== $this->name ) {
throw new InvalidArgumentException( __METHOD__ . ": fetching from a foreign repo is not supported" );
}
$base = $this->getZonePath( $zone );
if ( !$base ) {
throw new InvalidArgumentException( __METHOD__ . ": invalid zone: $zone" );
}
return $base . '/' . rawurldecode( $rel );
}
/**
* The storage container and base path of a zone
*
* @param string $zone
* @return array (container, base path) or (null, null)
*/
protected function getZoneLocation( $zone ) {
if ( !isset( $this->zones[$zone] ) ) {
return [ null, null ]; // bogus
}
return [ $this->zones[$zone]['container'], $this->zones[$zone]['directory'] ];
}
/**
* Get the storage path corresponding to one of the zones
*
* @param string $zone
* @return string|null Returns null if the zone is not defined
*/
public function getZonePath( $zone ) {
[ $container, $base ] = $this->getZoneLocation( $zone );
if ( $container === null || $base === null ) {
return null;
}
$backendName = $this->backend->getName();
if ( $base != '' ) { // may not be set
$base = "/{$base}";
}
return "mwstore://$backendName/{$container}{$base}";
}
/**
* Create a new File object from the local repository
*
* @param PageIdentity|LinkTarget|string $title
* @param string|false $time Time at which the image was uploaded. If this
* is specified, the returned object will be an instance of the
* repository's old file class instead of a current file. Repositories
* not supporting version control should return false if this parameter
* is set.
* @return File|null A File, or null if passed an invalid Title
*/
public function newFile( $title, $time = false ) {
$title = File::normalizeTitle( $title );
if ( !$title ) {
return null;
}
if ( $time ) {
if ( $this->oldFileFactory ) {
return call_user_func( $this->oldFileFactory, $title, $this, $time );
} else {
return null;
}
} else {
return call_user_func( $this->fileFactory, $title, $this );
}
}
/**
* Find an instance of the named file created at the specified time
* Returns false if the file does not exist. Repositories not supporting
* version control should return false if the time is specified.
*
* @param PageIdentity|LinkTarget|string $title
* @param array $options Associative array of options:
* time: requested time for a specific file version, or false for the
* current version. An image object will be returned which was
* created at the specified time (which may be archived or current).
* ignoreRedirect: If true, do not follow file redirects
* private: If an Authority object, return restricted (deleted) files if the
* performer is allowed to view them. Otherwise, such files will not
* be found. If set and not an Authority object, throws an exception.
* Authority is only accepted since 1.37, User was required before.
* latest: If true, load from the latest available data into File objects
* @return File|false False on failure
* @throws InvalidArgumentException
*/
public function findFile( $title, $options = [] ) {
if ( !empty( $options['private'] ) && !( $options['private'] instanceof Authority ) ) {
throw new InvalidArgumentException(
__METHOD__ . ' called with the `private` option set to something ' .
'other than an Authority object'
);
}
$title = File::normalizeTitle( $title );
if ( !$title ) {
return false;
}
if ( isset( $options['bypassCache'] ) ) {
$options['latest'] = $options['bypassCache']; // b/c
}
$time = $options['time'] ?? false;
$flags = !empty( $options['latest'] ) ? IDBAccessObject::READ_LATEST : 0;
# First try the current version of the file to see if it precedes the timestamp
$img = $this->newFile( $title );
if ( !$img ) {
return false;
}
$img->load( $flags );
if ( $img->exists() && ( !$time || $img->getTimestamp() == $time ) ) {
return $img;
}
# Now try an old version of the file
if ( $time !== false ) {
$img = $this->newFile( $title, $time );
if ( $img ) {
$img->load( $flags );
if ( $img->exists() ) {
if ( !$img->isDeleted( File::DELETED_FILE ) ) {
return $img; // always OK
} elseif (
// If its not empty, its an Authority object
!empty( $options['private'] ) &&
$img->userCan( File::DELETED_FILE, $options['private'] )
) {
return $img;
}
}
}
}
# Now try redirects
if ( !empty( $options['ignoreRedirect'] ) ) {
return false;
}
$redir = $this->checkRedirect( $title );
if ( $redir && $title->getNamespace() === NS_FILE ) {
$img = $this->newFile( $redir );
if ( !$img ) {
return false;
}
$img->load( $flags );
if ( $img->exists() ) {
$img->redirectedFrom( $title->getDBkey() );
return $img;
}
}
return false;
}
/**
* Find many files at once.
*
* @param array $items An array of titles, or an array of findFile() options with
* the "title" option giving the title. Example:
*
* $findItem = [ 'title' => $title, 'private' => true ];
* $findBatch = [ $findItem ];
* $repo->findFiles( $findBatch );
*
* No title should appear in $items twice, as the result use titles as keys
* @param int $flags Supports:
* - FileRepo::NAME_AND_TIME_ONLY : return a (search title => (title,timestamp)) map.
* The search title uses the input titles; the other is the final post-redirect title.
* All titles are returned as string DB keys and the inner array is associative.
* @return array Map of (file name => File objects) for matches or (search title => (title,timestamp))
*/
public function findFiles( array $items, $flags = 0 ) {
$result = [];
foreach ( $items as $item ) {
if ( is_array( $item ) ) {
$title = $item['title'];
$options = $item;
unset( $options['title'] );
if (
!empty( $options['private'] ) &&
!( $options['private'] instanceof Authority )
) {
$options['private'] = RequestContext::getMain()->getAuthority();
}
} else {
$title = $item;
$options = [];
}
$file = $this->findFile( $title, $options );
if ( $file ) {
$searchName = File::normalizeTitle( $title )->getDBkey(); // must be valid
if ( $flags & self::NAME_AND_TIME_ONLY ) {
$result[$searchName] = [
'title' => $file->getTitle()->getDBkey(),
'timestamp' => $file->getTimestamp()
];
} else {
$result[$searchName] = $file;
}
}
}
return $result;
}
/**
* Find an instance of the file with this key, created at the specified time
* Returns false if the file does not exist. Repositories not supporting
* version control should return false if the time is specified.
*
* @param string $sha1 Base 36 SHA-1 hash
* @param array $options Option array, same as findFile().
* @return File|false False on failure
* @throws InvalidArgumentException if the `private` option is set and not an Authority object
*/
public function findFileFromKey( $sha1, $options = [] ) {
if ( !empty( $options['private'] ) && !( $options['private'] instanceof Authority ) ) {
throw new InvalidArgumentException(
__METHOD__ . ' called with the `private` option set to something ' .
'other than an Authority object'
);
}
$time = $options['time'] ?? false;
# First try to find a matching current version of a file...
if ( !$this->fileFactoryKey ) {
return false; // find-by-sha1 not supported
}
$img = call_user_func( $this->fileFactoryKey, $sha1, $this, $time );
if ( $img && $img->exists() ) {
return $img;
}
# Now try to find a matching old version of a file...
if ( $time !== false && $this->oldFileFactoryKey ) { // find-by-sha1 supported?
$img = call_user_func( $this->oldFileFactoryKey, $sha1, $this, $time );
if ( $img && $img->exists() ) {
if ( !$img->isDeleted( File::DELETED_FILE ) ) {
return $img; // always OK
} elseif (
// If its not empty, its an Authority object
!empty( $options['private'] ) &&
$img->userCan( File::DELETED_FILE, $options['private'] )
) {
return $img;
}
}
}
return false;
}
/**
* Get an array or iterator of file objects for files that have a given
* SHA-1 content hash.
*
* STUB
* @param string $hash SHA-1 hash
* @return File[]
*/
public function findBySha1( $hash ) {
return [];
}
/**
* Get an array of arrays or iterators of file objects for files that
* have the given SHA-1 content hashes.
*
* @param string[] $hashes An array of hashes
* @return File[][] An Array of arrays or iterators of file objects and the hash as key
*/
public function findBySha1s( array $hashes ) {
$result = [];
foreach ( $hashes as $hash ) {
$files = $this->findBySha1( $hash );
if ( count( $files ) ) {
$result[$hash] = $files;
}
}
return $result;
}
/**
* Return an array of files where the name starts with $prefix.
*
* STUB
* @param string $prefix The prefix to search for
* @param int $limit The maximum amount of files to return
* @return LocalFile[]
*/
public function findFilesByPrefix( $prefix, $limit ) {
return [];
}
/**
* Get the URL of thumb.php
*
* @return string
*/
public function getThumbScriptUrl() {
return $this->thumbScriptUrl;
}
/**
* Get the URL thumb.php requests are being proxied to
*
* @return string
*/
public function getThumbProxyUrl() {
return $this->thumbProxyUrl;
}
/**
* Get the secret key for the proxied thumb service
*
* @return string
*/
public function getThumbProxySecret() {
return $this->thumbProxySecret;
}
/**
* Returns true if the repository can transform files via a 404 handler
*
* @return bool
*/
public function canTransformVia404() {
return $this->transformVia404;
}
/**
* Returns true if the repository can transform files locally.
*
* @since 1.36
* @return bool
*/
public function canTransformLocally() {
return !$this->disableLocalTransform;
}
/**
* Get the name of a file from its title
*
* @param PageIdentity|LinkTarget $title
* @return string
*/
public function getNameFromTitle( $title ) {
if (
$this->initialCapital !=
MediaWikiServices::getInstance()->getNamespaceInfo()->isCapitalized( NS_FILE )
) {
$name = $title->getDBkey();
if ( $this->initialCapital ) {
$name = MediaWikiServices::getInstance()->getContentLanguage()->ucfirst( $name );
}
} else {
$name = $title->getDBkey();
}
return $name;
}
/**
* Get the public zone root storage directory of the repository
*
* @return string
*/
public function getRootDirectory() {
return $this->getZonePath( 'public' );
}
/**
* Get a relative path including trailing slash, e.g. f/fa/
* If the repo is not hashed, returns an empty string
*
* @param string $name Name of file
* @return string
*/
public function getHashPath( $name ) {
return self::getHashPathForLevel( $name, $this->hashLevels );
}
/**
* Get a relative path including trailing slash, e.g. f/fa/
* If the repo is not hashed, returns an empty string
*
* @param string $suffix Basename of file from FileRepo::storeTemp()
* @return string
*/
public function getTempHashPath( $suffix ) {
$parts = explode( '!', $suffix, 2 ); // format is <timestamp>!<name> or just <name>
$name = $parts[1] ?? $suffix; // hash path is not based on timestamp
return self::getHashPathForLevel( $name, $this->hashLevels );
}
/**
* @param string $name
* @param int $levels
* @return string
*/
protected static function getHashPathForLevel( $name, $levels ) {
if ( $levels == 0 ) {
return '';
} else {
$hash = md5( $name );
$path = '';
for ( $i = 1; $i <= $levels; $i++ ) {
$path .= substr( $hash, 0, $i ) . '/';
}
return $path;
}
}
/**
* Get the number of hash directory levels
*
* @return int
*/
public function getHashLevels() {
return $this->hashLevels;
}
/**
* Get the name of this repository, as specified by $info['name]' to the constructor
*
* @return string
*/
public function getName() {
return $this->name;
}
/**
* Make an url to this repo
*
* @param string|array $query Query string to append
* @param string $entry Entry point; defaults to index
* @return string|false False on failure
*/
public function makeUrl( $query = '', $entry = 'index' ) {
if ( isset( $this->scriptDirUrl ) ) {
return wfAppendQuery( "{$this->scriptDirUrl}/{$entry}.php", $query );
}
return false;
}
/**
* Get the URL of an image description page. May return false if it is
* unknown or not applicable. In general this should only be called by the
* File class, since it may return invalid results for certain kinds of
* repositories. Use File::getDescriptionUrl() in user code.
*
* In particular, it uses the article paths as specified to the repository
* constructor, whereas local repositories use the local Title functions.
*
* @param string $name
* @return string|false
*/
public function getDescriptionUrl( $name ) {
$encName = wfUrlencode( $name );
if ( $this->descBaseUrl !== null ) {
# "http://example.com/wiki/File:"
return $this->descBaseUrl . $encName;
}
if ( $this->articleUrl !== null ) {
# "http://example.com/wiki/$1"
# We use "Image:" as the canonical namespace for
# compatibility across all MediaWiki versions.
return str_replace( '$1',
"Image:$encName", $this->articleUrl );
}
if ( $this->scriptDirUrl !== null ) {
# "http://example.com/w"
# We use "Image:" as the canonical namespace for
# compatibility across all MediaWiki versions,
# and just sort of hope index.php is right. ;)
return $this->makeUrl( "title=Image:$encName" );
}
return false;
}
/**
* Get the URL of the content-only fragment of the description page. For
* MediaWiki this means action=render. This should only be called by the
* repository's file class, since it may return invalid results. User code
* should use File::getDescriptionText().
*
* @param string $name Name of image to fetch
* @param string|null $lang Language to fetch it in, if any.
* @return string|false
*/
public function getDescriptionRenderUrl( $name, $lang = null ) {
$query = 'action=render';
if ( $lang !== null ) {
$query .= '&uselang=' . urlencode( $lang );
}
if ( isset( $this->scriptDirUrl ) ) {
return $this->makeUrl(
'title=' .
wfUrlencode( 'Image:' . $name ) .
"&$query" );
} else {
$descUrl = $this->getDescriptionUrl( $name );
if ( $descUrl ) {
return wfAppendQuery( $descUrl, $query );
} else {
return false;
}
}
}
/**
* Get the URL of the stylesheet to apply to description pages
*
* @return string|false False on failure
*/
public function getDescriptionStylesheetUrl() {
if ( isset( $this->scriptDirUrl ) ) {
// Must match canonical query parameter order for optimum caching
// See HTMLCacheUpdater::getUrls
return $this->makeUrl( 'title=MediaWiki:Filepage.css&action=raw&ctype=text/css' );
}
return false;
}
/**
* Store a file to a given destination.
*
* Using FSFile/TempFSFile can improve performance via caching.
* Using TempFSFile can further improve performance by signalling that it is safe
* to touch the source file or write extended attribute metadata to it directly.
*
* @param string|FSFile $srcPath Source file system path, storage path, or virtual URL
* @param string $dstZone Destination zone
* @param string $dstRel Destination relative path
* @param int $flags Bitwise combination of the following flags:
* self::OVERWRITE Overwrite an existing destination file instead of failing
* self::OVERWRITE_SAME Overwrite the file if the destination exists and has the
* same contents as the source
* self::SKIP_LOCKING Skip any file locking when doing the store
* @return Status
*/
public function store( $srcPath, $dstZone, $dstRel, $flags = 0 ) {
$this->assertWritableRepo(); // fail out if read-only
$status = $this->storeBatch( [ [ $srcPath, $dstZone, $dstRel ] ], $flags );
if ( $status->successCount == 0 ) {
$status->setOK( false );
}
return $status;
}
/**
* Store a batch of files
*
* @see FileRepo::store()
*
* @param array $triplets (src, dest zone, dest rel) triplets as per store()
* @param int $flags Bitwise combination of the following flags:
* self::OVERWRITE Overwrite an existing destination file instead of failing
* self::OVERWRITE_SAME Overwrite the file if the destination exists and has the
* same contents as the source
* self::SKIP_LOCKING Skip any file locking when doing the store
* @return Status
*/
public function storeBatch( array $triplets, $flags = 0 ) {
$this->assertWritableRepo(); // fail out if read-only
if ( $flags & self::DELETE_SOURCE ) {
throw new InvalidArgumentException( "DELETE_SOURCE not supported in " . __METHOD__ );
}
$status = $this->newGood();
$backend = $this->backend; // convenience
$operations = [];
// Validate each triplet and get the store operation...
foreach ( $triplets as [ $src, $dstZone, $dstRel ] ) {
$srcPath = ( $src instanceof FSFile ) ? $src->getPath() : $src;
wfDebug( __METHOD__
. "( \$src='$srcPath', \$dstZone='$dstZone', \$dstRel='$dstRel' )"
);
// Resolve source path
if ( $src instanceof FSFile ) {
$op = 'store';
} else {
$src = $this->resolveToStoragePathIfVirtual( $src );
$op = FileBackend::isStoragePath( $src ) ? 'copy' : 'store';
}
// Resolve destination path
$root = $this->getZonePath( $dstZone );
if ( !$root ) {
throw new RuntimeException( "Invalid zone: $dstZone" );
}
if ( !$this->validateFilename( $dstRel ) ) {
throw new RuntimeException( 'Validation error in $dstRel' );
}
$dstPath = "$root/$dstRel";
$dstDir = dirname( $dstPath );
// Create destination directories for this triplet
if ( !$this->initDirectory( $dstDir )->isOK() ) {
return $this->newFatal( 'directorycreateerror', $dstDir );
}
// Copy the source file to the destination
$operations[] = [
'op' => $op,
'src' => $src, // storage path (copy) or local file path (store)
'dst' => $dstPath,
'overwrite' => (bool)( $flags & self::OVERWRITE ),
'overwriteSame' => (bool)( $flags & self::OVERWRITE_SAME ),
];
}
// Execute the store operation for each triplet
$opts = [ 'force' => true ];
if ( $flags & self::SKIP_LOCKING ) {
$opts['nonLocking'] = true;
}
return $status->merge( $backend->doOperations( $operations, $opts ) );
}
/**
* Deletes a batch of files.
* Each file can be a (zone, rel) pair, virtual url, storage path.
* It will try to delete each file, but ignores any errors that may occur.
*
* @param string[] $files List of files to delete
* @param int $flags Bitwise combination of the following flags:
* self::SKIP_LOCKING Skip any file locking when doing the deletions
* @return Status
*/
public function cleanupBatch( array $files, $flags = 0 ) {
$this->assertWritableRepo(); // fail out if read-only
$status = $this->newGood();
$operations = [];
foreach ( $files as $path ) {
if ( is_array( $path ) ) {
// This is a pair, extract it
[ $zone, $rel ] = $path;
$path = $this->getZonePath( $zone ) . "/$rel";
} else {
// Resolve source to a storage path if virtual
$path = $this->resolveToStoragePathIfVirtual( $path );
}
$operations[] = [ 'op' => 'delete', 'src' => $path ];
}
// Actually delete files from storage...
$opts = [ 'force' => true ];
if ( $flags & self::SKIP_LOCKING ) {
$opts['nonLocking'] = true;
}
return $status->merge( $this->backend->doOperations( $operations, $opts ) );
}
/**
* Import a file from the local file system into the repo.
* This does no locking and overrides existing files.
* This function can be used to write to otherwise read-only foreign repos.
* This is intended for copying generated thumbnails into the repo.
*
* Using FSFile/TempFSFile can improve performance via caching.
* Using TempFSFile can further improve performance by signalling that it is safe
* to touch the source file or write extended attribute metadata to it directly.
*
* @param string|FSFile $src Source file system path, storage path, or virtual URL
* @param string $dst Virtual URL or storage path
* @param array|string|null $options An array consisting of a key named headers
* listing extra headers. If a string, taken as content-disposition header.
* (Support for array of options new in 1.23)
* @return Status
*/
final public function quickImport( $src, $dst, $options = null ) {
return $this->quickImportBatch( [ [ $src, $dst, $options ] ] );
}
/**
* Import a batch of files from the local file system into the repo.
* This does no locking and overrides existing files.
* This function can be used to write to otherwise read-only foreign repos.
* This is intended for copying generated thumbnails into the repo.
*
* @see FileRepo::quickImport()
*
* All path parameters may be a file system path, storage path, or virtual URL.
* When "headers" are given they are used as HTTP headers if supported.
*
* @param array $triples List of (source path or FSFile, destination path, disposition)
* @return Status
*/
public function quickImportBatch( array $triples ) {
$status = $this->newGood();
$operations = [];
foreach ( $triples as $triple ) {
[ $src, $dst ] = $triple;
if ( $src instanceof FSFile ) {
$op = 'store';
} else {
$src = $this->resolveToStoragePathIfVirtual( $src );
$op = FileBackend::isStoragePath( $src ) ? 'copy' : 'store';
}
$dst = $this->resolveToStoragePathIfVirtual( $dst );
if ( !isset( $triple[2] ) ) {
$headers = [];
} elseif ( is_string( $triple[2] ) ) {
// back-compat
$headers = [ 'Content-Disposition' => $triple[2] ];
} elseif ( is_array( $triple[2] ) && isset( $triple[2]['headers'] ) ) {
$headers = $triple[2]['headers'];
} else {
$headers = [];
}
$operations[] = [
'op' => $op,
'src' => $src, // storage path (copy) or local path/FSFile (store)
'dst' => $dst,
'headers' => $headers
];
$status->merge( $this->initDirectory( dirname( $dst ) ) );
}
return $status->merge( $this->backend->doQuickOperations( $operations ) );
}
/**
* Purge a file from the repo. This does no locking.
* This function can be used to write to otherwise read-only foreign repos.
* This is intended for purging thumbnails.
*
* @param string $path Virtual URL or storage path
* @return Status
*/
final public function quickPurge( $path ) {
return $this->quickPurgeBatch( [ $path ] );
}
/**
* Deletes a directory if empty.
* This function can be used to write to otherwise read-only foreign repos.
*
* @param string $dir Virtual URL (or storage path) of directory to clean
* @return Status
*/
public function quickCleanDir( $dir ) {
return $this->newGood()->merge(
$this->backend->clean(
[ 'dir' => $this->resolveToStoragePathIfVirtual( $dir ) ]
)
);
}
/**
* Purge a batch of files from the repo.
* This function can be used to write to otherwise read-only foreign repos.
* This does no locking and is intended for purging thumbnails.
*
* @param string[] $paths List of virtual URLs or storage paths
* @return Status
*/
public function quickPurgeBatch( array $paths ) {
$status = $this->newGood();
$operations = [];
foreach ( $paths as $path ) {
$operations[] = [
'op' => 'delete',
'src' => $this->resolveToStoragePathIfVirtual( $path ),
'ignoreMissingSource' => true
];
}
$status->merge( $this->backend->doQuickOperations( $operations ) );
return $status;
}
/**
* Pick a random name in the temp zone and store a file to it.
* Returns a Status object with the file Virtual URL in the value,
* file can later be disposed using FileRepo::freeTemp().
*
* @param string $originalName The base name of the file as specified
* by the user. The file extension will be maintained.
* @param string $srcPath The current location of the file.
* @return Status Object with the URL in the value.
*/
public function storeTemp( $originalName, $srcPath ) {
$this->assertWritableRepo(); // fail out if read-only
$date = MWTimestamp::getInstance()->format( 'YmdHis' );
$hashPath = $this->getHashPath( $originalName );
$dstUrlRel = $hashPath . $date . '!' . rawurlencode( $originalName );
$virtualUrl = $this->getVirtualUrl( 'temp' ) . '/' . $dstUrlRel;
$result = $this->quickImport( $srcPath, $virtualUrl );
$result->value = $virtualUrl;
return $result;
}
/**
* Remove a temporary file or mark it for garbage collection
*
* @param string $virtualUrl The virtual URL returned by FileRepo::storeTemp()
* @return bool True on success, false on failure
*/
public function freeTemp( $virtualUrl ) {
$this->assertWritableRepo(); // fail out if read-only
$temp = $this->getVirtualUrl( 'temp' );
if ( !str_starts_with( $virtualUrl, $temp ) ) {
wfDebug( __METHOD__ . ": Invalid temp virtual URL" );
return false;
}
return $this->quickPurge( $virtualUrl )->isOK();
}
/**
* Concatenate a list of temporary files into a target file location.
*
* @param string[] $srcPaths Ordered list of source virtual URLs/storage paths
* @param string $dstPath Target file system path
* @param int $flags Bitwise combination of the following flags:
* self::DELETE_SOURCE Delete the source files on success
* @return Status
*/
public function concatenate( array $srcPaths, $dstPath, $flags = 0 ) {
$this->assertWritableRepo(); // fail out if read-only
$status = $this->newGood();
$sources = [];
foreach ( $srcPaths as $srcPath ) {
// Resolve source to a storage path if virtual
$source = $this->resolveToStoragePathIfVirtual( $srcPath );
$sources[] = $source; // chunk to merge
}
// Concatenate the chunks into one FS file
$params = [ 'srcs' => $sources, 'dst' => $dstPath ];
$status->merge( $this->backend->concatenate( $params ) );
if ( !$status->isOK() ) {
return $status;
}
// Delete the sources if required
if ( $flags & self::DELETE_SOURCE ) {
$status->merge( $this->quickPurgeBatch( $srcPaths ) );
}
// Make sure status is OK, despite any quickPurgeBatch() fatals
$status->setResult( true );
return $status;
}
/**
* Copy or move a file either from a storage path, virtual URL,
* or file system path, into this repository at the specified destination location.
*
* Returns a Status object. On success, the value contains "new" or
* "archived", to indicate whether the file was new with that name.
*
* Using FSFile/TempFSFile can improve performance via caching.
* Using TempFSFile can further improve performance by signalling that it is safe
* to touch the source file or write extended attribute metadata to it directly.
*
* Options to $options include:
* - headers : name/value map of HTTP headers to use in response to GET/HEAD requests
*
* @param string|FSFile $src The source file system path, storage path, or URL
* @param string $dstRel The destination relative path
* @param string $archiveRel The relative path where the existing file is to
* be archived, if there is one. Relative to the public zone root.
* @param int $flags Bitfield, may be FileRepo::DELETE_SOURCE to indicate
* that the source file should be deleted if possible
* @param array $options Optional additional parameters
* @return Status
*/
public function publish(
$src, $dstRel, $archiveRel, $flags = 0, array $options = []
) {
$this->assertWritableRepo(); // fail out if read-only
$status = $this->publishBatch(
[ [ $src, $dstRel, $archiveRel, $options ] ], $flags );
if ( $status->successCount == 0 ) {
$status->setOK( false );
}
$status->value = $status->value[0] ?? false;
return $status;
}
/**
* Publish a batch of files
*
* @see FileRepo::publish()
*
* @param array $ntuples (source, dest, archive) triplets or
* (source, dest, archive, options) 4-tuples as per publish().
* @param int $flags Bitfield, may be FileRepo::DELETE_SOURCE to indicate
* that the source files should be deleted if possible
* @return Status
*/
public function publishBatch( array $ntuples, $flags = 0 ) {
$this->assertWritableRepo(); // fail out if read-only
$backend = $this->backend; // convenience
// Try creating directories
$this->initZones( 'public' );
$status = $this->newGood( [] );
$operations = [];
$sourceFSFilesToDelete = []; // cleanup for disk source files
// Validate each triplet and get the store operation...
foreach ( $ntuples as $ntuple ) {
[ $src, $dstRel, $archiveRel ] = $ntuple;
$srcPath = ( $src instanceof FSFile ) ? $src->getPath() : $src;
$options = $ntuple[3] ?? [];
// Resolve source to a storage path if virtual
$srcPath = $this->resolveToStoragePathIfVirtual( $srcPath );
if ( !$this->validateFilename( $dstRel ) ) {
throw new RuntimeException( 'Validation error in $dstRel' );
}
if ( !$this->validateFilename( $archiveRel ) ) {
throw new RuntimeException( 'Validation error in $archiveRel' );
}
$publicRoot = $this->getZonePath( 'public' );
$dstPath = "$publicRoot/$dstRel";
$archivePath = "$publicRoot/$archiveRel";
$dstDir = dirname( $dstPath );
$archiveDir = dirname( $archivePath );
// Abort immediately on directory creation errors since they're likely to be repetitive
if ( !$this->initDirectory( $dstDir )->isOK() ) {
return $this->newFatal( 'directorycreateerror', $dstDir );
}
if ( !$this->initDirectory( $archiveDir )->isOK() ) {
return $this->newFatal( 'directorycreateerror', $archiveDir );
}
// Set any desired headers to be use in GET/HEAD responses
$headers = $options['headers'] ?? [];
// Archive destination file if it exists.
// This will check if the archive file also exists and fail if does.
// This is a check to avoid data loss. On Windows and Linux,
// copy() will overwrite, so the existence check is vulnerable to
// race conditions unless a functioning LockManager is used.
// LocalFile also uses SELECT FOR UPDATE for synchronization.
$operations[] = [
'op' => 'copy',
'src' => $dstPath,
'dst' => $archivePath,
'ignoreMissingSource' => true
];
// Copy (or move) the source file to the destination
if ( FileBackend::isStoragePath( $srcPath ) ) {
$operations[] = [
'op' => ( $flags & self::DELETE_SOURCE ) ? 'move' : 'copy',
'src' => $srcPath,
'dst' => $dstPath,
'overwrite' => true, // replace current
'headers' => $headers
];
} else {
$operations[] = [
'op' => 'store',
'src' => $src, // storage path (copy) or local path/FSFile (store)
'dst' => $dstPath,
'overwrite' => true, // replace current
'headers' => $headers
];
if ( $flags & self::DELETE_SOURCE ) {
$sourceFSFilesToDelete[] = $srcPath;
}
}
}
// Execute the operations for each triplet
$status->merge( $backend->doOperations( $operations ) );
// Find out which files were archived...
foreach ( $ntuples as $i => $ntuple ) {
[ , , $archiveRel ] = $ntuple;
$archivePath = $this->getZonePath( 'public' ) . "/$archiveRel";
if ( $this->fileExists( $archivePath ) ) {
$status->value[$i] = 'archived';
} else {
$status->value[$i] = 'new';
}
}
// Cleanup for disk source files...
foreach ( $sourceFSFilesToDelete as $file ) {
AtEase::suppressWarnings();
unlink( $file ); // FS cleanup
AtEase::restoreWarnings();
}
return $status;
}
/**
* Creates a directory with the appropriate zone permissions.
* Callers are responsible for doing read-only and "writable repo" checks.
*
* @param string $dir Virtual URL (or storage path) of directory to clean
* @return Status Good status without value for success, fatal otherwise.
*/
protected function initDirectory( $dir ) {
$path = $this->resolveToStoragePathIfVirtual( $dir );
[ , $container, ] = FileBackend::splitStoragePath( $path );
$params = [ 'dir' => $path ];
if ( $this->isPrivate
|| $container === $this->zones['deleted']['container']
|| $container === $this->zones['temp']['container']
) {
# Take all available measures to prevent web accessibility of new deleted
# directories, in case the user has not configured offline storage
$params = [ 'noAccess' => true, 'noListing' => true ] + $params;
}
return $this->newGood()->merge( $this->backend->prepare( $params ) );
}
/**
* Deletes a directory if empty.
*
* @param string $dir Virtual URL (or storage path) of directory to clean
* @return Status
*/
public function cleanDir( $dir ) {
$this->assertWritableRepo(); // fail out if read-only
return $this->newGood()->merge(
$this->backend->clean(
[ 'dir' => $this->resolveToStoragePathIfVirtual( $dir ) ]
)
);
}
/**
* Checks existence of a file
*
* @param string $file Virtual URL (or storage path) of file to check
* @return bool|null Whether the file exists, or null in case of I/O errors
*/
public function fileExists( $file ) {
$result = $this->fileExistsBatch( [ $file ] );
return $result[0];
}
/**
* Checks existence of an array of files.
*
* @param string[] $files Virtual URLs (or storage paths) of files to check
* @return array<string|int,bool|null> Map of files and either bool indicating whether the files exist,
* or null in case of I/O errors
*/
public function fileExistsBatch( array $files ) {
$paths = array_map( [ $this, 'resolveToStoragePathIfVirtual' ], $files );
$this->backend->preloadFileStat( [ 'srcs' => $paths ] );
$result = [];
foreach ( $paths as $key => $path ) {
$result[$key] = $this->backend->fileExists( [ 'src' => $path ] );
}
return $result;
}
/**
* Move a file to the deletion archive.
* If no valid deletion archive exists, this may either delete the file
* or throw an exception, depending on the preference of the repository
*
* @param mixed $srcRel Relative path for the file to be deleted
* @param mixed $archiveRel Relative path for the archive location.
* Relative to a private archive directory.
* @return Status
*/
public function delete( $srcRel, $archiveRel ) {
$this->assertWritableRepo(); // fail out if read-only
return $this->deleteBatch( [ [ $srcRel, $archiveRel ] ] );
}
/**
* Move a group of files to the deletion archive.
*
* If no valid deletion archive is configured, this may either delete the
* file or throw an exception, depending on the preference of the repository.
*
* The overwrite policy is determined by the repository -- currently LocalRepo
* assumes a naming scheme in the deleted zone based on content hash, as
* opposed to the public zone which is assumed to be unique.
*
* @param array $sourceDestPairs Array of source/destination pairs. Each element
* is a two-element array containing the source file path relative to the
* public root in the first element, and the archive file path relative
* to the deleted zone root in the second element.
* @return Status
*/
public function deleteBatch( array $sourceDestPairs ) {
$this->assertWritableRepo(); // fail out if read-only
// Try creating directories
$this->initZones( [ 'public', 'deleted' ] );
$status = $this->newGood();
$backend = $this->backend; // convenience
$operations = [];
// Validate filenames and create archive directories
foreach ( $sourceDestPairs as [ $srcRel, $archiveRel ] ) {
if ( !$this->validateFilename( $srcRel ) ) {
throw new RuntimeException( __METHOD__ . ':Validation error in $srcRel' );
} elseif ( !$this->validateFilename( $archiveRel ) ) {
throw new RuntimeException( __METHOD__ . ':Validation error in $archiveRel' );
}
$publicRoot = $this->getZonePath( 'public' );
$srcPath = "{$publicRoot}/$srcRel";
$deletedRoot = $this->getZonePath( 'deleted' );
$archivePath = "{$deletedRoot}/{$archiveRel}";
$archiveDir = dirname( $archivePath ); // does not touch FS
// Create destination directories
if ( !$this->initDirectory( $archiveDir )->isGood() ) {
return $this->newFatal( 'directorycreateerror', $archiveDir );
}
$operations[] = [
'op' => 'move',
'src' => $srcPath,
'dst' => $archivePath,
// We may have 2+ identical files being deleted,
// all of which will map to the same destination file
'overwriteSame' => true // also see T33792
];
}
// Move the files by execute the operations for each pair.
// We're now committed to returning an OK result, which will
// lead to the files being moved in the DB also.
$opts = [ 'force' => true ];
return $status->merge( $backend->doOperations( $operations, $opts ) );
}
/**
* Delete files in the deleted directory if they are not referenced in the filearchive table
*
* STUB
* @param string[] $storageKeys
*/
public function cleanupDeletedBatch( array $storageKeys ) {
$this->assertWritableRepo();
}
/**
* Get a relative path for a deletion archive key,
* e.g. s/z/a/ for sza251lrxrc1jad41h5mgilp8nysje52.jpg
*
* @param string $key
* @return string
*/
public function getDeletedHashPath( $key ) {
if ( strlen( $key ) < 31 ) {
throw new InvalidArgumentException( "Invalid storage key '$key'." );
}
$path = '';
for ( $i = 0; $i < $this->deletedHashLevels; $i++ ) {
$path .= $key[$i] . '/';
}
return $path;
}
/**
* If a path is a virtual URL, resolve it to a storage path.
* Otherwise, just return the path as it is.
*
* @param string $path
* @return string
*/
protected function resolveToStoragePathIfVirtual( $path ) {
if ( self::isVirtualUrl( $path ) ) {
return $this->resolveVirtualUrl( $path );
}
return $path;
}
/**
* Get a local FS copy of a file with a given virtual URL/storage path.
* Temporary files may be purged when the file object falls out of scope.
*
* @param string $virtualUrl
* @return TempFSFile|null|false Returns false for missing file, null on failure
*/
public function getLocalCopy( $virtualUrl ) {
$path = $this->resolveToStoragePathIfVirtual( $virtualUrl );
return $this->backend->getLocalCopy( [ 'src' => $path ] );
}
/**
* Get a local FS file with a given virtual URL/storage path.
* The file is either an original or a copy. It should not be changed.
* Temporary files may be purged when the file object falls out of scope.
*
* @param string $virtualUrl
* @return FSFile|null|false Returns false for missing file, null on failure.
*/
public function getLocalReference( $virtualUrl ) {
$path = $this->resolveToStoragePathIfVirtual( $virtualUrl );
return $this->backend->getLocalReference( [ 'src' => $path ] );
}
/**
* Add a file to a Shellbox command as an input file
*
* @param BoxedCommand $command
* @param string $boxedName
* @param string $virtualUrl
* @return StatusValue
* @since 1.43
*/
public function addShellboxInputFile( BoxedCommand $command, string $boxedName,
string $virtualUrl
) {
$path = $this->resolveToStoragePathIfVirtual( $virtualUrl );
return $this->backend->addShellboxInputFile( $command, $boxedName, [ 'src' => $path ] );
}
/**
* Get properties of a file with a given virtual URL/storage path.
* Properties should ultimately be obtained via FSFile::getProps().
*
* @param string $virtualUrl
* @return array
*/
public function getFileProps( $virtualUrl ) {
$fsFile = $this->getLocalReference( $virtualUrl );
$mwProps = new MWFileProps( MediaWikiServices::getInstance()->getMimeAnalyzer() );
if ( $fsFile ) {
$props = $mwProps->getPropsFromPath( $fsFile->getPath(), true );
} else {
$props = $mwProps->newPlaceholderProps();
}
return $props;
}
/**
* Get the timestamp of a file with a given virtual URL/storage path
*
* @param string $virtualUrl
* @return string|false False on failure
*/
public function getFileTimestamp( $virtualUrl ) {
$path = $this->resolveToStoragePathIfVirtual( $virtualUrl );
return $this->backend->getFileTimestamp( [ 'src' => $path ] );
}
/**
* Get the size of a file with a given virtual URL/storage path
*
* @param string $virtualUrl
* @return int|false
*/
public function getFileSize( $virtualUrl ) {
$path = $this->resolveToStoragePathIfVirtual( $virtualUrl );
return $this->backend->getFileSize( [ 'src' => $path ] );
}
/**
* Get the sha1 (base 36) of a file with a given virtual URL/storage path
*
* @param string $virtualUrl
* @return string|false
*/
public function getFileSha1( $virtualUrl ) {
$path = $this->resolveToStoragePathIfVirtual( $virtualUrl );
return $this->backend->getFileSha1Base36( [ 'src' => $path ] );
}
/**
* Attempt to stream a file with the given virtual URL/storage path
*
* @param string $virtualUrl
* @param array $headers Additional HTTP headers to send on success
* @param array $optHeaders HTTP request headers (if-modified-since, range, ...)
* @return Status
* @since 1.27
*/
public function streamFileWithStatus( $virtualUrl, $headers = [], $optHeaders = [] ) {
$path = $this->resolveToStoragePathIfVirtual( $virtualUrl );
$params = [ 'src' => $path, 'headers' => $headers, 'options' => $optHeaders ];
// T172851: HHVM does not flush the output properly, causing OOM
ob_start( null, 1_048_576 );
ob_implicit_flush( true );
$status = $this->newGood()->merge( $this->backend->streamFile( $params ) );
// T186565: Close the buffer, unless it has already been closed
// in HTTPFileStreamer::resetOutputBuffers().
if ( ob_get_status() ) {
ob_end_flush();
}
return $status;
}
/**
* Call a callback function for every public regular file in the repository.
* This only acts on the current version of files, not any old versions.
* May use either the database or the filesystem.
*
* @param callable $callback
* @return void
*/
public function enumFiles( $callback ) {
$this->enumFilesInStorage( $callback );
}
/**
* Call a callback function for every public file in the repository.
* May use either the database or the filesystem.
*
* @param callable $callback
* @return void
*/
protected function enumFilesInStorage( $callback ) {
$publicRoot = $this->getZonePath( 'public' );
$numDirs = 1 << ( $this->hashLevels * 4 );
// Use a priori assumptions about directory structure
// to reduce the tree height of the scanning process.
for ( $flatIndex = 0; $flatIndex < $numDirs; $flatIndex++ ) {
$hexString = sprintf( "%0{$this->hashLevels}x", $flatIndex );
$path = $publicRoot;
for ( $hexPos = 0; $hexPos < $this->hashLevels; $hexPos++ ) {
$path .= '/' . substr( $hexString, 0, $hexPos + 1 );
}
$iterator = $this->backend->getFileList( [ 'dir' => $path ] );
if ( $iterator === null ) {
throw new RuntimeException( __METHOD__ . ': could not get file listing for ' . $path );
}
foreach ( $iterator as $name ) {
// Each item returned is a public file
call_user_func( $callback, "{$path}/{$name}" );
}
}
}
/**
* Determine if a relative path is valid, i.e. not blank or involving directory traversal
*
* @param string $filename
* @return bool
*/
public function validateFilename( $filename ) {
if ( strval( $filename ) == '' ) {
return false;
}
return FileBackend::isPathTraversalFree( $filename );
}
/**
* Get a callback function to use for cleaning error message parameters
*
* @return callable
*/
private function getErrorCleanupFunction() {
switch ( $this->pathDisclosureProtection ) {
case 'none':
case 'simple': // b/c
$callback = [ $this, 'passThrough' ];
break;
default: // 'paranoid'
$callback = [ $this, 'paranoidClean' ];
}
return $callback;
}
/**
* Path disclosure protection function
*
* @param string $param
* @return string
*/
public function paranoidClean( $param ) {
return '[hidden]';
}
/**
* Path disclosure protection function
*
* @param string $param
* @return string
*/
public function passThrough( $param ) {
return $param;
}
/**
* Create a new fatal error
*
* @param string $message
* @param mixed ...$parameters
* @return Status
*/
public function newFatal( $message, ...$parameters ) {
$status = Status::newFatal( $message, ...$parameters );
$status->cleanCallback = $this->getErrorCleanupFunction();
return $status;
}
/**
* Create a new good result
*
* @param null|mixed $value
* @return Status
*/
public function newGood( $value = null ) {
$status = Status::newGood( $value );
$status->cleanCallback = $this->getErrorCleanupFunction();
return $status;
}
/**
* Checks if there is a redirect named as $title. If there is, return the
* title object. If not, return false.
* STUB
*
* @param PageIdentity|LinkTarget $title Title of image
* @return Title|false
*/
public function checkRedirect( $title ) {
return false;
}
/**
* Invalidates image redirect cache related to that image
* Doesn't do anything for repositories that don't support image redirects.
*
* STUB
* @param PageIdentity|LinkTarget $title Title of image
*/
public function invalidateImageRedirect( $title ) {
}
/**
* Get the human-readable name of the repo
*
* @return string
*/
public function getDisplayName() {
$sitename = MediaWikiServices::getInstance()->getMainConfig()->get( MainConfigNames::Sitename );
if ( $this->isLocal() ) {
return $sitename;
}
// 'shared-repo-name-wikimediacommons' is used when $wgUseInstantCommons = true
return wfMessageFallback( 'shared-repo-name-' . $this->name, 'shared-repo' )->text();
}
/**
* Get the portion of the file that contains the origin file name.
* If that name is too long, then the name "thumbnail.<ext>" will be given.
*
* @param string $name
* @return string
*/
public function nameForThumb( $name ) {
if ( strlen( $name ) > $this->abbrvThreshold ) {
$ext = FileBackend::extensionFromPath( $name );
$name = ( $ext == '' ) ? 'thumbnail' : "thumbnail.$ext";
}
return $name;
}
/**
* Returns true if this the local file repository.
*
* @return bool
*/
public function isLocal() {
return $this->getName() == 'local';
}
/**
* Get a global, repository-qualified, WAN cache key
*
* This might be called from either the site context of the wiki that owns the repo or
* the site context of another wiki that simply has access to the repo. This returns
* false if the repository's cache is not accessible from the current site context.
*
* @param string $kClassSuffix Key collection name suffix (added to this repo class)
* @param mixed ...$components Additional key components
* @return string|false
*/
public function getSharedCacheKey( $kClassSuffix, ...$components ) {
return false;
}
/**
* Get a site-local, repository-qualified, WAN cache key
*
* These cache keys are not shared among different site context and thus cannot be
* directly invalidated when repo objects are modified. These are useful when there
* is no accessible global cache or the values depend on the current site context.
*
* @param string $kClassSuffix Key collection name suffix (added to this repo class)
* @param mixed ...$components Additional key components
* @return string
*/
public function getLocalCacheKey( $kClassSuffix, ...$components ) {
return $this->wanCache->makeKey(
'filerepo-' . $kClassSuffix,
$this->getName(),
...$components
);
}
/**
* Get a temporary private FileRepo associated with this repo.
*
* Files will be created in the temp zone of this repo.
* It will have the same backend as this repo.
*
* @return TempFileRepo
*/
public function getTempRepo() {
return new TempFileRepo( [
'name' => "{$this->name}-temp",
'backend' => $this->backend,
'zones' => [
'public' => [
// Same place storeTemp() uses in the base repo, though
// the path hashing is mismatched, which is annoying.
'container' => $this->zones['temp']['container'],
'directory' => $this->zones['temp']['directory']
],
'thumb' => [
'container' => $this->zones['temp']['container'],
'directory' => $this->zones['temp']['directory'] == ''
? 'thumb'
: $this->zones['temp']['directory'] . '/thumb'
],
'transcoded' => [
'container' => $this->zones['temp']['container'],
'directory' => $this->zones['temp']['directory'] == ''
? 'transcoded'
: $this->zones['temp']['directory'] . '/transcoded'
]
],
'hashLevels' => $this->hashLevels, // performance
'isPrivate' => true // all in temp zone
] );
}
/**
* Get an UploadStash associated with this repo.
*
* @param UserIdentity|null $user
* @return UploadStash
*/
public function getUploadStash( ?UserIdentity $user = null ) {
return new UploadStash( $this, $user );
}
/**
* Throw an exception if this repo is read-only by design.
* This does not and should not check getReadOnlyReason().
*
* @throws LogicException
*/
protected function assertWritableRepo() {
}
/**
* Return information about the repository.
*
* @return array
* @since 1.22
*/
public function getInfo() {
$ret = [
'name' => $this->getName(),
'displayname' => $this->getDisplayName(),
'rootUrl' => $this->getZoneUrl( 'public' ),
'local' => $this->isLocal(),
];
$optionalSettings = [
'url',
'thumbUrl',
'initialCapital',
'descBaseUrl',
'scriptDirUrl',
'articleUrl',
'fetchDescription',
'descriptionCacheExpiry',
];
foreach ( $optionalSettings as $k ) {
if ( isset( $this->$k ) ) {
$ret[$k] = $this->$k;
}
}
if ( isset( $this->favicon ) ) {
// Expand any local path to full URL to improve API usability (T77093).
$ret['favicon'] = MediaWikiServices::getInstance()->getUrlUtils()
->expand( $this->favicon );
}
return $ret;
}
/**
* Returns whether or not storage is SHA-1 based
* @return bool
*/
public function hasSha1Storage() {
return $this->hasSha1Storage;
}
/**
* Returns whether or not repo supports having originals SHA-1s in the thumb URLs
* @return bool
*/
public function supportsSha1URLs() {
return $this->supportsSha1URLs;
}
}
|