Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like LocalFile often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use LocalFile, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 46 | class LocalFile extends File { |
||
| 47 | const CACHE_FIELD_MAX_LEN = 1000; |
||
| 48 | |||
| 49 | /** @var bool Does the file exist on disk? (loadFromXxx) */ |
||
| 50 | protected $fileExists; |
||
| 51 | |||
| 52 | /** @var int Image width */ |
||
| 53 | protected $width; |
||
| 54 | |||
| 55 | /** @var int Image height */ |
||
| 56 | protected $height; |
||
| 57 | |||
| 58 | /** @var int Returned by getimagesize (loadFromXxx) */ |
||
| 59 | protected $bits; |
||
| 60 | |||
| 61 | /** @var string MEDIATYPE_xxx (bitmap, drawing, audio...) */ |
||
| 62 | protected $media_type; |
||
| 63 | |||
| 64 | /** @var string MIME type, determined by MimeMagic::guessMimeType */ |
||
| 65 | protected $mime; |
||
| 66 | |||
| 67 | /** @var int Size in bytes (loadFromXxx) */ |
||
| 68 | protected $size; |
||
| 69 | |||
| 70 | /** @var string Handler-specific metadata */ |
||
| 71 | protected $metadata; |
||
| 72 | |||
| 73 | /** @var string SHA-1 base 36 content hash */ |
||
| 74 | protected $sha1; |
||
| 75 | |||
| 76 | /** @var bool Whether or not core data has been loaded from the database (loadFromXxx) */ |
||
| 77 | protected $dataLoaded; |
||
| 78 | |||
| 79 | /** @var bool Whether or not lazy-loaded data has been loaded from the database */ |
||
| 80 | protected $extraDataLoaded; |
||
| 81 | |||
| 82 | /** @var int Bitfield akin to rev_deleted */ |
||
| 83 | protected $deleted; |
||
| 84 | |||
| 85 | /** @var string */ |
||
| 86 | protected $repoClass = 'LocalRepo'; |
||
| 87 | |||
| 88 | /** @var int Number of line to return by nextHistoryLine() (constructor) */ |
||
| 89 | private $historyLine; |
||
| 90 | |||
| 91 | /** @var int Result of the query for the file's history (nextHistoryLine) */ |
||
| 92 | private $historyRes; |
||
| 93 | |||
| 94 | /** @var string Major MIME type */ |
||
| 95 | private $major_mime; |
||
| 96 | |||
| 97 | /** @var string Minor MIME type */ |
||
| 98 | private $minor_mime; |
||
| 99 | |||
| 100 | /** @var string Upload timestamp */ |
||
| 101 | private $timestamp; |
||
| 102 | |||
| 103 | /** @var int User ID of uploader */ |
||
| 104 | private $user; |
||
| 105 | |||
| 106 | /** @var string User name of uploader */ |
||
| 107 | private $user_text; |
||
| 108 | |||
| 109 | /** @var string Description of current revision of the file */ |
||
| 110 | private $description; |
||
| 111 | |||
| 112 | /** @var string TS_MW timestamp of the last change of the file description */ |
||
| 113 | private $descriptionTouched; |
||
| 114 | |||
| 115 | /** @var bool Whether the row was upgraded on load */ |
||
| 116 | private $upgraded; |
||
| 117 | |||
| 118 | /** @var bool True if the image row is locked */ |
||
| 119 | private $locked; |
||
| 120 | |||
| 121 | /** @var bool True if the image row is locked with a lock initiated transaction */ |
||
| 122 | private $lockedOwnTrx; |
||
| 123 | |||
| 124 | /** @var bool True if file is not present in file system. Not to be cached in memcached */ |
||
| 125 | private $missing; |
||
| 126 | |||
| 127 | // @note: higher than IDBAccessObject constants |
||
| 128 | const LOAD_ALL = 16; // integer; load all the lazy fields too (like metadata) |
||
| 129 | |||
| 130 | /** |
||
| 131 | * Create a LocalFile from a title |
||
| 132 | * Do not call this except from inside a repo class. |
||
| 133 | * |
||
| 134 | * Note: $unused param is only here to avoid an E_STRICT |
||
| 135 | * |
||
| 136 | * @param Title $title |
||
| 137 | * @param FileRepo $repo |
||
| 138 | * @param null $unused |
||
| 139 | * |
||
| 140 | * @return LocalFile |
||
| 141 | */ |
||
| 142 | static function newFromTitle( $title, $repo, $unused = null ) { |
||
| 145 | |||
| 146 | /** |
||
| 147 | * Create a LocalFile from a title |
||
| 148 | * Do not call this except from inside a repo class. |
||
| 149 | * |
||
| 150 | * @param stdClass $row |
||
| 151 | * @param FileRepo $repo |
||
| 152 | * |
||
| 153 | * @return LocalFile |
||
| 154 | */ |
||
| 155 | View Code Duplication | static function newFromRow( $row, $repo ) { |
|
| 162 | |||
| 163 | /** |
||
| 164 | * Create a LocalFile from a SHA-1 key |
||
| 165 | * Do not call this except from inside a repo class. |
||
| 166 | * |
||
| 167 | * @param string $sha1 Base-36 SHA-1 |
||
| 168 | * @param LocalRepo $repo |
||
| 169 | * @param string|bool $timestamp MW_timestamp (optional) |
||
| 170 | * @return bool|LocalFile |
||
| 171 | */ |
||
| 172 | View Code Duplication | static function newFromKey( $sha1, $repo, $timestamp = false ) { |
|
| 187 | |||
| 188 | /** |
||
| 189 | * Fields in the image table |
||
| 190 | * @return array |
||
| 191 | */ |
||
| 192 | static function selectFields() { |
||
| 210 | |||
| 211 | /** |
||
| 212 | * Constructor. |
||
| 213 | * Do not call this except from inside a repo class. |
||
| 214 | * @param Title $title |
||
| 215 | * @param FileRepo $repo |
||
| 216 | */ |
||
| 217 | function __construct( $title, $repo ) { |
||
| 229 | |||
| 230 | /** |
||
| 231 | * Get the memcached key for the main data for this file, or false if |
||
| 232 | * there is no access to the shared cache. |
||
| 233 | * @return string|bool |
||
| 234 | */ |
||
| 235 | function getCacheKey() { |
||
| 240 | |||
| 241 | /** |
||
| 242 | * Try to load file metadata from memcached. Returns true on success. |
||
| 243 | * @return bool |
||
| 244 | */ |
||
| 245 | function loadFromCache() { |
||
| 278 | |||
| 279 | /** |
||
| 280 | * Save the file metadata to memcached |
||
| 281 | */ |
||
| 282 | function saveToCache() { |
||
| 314 | |||
| 315 | /** |
||
| 316 | * Purge the file object/metadata cache |
||
| 317 | */ |
||
| 318 | public function invalidateCache() { |
||
| 328 | |||
| 329 | /** |
||
| 330 | * Load metadata from the file itself |
||
| 331 | */ |
||
| 332 | function loadFromFile() { |
||
| 336 | |||
| 337 | /** |
||
| 338 | * @param string $prefix |
||
| 339 | * @return array |
||
| 340 | */ |
||
| 341 | function getCacheFields( $prefix = 'img_' ) { |
||
| 361 | |||
| 362 | /** |
||
| 363 | * @param string $prefix |
||
| 364 | * @return array |
||
| 365 | */ |
||
| 366 | function getLazyCacheFields( $prefix = 'img_' ) { |
||
| 384 | |||
| 385 | /** |
||
| 386 | * Load file metadata from the DB |
||
| 387 | * @param int $flags |
||
| 388 | */ |
||
| 389 | function loadFromDB( $flags = 0 ) { |
||
| 409 | |||
| 410 | /** |
||
| 411 | * Load lazy file metadata from the DB. |
||
| 412 | * This covers fields that are sometimes not cached. |
||
| 413 | */ |
||
| 414 | protected function loadExtraFromDB() { |
||
| 433 | |||
| 434 | /** |
||
| 435 | * @param IDatabase $dbr |
||
| 436 | * @param string $fname |
||
| 437 | * @return array|bool |
||
| 438 | */ |
||
| 439 | private function loadFieldsWithTimestamp( $dbr, $fname ) { |
||
| 459 | |||
| 460 | /** |
||
| 461 | * @param array|object $row |
||
| 462 | * @param string $prefix |
||
| 463 | * @throws MWException |
||
| 464 | * @return array |
||
| 465 | */ |
||
| 466 | protected function unprefixRow( $row, $prefix = 'img_' ) { |
||
| 482 | |||
| 483 | /** |
||
| 484 | * Decode a row from the database (either object or array) to an array |
||
| 485 | * with timestamps and MIME types decoded, and the field prefix removed. |
||
| 486 | * @param object $row |
||
| 487 | * @param string $prefix |
||
| 488 | * @throws MWException |
||
| 489 | * @return array |
||
| 490 | */ |
||
| 491 | function decodeRow( $row, $prefix = 'img_' ) { |
||
| 520 | |||
| 521 | /** |
||
| 522 | * Load file metadata from a DB result row |
||
| 523 | * |
||
| 524 | * @param object $row |
||
| 525 | * @param string $prefix |
||
| 526 | */ |
||
| 527 | function loadFromRow( $row, $prefix = 'img_' ) { |
||
| 540 | |||
| 541 | /** |
||
| 542 | * Load file metadata from cache or DB, unless already loaded |
||
| 543 | * @param int $flags |
||
| 544 | */ |
||
| 545 | function load( $flags = 0 ) { |
||
| 558 | |||
| 559 | /** |
||
| 560 | * Upgrade a row if it needs it |
||
| 561 | */ |
||
| 562 | function maybeUpgradeRow() { |
||
| 586 | |||
| 587 | function getUpgraded() { |
||
| 590 | |||
| 591 | /** |
||
| 592 | * Fix assorted version-related problems with the image row by reloading it from the file |
||
| 593 | */ |
||
| 594 | function upgradeRow() { |
||
| 595 | |||
| 596 | $this->lock(); // begin |
||
| 597 | |||
| 598 | $this->loadFromFile(); |
||
| 599 | |||
| 600 | # Don't destroy file info of missing files |
||
| 601 | if ( !$this->fileExists ) { |
||
| 602 | $this->unlock(); |
||
| 603 | wfDebug( __METHOD__ . ": file does not exist, aborting\n" ); |
||
| 604 | |||
| 605 | return; |
||
| 606 | } |
||
| 607 | |||
| 608 | $dbw = $this->repo->getMasterDB(); |
||
| 609 | list( $major, $minor ) = self::splitMime( $this->mime ); |
||
| 610 | |||
| 611 | if ( wfReadOnly() ) { |
||
| 612 | $this->unlock(); |
||
| 613 | |||
| 614 | return; |
||
| 615 | } |
||
| 616 | wfDebug( __METHOD__ . ': upgrading ' . $this->getName() . " to the current schema\n" ); |
||
| 617 | |||
| 618 | $dbw->update( 'image', |
||
| 619 | [ |
||
| 620 | 'img_size' => $this->size, // sanity |
||
| 621 | 'img_width' => $this->width, |
||
| 622 | 'img_height' => $this->height, |
||
| 623 | 'img_bits' => $this->bits, |
||
| 624 | 'img_media_type' => $this->media_type, |
||
| 625 | 'img_major_mime' => $major, |
||
| 626 | 'img_minor_mime' => $minor, |
||
| 627 | 'img_metadata' => $dbw->encodeBlob( $this->metadata ), |
||
| 628 | 'img_sha1' => $this->sha1, |
||
| 629 | ], |
||
| 630 | [ 'img_name' => $this->getName() ], |
||
| 631 | __METHOD__ |
||
| 632 | ); |
||
| 633 | |||
| 634 | $this->invalidateCache(); |
||
| 635 | |||
| 636 | $this->unlock(); // done |
||
| 637 | |||
| 638 | } |
||
| 639 | |||
| 640 | /** |
||
| 641 | * Set properties in this object to be equal to those given in the |
||
| 642 | * associative array $info. Only cacheable fields can be set. |
||
| 643 | * All fields *must* be set in $info except for getLazyCacheFields(). |
||
| 644 | * |
||
| 645 | * If 'mime' is given, it will be split into major_mime/minor_mime. |
||
| 646 | * If major_mime/minor_mime are given, $this->mime will also be set. |
||
| 647 | * |
||
| 648 | * @param array $info |
||
| 649 | */ |
||
| 650 | function setProps( $info ) { |
||
| 669 | |||
| 670 | /** splitMime inherited */ |
||
| 671 | /** getName inherited */ |
||
| 672 | /** getTitle inherited */ |
||
| 673 | /** getURL inherited */ |
||
| 674 | /** getViewURL inherited */ |
||
| 675 | /** getPath inherited */ |
||
| 676 | /** isVisible inherited */ |
||
| 677 | |||
| 678 | /** |
||
| 679 | * @return bool |
||
| 680 | */ |
||
| 681 | function isMissing() { |
||
| 689 | |||
| 690 | /** |
||
| 691 | * Return the width of the image |
||
| 692 | * |
||
| 693 | * @param int $page |
||
| 694 | * @return int |
||
| 695 | */ |
||
| 696 | View Code Duplication | public function getWidth( $page = 1 ) { |
|
| 716 | |||
| 717 | /** |
||
| 718 | * Return the height of the image |
||
| 719 | * |
||
| 720 | * @param int $page |
||
| 721 | * @return int |
||
| 722 | */ |
||
| 723 | View Code Duplication | public function getHeight( $page = 1 ) { |
|
| 743 | |||
| 744 | /** |
||
| 745 | * Returns ID or name of user who uploaded the file |
||
| 746 | * |
||
| 747 | * @param string $type 'text' or 'id' |
||
| 748 | * @return int|string |
||
| 749 | */ |
||
| 750 | function getUser( $type = 'text' ) { |
||
| 759 | |||
| 760 | /** |
||
| 761 | * Get short description URL for a file based on the page ID. |
||
| 762 | * |
||
| 763 | * @return string|null |
||
| 764 | * @throws MWException |
||
| 765 | * @since 1.27 |
||
| 766 | */ |
||
| 767 | public function getDescriptionShortUrl() { |
||
| 778 | |||
| 779 | /** |
||
| 780 | * Get handler-specific metadata |
||
| 781 | * @return string |
||
| 782 | */ |
||
| 783 | function getMetadata() { |
||
| 787 | |||
| 788 | /** |
||
| 789 | * @return int |
||
| 790 | */ |
||
| 791 | function getBitDepth() { |
||
| 796 | |||
| 797 | /** |
||
| 798 | * Returns the size of the image file, in bytes |
||
| 799 | * @return int |
||
| 800 | */ |
||
| 801 | public function getSize() { |
||
| 806 | |||
| 807 | /** |
||
| 808 | * Returns the MIME type of the file. |
||
| 809 | * @return string |
||
| 810 | */ |
||
| 811 | function getMimeType() { |
||
| 816 | |||
| 817 | /** |
||
| 818 | * Returns the type of the media in the file. |
||
| 819 | * Use the value returned by this function with the MEDIATYPE_xxx constants. |
||
| 820 | * @return string |
||
| 821 | */ |
||
| 822 | function getMediaType() { |
||
| 827 | |||
| 828 | /** canRender inherited */ |
||
| 829 | /** mustRender inherited */ |
||
| 830 | /** allowInlineDisplay inherited */ |
||
| 831 | /** isSafeFile inherited */ |
||
| 832 | /** isTrustedFile inherited */ |
||
| 833 | |||
| 834 | /** |
||
| 835 | * Returns true if the file exists on disk. |
||
| 836 | * @return bool Whether file exist on disk. |
||
| 837 | */ |
||
| 838 | public function exists() { |
||
| 843 | |||
| 844 | /** getTransformScript inherited */ |
||
| 845 | /** getUnscaledThumb inherited */ |
||
| 846 | /** thumbName inherited */ |
||
| 847 | /** createThumb inherited */ |
||
| 848 | /** transform inherited */ |
||
| 849 | |||
| 850 | /** getHandler inherited */ |
||
| 851 | /** iconThumb inherited */ |
||
| 852 | /** getLastError inherited */ |
||
| 853 | |||
| 854 | /** |
||
| 855 | * Get all thumbnail names previously generated for this file |
||
| 856 | * @param string|bool $archiveName Name of an archive file, default false |
||
| 857 | * @return array First element is the base dir, then files in that base dir. |
||
| 858 | */ |
||
| 859 | function getThumbnails( $archiveName = false ) { |
||
| 878 | |||
| 879 | /** |
||
| 880 | * Refresh metadata in memcached, but don't touch thumbnails or CDN |
||
| 881 | */ |
||
| 882 | function purgeMetadataCache() { |
||
| 885 | |||
| 886 | /** |
||
| 887 | * Delete all previously generated thumbnails, refresh metadata in memcached and purge the CDN. |
||
| 888 | * |
||
| 889 | * @param array $options An array potentially with the key forThumbRefresh. |
||
| 890 | * |
||
| 891 | * @note This used to purge old thumbnails by default as well, but doesn't anymore. |
||
| 892 | */ |
||
| 893 | function purgeCache( $options = [] ) { |
||
| 906 | |||
| 907 | /** |
||
| 908 | * Delete cached transformed files for an archived version only. |
||
| 909 | * @param string $archiveName Name of the archived file |
||
| 910 | */ |
||
| 911 | function purgeOldThumbnails( $archiveName ) { |
||
| 928 | |||
| 929 | /** |
||
| 930 | * Delete cached transformed files for the current version only. |
||
| 931 | * @param array $options |
||
| 932 | */ |
||
| 933 | public function purgeThumbnails( $options = [] ) { |
||
| 960 | |||
| 961 | /** |
||
| 962 | * Delete a list of thumbnails visible at urls |
||
| 963 | * @param string $dir Base dir of the files. |
||
| 964 | * @param array $files Array of strings: relative filenames (to $dir) |
||
| 965 | */ |
||
| 966 | protected function purgeThumbList( $dir, $files ) { |
||
| 989 | |||
| 990 | /** purgeDescription inherited */ |
||
| 991 | /** purgeEverything inherited */ |
||
| 992 | |||
| 993 | /** |
||
| 994 | * @param int $limit Optional: Limit to number of results |
||
| 995 | * @param int $start Optional: Timestamp, start from |
||
| 996 | * @param int $end Optional: Timestamp, end at |
||
| 997 | * @param bool $inc |
||
| 998 | * @return OldLocalFile[] |
||
| 999 | */ |
||
| 1000 | function getHistory( $limit = null, $start = null, $end = null, $inc = true ) { |
||
| 1041 | |||
| 1042 | /** |
||
| 1043 | * Returns the history of this file, line by line. |
||
| 1044 | * starts with current version, then old versions. |
||
| 1045 | * uses $this->historyLine to check which line to return: |
||
| 1046 | * 0 return line for current version |
||
| 1047 | * 1 query for old versions, return first one |
||
| 1048 | * 2, ... return next old version from above query |
||
| 1049 | * @return bool |
||
| 1050 | */ |
||
| 1051 | public function nextHistoryLine() { |
||
| 1052 | # Polymorphic function name to distinguish foreign and local fetches |
||
| 1053 | $fname = get_class( $this ) . '::' . __FUNCTION__; |
||
| 1054 | |||
| 1055 | $dbr = $this->repo->getSlaveDB(); |
||
| 1056 | |||
| 1057 | if ( $this->historyLine == 0 ) { // called for the first time, return line from cur |
||
| 1058 | $this->historyRes = $dbr->select( 'image', |
||
| 1059 | [ |
||
| 1060 | '*', |
||
| 1061 | "'' AS oi_archive_name", |
||
| 1062 | '0 as oi_deleted', |
||
| 1063 | 'img_sha1' |
||
| 1064 | ], |
||
| 1065 | [ 'img_name' => $this->title->getDBkey() ], |
||
| 1066 | $fname |
||
| 1067 | ); |
||
| 1068 | |||
| 1069 | if ( 0 == $dbr->numRows( $this->historyRes ) ) { |
||
| 1070 | $this->historyRes = null; |
||
| 1071 | |||
| 1072 | return false; |
||
| 1073 | } |
||
| 1074 | } elseif ( $this->historyLine == 1 ) { |
||
| 1075 | $this->historyRes = $dbr->select( 'oldimage', '*', |
||
| 1076 | [ 'oi_name' => $this->title->getDBkey() ], |
||
| 1077 | $fname, |
||
| 1078 | [ 'ORDER BY' => 'oi_timestamp DESC' ] |
||
| 1079 | ); |
||
| 1080 | } |
||
| 1081 | $this->historyLine++; |
||
| 1082 | |||
| 1083 | return $dbr->fetchObject( $this->historyRes ); |
||
| 1084 | } |
||
| 1085 | |||
| 1086 | /** |
||
| 1087 | * Reset the history pointer to the first element of the history |
||
| 1088 | */ |
||
| 1089 | public function resetHistory() { |
||
| 1096 | |||
| 1097 | /** getHashPath inherited */ |
||
| 1098 | /** getRel inherited */ |
||
| 1099 | /** getUrlRel inherited */ |
||
| 1100 | /** getArchiveRel inherited */ |
||
| 1101 | /** getArchivePath inherited */ |
||
| 1102 | /** getThumbPath inherited */ |
||
| 1103 | /** getArchiveUrl inherited */ |
||
| 1104 | /** getThumbUrl inherited */ |
||
| 1105 | /** getArchiveVirtualUrl inherited */ |
||
| 1106 | /** getThumbVirtualUrl inherited */ |
||
| 1107 | /** isHashed inherited */ |
||
| 1108 | |||
| 1109 | /** |
||
| 1110 | * Upload a file and record it in the DB |
||
| 1111 | * @param string|FSFile $src Source storage path, virtual URL, or filesystem path |
||
| 1112 | * @param string $comment Upload description |
||
| 1113 | * @param string $pageText Text to use for the new description page, |
||
| 1114 | * if a new description page is created |
||
| 1115 | * @param int|bool $flags Flags for publish() |
||
| 1116 | * @param array|bool $props File properties, if known. This can be used to |
||
| 1117 | * reduce the upload time when uploading virtual URLs for which the file |
||
| 1118 | * info is already known |
||
| 1119 | * @param string|bool $timestamp Timestamp for img_timestamp, or false to use the |
||
| 1120 | * current time |
||
| 1121 | * @param User|null $user User object or null to use $wgUser |
||
| 1122 | * @param string[] $tags Change tags to add to the log entry and page revision. |
||
| 1123 | * (This doesn't check $user's permissions.) |
||
| 1124 | * @return FileRepoStatus On success, the value member contains the |
||
| 1125 | * archive name, or an empty string if it was a new file. |
||
| 1126 | */ |
||
| 1127 | function upload( $src, $comment, $pageText, $flags = 0, $props = false, |
||
| 1128 | $timestamp = false, $user = null, $tags = [] |
||
| 1129 | ) { |
||
| 1130 | global $wgContLang; |
||
| 1131 | |||
| 1132 | if ( $this->getRepo()->getReadOnlyReason() !== false ) { |
||
| 1133 | return $this->readOnlyFatalStatus(); |
||
| 1134 | } |
||
| 1135 | |||
| 1136 | $srcPath = ( $src instanceof FSFile ) ? $src->getPath() : $src; |
||
| 1137 | if ( !$props ) { |
||
| 1138 | if ( $this->repo->isVirtualUrl( $srcPath ) |
||
| 1139 | || FileBackend::isStoragePath( $srcPath ) |
||
| 1140 | ) { |
||
| 1141 | $props = $this->repo->getFileProps( $srcPath ); |
||
| 1142 | } else { |
||
| 1143 | $props = FSFile::getPropsFromPath( $srcPath ); |
||
| 1144 | } |
||
| 1145 | } |
||
| 1146 | |||
| 1147 | $options = []; |
||
| 1148 | $handler = MediaHandler::getHandler( $props['mime'] ); |
||
| 1149 | View Code Duplication | if ( $handler ) { |
|
| 1150 | $options['headers'] = $handler->getStreamHeaders( $props['metadata'] ); |
||
| 1151 | } else { |
||
| 1152 | $options['headers'] = []; |
||
| 1153 | } |
||
| 1154 | |||
| 1155 | // Trim spaces on user supplied text |
||
| 1156 | $comment = trim( $comment ); |
||
| 1157 | |||
| 1158 | // Truncate nicely or the DB will do it for us |
||
| 1159 | // non-nicely (dangling multi-byte chars, non-truncated version in cache). |
||
| 1160 | $comment = $wgContLang->truncate( $comment, 255 ); |
||
| 1161 | $this->lock(); // begin |
||
| 1162 | $status = $this->publish( $src, $flags, $options ); |
||
| 1163 | |||
| 1164 | if ( $status->successCount >= 2 ) { |
||
| 1165 | // There will be a copy+(one of move,copy,store). |
||
| 1166 | // The first succeeding does not commit us to updating the DB |
||
| 1167 | // since it simply copied the current version to a timestamped file name. |
||
| 1168 | // It is only *preferable* to avoid leaving such files orphaned. |
||
| 1169 | // Once the second operation goes through, then the current version was |
||
| 1170 | // updated and we must therefore update the DB too. |
||
| 1171 | $oldver = $status->value; |
||
| 1172 | if ( !$this->recordUpload2( $oldver, $comment, $pageText, $props, $timestamp, $user, $tags ) ) { |
||
| 1173 | $status->fatal( 'filenotfound', $srcPath ); |
||
| 1174 | } |
||
| 1175 | } |
||
| 1176 | |||
| 1177 | $this->unlock(); // done |
||
| 1178 | |||
| 1179 | return $status; |
||
| 1180 | } |
||
| 1181 | |||
| 1182 | /** |
||
| 1183 | * Record a file upload in the upload log and the image table |
||
| 1184 | * @param string $oldver |
||
| 1185 | * @param string $desc |
||
| 1186 | * @param string $license |
||
| 1187 | * @param string $copyStatus |
||
| 1188 | * @param string $source |
||
| 1189 | * @param bool $watch |
||
| 1190 | * @param string|bool $timestamp |
||
| 1191 | * @param User|null $user User object or null to use $wgUser |
||
| 1192 | * @return bool |
||
| 1193 | */ |
||
| 1194 | function recordUpload( $oldver, $desc, $license = '', $copyStatus = '', $source = '', |
||
| 1195 | $watch = false, $timestamp = false, User $user = null ) { |
||
| 1196 | if ( !$user ) { |
||
| 1197 | global $wgUser; |
||
| 1198 | $user = $wgUser; |
||
| 1199 | } |
||
| 1200 | |||
| 1201 | $pageText = SpecialUpload::getInitialPageText( $desc, $license, $copyStatus, $source ); |
||
| 1202 | |||
| 1203 | if ( !$this->recordUpload2( $oldver, $desc, $pageText, false, $timestamp, $user ) ) { |
||
| 1204 | return false; |
||
| 1205 | } |
||
| 1206 | |||
| 1207 | if ( $watch ) { |
||
| 1208 | $user->addWatch( $this->getTitle() ); |
||
| 1209 | } |
||
| 1210 | |||
| 1211 | return true; |
||
| 1212 | } |
||
| 1213 | |||
| 1214 | /** |
||
| 1215 | * Record a file upload in the upload log and the image table |
||
| 1216 | * @param string $oldver |
||
| 1217 | * @param string $comment |
||
| 1218 | * @param string $pageText |
||
| 1219 | * @param bool|array $props |
||
| 1220 | * @param string|bool $timestamp |
||
| 1221 | * @param null|User $user |
||
| 1222 | * @param string[] $tags |
||
| 1223 | * @return bool |
||
| 1224 | */ |
||
| 1225 | function recordUpload2( |
||
| 1226 | $oldver, $comment, $pageText, $props = false, $timestamp = false, $user = null, $tags = [] |
||
| 1227 | ) { |
||
| 1228 | if ( is_null( $user ) ) { |
||
| 1229 | global $wgUser; |
||
| 1230 | $user = $wgUser; |
||
| 1231 | } |
||
| 1232 | |||
| 1233 | $dbw = $this->repo->getMasterDB(); |
||
| 1234 | |||
| 1235 | # Imports or such might force a certain timestamp; otherwise we generate |
||
| 1236 | # it and can fudge it slightly to keep (name,timestamp) unique on re-upload. |
||
| 1237 | if ( $timestamp === false ) { |
||
| 1238 | $timestamp = $dbw->timestamp(); |
||
| 1239 | $allowTimeKludge = true; |
||
| 1240 | } else { |
||
| 1241 | $allowTimeKludge = false; |
||
| 1242 | } |
||
| 1243 | |||
| 1244 | $props = $props ?: $this->repo->getFileProps( $this->getVirtualUrl() ); |
||
| 1245 | $props['description'] = $comment; |
||
| 1246 | $props['user'] = $user->getId(); |
||
| 1247 | $props['user_text'] = $user->getName(); |
||
| 1248 | $props['timestamp'] = wfTimestamp( TS_MW, $timestamp ); // DB -> TS_MW |
||
| 1249 | $this->setProps( $props ); |
||
| 1250 | |||
| 1251 | # Fail now if the file isn't there |
||
| 1252 | if ( !$this->fileExists ) { |
||
| 1253 | wfDebug( __METHOD__ . ": File " . $this->getRel() . " went missing!\n" ); |
||
| 1254 | |||
| 1255 | return false; |
||
| 1256 | } |
||
| 1257 | |||
| 1258 | $dbw->startAtomic( __METHOD__ ); |
||
| 1259 | |||
| 1260 | # Test to see if the row exists using INSERT IGNORE |
||
| 1261 | # This avoids race conditions by locking the row until the commit, and also |
||
| 1262 | # doesn't deadlock. SELECT FOR UPDATE causes a deadlock for every race condition. |
||
| 1263 | $dbw->insert( 'image', |
||
| 1264 | [ |
||
| 1265 | 'img_name' => $this->getName(), |
||
| 1266 | 'img_size' => $this->size, |
||
| 1267 | 'img_width' => intval( $this->width ), |
||
| 1268 | 'img_height' => intval( $this->height ), |
||
| 1269 | 'img_bits' => $this->bits, |
||
| 1270 | 'img_media_type' => $this->media_type, |
||
| 1271 | 'img_major_mime' => $this->major_mime, |
||
| 1272 | 'img_minor_mime' => $this->minor_mime, |
||
| 1273 | 'img_timestamp' => $timestamp, |
||
| 1274 | 'img_description' => $comment, |
||
| 1275 | 'img_user' => $user->getId(), |
||
| 1276 | 'img_user_text' => $user->getName(), |
||
| 1277 | 'img_metadata' => $dbw->encodeBlob( $this->metadata ), |
||
| 1278 | 'img_sha1' => $this->sha1 |
||
| 1279 | ], |
||
| 1280 | __METHOD__, |
||
| 1281 | 'IGNORE' |
||
| 1282 | ); |
||
| 1283 | |||
| 1284 | $reupload = ( $dbw->affectedRows() == 0 ); |
||
| 1285 | if ( $reupload ) { |
||
| 1286 | if ( $allowTimeKludge ) { |
||
| 1287 | # Use LOCK IN SHARE MODE to ignore any transaction snapshotting |
||
| 1288 | $ltimestamp = $dbw->selectField( |
||
| 1289 | 'image', |
||
| 1290 | 'img_timestamp', |
||
| 1291 | [ 'img_name' => $this->getName() ], |
||
| 1292 | __METHOD__, |
||
| 1293 | [ 'LOCK IN SHARE MODE' ] |
||
| 1294 | ); |
||
| 1295 | $lUnixtime = $ltimestamp ? wfTimestamp( TS_UNIX, $ltimestamp ) : false; |
||
| 1296 | # Avoid a timestamp that is not newer than the last version |
||
| 1297 | # TODO: the image/oldimage tables should be like page/revision with an ID field |
||
| 1298 | if ( $lUnixtime && wfTimestamp( TS_UNIX, $timestamp ) <= $lUnixtime ) { |
||
| 1299 | sleep( 1 ); // fast enough re-uploads would go far in the future otherwise |
||
| 1300 | $timestamp = $dbw->timestamp( $lUnixtime + 1 ); |
||
| 1301 | $this->timestamp = wfTimestamp( TS_MW, $timestamp ); // DB -> TS_MW |
||
| 1302 | } |
||
| 1303 | } |
||
| 1304 | |||
| 1305 | # (bug 34993) Note: $oldver can be empty here, if the previous |
||
| 1306 | # version of the file was broken. Allow registration of the new |
||
| 1307 | # version to continue anyway, because that's better than having |
||
| 1308 | # an image that's not fixable by user operations. |
||
| 1309 | # Collision, this is an update of a file |
||
| 1310 | # Insert previous contents into oldimage |
||
| 1311 | $dbw->insertSelect( 'oldimage', 'image', |
||
| 1312 | [ |
||
| 1313 | 'oi_name' => 'img_name', |
||
| 1314 | 'oi_archive_name' => $dbw->addQuotes( $oldver ), |
||
| 1315 | 'oi_size' => 'img_size', |
||
| 1316 | 'oi_width' => 'img_width', |
||
| 1317 | 'oi_height' => 'img_height', |
||
| 1318 | 'oi_bits' => 'img_bits', |
||
| 1319 | 'oi_timestamp' => 'img_timestamp', |
||
| 1320 | 'oi_description' => 'img_description', |
||
| 1321 | 'oi_user' => 'img_user', |
||
| 1322 | 'oi_user_text' => 'img_user_text', |
||
| 1323 | 'oi_metadata' => 'img_metadata', |
||
| 1324 | 'oi_media_type' => 'img_media_type', |
||
| 1325 | 'oi_major_mime' => 'img_major_mime', |
||
| 1326 | 'oi_minor_mime' => 'img_minor_mime', |
||
| 1327 | 'oi_sha1' => 'img_sha1' |
||
| 1328 | ], |
||
| 1329 | [ 'img_name' => $this->getName() ], |
||
| 1330 | __METHOD__ |
||
| 1331 | ); |
||
| 1332 | |||
| 1333 | # Update the current image row |
||
| 1334 | $dbw->update( 'image', |
||
| 1335 | [ |
||
| 1336 | 'img_size' => $this->size, |
||
| 1337 | 'img_width' => intval( $this->width ), |
||
| 1338 | 'img_height' => intval( $this->height ), |
||
| 1339 | 'img_bits' => $this->bits, |
||
| 1340 | 'img_media_type' => $this->media_type, |
||
| 1341 | 'img_major_mime' => $this->major_mime, |
||
| 1342 | 'img_minor_mime' => $this->minor_mime, |
||
| 1343 | 'img_timestamp' => $timestamp, |
||
| 1344 | 'img_description' => $comment, |
||
| 1345 | 'img_user' => $user->getId(), |
||
| 1346 | 'img_user_text' => $user->getName(), |
||
| 1347 | 'img_metadata' => $dbw->encodeBlob( $this->metadata ), |
||
| 1348 | 'img_sha1' => $this->sha1 |
||
| 1349 | ], |
||
| 1350 | [ 'img_name' => $this->getName() ], |
||
| 1351 | __METHOD__ |
||
| 1352 | ); |
||
| 1353 | } |
||
| 1354 | |||
| 1355 | $descTitle = $this->getTitle(); |
||
| 1356 | $descId = $descTitle->getArticleID(); |
||
| 1357 | $wikiPage = new WikiFilePage( $descTitle ); |
||
| 1358 | $wikiPage->setFile( $this ); |
||
| 1359 | |||
| 1360 | // Add the log entry... |
||
| 1361 | $logEntry = new ManualLogEntry( 'upload', $reupload ? 'overwrite' : 'upload' ); |
||
| 1362 | $logEntry->setTimestamp( $this->timestamp ); |
||
| 1363 | $logEntry->setPerformer( $user ); |
||
| 1364 | $logEntry->setComment( $comment ); |
||
| 1365 | $logEntry->setTarget( $descTitle ); |
||
| 1366 | // Allow people using the api to associate log entries with the upload. |
||
| 1367 | // Log has a timestamp, but sometimes different from upload timestamp. |
||
| 1368 | $logEntry->setParameters( |
||
| 1369 | [ |
||
| 1370 | 'img_sha1' => $this->sha1, |
||
| 1371 | 'img_timestamp' => $timestamp, |
||
| 1372 | ] |
||
| 1373 | ); |
||
| 1374 | // Note we keep $logId around since during new image |
||
| 1375 | // creation, page doesn't exist yet, so log_page = 0 |
||
| 1376 | // but we want it to point to the page we're making, |
||
| 1377 | // so we later modify the log entry. |
||
| 1378 | // For a similar reason, we avoid making an RC entry |
||
| 1379 | // now and wait until the page exists. |
||
| 1380 | $logId = $logEntry->insert(); |
||
| 1381 | |||
| 1382 | if ( $descTitle->exists() ) { |
||
| 1383 | // Use own context to get the action text in content language |
||
| 1384 | $formatter = LogFormatter::newFromEntry( $logEntry ); |
||
| 1385 | $formatter->setContext( RequestContext::newExtraneousContext( $descTitle ) ); |
||
| 1386 | $editSummary = $formatter->getPlainActionText(); |
||
| 1387 | |||
| 1388 | $nullRevision = Revision::newNullRevision( |
||
| 1389 | $dbw, |
||
| 1390 | $descId, |
||
| 1391 | $editSummary, |
||
| 1392 | false, |
||
| 1393 | $user |
||
| 1394 | ); |
||
| 1395 | if ( $nullRevision ) { |
||
| 1396 | $nullRevision->insertOn( $dbw ); |
||
| 1397 | Hooks::run( |
||
| 1398 | 'NewRevisionFromEditComplete', |
||
| 1399 | [ $wikiPage, $nullRevision, $nullRevision->getParentId(), $user ] |
||
| 1400 | ); |
||
| 1401 | $wikiPage->updateRevisionOn( $dbw, $nullRevision ); |
||
| 1402 | // Associate null revision id |
||
| 1403 | $logEntry->setAssociatedRevId( $nullRevision->getId() ); |
||
| 1404 | } |
||
| 1405 | |||
| 1406 | $newPageContent = null; |
||
| 1407 | } else { |
||
| 1408 | // Make the description page and RC log entry post-commit |
||
| 1409 | $newPageContent = ContentHandler::makeContent( $pageText, $descTitle ); |
||
| 1410 | } |
||
| 1411 | |||
| 1412 | # Defer purges, page creation, and link updates in case they error out. |
||
| 1413 | # The most important thing is that files and the DB registry stay synced. |
||
| 1414 | $dbw->endAtomic( __METHOD__ ); |
||
| 1415 | |||
| 1416 | # Do some cache purges after final commit so that: |
||
| 1417 | # a) Changes are more likely to be seen post-purge |
||
| 1418 | # b) They won't cause rollback of the log publish/update above |
||
| 1419 | $that = $this; |
||
| 1420 | $dbw->onTransactionIdle( function () use ( |
||
| 1421 | $that, $reupload, $wikiPage, $newPageContent, $comment, $user, $logEntry, $logId, $descId, $tags |
||
| 1422 | ) { |
||
| 1423 | # Update memcache after the commit |
||
| 1424 | $that->invalidateCache(); |
||
| 1425 | |||
| 1426 | $updateLogPage = false; |
||
| 1427 | if ( $newPageContent ) { |
||
| 1428 | # New file page; create the description page. |
||
| 1429 | # There's already a log entry, so don't make a second RC entry |
||
| 1430 | # CDN and file cache for the description page are purged by doEditContent. |
||
| 1431 | $status = $wikiPage->doEditContent( |
||
| 1432 | $newPageContent, |
||
| 1433 | $comment, |
||
| 1434 | EDIT_NEW | EDIT_SUPPRESS_RC, |
||
| 1435 | false, |
||
| 1436 | $user |
||
| 1437 | ); |
||
| 1438 | |||
| 1439 | if ( isset( $status->value['revision'] ) ) { |
||
| 1440 | // Associate new page revision id |
||
| 1441 | $logEntry->setAssociatedRevId( $status->value['revision']->getId() ); |
||
| 1442 | } |
||
| 1443 | // This relies on the resetArticleID() call in WikiPage::insertOn(), |
||
| 1444 | // which is triggered on $descTitle by doEditContent() above. |
||
| 1445 | if ( isset( $status->value['revision'] ) ) { |
||
| 1446 | /** @var $rev Revision */ |
||
| 1447 | $rev = $status->value['revision']; |
||
| 1448 | $updateLogPage = $rev->getPage(); |
||
| 1449 | } |
||
| 1450 | } else { |
||
| 1451 | # Existing file page: invalidate description page cache |
||
| 1452 | $wikiPage->getTitle()->invalidateCache(); |
||
| 1453 | $wikiPage->getTitle()->purgeSquid(); |
||
| 1454 | # Allow the new file version to be patrolled from the page footer |
||
| 1455 | Article::purgePatrolFooterCache( $descId ); |
||
| 1456 | } |
||
| 1457 | |||
| 1458 | # Update associated rev id. This should be done by $logEntry->insert() earlier, |
||
| 1459 | # but setAssociatedRevId() wasn't called at that point yet... |
||
| 1460 | $logParams = $logEntry->getParameters(); |
||
| 1461 | $logParams['associated_rev_id'] = $logEntry->getAssociatedRevId(); |
||
| 1462 | $update = [ 'log_params' => LogEntryBase::makeParamBlob( $logParams ) ]; |
||
| 1463 | if ( $updateLogPage ) { |
||
| 1464 | # Also log page, in case where we just created it above |
||
| 1465 | $update['log_page'] = $updateLogPage; |
||
| 1466 | } |
||
| 1467 | $that->getRepo()->getMasterDB()->update( |
||
| 1468 | 'logging', |
||
| 1469 | $update, |
||
| 1470 | [ 'log_id' => $logId ], |
||
| 1471 | __METHOD__ |
||
| 1472 | ); |
||
| 1473 | $that->getRepo()->getMasterDB()->insert( |
||
| 1474 | 'log_search', |
||
| 1475 | [ |
||
| 1476 | 'ls_field' => 'associated_rev_id', |
||
| 1477 | 'ls_value' => $logEntry->getAssociatedRevId(), |
||
| 1478 | 'ls_log_id' => $logId, |
||
| 1479 | ], |
||
| 1480 | __METHOD__ |
||
| 1481 | ); |
||
| 1482 | |||
| 1483 | # Add change tags, if any |
||
| 1484 | if ( $tags ) { |
||
| 1485 | $logEntry->setTags( $tags ); |
||
| 1486 | } |
||
| 1487 | |||
| 1488 | # Now that the log entry is up-to-date, make an RC entry. |
||
| 1489 | $logEntry->publish( $logId ); |
||
| 1490 | |||
| 1491 | # Run hook for other updates (typically more cache purging) |
||
| 1492 | Hooks::run( 'FileUpload', [ $that, $reupload, !$newPageContent ] ); |
||
| 1493 | |||
| 1494 | if ( $reupload ) { |
||
| 1495 | # Delete old thumbnails |
||
| 1496 | $that->purgeThumbnails(); |
||
| 1497 | # Remove the old file from the CDN cache |
||
| 1498 | DeferredUpdates::addUpdate( |
||
| 1499 | new CdnCacheUpdate( [ $that->getUrl() ] ), |
||
| 1500 | DeferredUpdates::PRESEND |
||
| 1501 | ); |
||
| 1502 | } else { |
||
| 1503 | # Update backlink pages pointing to this title if created |
||
| 1504 | LinksUpdate::queueRecursiveJobsForTable( $that->getTitle(), 'imagelinks' ); |
||
| 1505 | } |
||
| 1506 | } ); |
||
| 1507 | |||
| 1508 | if ( !$reupload ) { |
||
| 1509 | # This is a new file, so update the image count |
||
| 1510 | DeferredUpdates::addUpdate( SiteStatsUpdate::factory( [ 'images' => 1 ] ) ); |
||
| 1511 | } |
||
| 1512 | |||
| 1513 | # Invalidate cache for all pages using this file |
||
| 1514 | DeferredUpdates::addUpdate( new HTMLCacheUpdate( $this->getTitle(), 'imagelinks' ) ); |
||
| 1515 | |||
| 1516 | return true; |
||
| 1517 | } |
||
| 1518 | |||
| 1519 | /** |
||
| 1520 | * Move or copy a file to its public location. If a file exists at the |
||
| 1521 | * destination, move it to an archive. Returns a FileRepoStatus object with |
||
| 1522 | * the archive name in the "value" member on success. |
||
| 1523 | * |
||
| 1524 | * The archive name should be passed through to recordUpload for database |
||
| 1525 | * registration. |
||
| 1526 | * |
||
| 1527 | * @param string|FSFile $src Local filesystem path or virtual URL to the source image |
||
| 1528 | * @param int $flags A bitwise combination of: |
||
| 1529 | * File::DELETE_SOURCE Delete the source file, i.e. move rather than copy |
||
| 1530 | * @param array $options Optional additional parameters |
||
| 1531 | * @return FileRepoStatus On success, the value member contains the |
||
| 1532 | * archive name, or an empty string if it was a new file. |
||
| 1533 | */ |
||
| 1534 | function publish( $src, $flags = 0, array $options = [] ) { |
||
| 1535 | return $this->publishTo( $src, $this->getRel(), $flags, $options ); |
||
| 1536 | } |
||
| 1537 | |||
| 1538 | /** |
||
| 1539 | * Move or copy a file to a specified location. Returns a FileRepoStatus |
||
| 1540 | * object with the archive name in the "value" member on success. |
||
| 1541 | * |
||
| 1542 | * The archive name should be passed through to recordUpload for database |
||
| 1543 | * registration. |
||
| 1544 | * |
||
| 1545 | * @param string|FSFile $src Local filesystem path or virtual URL to the source image |
||
| 1546 | * @param string $dstRel Target relative path |
||
| 1547 | * @param int $flags A bitwise combination of: |
||
| 1548 | * File::DELETE_SOURCE Delete the source file, i.e. move rather than copy |
||
| 1549 | * @param array $options Optional additional parameters |
||
| 1550 | * @return FileRepoStatus On success, the value member contains the |
||
| 1551 | * archive name, or an empty string if it was a new file. |
||
| 1552 | */ |
||
| 1553 | function publishTo( $src, $dstRel, $flags = 0, array $options = [] ) { |
||
| 1554 | $srcPath = ( $src instanceof FSFile ) ? $src->getPath() : $src; |
||
| 1555 | |||
| 1556 | $repo = $this->getRepo(); |
||
| 1557 | if ( $repo->getReadOnlyReason() !== false ) { |
||
| 1558 | return $this->readOnlyFatalStatus(); |
||
| 1559 | } |
||
| 1560 | |||
| 1561 | $this->lock(); // begin |
||
| 1562 | |||
| 1563 | $archiveName = wfTimestamp( TS_MW ) . '!' . $this->getName(); |
||
| 1564 | $archiveRel = 'archive/' . $this->getHashPath() . $archiveName; |
||
| 1565 | |||
| 1566 | if ( $repo->hasSha1Storage() ) { |
||
| 1567 | $sha1 = $repo->isVirtualUrl( $srcPath ) |
||
| 1568 | ? $repo->getFileSha1( $srcPath ) |
||
| 1569 | : FSFile::getSha1Base36FromPath( $srcPath ); |
||
| 1570 | $dst = $repo->getBackend()->getPathForSHA1( $sha1 ); |
||
| 1571 | $status = $repo->quickImport( $src, $dst ); |
||
| 1572 | if ( $flags & File::DELETE_SOURCE ) { |
||
| 1573 | unlink( $srcPath ); |
||
| 1574 | } |
||
| 1575 | |||
| 1576 | if ( $this->exists() ) { |
||
| 1577 | $status->value = $archiveName; |
||
| 1578 | } |
||
| 1579 | } else { |
||
| 1580 | $flags = $flags & File::DELETE_SOURCE ? LocalRepo::DELETE_SOURCE : 0; |
||
| 1581 | $status = $repo->publish( $srcPath, $dstRel, $archiveRel, $flags, $options ); |
||
| 1582 | |||
| 1583 | if ( $status->value == 'new' ) { |
||
| 1584 | $status->value = ''; |
||
| 1585 | } else { |
||
| 1586 | $status->value = $archiveName; |
||
| 1587 | } |
||
| 1588 | } |
||
| 1589 | |||
| 1590 | $this->unlock(); // done |
||
| 1591 | |||
| 1592 | return $status; |
||
| 1593 | } |
||
| 1594 | |||
| 1595 | /** getLinksTo inherited */ |
||
| 1596 | /** getExifData inherited */ |
||
| 1597 | /** isLocal inherited */ |
||
| 1598 | /** wasDeleted inherited */ |
||
| 1599 | |||
| 1600 | /** |
||
| 1601 | * Move file to the new title |
||
| 1602 | * |
||
| 1603 | * Move current, old version and all thumbnails |
||
| 1604 | * to the new filename. Old file is deleted. |
||
| 1605 | * |
||
| 1606 | * Cache purging is done; checks for validity |
||
| 1607 | * and logging are caller's responsibility |
||
| 1608 | * |
||
| 1609 | * @param Title $target New file name |
||
| 1610 | * @return FileRepoStatus |
||
| 1611 | */ |
||
| 1612 | function move( $target ) { |
||
| 1653 | |||
| 1654 | /** |
||
| 1655 | * Delete all versions of the file. |
||
| 1656 | * |
||
| 1657 | * Moves the files into an archive directory (or deletes them) |
||
| 1658 | * and removes the database rows. |
||
| 1659 | * |
||
| 1660 | * Cache purging is done; logging is caller's responsibility. |
||
| 1661 | * |
||
| 1662 | * @param string $reason |
||
| 1663 | * @param bool $suppress |
||
| 1664 | * @param User|null $user |
||
| 1665 | * @return FileRepoStatus |
||
| 1666 | */ |
||
| 1667 | function delete( $reason, $suppress = false, $user = null ) { |
||
| 1706 | |||
| 1707 | /** |
||
| 1708 | * Delete an old version of the file. |
||
| 1709 | * |
||
| 1710 | * Moves the file into an archive directory (or deletes it) |
||
| 1711 | * and removes the database row. |
||
| 1712 | * |
||
| 1713 | * Cache purging is done; logging is caller's responsibility. |
||
| 1714 | * |
||
| 1715 | * @param string $archiveName |
||
| 1716 | * @param string $reason |
||
| 1717 | * @param bool $suppress |
||
| 1718 | * @param User|null $user |
||
| 1719 | * @throws MWException Exception on database or file store failure |
||
| 1720 | * @return FileRepoStatus |
||
| 1721 | */ |
||
| 1722 | function deleteOld( $archiveName, $reason, $suppress = false, $user = null ) { |
||
| 1746 | |||
| 1747 | /** |
||
| 1748 | * Restore all or specified deleted revisions to the given file. |
||
| 1749 | * Permissions and logging are left to the caller. |
||
| 1750 | * |
||
| 1751 | * May throw database exceptions on error. |
||
| 1752 | * |
||
| 1753 | * @param array $versions Set of record ids of deleted items to restore, |
||
| 1754 | * or empty to restore all revisions. |
||
| 1755 | * @param bool $unsuppress |
||
| 1756 | * @return FileRepoStatus |
||
| 1757 | */ |
||
| 1758 | function restore( $versions = [], $unsuppress = false ) { |
||
| 1782 | |||
| 1783 | /** isMultipage inherited */ |
||
| 1784 | /** pageCount inherited */ |
||
| 1785 | /** scaleHeight inherited */ |
||
| 1786 | /** getImageSize inherited */ |
||
| 1787 | |||
| 1788 | /** |
||
| 1789 | * Get the URL of the file description page. |
||
| 1790 | * @return string |
||
| 1791 | */ |
||
| 1792 | function getDescriptionUrl() { |
||
| 1795 | |||
| 1796 | /** |
||
| 1797 | * Get the HTML text of the description page |
||
| 1798 | * This is not used by ImagePage for local files, since (among other things) |
||
| 1799 | * it skips the parser cache. |
||
| 1800 | * |
||
| 1801 | * @param Language $lang What language to get description in (Optional) |
||
| 1802 | * @return bool|mixed |
||
| 1803 | */ |
||
| 1804 | function getDescriptionText( $lang = null ) { |
||
| 1817 | |||
| 1818 | /** |
||
| 1819 | * @param int $audience |
||
| 1820 | * @param User $user |
||
| 1821 | * @return string |
||
| 1822 | */ |
||
| 1823 | View Code Duplication | function getDescription( $audience = self::FOR_PUBLIC, User $user = null ) { |
|
| 1835 | |||
| 1836 | /** |
||
| 1837 | * @return bool|string |
||
| 1838 | */ |
||
| 1839 | function getTimestamp() { |
||
| 1844 | |||
| 1845 | /** |
||
| 1846 | * @return bool|string |
||
| 1847 | */ |
||
| 1848 | public function getDescriptionTouched() { |
||
| 1863 | |||
| 1864 | /** |
||
| 1865 | * @return string |
||
| 1866 | */ |
||
| 1867 | function getSha1() { |
||
| 1888 | |||
| 1889 | /** |
||
| 1890 | * @return bool Whether to cache in RepoGroup (this avoids OOMs) |
||
| 1891 | */ |
||
| 1892 | function isCacheable() { |
||
| 1899 | |||
| 1900 | /** |
||
| 1901 | * Start a transaction and lock the image for update |
||
| 1902 | * Increments a reference counter if the lock is already held |
||
| 1903 | * @throws MWException Throws an error if the lock was not acquired |
||
| 1904 | * @return bool Whether the file lock owns/spawned the DB transaction |
||
| 1905 | */ |
||
| 1906 | function lock() { |
||
| 1931 | |||
| 1932 | /** |
||
| 1933 | * Decrement the lock reference count. If the reference count is reduced to zero, commits |
||
| 1934 | * the transaction and thereby releases the image lock. |
||
| 1935 | */ |
||
| 1936 | function unlock() { |
||
| 1946 | |||
| 1947 | /** |
||
| 1948 | * Roll back the DB transaction and mark the image unlocked |
||
| 1949 | */ |
||
| 1950 | function unlockAndRollback() { |
||
| 1956 | |||
| 1957 | /** |
||
| 1958 | * @return Status |
||
| 1959 | */ |
||
| 1960 | protected function readOnlyFatalStatus() { |
||
| 1964 | |||
| 1965 | /** |
||
| 1966 | * Clean up any dangling locks |
||
| 1967 | */ |
||
| 1968 | function __destruct() { |
||
| 1971 | } // LocalFile class |
||
| 1972 | |||
| 1973 | # ------------------------------------------------------------------------------ |
||
| 1974 | |||
| 1975 | /** |
||
| 1976 | * Helper class for file deletion |
||
| 1977 | * @ingroup FileAbstraction |
||
| 1978 | */ |
||
| 3035 |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.