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 WikiPage 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 WikiPage, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 29 | class WikiPage implements Page, IDBAccessObject { |
||
| 30 | // Constants for $mDataLoadedFrom and related |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @var Title |
||
| 34 | */ |
||
| 35 | public $mTitle = null; |
||
| 36 | |||
| 37 | /**@{{ |
||
| 38 | * @protected |
||
| 39 | */ |
||
| 40 | public $mDataLoaded = false; // !< Boolean |
||
| 41 | public $mIsRedirect = false; // !< Boolean |
||
| 42 | public $mLatest = false; // !< Integer (false means "not loaded") |
||
| 43 | /**@}}*/ |
||
| 44 | |||
| 45 | /** @var stdClass Map of cache fields (text, parser output, ect) for a proposed/new edit */ |
||
| 46 | public $mPreparedEdit = false; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @var int |
||
| 50 | */ |
||
| 51 | protected $mId = null; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @var int One of the READ_* constants |
||
| 55 | */ |
||
| 56 | protected $mDataLoadedFrom = self::READ_NONE; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @var Title |
||
| 60 | */ |
||
| 61 | protected $mRedirectTarget = null; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @var Revision |
||
| 65 | */ |
||
| 66 | protected $mLastRevision = null; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @var string Timestamp of the current revision or empty string if not loaded |
||
| 70 | */ |
||
| 71 | protected $mTimestamp = ''; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * @var string |
||
| 75 | */ |
||
| 76 | protected $mTouched = '19700101000000'; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * @var string |
||
| 80 | */ |
||
| 81 | protected $mLinksUpdated = '19700101000000'; |
||
| 82 | |||
| 83 | /** |
||
| 84 | * Constructor and clear the article |
||
| 85 | * @param Title $title Reference to a Title object. |
||
| 86 | */ |
||
| 87 | public function __construct( Title $title ) { |
||
| 90 | |||
| 91 | /** |
||
| 92 | * Create a WikiPage object of the appropriate class for the given title. |
||
| 93 | * |
||
| 94 | * @param Title $title |
||
| 95 | * |
||
| 96 | * @throws MWException |
||
| 97 | * @return WikiPage|WikiCategoryPage|WikiFilePage |
||
| 98 | */ |
||
| 99 | public static function factory( Title $title ) { |
||
| 121 | |||
| 122 | /** |
||
| 123 | * Constructor from a page id |
||
| 124 | * |
||
| 125 | * @param int $id Article ID to load |
||
| 126 | * @param string|int $from One of the following values: |
||
| 127 | * - "fromdb" or WikiPage::READ_NORMAL to select from a slave database |
||
| 128 | * - "fromdbmaster" or WikiPage::READ_LATEST to select from the master database |
||
| 129 | * |
||
| 130 | * @return WikiPage|null |
||
| 131 | */ |
||
| 132 | public static function newFromID( $id, $from = 'fromdb' ) { |
||
| 147 | |||
| 148 | /** |
||
| 149 | * Constructor from a database row |
||
| 150 | * |
||
| 151 | * @since 1.20 |
||
| 152 | * @param object $row Database row containing at least fields returned by selectFields(). |
||
| 153 | * @param string|int $from Source of $data: |
||
| 154 | * - "fromdb" or WikiPage::READ_NORMAL: from a slave DB |
||
| 155 | * - "fromdbmaster" or WikiPage::READ_LATEST: from the master DB |
||
| 156 | * - "forupdate" or WikiPage::READ_LOCKING: from the master DB using SELECT FOR UPDATE |
||
| 157 | * @return WikiPage |
||
| 158 | */ |
||
| 159 | public static function newFromRow( $row, $from = 'fromdb' ) { |
||
| 164 | |||
| 165 | /** |
||
| 166 | * Convert 'fromdb', 'fromdbmaster' and 'forupdate' to READ_* constants. |
||
| 167 | * |
||
| 168 | * @param object|string|int $type |
||
| 169 | * @return mixed |
||
| 170 | */ |
||
| 171 | private static function convertSelectType( $type ) { |
||
| 184 | |||
| 185 | /** |
||
| 186 | * @todo Move this UI stuff somewhere else |
||
| 187 | * |
||
| 188 | * @see ContentHandler::getActionOverrides |
||
| 189 | */ |
||
| 190 | public function getActionOverrides() { |
||
| 193 | |||
| 194 | /** |
||
| 195 | * Returns the ContentHandler instance to be used to deal with the content of this WikiPage. |
||
| 196 | * |
||
| 197 | * Shorthand for ContentHandler::getForModelID( $this->getContentModel() ); |
||
| 198 | * |
||
| 199 | * @return ContentHandler |
||
| 200 | * |
||
| 201 | * @since 1.21 |
||
| 202 | */ |
||
| 203 | public function getContentHandler() { |
||
| 206 | |||
| 207 | /** |
||
| 208 | * Get the title object of the article |
||
| 209 | * @return Title Title object of this page |
||
| 210 | */ |
||
| 211 | public function getTitle() { |
||
| 214 | |||
| 215 | /** |
||
| 216 | * Clear the object |
||
| 217 | * @return void |
||
| 218 | */ |
||
| 219 | public function clear() { |
||
| 225 | |||
| 226 | /** |
||
| 227 | * Clear the object cache fields |
||
| 228 | * @return void |
||
| 229 | */ |
||
| 230 | protected function clearCacheFields() { |
||
| 244 | |||
| 245 | /** |
||
| 246 | * Clear the mPreparedEdit cache field, as may be needed by mutable content types |
||
| 247 | * @return void |
||
| 248 | * @since 1.23 |
||
| 249 | */ |
||
| 250 | public function clearPreparedEdit() { |
||
| 253 | |||
| 254 | /** |
||
| 255 | * Return the list of revision fields that should be selected to create |
||
| 256 | * a new page. |
||
| 257 | * |
||
| 258 | * @return array |
||
| 259 | */ |
||
| 260 | public static function selectFields() { |
||
| 287 | |||
| 288 | /** |
||
| 289 | * Fetch a page record with the given conditions |
||
| 290 | * @param IDatabase $dbr |
||
| 291 | * @param array $conditions |
||
| 292 | * @param array $options |
||
| 293 | * @return object|bool Database result resource, or false on failure |
||
| 294 | */ |
||
| 295 | protected function pageData( $dbr, $conditions, $options = [] ) { |
||
| 306 | |||
| 307 | /** |
||
| 308 | * Fetch a page record matching the Title object's namespace and title |
||
| 309 | * using a sanitized title string |
||
| 310 | * |
||
| 311 | * @param IDatabase $dbr |
||
| 312 | * @param Title $title |
||
| 313 | * @param array $options |
||
| 314 | * @return object|bool Database result resource, or false on failure |
||
| 315 | */ |
||
| 316 | public function pageDataFromTitle( $dbr, $title, $options = [] ) { |
||
| 321 | |||
| 322 | /** |
||
| 323 | * Fetch a page record matching the requested ID |
||
| 324 | * |
||
| 325 | * @param IDatabase $dbr |
||
| 326 | * @param int $id |
||
| 327 | * @param array $options |
||
| 328 | * @return object|bool Database result resource, or false on failure |
||
| 329 | */ |
||
| 330 | public function pageDataFromId( $dbr, $id, $options = [] ) { |
||
| 333 | |||
| 334 | /** |
||
| 335 | * Load the object from a given source by title |
||
| 336 | * |
||
| 337 | * @param object|string|int $from One of the following: |
||
| 338 | * - A DB query result object. |
||
| 339 | * - "fromdb" or WikiPage::READ_NORMAL to get from a slave DB. |
||
| 340 | * - "fromdbmaster" or WikiPage::READ_LATEST to get from the master DB. |
||
| 341 | * - "forupdate" or WikiPage::READ_LOCKING to get from the master DB |
||
| 342 | * using SELECT FOR UPDATE. |
||
| 343 | * |
||
| 344 | * @return void |
||
| 345 | */ |
||
| 346 | public function loadPageData( $from = 'fromdb' ) { |
||
| 374 | |||
| 375 | /** |
||
| 376 | * Load the object from a database row |
||
| 377 | * |
||
| 378 | * @since 1.20 |
||
| 379 | * @param object|bool $data DB row containing fields returned by selectFields() or false |
||
| 380 | * @param string|int $from One of the following: |
||
| 381 | * - "fromdb" or WikiPage::READ_NORMAL if the data comes from a slave DB |
||
| 382 | * - "fromdbmaster" or WikiPage::READ_LATEST if the data comes from the master DB |
||
| 383 | * - "forupdate" or WikiPage::READ_LOCKING if the data comes from |
||
| 384 | * the master DB using SELECT FOR UPDATE |
||
| 385 | */ |
||
| 386 | public function loadFromRow( $data, $from ) { |
||
| 422 | |||
| 423 | /** |
||
| 424 | * @return int Page ID |
||
| 425 | */ |
||
| 426 | public function getId() { |
||
| 432 | |||
| 433 | /** |
||
| 434 | * @return bool Whether or not the page exists in the database |
||
| 435 | */ |
||
| 436 | public function exists() { |
||
| 442 | |||
| 443 | /** |
||
| 444 | * Check if this page is something we're going to be showing |
||
| 445 | * some sort of sensible content for. If we return false, page |
||
| 446 | * views (plain action=view) will return an HTTP 404 response, |
||
| 447 | * so spiders and robots can know they're following a bad link. |
||
| 448 | * |
||
| 449 | * @return bool |
||
| 450 | */ |
||
| 451 | public function hasViewableContent() { |
||
| 454 | |||
| 455 | /** |
||
| 456 | * Tests if the article content represents a redirect |
||
| 457 | * |
||
| 458 | * @return bool |
||
| 459 | */ |
||
| 460 | public function isRedirect() { |
||
| 467 | |||
| 468 | /** |
||
| 469 | * Returns the page's content model id (see the CONTENT_MODEL_XXX constants). |
||
| 470 | * |
||
| 471 | * Will use the revisions actual content model if the page exists, |
||
| 472 | * and the page's default if the page doesn't exist yet. |
||
| 473 | * |
||
| 474 | * @return string |
||
| 475 | * |
||
| 476 | * @since 1.21 |
||
| 477 | */ |
||
| 478 | public function getContentModel() { |
||
| 494 | |||
| 495 | /** |
||
| 496 | * Loads page_touched and returns a value indicating if it should be used |
||
| 497 | * @return bool True if not a redirect |
||
| 498 | */ |
||
| 499 | public function checkTouched() { |
||
| 505 | |||
| 506 | /** |
||
| 507 | * Get the page_touched field |
||
| 508 | * @return string Containing GMT timestamp |
||
| 509 | */ |
||
| 510 | public function getTouched() { |
||
| 516 | |||
| 517 | /** |
||
| 518 | * Get the page_links_updated field |
||
| 519 | * @return string|null Containing GMT timestamp |
||
| 520 | */ |
||
| 521 | public function getLinksTimestamp() { |
||
| 527 | |||
| 528 | /** |
||
| 529 | * Get the page_latest field |
||
| 530 | * @return int The rev_id of current revision |
||
| 531 | */ |
||
| 532 | public function getLatest() { |
||
| 538 | |||
| 539 | /** |
||
| 540 | * Get the Revision object of the oldest revision |
||
| 541 | * @return Revision|null |
||
| 542 | */ |
||
| 543 | public function getOldestRevision() { |
||
| 576 | |||
| 577 | /** |
||
| 578 | * Loads everything except the text |
||
| 579 | * This isn't necessary for all uses, so it's only done if needed. |
||
| 580 | */ |
||
| 581 | protected function loadLastEdit() { |
||
| 613 | |||
| 614 | /** |
||
| 615 | * Set the latest revision |
||
| 616 | * @param Revision $revision |
||
| 617 | */ |
||
| 618 | protected function setLastEdit( Revision $revision ) { |
||
| 622 | |||
| 623 | /** |
||
| 624 | * Get the latest revision |
||
| 625 | * @return Revision|null |
||
| 626 | */ |
||
| 627 | public function getRevision() { |
||
| 634 | |||
| 635 | /** |
||
| 636 | * Get the content of the current revision. No side-effects... |
||
| 637 | * |
||
| 638 | * @param int $audience One of: |
||
| 639 | * Revision::FOR_PUBLIC to be displayed to all users |
||
| 640 | * Revision::FOR_THIS_USER to be displayed to $wgUser |
||
| 641 | * Revision::RAW get the text regardless of permissions |
||
| 642 | * @param User $user User object to check for, only if FOR_THIS_USER is passed |
||
| 643 | * to the $audience parameter |
||
| 644 | * @return Content|null The content of the current revision |
||
| 645 | * |
||
| 646 | * @since 1.21 |
||
| 647 | */ |
||
| 648 | View Code Duplication | public function getContent( $audience = Revision::FOR_PUBLIC, User $user = null ) { |
|
| 655 | |||
| 656 | /** |
||
| 657 | * Get the text of the current revision. No side-effects... |
||
| 658 | * |
||
| 659 | * @param int $audience One of: |
||
| 660 | * Revision::FOR_PUBLIC to be displayed to all users |
||
| 661 | * Revision::FOR_THIS_USER to be displayed to the given user |
||
| 662 | * Revision::RAW get the text regardless of permissions |
||
| 663 | * @param User $user User object to check for, only if FOR_THIS_USER is passed |
||
| 664 | * to the $audience parameter |
||
| 665 | * @return string|bool The text of the current revision |
||
| 666 | * @deprecated since 1.21, getContent() should be used instead. |
||
| 667 | */ |
||
| 668 | public function getText( $audience = Revision::FOR_PUBLIC, User $user = null ) { |
||
| 677 | |||
| 678 | /** |
||
| 679 | * @return string MW timestamp of last article revision |
||
| 680 | */ |
||
| 681 | public function getTimestamp() { |
||
| 689 | |||
| 690 | /** |
||
| 691 | * Set the page timestamp (use only to avoid DB queries) |
||
| 692 | * @param string $ts MW timestamp of last article revision |
||
| 693 | * @return void |
||
| 694 | */ |
||
| 695 | public function setTimestamp( $ts ) { |
||
| 698 | |||
| 699 | /** |
||
| 700 | * @param int $audience One of: |
||
| 701 | * Revision::FOR_PUBLIC to be displayed to all users |
||
| 702 | * Revision::FOR_THIS_USER to be displayed to the given user |
||
| 703 | * Revision::RAW get the text regardless of permissions |
||
| 704 | * @param User $user User object to check for, only if FOR_THIS_USER is passed |
||
| 705 | * to the $audience parameter |
||
| 706 | * @return int User ID for the user that made the last article revision |
||
| 707 | */ |
||
| 708 | View Code Duplication | public function getUser( $audience = Revision::FOR_PUBLIC, User $user = null ) { |
|
| 716 | |||
| 717 | /** |
||
| 718 | * Get the User object of the user who created the page |
||
| 719 | * @param int $audience One of: |
||
| 720 | * Revision::FOR_PUBLIC to be displayed to all users |
||
| 721 | * Revision::FOR_THIS_USER to be displayed to the given user |
||
| 722 | * Revision::RAW get the text regardless of permissions |
||
| 723 | * @param User $user User object to check for, only if FOR_THIS_USER is passed |
||
| 724 | * to the $audience parameter |
||
| 725 | * @return User|null |
||
| 726 | */ |
||
| 727 | public function getCreator( $audience = Revision::FOR_PUBLIC, User $user = null ) { |
||
| 736 | |||
| 737 | /** |
||
| 738 | * @param int $audience One of: |
||
| 739 | * Revision::FOR_PUBLIC to be displayed to all users |
||
| 740 | * Revision::FOR_THIS_USER to be displayed to the given user |
||
| 741 | * Revision::RAW get the text regardless of permissions |
||
| 742 | * @param User $user User object to check for, only if FOR_THIS_USER is passed |
||
| 743 | * to the $audience parameter |
||
| 744 | * @return string Username of the user that made the last article revision |
||
| 745 | */ |
||
| 746 | View Code Duplication | public function getUserText( $audience = Revision::FOR_PUBLIC, User $user = null ) { |
|
| 754 | |||
| 755 | /** |
||
| 756 | * @param int $audience One of: |
||
| 757 | * Revision::FOR_PUBLIC to be displayed to all users |
||
| 758 | * Revision::FOR_THIS_USER to be displayed to the given user |
||
| 759 | * Revision::RAW get the text regardless of permissions |
||
| 760 | * @param User $user User object to check for, only if FOR_THIS_USER is passed |
||
| 761 | * to the $audience parameter |
||
| 762 | * @return string Comment stored for the last article revision |
||
| 763 | */ |
||
| 764 | View Code Duplication | public function getComment( $audience = Revision::FOR_PUBLIC, User $user = null ) { |
|
| 772 | |||
| 773 | /** |
||
| 774 | * Returns true if last revision was marked as "minor edit" |
||
| 775 | * |
||
| 776 | * @return bool Minor edit indicator for the last article revision. |
||
| 777 | */ |
||
| 778 | public function getMinorEdit() { |
||
| 786 | |||
| 787 | /** |
||
| 788 | * Determine whether a page would be suitable for being counted as an |
||
| 789 | * article in the site_stats table based on the title & its content |
||
| 790 | * |
||
| 791 | * @param object|bool $editInfo (false): object returned by prepareTextForEdit(), |
||
| 792 | * if false, the current database state will be used |
||
| 793 | * @return bool |
||
| 794 | */ |
||
| 795 | public function isCountable( $editInfo = false ) { |
||
| 831 | |||
| 832 | /** |
||
| 833 | * If this page is a redirect, get its target |
||
| 834 | * |
||
| 835 | * The target will be fetched from the redirect table if possible. |
||
| 836 | * If this page doesn't have an entry there, call insertRedirect() |
||
| 837 | * @return Title|null Title object, or null if this page is not a redirect |
||
| 838 | */ |
||
| 839 | public function getRedirectTarget() { |
||
| 869 | |||
| 870 | /** |
||
| 871 | * Insert an entry for this page into the redirect table if the content is a redirect |
||
| 872 | * |
||
| 873 | * The database update will be deferred via DeferredUpdates |
||
| 874 | * |
||
| 875 | * Don't call this function directly unless you know what you're doing. |
||
| 876 | * @return Title|null Title object or null if not a redirect |
||
| 877 | */ |
||
| 878 | public function insertRedirect() { |
||
| 894 | |||
| 895 | /** |
||
| 896 | * Insert or update the redirect table entry for this page to indicate it redirects to $rt |
||
| 897 | * @param Title $rt Redirect target |
||
| 898 | * @param int|null $oldLatest Prior page_latest for check and set |
||
| 899 | */ |
||
| 900 | public function insertRedirectEntry( Title $rt, $oldLatest = null ) { |
||
| 920 | |||
| 921 | /** |
||
| 922 | * Get the Title object or URL this page redirects to |
||
| 923 | * |
||
| 924 | * @return bool|Title|string False, Title of in-wiki target, or string with URL |
||
| 925 | */ |
||
| 926 | public function followRedirect() { |
||
| 929 | |||
| 930 | /** |
||
| 931 | * Get the Title object or URL to use for a redirect. We use Title |
||
| 932 | * objects for same-wiki, non-special redirects and URLs for everything |
||
| 933 | * else. |
||
| 934 | * @param Title $rt Redirect target |
||
| 935 | * @return bool|Title|string False, Title object of local target, or string with URL |
||
| 936 | */ |
||
| 937 | public function getRedirectURL( $rt ) { |
||
| 969 | |||
| 970 | /** |
||
| 971 | * Get a list of users who have edited this article, not including the user who made |
||
| 972 | * the most recent revision, which you can get from $article->getUser() if you want it |
||
| 973 | * @return UserArrayFromResult |
||
| 974 | */ |
||
| 975 | public function getContributors() { |
||
| 1021 | |||
| 1022 | /** |
||
| 1023 | * Should the parser cache be used? |
||
| 1024 | * |
||
| 1025 | * @param ParserOptions $parserOptions ParserOptions to check |
||
| 1026 | * @param int $oldId |
||
| 1027 | * @return bool |
||
| 1028 | */ |
||
| 1029 | public function shouldCheckParserCache( ParserOptions $parserOptions, $oldId ) { |
||
| 1035 | |||
| 1036 | /** |
||
| 1037 | * Get a ParserOutput for the given ParserOptions and revision ID. |
||
| 1038 | * |
||
| 1039 | * The parser cache will be used if possible. Cache misses that result |
||
| 1040 | * in parser runs are debounced with PoolCounter. |
||
| 1041 | * |
||
| 1042 | * @since 1.19 |
||
| 1043 | * @param ParserOptions $parserOptions ParserOptions to use for the parse operation |
||
| 1044 | * @param null|int $oldid Revision ID to get the text from, passing null or 0 will |
||
| 1045 | * get the current revision (default value) |
||
| 1046 | * |
||
| 1047 | * @return ParserOutput|bool ParserOutput or false if the revision was not found |
||
| 1048 | */ |
||
| 1049 | public function getParserOutput( ParserOptions $parserOptions, $oldid = null ) { |
||
| 1074 | |||
| 1075 | /** |
||
| 1076 | * Do standard deferred updates after page view (existing or missing page) |
||
| 1077 | * @param User $user The relevant user |
||
| 1078 | * @param int $oldid Revision id being viewed; if not given or 0, latest revision is assumed |
||
| 1079 | */ |
||
| 1080 | public function doViewUpdates( User $user, $oldid = 0 ) { |
||
| 1094 | |||
| 1095 | /** |
||
| 1096 | * Perform the actions of a page purging |
||
| 1097 | * @return bool |
||
| 1098 | */ |
||
| 1099 | public function doPurge() { |
||
| 1137 | |||
| 1138 | /** |
||
| 1139 | * Insert a new empty page record for this article. |
||
| 1140 | * This *must* be followed up by creating a revision |
||
| 1141 | * and running $this->updateRevisionOn( ... ); |
||
| 1142 | * or else the record will be left in a funky state. |
||
| 1143 | * Best if all done inside a transaction. |
||
| 1144 | * |
||
| 1145 | * @param IDatabase $dbw |
||
| 1146 | * @param int|null $pageId Custom page ID that will be used for the insert statement |
||
| 1147 | * |
||
| 1148 | * @return bool|int The newly created page_id key; false if the title already existed |
||
| 1149 | */ |
||
| 1150 | public function insertOn( $dbw, $pageId = null ) { |
||
| 1180 | |||
| 1181 | /** |
||
| 1182 | * Update the page record to point to a newly saved revision. |
||
| 1183 | * |
||
| 1184 | * @param IDatabase $dbw |
||
| 1185 | * @param Revision $revision For ID number, and text used to set |
||
| 1186 | * length and redirect status fields |
||
| 1187 | * @param int $lastRevision If given, will not overwrite the page field |
||
| 1188 | * when different from the currently set value. |
||
| 1189 | * Giving 0 indicates the new page flag should be set on. |
||
| 1190 | * @param bool $lastRevIsRedirect If given, will optimize adding and |
||
| 1191 | * removing rows in redirect table. |
||
| 1192 | * @return bool Success; false if the page row was missing or page_latest changed |
||
| 1193 | */ |
||
| 1194 | public function updateRevisionOn( $dbw, $revision, $lastRevision = null, |
||
| 1253 | |||
| 1254 | /** |
||
| 1255 | * Add row to the redirect table if this is a redirect, remove otherwise. |
||
| 1256 | * |
||
| 1257 | * @param IDatabase $dbw |
||
| 1258 | * @param Title $redirectTitle Title object pointing to the redirect target, |
||
| 1259 | * or NULL if this is not a redirect |
||
| 1260 | * @param null|bool $lastRevIsRedirect If given, will optimize adding and |
||
| 1261 | * removing rows in redirect table. |
||
| 1262 | * @return bool True on success, false on failure |
||
| 1263 | * @private |
||
| 1264 | */ |
||
| 1265 | public function updateRedirectOn( $dbw, $redirectTitle, $lastRevIsRedirect = null ) { |
||
| 1289 | |||
| 1290 | /** |
||
| 1291 | * If the given revision is newer than the currently set page_latest, |
||
| 1292 | * update the page record. Otherwise, do nothing. |
||
| 1293 | * |
||
| 1294 | * @deprecated since 1.24, use updateRevisionOn instead |
||
| 1295 | * |
||
| 1296 | * @param IDatabase $dbw |
||
| 1297 | * @param Revision $revision |
||
| 1298 | * @return bool |
||
| 1299 | */ |
||
| 1300 | public function updateIfNewerOn( $dbw, $revision ) { |
||
| 1326 | |||
| 1327 | /** |
||
| 1328 | * Get the content that needs to be saved in order to undo all revisions |
||
| 1329 | * between $undo and $undoafter. Revisions must belong to the same page, |
||
| 1330 | * must exist and must not be deleted |
||
| 1331 | * @param Revision $undo |
||
| 1332 | * @param Revision $undoafter Must be an earlier revision than $undo |
||
| 1333 | * @return Content|bool Content on success, false on failure |
||
| 1334 | * @since 1.21 |
||
| 1335 | * Before we had the Content object, this was done in getUndoText |
||
| 1336 | */ |
||
| 1337 | public function getUndoContent( Revision $undo, Revision $undoafter = null ) { |
||
| 1341 | |||
| 1342 | /** |
||
| 1343 | * Returns true if this page's content model supports sections. |
||
| 1344 | * |
||
| 1345 | * @return bool |
||
| 1346 | * |
||
| 1347 | * @todo The skin should check this and not offer section functionality if |
||
| 1348 | * sections are not supported. |
||
| 1349 | * @todo The EditPage should check this and not offer section functionality |
||
| 1350 | * if sections are not supported. |
||
| 1351 | */ |
||
| 1352 | public function supportsSections() { |
||
| 1355 | |||
| 1356 | /** |
||
| 1357 | * @param string|number|null|bool $sectionId Section identifier as a number or string |
||
| 1358 | * (e.g. 0, 1 or 'T-1'), null/false or an empty string for the whole page |
||
| 1359 | * or 'new' for a new section. |
||
| 1360 | * @param Content $sectionContent New content of the section. |
||
| 1361 | * @param string $sectionTitle New section's subject, only if $section is "new". |
||
| 1362 | * @param string $edittime Revision timestamp or null to use the current revision. |
||
| 1363 | * |
||
| 1364 | * @throws MWException |
||
| 1365 | * @return Content|null New complete article content, or null if error. |
||
| 1366 | * |
||
| 1367 | * @since 1.21 |
||
| 1368 | * @deprecated since 1.24, use replaceSectionAtRev instead |
||
| 1369 | */ |
||
| 1370 | public function replaceSectionContent( |
||
| 1395 | |||
| 1396 | /** |
||
| 1397 | * @param string|number|null|bool $sectionId Section identifier as a number or string |
||
| 1398 | * (e.g. 0, 1 or 'T-1'), null/false or an empty string for the whole page |
||
| 1399 | * or 'new' for a new section. |
||
| 1400 | * @param Content $sectionContent New content of the section. |
||
| 1401 | * @param string $sectionTitle New section's subject, only if $section is "new". |
||
| 1402 | * @param int|null $baseRevId |
||
| 1403 | * |
||
| 1404 | * @throws MWException |
||
| 1405 | * @return Content|null New complete article content, or null if error. |
||
| 1406 | * |
||
| 1407 | * @since 1.24 |
||
| 1408 | */ |
||
| 1409 | public function replaceSectionAtRev( $sectionId, Content $sectionContent, |
||
| 1446 | |||
| 1447 | /** |
||
| 1448 | * Check flags and add EDIT_NEW or EDIT_UPDATE to them as needed. |
||
| 1449 | * @param int $flags |
||
| 1450 | * @return int Updated $flags |
||
| 1451 | */ |
||
| 1452 | public function checkFlags( $flags ) { |
||
| 1463 | |||
| 1464 | /** |
||
| 1465 | * Change an existing article or create a new article. Updates RC and all necessary caches, |
||
| 1466 | * optionally via the deferred update array. |
||
| 1467 | * |
||
| 1468 | * @param string $text New text |
||
| 1469 | * @param string $summary Edit summary |
||
| 1470 | * @param int $flags Bitfield: |
||
| 1471 | * EDIT_NEW |
||
| 1472 | * Article is known or assumed to be non-existent, create a new one |
||
| 1473 | * EDIT_UPDATE |
||
| 1474 | * Article is known or assumed to be pre-existing, update it |
||
| 1475 | * EDIT_MINOR |
||
| 1476 | * Mark this edit minor, if the user is allowed to do so |
||
| 1477 | * EDIT_SUPPRESS_RC |
||
| 1478 | * Do not log the change in recentchanges |
||
| 1479 | * EDIT_FORCE_BOT |
||
| 1480 | * Mark the edit a "bot" edit regardless of user rights |
||
| 1481 | * EDIT_AUTOSUMMARY |
||
| 1482 | * Fill in blank summaries with generated text where possible |
||
| 1483 | * EDIT_INTERNAL |
||
| 1484 | * Signal that the page retrieve/save cycle happened entirely in this request. |
||
| 1485 | * |
||
| 1486 | * If neither EDIT_NEW nor EDIT_UPDATE is specified, the status of the |
||
| 1487 | * article will be detected. If EDIT_UPDATE is specified and the article |
||
| 1488 | * doesn't exist, the function will return an edit-gone-missing error. If |
||
| 1489 | * EDIT_NEW is specified and the article does exist, an edit-already-exists |
||
| 1490 | * error will be returned. These two conditions are also possible with |
||
| 1491 | * auto-detection due to MediaWiki's performance-optimised locking strategy. |
||
| 1492 | * |
||
| 1493 | * @param bool|int $baseRevId The revision ID this edit was based off, if any. |
||
| 1494 | * This is not the parent revision ID, rather the revision ID for older |
||
| 1495 | * content used as the source for a rollback, for example. |
||
| 1496 | * @param User $user The user doing the edit |
||
| 1497 | * |
||
| 1498 | * @throws MWException |
||
| 1499 | * @return Status Possible errors: |
||
| 1500 | * edit-hook-aborted: The ArticleSave hook aborted the edit but didn't |
||
| 1501 | * set the fatal flag of $status |
||
| 1502 | * edit-gone-missing: In update mode, but the article didn't exist. |
||
| 1503 | * edit-conflict: In update mode, the article changed unexpectedly. |
||
| 1504 | * edit-no-change: Warning that the text was the same as before. |
||
| 1505 | * edit-already-exists: In creation mode, but the article already exists. |
||
| 1506 | * |
||
| 1507 | * Extensions may define additional errors. |
||
| 1508 | * |
||
| 1509 | * $return->value will contain an associative array with members as follows: |
||
| 1510 | * new: Boolean indicating if the function attempted to create a new article. |
||
| 1511 | * revision: The revision object for the inserted revision, or null. |
||
| 1512 | * |
||
| 1513 | * Compatibility note: this function previously returned a boolean value |
||
| 1514 | * indicating success/failure |
||
| 1515 | * |
||
| 1516 | * @deprecated since 1.21: use doEditContent() instead. |
||
| 1517 | */ |
||
| 1518 | public function doEdit( $text, $summary, $flags = 0, $baseRevId = false, $user = null ) { |
||
| 1525 | |||
| 1526 | /** |
||
| 1527 | * Change an existing article or create a new article. Updates RC and all necessary caches, |
||
| 1528 | * optionally via the deferred update array. |
||
| 1529 | * |
||
| 1530 | * @param Content $content New content |
||
| 1531 | * @param string $summary Edit summary |
||
| 1532 | * @param int $flags Bitfield: |
||
| 1533 | * EDIT_NEW |
||
| 1534 | * Article is known or assumed to be non-existent, create a new one |
||
| 1535 | * EDIT_UPDATE |
||
| 1536 | * Article is known or assumed to be pre-existing, update it |
||
| 1537 | * EDIT_MINOR |
||
| 1538 | * Mark this edit minor, if the user is allowed to do so |
||
| 1539 | * EDIT_SUPPRESS_RC |
||
| 1540 | * Do not log the change in recentchanges |
||
| 1541 | * EDIT_FORCE_BOT |
||
| 1542 | * Mark the edit a "bot" edit regardless of user rights |
||
| 1543 | * EDIT_AUTOSUMMARY |
||
| 1544 | * Fill in blank summaries with generated text where possible |
||
| 1545 | * EDIT_INTERNAL |
||
| 1546 | * Signal that the page retrieve/save cycle happened entirely in this request. |
||
| 1547 | * |
||
| 1548 | * If neither EDIT_NEW nor EDIT_UPDATE is specified, the status of the |
||
| 1549 | * article will be detected. If EDIT_UPDATE is specified and the article |
||
| 1550 | * doesn't exist, the function will return an edit-gone-missing error. If |
||
| 1551 | * EDIT_NEW is specified and the article does exist, an edit-already-exists |
||
| 1552 | * error will be returned. These two conditions are also possible with |
||
| 1553 | * auto-detection due to MediaWiki's performance-optimised locking strategy. |
||
| 1554 | * |
||
| 1555 | * @param bool|int $baseRevId The revision ID this edit was based off, if any. |
||
| 1556 | * This is not the parent revision ID, rather the revision ID for older |
||
| 1557 | * content used as the source for a rollback, for example. |
||
| 1558 | * @param User $user The user doing the edit |
||
| 1559 | * @param string $serialFormat Format for storing the content in the |
||
| 1560 | * database. |
||
| 1561 | * @param array|null $tags Change tags to apply to this edit |
||
| 1562 | * Callers are responsible for permission checks |
||
| 1563 | * (with ChangeTags::canAddTagsAccompanyingChange) |
||
| 1564 | * |
||
| 1565 | * @throws MWException |
||
| 1566 | * @return Status Possible errors: |
||
| 1567 | * edit-hook-aborted: The ArticleSave hook aborted the edit but didn't |
||
| 1568 | * set the fatal flag of $status. |
||
| 1569 | * edit-gone-missing: In update mode, but the article didn't exist. |
||
| 1570 | * edit-conflict: In update mode, the article changed unexpectedly. |
||
| 1571 | * edit-no-change: Warning that the text was the same as before. |
||
| 1572 | * edit-already-exists: In creation mode, but the article already exists. |
||
| 1573 | * |
||
| 1574 | * Extensions may define additional errors. |
||
| 1575 | * |
||
| 1576 | * $return->value will contain an associative array with members as follows: |
||
| 1577 | * new: Boolean indicating if the function attempted to create a new article. |
||
| 1578 | * revision: The revision object for the inserted revision, or null. |
||
| 1579 | * |
||
| 1580 | * @since 1.21 |
||
| 1581 | * @throws MWException |
||
| 1582 | */ |
||
| 1583 | public function doEditContent( |
||
| 1584 | Content $content, $summary, $flags = 0, $baseRevId = false, |
||
| 1585 | User $user = null, $serialFormat = null, $tags = null |
||
| 1586 | ) { |
||
| 1587 | global $wgUser, $wgUseAutomaticEditSummaries; |
||
| 1588 | |||
| 1589 | // Low-level sanity check |
||
| 1590 | if ( $this->mTitle->getText() === '' ) { |
||
| 1591 | throw new MWException( 'Something is trying to edit an article with an empty title' ); |
||
| 1592 | } |
||
| 1593 | // Make sure the given content type is allowed for this page |
||
| 1594 | if ( !$content->getContentHandler()->canBeUsedOn( $this->mTitle ) ) { |
||
| 1595 | return Status::newFatal( 'content-not-allowed-here', |
||
| 1596 | ContentHandler::getLocalizedName( $content->getModel() ), |
||
| 1597 | $this->mTitle->getPrefixedText() |
||
| 1598 | ); |
||
| 1599 | } |
||
| 1600 | |||
| 1601 | // Load the data from the master database if needed. |
||
| 1602 | // The caller may already loaded it from the master or even loaded it using |
||
| 1603 | // SELECT FOR UPDATE, so do not override that using clear(). |
||
| 1604 | $this->loadPageData( 'fromdbmaster' ); |
||
| 1605 | |||
| 1606 | $user = $user ?: $wgUser; |
||
| 1607 | $flags = $this->checkFlags( $flags ); |
||
| 1608 | |||
| 1609 | // Trigger pre-save hook (using provided edit summary) |
||
| 1610 | $hookStatus = Status::newGood( [] ); |
||
| 1611 | $hook_args = [ &$this, &$user, &$content, &$summary, |
||
| 1612 | $flags & EDIT_MINOR, null, null, &$flags, &$hookStatus ]; |
||
| 1613 | // Check if the hook rejected the attempted save |
||
| 1614 | if ( !Hooks::run( 'PageContentSave', $hook_args ) |
||
| 1615 | || !ContentHandler::runLegacyHooks( 'ArticleSave', $hook_args ) |
||
| 1616 | ) { |
||
| 1617 | if ( $hookStatus->isOK() ) { |
||
| 1618 | // Hook returned false but didn't call fatal(); use generic message |
||
| 1619 | $hookStatus->fatal( 'edit-hook-aborted' ); |
||
| 1620 | } |
||
| 1621 | |||
| 1622 | return $hookStatus; |
||
| 1623 | } |
||
| 1624 | |||
| 1625 | $old_revision = $this->getRevision(); // current revision |
||
| 1626 | $old_content = $this->getContent( Revision::RAW ); // current revision's content |
||
| 1627 | |||
| 1628 | // Provide autosummaries if one is not provided and autosummaries are enabled |
||
| 1629 | if ( $wgUseAutomaticEditSummaries && ( $flags & EDIT_AUTOSUMMARY ) && $summary == '' ) { |
||
| 1630 | $handler = $content->getContentHandler(); |
||
| 1631 | $summary = $handler->getAutosummary( $old_content, $content, $flags ); |
||
| 1632 | } |
||
| 1633 | |||
| 1634 | // Avoid statsd noise and wasted cycles check the edit stash (T136678) |
||
| 1635 | if ( ( $flags & EDIT_INTERNAL ) || ( $flags & EDIT_FORCE_BOT ) ) { |
||
| 1636 | $useCache = false; |
||
| 1637 | } else { |
||
| 1638 | $useCache = true; |
||
| 1639 | } |
||
| 1640 | |||
| 1641 | // Get the pre-save transform content and final parser output |
||
| 1642 | $editInfo = $this->prepareContentForEdit( $content, null, $user, $serialFormat, $useCache ); |
||
| 1643 | $pstContent = $editInfo->pstContent; // Content object |
||
| 1644 | $meta = [ |
||
| 1645 | 'bot' => ( $flags & EDIT_FORCE_BOT ), |
||
| 1646 | 'minor' => ( $flags & EDIT_MINOR ) && $user->isAllowed( 'minoredit' ), |
||
| 1647 | 'serialized' => $editInfo->pst, |
||
| 1648 | 'serialFormat' => $serialFormat, |
||
| 1649 | 'baseRevId' => $baseRevId, |
||
| 1650 | 'oldRevision' => $old_revision, |
||
| 1651 | 'oldContent' => $old_content, |
||
| 1652 | 'oldId' => $this->getLatest(), |
||
| 1653 | 'oldIsRedirect' => $this->isRedirect(), |
||
| 1654 | 'oldCountable' => $this->isCountable(), |
||
| 1655 | 'tags' => ( $tags !== null ) ? (array)$tags : [] |
||
| 1656 | ]; |
||
| 1657 | |||
| 1658 | // Actually create the revision and create/update the page |
||
| 1659 | if ( $flags & EDIT_UPDATE ) { |
||
| 1660 | $status = $this->doModify( $pstContent, $flags, $user, $summary, $meta ); |
||
| 1661 | } else { |
||
| 1662 | $status = $this->doCreate( $pstContent, $flags, $user, $summary, $meta ); |
||
| 1663 | } |
||
| 1664 | |||
| 1665 | // Promote user to any groups they meet the criteria for |
||
| 1666 | DeferredUpdates::addCallableUpdate( function () use ( $user ) { |
||
| 1667 | $user->addAutopromoteOnceGroups( 'onEdit' ); |
||
| 1668 | $user->addAutopromoteOnceGroups( 'onView' ); // b/c |
||
| 1669 | } ); |
||
| 1670 | |||
| 1671 | return $status; |
||
| 1672 | } |
||
| 1673 | |||
| 1674 | /** |
||
| 1675 | * @param Content $content Pre-save transform content |
||
| 1676 | * @param integer $flags |
||
| 1677 | * @param User $user |
||
| 1678 | * @param string $summary |
||
| 1679 | * @param array $meta |
||
| 1680 | * @return Status |
||
| 1681 | * @throws DBUnexpectedError |
||
| 1682 | * @throws Exception |
||
| 1683 | * @throws FatalError |
||
| 1684 | * @throws MWException |
||
| 1685 | */ |
||
| 1686 | private function doModify( |
||
| 1844 | |||
| 1845 | /** |
||
| 1846 | * @param Content $content Pre-save transform content |
||
| 1847 | * @param integer $flags |
||
| 1848 | * @param User $user |
||
| 1849 | * @param string $summary |
||
| 1850 | * @param array $meta |
||
| 1851 | * @return Status |
||
| 1852 | * @throws DBUnexpectedError |
||
| 1853 | * @throws Exception |
||
| 1854 | * @throws FatalError |
||
| 1855 | * @throws MWException |
||
| 1856 | */ |
||
| 1857 | private function doCreate( |
||
| 1968 | |||
| 1969 | /** |
||
| 1970 | * Get parser options suitable for rendering the primary article wikitext |
||
| 1971 | * |
||
| 1972 | * @see ContentHandler::makeParserOptions |
||
| 1973 | * |
||
| 1974 | * @param IContextSource|User|string $context One of the following: |
||
| 1975 | * - IContextSource: Use the User and the Language of the provided |
||
| 1976 | * context |
||
| 1977 | * - User: Use the provided User object and $wgLang for the language, |
||
| 1978 | * so use an IContextSource object if possible. |
||
| 1979 | * - 'canonical': Canonical options (anonymous user with default |
||
| 1980 | * preferences and content language). |
||
| 1981 | * @return ParserOptions |
||
| 1982 | */ |
||
| 1983 | public function makeParserOptions( $context ) { |
||
| 1994 | |||
| 1995 | /** |
||
| 1996 | * Prepare text which is about to be saved. |
||
| 1997 | * Returns a stdClass with source, pst and output members |
||
| 1998 | * |
||
| 1999 | * @param string $text |
||
| 2000 | * @param int|null $revid |
||
| 2001 | * @param User|null $user |
||
| 2002 | * @deprecated since 1.21: use prepareContentForEdit instead. |
||
| 2003 | * @return object |
||
| 2004 | */ |
||
| 2005 | public function prepareTextForEdit( $text, $revid = null, User $user = null ) { |
||
| 2010 | |||
| 2011 | /** |
||
| 2012 | * Prepare content which is about to be saved. |
||
| 2013 | * Returns a stdClass with source, pst and output members |
||
| 2014 | * |
||
| 2015 | * @param Content $content |
||
| 2016 | * @param Revision|int|null $revision Revision object. For backwards compatibility, a |
||
| 2017 | * revision ID is also accepted, but this is deprecated. |
||
| 2018 | * @param User|null $user |
||
| 2019 | * @param string|null $serialFormat |
||
| 2020 | * @param bool $useCache Check shared prepared edit cache |
||
| 2021 | * |
||
| 2022 | * @return object |
||
| 2023 | * |
||
| 2024 | * @since 1.21 |
||
| 2025 | */ |
||
| 2026 | public function prepareContentForEdit( |
||
| 2136 | |||
| 2137 | /** |
||
| 2138 | * Do standard deferred updates after page edit. |
||
| 2139 | * Update links tables, site stats, search index and message cache. |
||
| 2140 | * Purges pages that include this page if the text was changed here. |
||
| 2141 | * Every 100th edit, prune the recent changes table. |
||
| 2142 | * |
||
| 2143 | * @param Revision $revision |
||
| 2144 | * @param User $user User object that did the revision |
||
| 2145 | * @param array $options Array of options, following indexes are used: |
||
| 2146 | * - changed: boolean, whether the revision changed the content (default true) |
||
| 2147 | * - created: boolean, whether the revision created the page (default false) |
||
| 2148 | * - moved: boolean, whether the page was moved (default false) |
||
| 2149 | * - restored: boolean, whether the page was undeleted (default false) |
||
| 2150 | * - oldrevision: Revision object for the pre-update revision (default null) |
||
| 2151 | * - oldcountable: boolean, null, or string 'no-change' (default null): |
||
| 2152 | * - boolean: whether the page was counted as an article before that |
||
| 2153 | * revision, only used in changed is true and created is false |
||
| 2154 | * - null: if created is false, don't update the article count; if created |
||
| 2155 | * is true, do update the article count |
||
| 2156 | * - 'no-change': don't update the article count, ever |
||
| 2157 | */ |
||
| 2158 | public function doEditUpdates( Revision $revision, User $user, array $options = [] ) { |
||
| 2310 | |||
| 2311 | /** |
||
| 2312 | * Edit an article without doing all that other stuff |
||
| 2313 | * The article must already exist; link tables etc |
||
| 2314 | * are not updated, caches are not flushed. |
||
| 2315 | * |
||
| 2316 | * @param Content $content Content submitted |
||
| 2317 | * @param User $user The relevant user |
||
| 2318 | * @param string $comment Comment submitted |
||
| 2319 | * @param bool $minor Whereas it's a minor modification |
||
| 2320 | * @param string $serialFormat Format for storing the content in the database |
||
| 2321 | */ |
||
| 2322 | public function doQuickEditContent( |
||
| 2345 | |||
| 2346 | /** |
||
| 2347 | * Update the article's restriction field, and leave a log entry. |
||
| 2348 | * This works for protection both existing and non-existing pages. |
||
| 2349 | * |
||
| 2350 | * @param array $limit Set of restriction keys |
||
| 2351 | * @param array $expiry Per restriction type expiration |
||
| 2352 | * @param int &$cascade Set to false if cascading protection isn't allowed. |
||
| 2353 | * @param string $reason |
||
| 2354 | * @param User $user The user updating the restrictions |
||
| 2355 | * @param string|string[] $tags Change tags to add to the pages and protection log entries |
||
| 2356 | * ($user should be able to add the specified tags before this is called) |
||
| 2357 | * @return Status Status object; if action is taken, $status->value is the log_id of the |
||
| 2358 | * protection log entry. |
||
| 2359 | */ |
||
| 2360 | public function doUpdateRestrictions( array $limit, array $expiry, |
||
| 2601 | |||
| 2602 | /** |
||
| 2603 | * Insert a new null revision for this page. |
||
| 2604 | * |
||
| 2605 | * @param string $revCommentMsg Comment message key for the revision |
||
| 2606 | * @param array $limit Set of restriction keys |
||
| 2607 | * @param array $expiry Per restriction type expiration |
||
| 2608 | * @param int $cascade Set to false if cascading protection isn't allowed. |
||
| 2609 | * @param string $reason |
||
| 2610 | * @param User|null $user |
||
| 2611 | * @return Revision|null Null on error |
||
| 2612 | */ |
||
| 2613 | public function insertProtectNullRevision( $revCommentMsg, array $limit, |
||
| 2653 | |||
| 2654 | /** |
||
| 2655 | * @param string $expiry 14-char timestamp or "infinity", or false if the input was invalid |
||
| 2656 | * @return string |
||
| 2657 | */ |
||
| 2658 | protected function formatExpiry( $expiry ) { |
||
| 2673 | |||
| 2674 | /** |
||
| 2675 | * Builds the description to serve as comment for the edit. |
||
| 2676 | * |
||
| 2677 | * @param array $limit Set of restriction keys |
||
| 2678 | * @param array $expiry Per restriction type expiration |
||
| 2679 | * @return string |
||
| 2680 | */ |
||
| 2681 | public function protectDescription( array $limit, array $expiry ) { |
||
| 2711 | |||
| 2712 | /** |
||
| 2713 | * Builds the description to serve as comment for the log entry. |
||
| 2714 | * |
||
| 2715 | * Some bots may parse IRC lines, which are generated from log entries which contain plain |
||
| 2716 | * protect description text. Keep them in old format to avoid breaking compatibility. |
||
| 2717 | * TODO: Fix protection log to store structured description and format it on-the-fly. |
||
| 2718 | * |
||
| 2719 | * @param array $limit Set of restriction keys |
||
| 2720 | * @param array $expiry Per restriction type expiration |
||
| 2721 | * @return string |
||
| 2722 | */ |
||
| 2723 | public function protectDescriptionLog( array $limit, array $expiry ) { |
||
| 2736 | |||
| 2737 | /** |
||
| 2738 | * Take an array of page restrictions and flatten it to a string |
||
| 2739 | * suitable for insertion into the page_restrictions field. |
||
| 2740 | * |
||
| 2741 | * @param string[] $limit |
||
| 2742 | * |
||
| 2743 | * @throws MWException |
||
| 2744 | * @return string |
||
| 2745 | */ |
||
| 2746 | protected static function flattenRestrictions( $limit ) { |
||
| 2760 | |||
| 2761 | /** |
||
| 2762 | * Same as doDeleteArticleReal(), but returns a simple boolean. This is kept around for |
||
| 2763 | * backwards compatibility, if you care about error reporting you should use |
||
| 2764 | * doDeleteArticleReal() instead. |
||
| 2765 | * |
||
| 2766 | * Deletes the article with database consistency, writes logs, purges caches |
||
| 2767 | * |
||
| 2768 | * @param string $reason Delete reason for deletion log |
||
| 2769 | * @param bool $suppress Suppress all revisions and log the deletion in |
||
| 2770 | * the suppression log instead of the deletion log |
||
| 2771 | * @param int $u1 Unused |
||
| 2772 | * @param bool $u2 Unused |
||
| 2773 | * @param array|string &$error Array of errors to append to |
||
| 2774 | * @param User $user The deleting user |
||
| 2775 | * @return bool True if successful |
||
| 2776 | */ |
||
| 2777 | public function doDeleteArticle( |
||
| 2783 | |||
| 2784 | /** |
||
| 2785 | * Back-end article deletion |
||
| 2786 | * Deletes the article with database consistency, writes logs, purges caches |
||
| 2787 | * |
||
| 2788 | * @since 1.19 |
||
| 2789 | * |
||
| 2790 | * @param string $reason Delete reason for deletion log |
||
| 2791 | * @param bool $suppress Suppress all revisions and log the deletion in |
||
| 2792 | * the suppression log instead of the deletion log |
||
| 2793 | * @param int $u1 Unused |
||
| 2794 | * @param bool $u2 Unused |
||
| 2795 | * @param array|string &$error Array of errors to append to |
||
| 2796 | * @param User $user The deleting user |
||
| 2797 | * @return Status Status object; if successful, $status->value is the log_id of the |
||
| 2798 | * deletion log entry. If the page couldn't be deleted because it wasn't |
||
| 2799 | * found, $status is a non-fatal 'cannotdelete' error |
||
| 2800 | */ |
||
| 2801 | public function doDeleteArticleReal( |
||
| 2953 | |||
| 2954 | /** |
||
| 2955 | * Lock the page row for this title+id and return page_latest (or 0) |
||
| 2956 | * |
||
| 2957 | * @return integer Returns 0 if no row was found with this title+id |
||
| 2958 | * @since 1.27 |
||
| 2959 | */ |
||
| 2960 | public function lockAndGetLatest() { |
||
| 2975 | |||
| 2976 | /** |
||
| 2977 | * Do some database updates after deletion |
||
| 2978 | * |
||
| 2979 | * @param int $id The page_id value of the page being deleted |
||
| 2980 | * @param Content $content Optional page content to be used when determining |
||
| 2981 | * the required updates. This may be needed because $this->getContent() |
||
| 2982 | * may already return null when the page proper was deleted. |
||
| 2983 | */ |
||
| 2984 | public function doDeleteUpdates( $id, Content $content = null ) { |
||
| 3011 | |||
| 3012 | /** |
||
| 3013 | * Roll back the most recent consecutive set of edits to a page |
||
| 3014 | * from the same user; fails if there are no eligible edits to |
||
| 3015 | * roll back to, e.g. user is the sole contributor. This function |
||
| 3016 | * performs permissions checks on $user, then calls commitRollback() |
||
| 3017 | * to do the dirty work |
||
| 3018 | * |
||
| 3019 | * @todo Separate the business/permission stuff out from backend code |
||
| 3020 | * @todo Remove $token parameter. Already verified by RollbackAction and ApiRollback. |
||
| 3021 | * |
||
| 3022 | * @param string $fromP Name of the user whose edits to rollback. |
||
| 3023 | * @param string $summary Custom summary. Set to default summary if empty. |
||
| 3024 | * @param string $token Rollback token. |
||
| 3025 | * @param bool $bot If true, mark all reverted edits as bot. |
||
| 3026 | * |
||
| 3027 | * @param array $resultDetails Array contains result-specific array of additional values |
||
| 3028 | * 'alreadyrolled' : 'current' (rev) |
||
| 3029 | * success : 'summary' (str), 'current' (rev), 'target' (rev) |
||
| 3030 | * |
||
| 3031 | * @param User $user The user performing the rollback |
||
| 3032 | * @param array|null $tags Change tags to apply to the rollback |
||
| 3033 | * Callers are responsible for permission checks |
||
| 3034 | * (with ChangeTags::canAddTagsAccompanyingChange) |
||
| 3035 | * |
||
| 3036 | * @return array Array of errors, each error formatted as |
||
| 3037 | * array(messagekey, param1, param2, ...). |
||
| 3038 | * On success, the array is empty. This array can also be passed to |
||
| 3039 | * OutputPage::showPermissionsErrorPage(). |
||
| 3040 | */ |
||
| 3041 | public function doRollback( |
||
| 3066 | |||
| 3067 | /** |
||
| 3068 | * Backend implementation of doRollback(), please refer there for parameter |
||
| 3069 | * and return value documentation |
||
| 3070 | * |
||
| 3071 | * NOTE: This function does NOT check ANY permissions, it just commits the |
||
| 3072 | * rollback to the DB. Therefore, you should only call this function direct- |
||
| 3073 | * ly if you want to use custom permissions checks. If you don't, use |
||
| 3074 | * doRollback() instead. |
||
| 3075 | * @param string $fromP Name of the user whose edits to rollback. |
||
| 3076 | * @param string $summary Custom summary. Set to default summary if empty. |
||
| 3077 | * @param bool $bot If true, mark all reverted edits as bot. |
||
| 3078 | * |
||
| 3079 | * @param array $resultDetails Contains result-specific array of additional values |
||
| 3080 | * @param User $guser The user performing the rollback |
||
| 3081 | * @param array|null $tags Change tags to apply to the rollback |
||
| 3082 | * Callers are responsible for permission checks |
||
| 3083 | * (with ChangeTags::canAddTagsAccompanyingChange) |
||
| 3084 | * |
||
| 3085 | * @return array |
||
| 3086 | */ |
||
| 3087 | public function commitRollback( $fromP, $summary, $bot, |
||
| 3243 | |||
| 3244 | /** |
||
| 3245 | * The onArticle*() functions are supposed to be a kind of hooks |
||
| 3246 | * which should be called whenever any of the specified actions |
||
| 3247 | * are done. |
||
| 3248 | * |
||
| 3249 | * This is a good place to put code to clear caches, for instance. |
||
| 3250 | * |
||
| 3251 | * This is called on page move and undelete, as well as edit |
||
| 3252 | * |
||
| 3253 | * @param Title $title |
||
| 3254 | */ |
||
| 3255 | public static function onArticleCreate( Title $title ) { |
||
| 3265 | |||
| 3266 | /** |
||
| 3267 | * Clears caches when article is deleted |
||
| 3268 | * |
||
| 3269 | * @param Title $title |
||
| 3270 | */ |
||
| 3271 | public static function onArticleDelete( Title $title ) { |
||
| 3311 | |||
| 3312 | /** |
||
| 3313 | * Purge caches on page update etc |
||
| 3314 | * |
||
| 3315 | * @param Title $title |
||
| 3316 | * @param Revision|null $revision Revision that was just saved, may be null |
||
| 3317 | */ |
||
| 3318 | public static function onArticleEdit( Title $title, Revision $revision = null ) { |
||
| 3335 | |||
| 3336 | /**#@-*/ |
||
| 3337 | |||
| 3338 | /** |
||
| 3339 | * Returns a list of categories this page is a member of. |
||
| 3340 | * Results will include hidden categories |
||
| 3341 | * |
||
| 3342 | * @return TitleArray |
||
| 3343 | */ |
||
| 3344 | public function getCategories() { |
||
| 3360 | |||
| 3361 | /** |
||
| 3362 | * Returns a list of hidden categories this page is a member of. |
||
| 3363 | * Uses the page_props and categorylinks tables. |
||
| 3364 | * |
||
| 3365 | * @return array Array of Title objects |
||
| 3366 | */ |
||
| 3367 | public function getHiddenCategories() { |
||
| 3390 | |||
| 3391 | /** |
||
| 3392 | * Return an applicable autosummary if one exists for the given edit. |
||
| 3393 | * @param string|null $oldtext The previous text of the page. |
||
| 3394 | * @param string|null $newtext The submitted text of the page. |
||
| 3395 | * @param int $flags Bitmask: a bitmask of flags submitted for the edit. |
||
| 3396 | * @return string An appropriate autosummary, or an empty string. |
||
| 3397 | * |
||
| 3398 | * @deprecated since 1.21, use ContentHandler::getAutosummary() instead |
||
| 3399 | */ |
||
| 3400 | public static function getAutosummary( $oldtext, $newtext, $flags ) { |
||
| 3412 | |||
| 3413 | /** |
||
| 3414 | * Auto-generates a deletion reason |
||
| 3415 | * |
||
| 3416 | * @param bool &$hasHistory Whether the page has a history |
||
| 3417 | * @return string|bool String containing deletion reason or empty string, or boolean false |
||
| 3418 | * if no revision occurred |
||
| 3419 | */ |
||
| 3420 | public function getAutoDeleteReason( &$hasHistory ) { |
||
| 3423 | |||
| 3424 | /** |
||
| 3425 | * Update all the appropriate counts in the category table, given that |
||
| 3426 | * we've added the categories $added and deleted the categories $deleted. |
||
| 3427 | * |
||
| 3428 | * @param array $added The names of categories that were added |
||
| 3429 | * @param array $deleted The names of categories that were deleted |
||
| 3430 | * @param integer $id Page ID (this should be the original deleted page ID) |
||
| 3431 | */ |
||
| 3432 | public function updateCategoryCounts( array $added, array $deleted, $id = 0 ) { |
||
| 3433 | $id = $id ?: $this->getId(); |
||
| 3434 | $dbw = wfGetDB( DB_MASTER ); |
||
| 3435 | $method = __METHOD__; |
||
| 3436 | // Do this at the end of the commit to reduce lock wait timeouts |
||
| 3437 | $dbw->onTransactionPreCommitOrIdle( |
||
| 3438 | function () use ( $dbw, $added, $deleted, $id, $method ) { |
||
| 3439 | $ns = $this->getTitle()->getNamespace(); |
||
| 3440 | |||
| 3441 | $addFields = [ 'cat_pages = cat_pages + 1' ]; |
||
| 3442 | $removeFields = [ 'cat_pages = cat_pages - 1' ]; |
||
| 3443 | if ( $ns == NS_CATEGORY ) { |
||
| 3444 | $addFields[] = 'cat_subcats = cat_subcats + 1'; |
||
| 3445 | $removeFields[] = 'cat_subcats = cat_subcats - 1'; |
||
| 3446 | } elseif ( $ns == NS_FILE ) { |
||
| 3447 | $addFields[] = 'cat_files = cat_files + 1'; |
||
| 3448 | $removeFields[] = 'cat_files = cat_files - 1'; |
||
| 3449 | } |
||
| 3450 | |||
| 3451 | if ( count( $added ) ) { |
||
| 3452 | $existingAdded = $dbw->selectFieldValues( |
||
| 3453 | 'category', |
||
| 3454 | 'cat_title', |
||
| 3455 | [ 'cat_title' => $added ], |
||
| 3456 | $method |
||
| 3457 | ); |
||
| 3458 | |||
| 3459 | // For category rows that already exist, do a plain |
||
| 3460 | // UPDATE instead of INSERT...ON DUPLICATE KEY UPDATE |
||
| 3461 | // to avoid creating gaps in the cat_id sequence. |
||
| 3462 | if ( count( $existingAdded ) ) { |
||
| 3463 | $dbw->update( |
||
| 3464 | 'category', |
||
| 3465 | $addFields, |
||
| 3466 | [ 'cat_title' => $existingAdded ], |
||
| 3467 | $method |
||
| 3468 | ); |
||
| 3469 | } |
||
| 3470 | |||
| 3471 | $missingAdded = array_diff( $added, $existingAdded ); |
||
| 3472 | if ( count( $missingAdded ) ) { |
||
| 3473 | $insertRows = []; |
||
| 3474 | foreach ( $missingAdded as $cat ) { |
||
| 3475 | $insertRows[] = [ |
||
| 3476 | 'cat_title' => $cat, |
||
| 3477 | 'cat_pages' => 1, |
||
| 3478 | 'cat_subcats' => ( $ns == NS_CATEGORY ) ? 1 : 0, |
||
| 3479 | 'cat_files' => ( $ns == NS_FILE ) ? 1 : 0, |
||
| 3480 | ]; |
||
| 3481 | } |
||
| 3482 | $dbw->upsert( |
||
| 3483 | 'category', |
||
| 3484 | $insertRows, |
||
| 3485 | [ 'cat_title' ], |
||
| 3486 | $addFields, |
||
| 3487 | $method |
||
| 3488 | ); |
||
| 3489 | } |
||
| 3490 | } |
||
| 3491 | |||
| 3492 | if ( count( $deleted ) ) { |
||
| 3493 | $dbw->update( |
||
| 3494 | 'category', |
||
| 3495 | $removeFields, |
||
| 3496 | [ 'cat_title' => $deleted ], |
||
| 3497 | $method |
||
| 3498 | ); |
||
| 3499 | } |
||
| 3500 | |||
| 3501 | foreach ( $added as $catName ) { |
||
| 3502 | $cat = Category::newFromName( $catName ); |
||
| 3503 | Hooks::run( 'CategoryAfterPageAdded', [ $cat, $this ] ); |
||
| 3504 | } |
||
| 3505 | |||
| 3506 | foreach ( $deleted as $catName ) { |
||
| 3507 | $cat = Category::newFromName( $catName ); |
||
| 3508 | Hooks::run( 'CategoryAfterPageRemoved', [ $cat, $this, $id ] ); |
||
| 3509 | } |
||
| 3510 | } |
||
| 3511 | ); |
||
| 3512 | } |
||
| 3513 | |||
| 3514 | /** |
||
| 3515 | * Opportunistically enqueue link update jobs given fresh parser output if useful |
||
| 3516 | * |
||
| 3517 | * @param ParserOutput $parserOutput Current version page output |
||
| 3518 | * @since 1.25 |
||
| 3519 | */ |
||
| 3520 | public function triggerOpportunisticLinksUpdate( ParserOutput $parserOutput ) { |
||
| 3564 | |||
| 3565 | /** |
||
| 3566 | * Returns a list of updates to be performed when this page is deleted. The |
||
| 3567 | * updates should remove any information about this page from secondary data |
||
| 3568 | * stores such as links tables. |
||
| 3569 | * |
||
| 3570 | * @param Content|null $content Optional Content object for determining the |
||
| 3571 | * necessary updates. |
||
| 3572 | * @return DataUpdate[] |
||
| 3573 | */ |
||
| 3574 | public function getDeletionUpdates( Content $content = null ) { |
||
| 3590 | } |
||
| 3591 |
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.