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 Revision 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 Revision, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 26 | class Revision implements IDBAccessObject { |
||
| 27 | protected $mId; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @var int|null |
||
| 31 | */ |
||
| 32 | protected $mPage; |
||
| 33 | protected $mUserText; |
||
| 34 | protected $mOrigUserText; |
||
| 35 | protected $mUser; |
||
| 36 | protected $mMinorEdit; |
||
| 37 | protected $mTimestamp; |
||
| 38 | protected $mDeleted; |
||
| 39 | protected $mSize; |
||
| 40 | protected $mSha1; |
||
| 41 | protected $mParentId; |
||
| 42 | protected $mComment; |
||
| 43 | protected $mText; |
||
| 44 | protected $mTextId; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @var stdClass|null |
||
| 48 | */ |
||
| 49 | protected $mTextRow; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @var null|Title |
||
| 53 | */ |
||
| 54 | protected $mTitle; |
||
| 55 | protected $mCurrent; |
||
| 56 | protected $mContentModel; |
||
| 57 | protected $mContentFormat; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @var Content|null|bool |
||
| 61 | */ |
||
| 62 | protected $mContent; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @var null|ContentHandler |
||
| 66 | */ |
||
| 67 | protected $mContentHandler; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @var int |
||
| 71 | */ |
||
| 72 | protected $mQueryFlags = 0; |
||
| 73 | |||
| 74 | // Revision deletion constants |
||
| 75 | const DELETED_TEXT = 1; |
||
| 76 | const DELETED_COMMENT = 2; |
||
| 77 | const DELETED_USER = 4; |
||
| 78 | const DELETED_RESTRICTED = 8; |
||
| 79 | const SUPPRESSED_USER = 12; // convenience |
||
| 80 | |||
| 81 | // Audience options for accessors |
||
| 82 | const FOR_PUBLIC = 1; |
||
| 83 | const FOR_THIS_USER = 2; |
||
| 84 | const RAW = 3; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * Load a page revision from a given revision ID number. |
||
| 88 | * Returns null if no such revision can be found. |
||
| 89 | * |
||
| 90 | * $flags include: |
||
| 91 | * Revision::READ_LATEST : Select the data from the master |
||
| 92 | * Revision::READ_LOCKING : Select & lock the data from the master |
||
| 93 | * |
||
| 94 | * @param int $id |
||
| 95 | * @param int $flags (optional) |
||
| 96 | * @return Revision|null |
||
| 97 | */ |
||
| 98 | public static function newFromId( $id, $flags = 0 ) { |
||
| 101 | |||
| 102 | /** |
||
| 103 | * Load either the current, or a specified, revision |
||
| 104 | * that's attached to a given link target. If not attached |
||
| 105 | * to that link target, will return null. |
||
| 106 | * |
||
| 107 | * $flags include: |
||
| 108 | * Revision::READ_LATEST : Select the data from the master |
||
| 109 | * Revision::READ_LOCKING : Select & lock the data from the master |
||
| 110 | * |
||
| 111 | * @param LinkTarget $linkTarget |
||
| 112 | * @param int $id (optional) |
||
| 113 | * @param int $flags Bitfield (optional) |
||
| 114 | * @return Revision|null |
||
| 115 | */ |
||
| 116 | View Code Duplication | public static function newFromTitle( LinkTarget $linkTarget, $id = 0, $flags = 0 ) { |
|
| 132 | |||
| 133 | /** |
||
| 134 | * Load either the current, or a specified, revision |
||
| 135 | * that's attached to a given page ID. |
||
| 136 | * Returns null if no such revision can be found. |
||
| 137 | * |
||
| 138 | * $flags include: |
||
| 139 | * Revision::READ_LATEST : Select the data from the master (since 1.20) |
||
| 140 | * Revision::READ_LOCKING : Select & lock the data from the master |
||
| 141 | * |
||
| 142 | * @param int $pageId |
||
| 143 | * @param int $revId (optional) |
||
| 144 | * @param int $flags Bitfield (optional) |
||
| 145 | * @return Revision|null |
||
| 146 | */ |
||
| 147 | View Code Duplication | public static function newFromPageId( $pageId, $revId = 0, $flags = 0 ) { |
|
| 159 | |||
| 160 | /** |
||
| 161 | * Make a fake revision object from an archive table row. This is queried |
||
| 162 | * for permissions or even inserted (as in Special:Undelete) |
||
| 163 | * @todo FIXME: Should be a subclass for RevisionDelete. [TS] |
||
| 164 | * |
||
| 165 | * @param object $row |
||
| 166 | * @param array $overrides |
||
| 167 | * |
||
| 168 | * @throws MWException |
||
| 169 | * @return Revision |
||
| 170 | */ |
||
| 171 | public static function newFromArchiveRow( $row, $overrides = [] ) { |
||
| 211 | |||
| 212 | /** |
||
| 213 | * @since 1.19 |
||
| 214 | * |
||
| 215 | * @param object $row |
||
| 216 | * @return Revision |
||
| 217 | */ |
||
| 218 | public static function newFromRow( $row ) { |
||
| 221 | |||
| 222 | /** |
||
| 223 | * Load a page revision from a given revision ID number. |
||
| 224 | * Returns null if no such revision can be found. |
||
| 225 | * |
||
| 226 | * @param IDatabase $db |
||
| 227 | * @param int $id |
||
| 228 | * @return Revision|null |
||
| 229 | */ |
||
| 230 | public static function loadFromId( $db, $id ) { |
||
| 233 | |||
| 234 | /** |
||
| 235 | * Load either the current, or a specified, revision |
||
| 236 | * that's attached to a given page. If not attached |
||
| 237 | * to that page, will return null. |
||
| 238 | * |
||
| 239 | * @param IDatabase $db |
||
| 240 | * @param int $pageid |
||
| 241 | * @param int $id |
||
| 242 | * @return Revision|null |
||
| 243 | */ |
||
| 244 | public static function loadFromPageId( $db, $pageid, $id = 0 ) { |
||
| 253 | |||
| 254 | /** |
||
| 255 | * Load either the current, or a specified, revision |
||
| 256 | * that's attached to a given page. If not attached |
||
| 257 | * to that page, will return null. |
||
| 258 | * |
||
| 259 | * @param IDatabase $db |
||
| 260 | * @param Title $title |
||
| 261 | * @param int $id |
||
| 262 | * @return Revision|null |
||
| 263 | */ |
||
| 264 | public static function loadFromTitle( $db, $title, $id = 0 ) { |
||
| 265 | if ( $id ) { |
||
| 266 | $matchId = intval( $id ); |
||
| 267 | } else { |
||
| 268 | $matchId = 'page_latest'; |
||
| 269 | } |
||
| 270 | return self::loadFromConds( $db, |
||
| 271 | [ |
||
| 272 | "rev_id=$matchId", |
||
| 273 | 'page_namespace' => $title->getNamespace(), |
||
| 274 | 'page_title' => $title->getDBkey() |
||
| 275 | ] |
||
| 276 | ); |
||
| 277 | } |
||
| 278 | |||
| 279 | /** |
||
| 280 | * Load the revision for the given title with the given timestamp. |
||
| 281 | * WARNING: Timestamps may in some circumstances not be unique, |
||
| 282 | * so this isn't the best key to use. |
||
| 283 | * |
||
| 284 | * @param IDatabase $db |
||
| 285 | * @param Title $title |
||
| 286 | * @param string $timestamp |
||
| 287 | * @return Revision|null |
||
| 288 | */ |
||
| 289 | public static function loadFromTimestamp( $db, $title, $timestamp ) { |
||
| 290 | return self::loadFromConds( $db, |
||
| 291 | [ |
||
| 292 | 'rev_timestamp' => $db->timestamp( $timestamp ), |
||
| 293 | 'page_namespace' => $title->getNamespace(), |
||
| 294 | 'page_title' => $title->getDBkey() |
||
| 295 | ] |
||
| 296 | ); |
||
| 297 | } |
||
| 298 | |||
| 299 | /** |
||
| 300 | * Given a set of conditions, fetch a revision |
||
| 301 | * |
||
| 302 | * This method is used then a revision ID is qualified and |
||
| 303 | * will incorporate some basic slave/master fallback logic |
||
| 304 | * |
||
| 305 | * @param array $conditions |
||
| 306 | * @param int $flags (optional) |
||
| 307 | * @return Revision|null |
||
| 308 | */ |
||
| 309 | private static function newFromConds( $conditions, $flags = 0 ) { |
||
| 331 | |||
| 332 | /** |
||
| 333 | * Given a set of conditions, fetch a revision from |
||
| 334 | * the given database connection. |
||
| 335 | * |
||
| 336 | * @param IDatabase $db |
||
| 337 | * @param array $conditions |
||
| 338 | * @param int $flags (optional) |
||
| 339 | * @return Revision|null |
||
| 340 | */ |
||
| 341 | private static function loadFromConds( $db, $conditions, $flags = 0 ) { |
||
| 353 | |||
| 354 | /** |
||
| 355 | * Return a wrapper for a series of database rows to |
||
| 356 | * fetch all of a given page's revisions in turn. |
||
| 357 | * Each row can be fed to the constructor to get objects. |
||
| 358 | * |
||
| 359 | * @param Title $title |
||
| 360 | * @return ResultWrapper |
||
| 361 | */ |
||
| 362 | public static function fetchRevision( $title ) { |
||
| 363 | return self::fetchFromConds( |
||
| 364 | wfGetDB( DB_SLAVE ), |
||
| 365 | [ |
||
| 366 | 'rev_id=page_latest', |
||
| 367 | 'page_namespace' => $title->getNamespace(), |
||
| 368 | 'page_title' => $title->getDBkey() |
||
| 369 | ] |
||
| 370 | ); |
||
| 371 | } |
||
| 372 | |||
| 373 | /** |
||
| 374 | * Given a set of conditions, return a ResultWrapper |
||
| 375 | * which will return matching database rows with the |
||
| 376 | * fields necessary to build Revision objects. |
||
| 377 | * |
||
| 378 | * @param IDatabase $db |
||
| 379 | * @param array $conditions |
||
| 380 | * @param int $flags (optional) |
||
| 381 | * @return ResultWrapper |
||
| 382 | */ |
||
| 383 | private static function fetchFromConds( $db, $conditions, $flags = 0 ) { |
||
| 402 | |||
| 403 | /** |
||
| 404 | * Return the value of a select() JOIN conds array for the user table. |
||
| 405 | * This will get user table rows for logged-in users. |
||
| 406 | * @since 1.19 |
||
| 407 | * @return array |
||
| 408 | */ |
||
| 409 | public static function userJoinCond() { |
||
| 412 | |||
| 413 | /** |
||
| 414 | * Return the value of a select() page conds array for the page table. |
||
| 415 | * This will assure that the revision(s) are not orphaned from live pages. |
||
| 416 | * @since 1.19 |
||
| 417 | * @return array |
||
| 418 | */ |
||
| 419 | public static function pageJoinCond() { |
||
| 422 | |||
| 423 | /** |
||
| 424 | * Return the list of revision fields that should be selected to create |
||
| 425 | * a new revision. |
||
| 426 | * @return array |
||
| 427 | */ |
||
| 428 | public static function selectFields() { |
||
| 453 | |||
| 454 | /** |
||
| 455 | * Return the list of revision fields that should be selected to create |
||
| 456 | * a new revision from an archive row. |
||
| 457 | * @return array |
||
| 458 | */ |
||
| 459 | public static function selectArchiveFields() { |
||
| 484 | |||
| 485 | /** |
||
| 486 | * Return the list of text fields that should be selected to read the |
||
| 487 | * revision text |
||
| 488 | * @return array |
||
| 489 | */ |
||
| 490 | public static function selectTextFields() { |
||
| 496 | |||
| 497 | /** |
||
| 498 | * Return the list of page fields that should be selected from page table |
||
| 499 | * @return array |
||
| 500 | */ |
||
| 501 | public static function selectPageFields() { |
||
| 511 | |||
| 512 | /** |
||
| 513 | * Return the list of user fields that should be selected from user table |
||
| 514 | * @return array |
||
| 515 | */ |
||
| 516 | public static function selectUserFields() { |
||
| 519 | |||
| 520 | /** |
||
| 521 | * Do a batched query to get the parent revision lengths |
||
| 522 | * @param IDatabase $db |
||
| 523 | * @param array $revIds |
||
| 524 | * @return array |
||
| 525 | */ |
||
| 526 | public static function getParentLengths( $db, array $revIds ) { |
||
| 540 | |||
| 541 | /** |
||
| 542 | * Constructor |
||
| 543 | * |
||
| 544 | * @param object|array $row Either a database row or an array |
||
| 545 | * @throws MWException |
||
| 546 | * @access private |
||
| 547 | */ |
||
| 548 | function __construct( $row ) { |
||
| 709 | |||
| 710 | /** |
||
| 711 | * Get revision ID |
||
| 712 | * |
||
| 713 | * @return int|null |
||
| 714 | */ |
||
| 715 | public function getId() { |
||
| 718 | |||
| 719 | /** |
||
| 720 | * Set the revision ID |
||
| 721 | * |
||
| 722 | * @since 1.19 |
||
| 723 | * @param int $id |
||
| 724 | */ |
||
| 725 | public function setId( $id ) { |
||
| 728 | |||
| 729 | /** |
||
| 730 | * Get text row ID |
||
| 731 | * |
||
| 732 | * @return int|null |
||
| 733 | */ |
||
| 734 | public function getTextId() { |
||
| 737 | |||
| 738 | /** |
||
| 739 | * Get parent revision ID (the original previous page revision) |
||
| 740 | * |
||
| 741 | * @return int|null |
||
| 742 | */ |
||
| 743 | public function getParentId() { |
||
| 746 | |||
| 747 | /** |
||
| 748 | * Returns the length of the text in this revision, or null if unknown. |
||
| 749 | * |
||
| 750 | * @return int|null |
||
| 751 | */ |
||
| 752 | public function getSize() { |
||
| 755 | |||
| 756 | /** |
||
| 757 | * Returns the base36 sha1 of the text in this revision, or null if unknown. |
||
| 758 | * |
||
| 759 | * @return string|null |
||
| 760 | */ |
||
| 761 | public function getSha1() { |
||
| 764 | |||
| 765 | /** |
||
| 766 | * Returns the title of the page associated with this entry or null. |
||
| 767 | * |
||
| 768 | * Will do a query, when title is not set and id is given. |
||
| 769 | * |
||
| 770 | * @return Title|null |
||
| 771 | */ |
||
| 772 | public function getTitle() { |
||
| 796 | |||
| 797 | /** |
||
| 798 | * Set the title of the revision |
||
| 799 | * |
||
| 800 | * @param Title $title |
||
| 801 | */ |
||
| 802 | public function setTitle( $title ) { |
||
| 805 | |||
| 806 | /** |
||
| 807 | * Get the page ID |
||
| 808 | * |
||
| 809 | * @return int|null |
||
| 810 | */ |
||
| 811 | public function getPage() { |
||
| 814 | |||
| 815 | /** |
||
| 816 | * Fetch revision's user id if it's available to the specified audience. |
||
| 817 | * If the specified audience does not have access to it, zero will be |
||
| 818 | * returned. |
||
| 819 | * |
||
| 820 | * @param int $audience One of: |
||
| 821 | * Revision::FOR_PUBLIC to be displayed to all users |
||
| 822 | * Revision::FOR_THIS_USER to be displayed to the given user |
||
| 823 | * Revision::RAW get the ID regardless of permissions |
||
| 824 | * @param User $user User object to check for, only if FOR_THIS_USER is passed |
||
| 825 | * to the $audience parameter |
||
| 826 | * @return int |
||
| 827 | */ |
||
| 828 | View Code Duplication | public function getUser( $audience = self::FOR_PUBLIC, User $user = null ) { |
|
| 837 | |||
| 838 | /** |
||
| 839 | * Fetch revision's user id without regard for the current user's permissions |
||
| 840 | * |
||
| 841 | * @return string |
||
| 842 | * @deprecated since 1.25, use getUser( Revision::RAW ) |
||
| 843 | */ |
||
| 844 | public function getRawUser() { |
||
| 848 | |||
| 849 | /** |
||
| 850 | * Fetch revision's username if it's available to the specified audience. |
||
| 851 | * If the specified audience does not have access to the username, an |
||
| 852 | * empty string will be returned. |
||
| 853 | * |
||
| 854 | * @param int $audience One of: |
||
| 855 | * Revision::FOR_PUBLIC to be displayed to all users |
||
| 856 | * Revision::FOR_THIS_USER to be displayed to the given user |
||
| 857 | * Revision::RAW get the text regardless of permissions |
||
| 858 | * @param User $user User object to check for, only if FOR_THIS_USER is passed |
||
| 859 | * to the $audience parameter |
||
| 860 | * @return string |
||
| 861 | */ |
||
| 862 | public function getUserText( $audience = self::FOR_PUBLIC, User $user = null ) { |
||
| 879 | |||
| 880 | /** |
||
| 881 | * Fetch revision's username without regard for view restrictions |
||
| 882 | * |
||
| 883 | * @return string |
||
| 884 | * @deprecated since 1.25, use getUserText( Revision::RAW ) |
||
| 885 | */ |
||
| 886 | public function getRawUserText() { |
||
| 890 | |||
| 891 | /** |
||
| 892 | * Fetch revision comment if it's available to the specified audience. |
||
| 893 | * If the specified audience does not have access to the comment, an |
||
| 894 | * empty string will be returned. |
||
| 895 | * |
||
| 896 | * @param int $audience One of: |
||
| 897 | * Revision::FOR_PUBLIC to be displayed to all users |
||
| 898 | * Revision::FOR_THIS_USER to be displayed to the given user |
||
| 899 | * Revision::RAW get the text regardless of permissions |
||
| 900 | * @param User $user User object to check for, only if FOR_THIS_USER is passed |
||
| 901 | * to the $audience parameter |
||
| 902 | * @return string |
||
| 903 | */ |
||
| 904 | View Code Duplication | function getComment( $audience = self::FOR_PUBLIC, User $user = null ) { |
|
| 913 | |||
| 914 | /** |
||
| 915 | * Fetch revision comment without regard for the current user's permissions |
||
| 916 | * |
||
| 917 | * @return string |
||
| 918 | * @deprecated since 1.25, use getComment( Revision::RAW ) |
||
| 919 | */ |
||
| 920 | public function getRawComment() { |
||
| 924 | |||
| 925 | /** |
||
| 926 | * @return bool |
||
| 927 | */ |
||
| 928 | public function isMinor() { |
||
| 931 | |||
| 932 | /** |
||
| 933 | * @return int Rcid of the unpatrolled row, zero if there isn't one |
||
| 934 | */ |
||
| 935 | public function isUnpatrolled() { |
||
| 947 | |||
| 948 | /** |
||
| 949 | * Get the RC object belonging to the current revision, if there's one |
||
| 950 | * |
||
| 951 | * @param int $flags (optional) $flags include: |
||
| 952 | * Revision::READ_LATEST : Select the data from the master |
||
| 953 | * |
||
| 954 | * @since 1.22 |
||
| 955 | * @return RecentChange|null |
||
| 956 | */ |
||
| 957 | public function getRecentChange( $flags = 0 ) { |
||
| 958 | $dbr = wfGetDB( DB_SLAVE ); |
||
| 959 | |||
| 960 | list( $dbType, ) = DBAccessObjectUtils::getDBOptions( $flags ); |
||
| 961 | |||
| 962 | return RecentChange::newFromConds( |
||
| 963 | [ |
||
| 964 | 'rc_user_text' => $this->getUserText( Revision::RAW ), |
||
| 965 | 'rc_timestamp' => $dbr->timestamp( $this->getTimestamp() ), |
||
| 966 | 'rc_this_oldid' => $this->getId() |
||
| 967 | ], |
||
| 968 | __METHOD__, |
||
| 969 | $dbType |
||
| 970 | ); |
||
| 971 | } |
||
| 972 | |||
| 973 | /** |
||
| 974 | * @param int $field One of DELETED_* bitfield constants |
||
| 975 | * |
||
| 976 | * @return bool |
||
| 977 | */ |
||
| 978 | public function isDeleted( $field ) { |
||
| 981 | |||
| 982 | /** |
||
| 983 | * Get the deletion bitfield of the revision |
||
| 984 | * |
||
| 985 | * @return int |
||
| 986 | */ |
||
| 987 | public function getVisibility() { |
||
| 990 | |||
| 991 | /** |
||
| 992 | * Fetch revision text if it's available to the specified audience. |
||
| 993 | * If the specified audience does not have the ability to view this |
||
| 994 | * revision, an empty string will be returned. |
||
| 995 | * |
||
| 996 | * @param int $audience One of: |
||
| 997 | * Revision::FOR_PUBLIC to be displayed to all users |
||
| 998 | * Revision::FOR_THIS_USER to be displayed to the given user |
||
| 999 | * Revision::RAW get the text regardless of permissions |
||
| 1000 | * @param User $user User object to check for, only if FOR_THIS_USER is passed |
||
| 1001 | * to the $audience parameter |
||
| 1002 | * |
||
| 1003 | * @deprecated since 1.21, use getContent() instead |
||
| 1004 | * @todo Replace usage in core |
||
| 1005 | * @return string |
||
| 1006 | */ |
||
| 1007 | public function getText( $audience = self::FOR_PUBLIC, User $user = null ) { |
||
| 1013 | |||
| 1014 | /** |
||
| 1015 | * Fetch revision content if it's available to the specified audience. |
||
| 1016 | * If the specified audience does not have the ability to view this |
||
| 1017 | * revision, null will be returned. |
||
| 1018 | * |
||
| 1019 | * @param int $audience One of: |
||
| 1020 | * Revision::FOR_PUBLIC to be displayed to all users |
||
| 1021 | * Revision::FOR_THIS_USER to be displayed to $wgUser |
||
| 1022 | * Revision::RAW get the text regardless of permissions |
||
| 1023 | * @param User $user User object to check for, only if FOR_THIS_USER is passed |
||
| 1024 | * to the $audience parameter |
||
| 1025 | * @since 1.21 |
||
| 1026 | * @return Content|null |
||
| 1027 | */ |
||
| 1028 | public function getContent( $audience = self::FOR_PUBLIC, User $user = null ) { |
||
| 1037 | |||
| 1038 | /** |
||
| 1039 | * Fetch revision text without regard for view restrictions |
||
| 1040 | * |
||
| 1041 | * @return string |
||
| 1042 | * |
||
| 1043 | * @deprecated since 1.21. Instead, use Revision::getContent( Revision::RAW ) |
||
| 1044 | * or Revision::getSerializedData() as appropriate. |
||
| 1045 | */ |
||
| 1046 | public function getRawText() { |
||
| 1050 | |||
| 1051 | /** |
||
| 1052 | * Fetch original serialized data without regard for view restrictions |
||
| 1053 | * |
||
| 1054 | * @since 1.21 |
||
| 1055 | * @return string |
||
| 1056 | */ |
||
| 1057 | public function getSerializedData() { |
||
| 1064 | |||
| 1065 | /** |
||
| 1066 | * Gets the content object for the revision (or null on failure). |
||
| 1067 | * |
||
| 1068 | * Note that for mutable Content objects, each call to this method will return a |
||
| 1069 | * fresh clone. |
||
| 1070 | * |
||
| 1071 | * @since 1.21 |
||
| 1072 | * @return Content|null The Revision's content, or null on failure. |
||
| 1073 | */ |
||
| 1074 | protected function getContentInternal() { |
||
| 1093 | |||
| 1094 | /** |
||
| 1095 | * Returns the content model for this revision. |
||
| 1096 | * |
||
| 1097 | * If no content model was stored in the database, the default content model for the title is |
||
| 1098 | * used to determine the content model to use. If no title is know, CONTENT_MODEL_WIKITEXT |
||
| 1099 | * is used as a last resort. |
||
| 1100 | * |
||
| 1101 | * @return string The content model id associated with this revision, |
||
| 1102 | * see the CONTENT_MODEL_XXX constants. |
||
| 1103 | **/ |
||
| 1104 | public function getContentModel() { |
||
| 1118 | |||
| 1119 | /** |
||
| 1120 | * Returns the content format for this revision. |
||
| 1121 | * |
||
| 1122 | * If no content format was stored in the database, the default format for this |
||
| 1123 | * revision's content model is returned. |
||
| 1124 | * |
||
| 1125 | * @return string The content format id associated with this revision, |
||
| 1126 | * see the CONTENT_FORMAT_XXX constants. |
||
| 1127 | **/ |
||
| 1128 | public function getContentFormat() { |
||
| 1138 | |||
| 1139 | /** |
||
| 1140 | * Returns the content handler appropriate for this revision's content model. |
||
| 1141 | * |
||
| 1142 | * @throws MWException |
||
| 1143 | * @return ContentHandler |
||
| 1144 | */ |
||
| 1145 | public function getContentHandler() { |
||
| 1160 | |||
| 1161 | /** |
||
| 1162 | * @return string |
||
| 1163 | */ |
||
| 1164 | public function getTimestamp() { |
||
| 1167 | |||
| 1168 | /** |
||
| 1169 | * @return bool |
||
| 1170 | */ |
||
| 1171 | public function isCurrent() { |
||
| 1174 | |||
| 1175 | /** |
||
| 1176 | * Get previous revision for this title |
||
| 1177 | * |
||
| 1178 | * @return Revision|null |
||
| 1179 | */ |
||
| 1180 | View Code Duplication | public function getPrevious() { |
|
| 1189 | |||
| 1190 | /** |
||
| 1191 | * Get next revision for this title |
||
| 1192 | * |
||
| 1193 | * @return Revision|null |
||
| 1194 | */ |
||
| 1195 | View Code Duplication | public function getNext() { |
|
| 1204 | |||
| 1205 | /** |
||
| 1206 | * Get previous revision Id for this page_id |
||
| 1207 | * This is used to populate rev_parent_id on save |
||
| 1208 | * |
||
| 1209 | * @param IDatabase $db |
||
| 1210 | * @return int |
||
| 1211 | */ |
||
| 1212 | private function getPreviousRevisionId( $db ) { |
||
| 1229 | |||
| 1230 | /** |
||
| 1231 | * Get revision text associated with an old or archive row |
||
| 1232 | * $row is usually an object from wfFetchRow(), both the flags and the text |
||
| 1233 | * field must be included. |
||
| 1234 | * |
||
| 1235 | * @param stdClass $row The text data |
||
| 1236 | * @param string $prefix Table prefix (default 'old_') |
||
| 1237 | * @param string|bool $wiki The name of the wiki to load the revision text from |
||
| 1238 | * (same as the the wiki $row was loaded from) or false to indicate the local |
||
| 1239 | * wiki (this is the default). Otherwise, it must be a symbolic wiki database |
||
| 1240 | * identifier as understood by the LoadBalancer class. |
||
| 1241 | * @return string Text the text requested or false on failure |
||
| 1242 | */ |
||
| 1243 | public static function getRevisionText( $row, $prefix = 'old_', $wiki = false ) { |
||
| 1277 | |||
| 1278 | /** |
||
| 1279 | * If $wgCompressRevisions is enabled, we will compress data. |
||
| 1280 | * The input string is modified in place. |
||
| 1281 | * Return value is the flags field: contains 'gzip' if the |
||
| 1282 | * data is compressed, and 'utf-8' if we're saving in UTF-8 |
||
| 1283 | * mode. |
||
| 1284 | * |
||
| 1285 | * @param mixed $text Reference to a text |
||
| 1286 | * @return string |
||
| 1287 | */ |
||
| 1288 | public static function compressRevisionText( &$text ) { |
||
| 1312 | |||
| 1313 | /** |
||
| 1314 | * Re-converts revision text according to it's flags. |
||
| 1315 | * |
||
| 1316 | * @param mixed $text Reference to a text |
||
| 1317 | * @param array $flags Compression flags |
||
| 1318 | * @return string|bool Decompressed text, or false on failure |
||
| 1319 | */ |
||
| 1320 | public static function decompressRevisionText( $text, $flags ) { |
||
| 1357 | |||
| 1358 | /** |
||
| 1359 | * Insert a new revision into the database, returning the new revision ID |
||
| 1360 | * number on success and dies horribly on failure. |
||
| 1361 | * |
||
| 1362 | * @param IDatabase $dbw (master connection) |
||
| 1363 | * @throws MWException |
||
| 1364 | * @return int |
||
| 1365 | */ |
||
| 1366 | public function insertOn( $dbw ) { |
||
| 1367 | global $wgDefaultExternalStore, $wgContentHandlerUseDB; |
||
| 1368 | |||
| 1369 | // Not allowed to have rev_page equal to 0, false, etc. |
||
| 1370 | View Code Duplication | if ( !$this->mPage ) { |
|
| 1371 | $title = $this->getTitle(); |
||
| 1372 | if ( $title instanceof Title ) { |
||
| 1373 | $titleText = ' for page ' . $title->getPrefixedText(); |
||
| 1374 | } else { |
||
| 1375 | $titleText = ''; |
||
| 1376 | } |
||
| 1377 | throw new MWException( "Cannot insert revision$titleText: page ID must be nonzero" ); |
||
| 1378 | } |
||
| 1379 | |||
| 1380 | $this->checkContentModel(); |
||
| 1381 | |||
| 1382 | $data = $this->mText; |
||
| 1383 | $flags = self::compressRevisionText( $data ); |
||
| 1384 | |||
| 1385 | # Write to external storage if required |
||
| 1386 | if ( $wgDefaultExternalStore ) { |
||
| 1387 | // Store and get the URL |
||
| 1388 | $data = ExternalStore::insertToDefault( $data ); |
||
| 1389 | if ( !$data ) { |
||
| 1390 | throw new MWException( "Unable to store text to external storage" ); |
||
| 1391 | } |
||
| 1392 | if ( $flags ) { |
||
| 1393 | $flags .= ','; |
||
| 1394 | } |
||
| 1395 | $flags .= 'external'; |
||
| 1396 | } |
||
| 1397 | |||
| 1398 | # Record the text (or external storage URL) to the text table |
||
| 1399 | if ( $this->mTextId === null ) { |
||
| 1400 | $old_id = $dbw->nextSequenceValue( 'text_old_id_seq' ); |
||
| 1401 | $dbw->insert( 'text', |
||
| 1402 | [ |
||
| 1403 | 'old_id' => $old_id, |
||
| 1404 | 'old_text' => $data, |
||
| 1405 | 'old_flags' => $flags, |
||
| 1406 | ], __METHOD__ |
||
| 1407 | ); |
||
| 1408 | $this->mTextId = $dbw->insertId(); |
||
| 1409 | } |
||
| 1410 | |||
| 1411 | if ( $this->mComment === null ) { |
||
| 1412 | $this->mComment = ""; |
||
| 1413 | } |
||
| 1414 | |||
| 1415 | # Record the edit in revisions |
||
| 1416 | $rev_id = $this->mId !== null |
||
| 1417 | ? $this->mId |
||
| 1418 | : $dbw->nextSequenceValue( 'revision_rev_id_seq' ); |
||
| 1419 | $row = [ |
||
| 1420 | 'rev_id' => $rev_id, |
||
| 1421 | 'rev_page' => $this->mPage, |
||
| 1422 | 'rev_text_id' => $this->mTextId, |
||
| 1423 | 'rev_comment' => $this->mComment, |
||
| 1424 | 'rev_minor_edit' => $this->mMinorEdit ? 1 : 0, |
||
| 1425 | 'rev_user' => $this->mUser, |
||
| 1426 | 'rev_user_text' => $this->mUserText, |
||
| 1427 | 'rev_timestamp' => $dbw->timestamp( $this->mTimestamp ), |
||
| 1428 | 'rev_deleted' => $this->mDeleted, |
||
| 1429 | 'rev_len' => $this->mSize, |
||
| 1430 | 'rev_parent_id' => $this->mParentId === null |
||
| 1431 | ? $this->getPreviousRevisionId( $dbw ) |
||
| 1432 | : $this->mParentId, |
||
| 1433 | 'rev_sha1' => $this->mSha1 === null |
||
| 1434 | ? Revision::base36Sha1( $this->mText ) |
||
| 1435 | : $this->mSha1, |
||
| 1436 | ]; |
||
| 1437 | |||
| 1438 | if ( $wgContentHandlerUseDB ) { |
||
| 1439 | // NOTE: Store null for the default model and format, to save space. |
||
| 1440 | // XXX: Makes the DB sensitive to changed defaults. |
||
| 1441 | // Make this behavior optional? Only in miser mode? |
||
| 1442 | |||
| 1443 | $model = $this->getContentModel(); |
||
| 1444 | $format = $this->getContentFormat(); |
||
| 1445 | |||
| 1446 | $title = $this->getTitle(); |
||
| 1447 | |||
| 1448 | if ( $title === null ) { |
||
| 1449 | throw new MWException( "Insufficient information to determine the title of the " |
||
| 1450 | . "revision's page!" ); |
||
| 1451 | } |
||
| 1452 | |||
| 1453 | $defaultModel = ContentHandler::getDefaultModelFor( $title ); |
||
| 1454 | $defaultFormat = ContentHandler::getForModelID( $defaultModel )->getDefaultFormat(); |
||
| 1455 | |||
| 1456 | $row['rev_content_model'] = ( $model === $defaultModel ) ? null : $model; |
||
| 1457 | $row['rev_content_format'] = ( $format === $defaultFormat ) ? null : $format; |
||
| 1458 | } |
||
| 1459 | |||
| 1460 | $dbw->insert( 'revision', $row, __METHOD__ ); |
||
| 1461 | |||
| 1462 | $this->mId = $rev_id !== null ? $rev_id : $dbw->insertId(); |
||
| 1463 | |||
| 1464 | // Assertion to try to catch T92046 |
||
| 1465 | if ( (int)$this->mId === 0 ) { |
||
| 1466 | throw new UnexpectedValueException( |
||
| 1467 | 'After insert, Revision mId is ' . var_export( $this->mId, 1 ) . ': ' . |
||
| 1468 | var_export( $row, 1 ) |
||
| 1469 | ); |
||
| 1470 | } |
||
| 1471 | |||
| 1472 | Hooks::run( 'RevisionInsertComplete', [ &$this, $data, $flags ] ); |
||
| 1473 | |||
| 1474 | return $this->mId; |
||
| 1475 | } |
||
| 1476 | |||
| 1477 | protected function checkContentModel() { |
||
| 1533 | |||
| 1534 | /** |
||
| 1535 | * Get the base 36 SHA-1 value for a string of text |
||
| 1536 | * @param string $text |
||
| 1537 | * @return string |
||
| 1538 | */ |
||
| 1539 | public static function base36Sha1( $text ) { |
||
| 1542 | |||
| 1543 | /** |
||
| 1544 | * Lazy-load the revision's text. |
||
| 1545 | * Currently hardcoded to the 'text' table storage engine. |
||
| 1546 | * |
||
| 1547 | * @return string|bool The revision's text, or false on failure |
||
| 1548 | */ |
||
| 1549 | protected function loadText() { |
||
| 1620 | |||
| 1621 | /** |
||
| 1622 | * Create a new null-revision for insertion into a page's |
||
| 1623 | * history. This will not re-save the text, but simply refer |
||
| 1624 | * to the text from the previous version. |
||
| 1625 | * |
||
| 1626 | * Such revisions can for instance identify page rename |
||
| 1627 | * operations and other such meta-modifications. |
||
| 1628 | * |
||
| 1629 | * @param IDatabase $dbw |
||
| 1630 | * @param int $pageId ID number of the page to read from |
||
| 1631 | * @param string $summary Revision's summary |
||
| 1632 | * @param bool $minor Whether the revision should be considered as minor |
||
| 1633 | * @param User|null $user User object to use or null for $wgUser |
||
| 1634 | * @return Revision|null Revision or null on error |
||
| 1635 | */ |
||
| 1636 | public static function newNullRevision( $dbw, $pageId, $summary, $minor, $user = null ) { |
||
| 1693 | |||
| 1694 | /** |
||
| 1695 | * Determine if the current user is allowed to view a particular |
||
| 1696 | * field of this revision, if it's marked as deleted. |
||
| 1697 | * |
||
| 1698 | * @param int $field One of self::DELETED_TEXT, |
||
| 1699 | * self::DELETED_COMMENT, |
||
| 1700 | * self::DELETED_USER |
||
| 1701 | * @param User|null $user User object to check, or null to use $wgUser |
||
| 1702 | * @return bool |
||
| 1703 | */ |
||
| 1704 | public function userCan( $field, User $user = null ) { |
||
| 1707 | |||
| 1708 | /** |
||
| 1709 | * Determine if the current user is allowed to view a particular |
||
| 1710 | * field of this revision, if it's marked as deleted. This is used |
||
| 1711 | * by various classes to avoid duplication. |
||
| 1712 | * |
||
| 1713 | * @param int $bitfield Current field |
||
| 1714 | * @param int $field One of self::DELETED_TEXT = File::DELETED_FILE, |
||
| 1715 | * self::DELETED_COMMENT = File::DELETED_COMMENT, |
||
| 1716 | * self::DELETED_USER = File::DELETED_USER |
||
| 1717 | * @param User|null $user User object to check, or null to use $wgUser |
||
| 1718 | * @param Title|null $title A Title object to check for per-page restrictions on, |
||
| 1719 | * instead of just plain userrights |
||
| 1720 | * @return bool |
||
| 1721 | */ |
||
| 1722 | public static function userCanBitfield( $bitfield, $field, User $user = null, |
||
| 1755 | |||
| 1756 | /** |
||
| 1757 | * Get rev_timestamp from rev_id, without loading the rest of the row |
||
| 1758 | * |
||
| 1759 | * @param Title $title |
||
| 1760 | * @param int $id |
||
| 1761 | * @return string|bool False if not found |
||
| 1762 | */ |
||
| 1763 | static function getTimestampFromId( $title, $id, $flags = 0 ) { |
||
| 1777 | |||
| 1778 | /** |
||
| 1779 | * Get count of revisions per page...not very efficient |
||
| 1780 | * |
||
| 1781 | * @param IDatabase $db |
||
| 1782 | * @param int $id Page id |
||
| 1783 | * @return int |
||
| 1784 | */ |
||
| 1785 | static function countByPageId( $db, $id ) { |
||
| 1793 | |||
| 1794 | /** |
||
| 1795 | * Get count of revisions per page...not very efficient |
||
| 1796 | * |
||
| 1797 | * @param IDatabase $db |
||
| 1798 | * @param Title $title |
||
| 1799 | * @return int |
||
| 1800 | */ |
||
| 1801 | static function countByTitle( $db, $title ) { |
||
| 1808 | |||
| 1809 | /** |
||
| 1810 | * Check if no edits were made by other users since |
||
| 1811 | * the time a user started editing the page. Limit to |
||
| 1812 | * 50 revisions for the sake of performance. |
||
| 1813 | * |
||
| 1814 | * @since 1.20 |
||
| 1815 | * @deprecated since 1.24 |
||
| 1816 | * |
||
| 1817 | * @param IDatabase|int $db The Database to perform the check on. May be given as a |
||
| 1818 | * Database object or a database identifier usable with wfGetDB. |
||
| 1819 | * @param int $pageId The ID of the page in question |
||
| 1820 | * @param int $userId The ID of the user in question |
||
| 1821 | * @param string $since Look at edits since this time |
||
| 1822 | * |
||
| 1823 | * @return bool True if the given user was the only one to edit since the given timestamp |
||
| 1824 | */ |
||
| 1825 | public static function userWasLastToEdit( $db, $pageId, $userId, $since ) { |
||
| 1849 | } |
||
| 1850 |
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.