Complex classes like Page 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 Page, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 24 | class Page { |
||
| 25 | |||
| 26 | /** |
||
| 27 | * Wiki class |
||
| 28 | * @var Wiki |
||
| 29 | */ |
||
| 30 | protected $wiki; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Title of the page |
||
| 34 | * @var string |
||
| 35 | */ |
||
| 36 | protected $title; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * The ID of the page |
||
| 40 | * @var int |
||
| 41 | */ |
||
| 42 | protected $pageid; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * If the page exists or not |
||
| 46 | * (default value: true) |
||
| 47 | * @var bool |
||
| 48 | */ |
||
| 49 | protected $exists = true; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Whether or not the page is a special page |
||
| 53 | * (default value: false) |
||
| 54 | * @var bool |
||
| 55 | */ |
||
| 56 | protected $special = false; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * When retriving the page information was a redirect followed |
||
| 60 | * (default value: false) |
||
| 61 | * @var bool |
||
| 62 | */ |
||
| 63 | protected $redirectFollowed = false; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * The page title without the namespace bit |
||
| 67 | * @var string |
||
| 68 | */ |
||
| 69 | protected $title_wo_namespace; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * The ID of the namespace |
||
| 73 | * @var int |
||
| 74 | */ |
||
| 75 | protected $namespace_id; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * Page text |
||
| 79 | * @var string |
||
| 80 | */ |
||
| 81 | protected $content; |
||
| 82 | |||
| 83 | /** |
||
| 84 | * Templates used in the page |
||
| 85 | * (default value: null) |
||
| 86 | * @var array |
||
| 87 | */ |
||
| 88 | protected $templates; |
||
| 89 | |||
| 90 | /** |
||
| 91 | * Protection information for the page |
||
| 92 | * (default value: null) |
||
| 93 | * @var array |
||
| 94 | */ |
||
| 95 | protected $protection; |
||
| 96 | |||
| 97 | /** |
||
| 98 | * Categories that the page is in |
||
| 99 | * (default value: null) |
||
| 100 | * @var array |
||
| 101 | */ |
||
| 102 | protected $categories; |
||
| 103 | |||
| 104 | /** |
||
| 105 | * Images used in the page |
||
| 106 | * (default value: null) |
||
| 107 | * @var array |
||
| 108 | */ |
||
| 109 | protected $images; |
||
| 110 | |||
| 111 | /** |
||
| 112 | * Internal links in the page |
||
| 113 | * (default value: null) |
||
| 114 | * @var array |
||
| 115 | */ |
||
| 116 | protected $links; |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Timestamp of the last edit |
||
| 120 | * @var int |
||
| 121 | */ |
||
| 122 | protected $lastedit; |
||
| 123 | |||
| 124 | /** |
||
| 125 | * Length of the page in bytes |
||
| 126 | * @var int |
||
| 127 | */ |
||
| 128 | protected $length; |
||
| 129 | |||
| 130 | /** |
||
| 131 | * Amount of hits (views) the page has |
||
| 132 | * @var int |
||
| 133 | */ |
||
| 134 | protected $hits; |
||
| 135 | |||
| 136 | /** |
||
| 137 | * Language links on the page |
||
| 138 | * (default value: null) |
||
| 139 | * @var array |
||
| 140 | */ |
||
| 141 | protected $langlinks; |
||
| 142 | |||
| 143 | /** |
||
| 144 | * External links on the page |
||
| 145 | * (default value: null) |
||
| 146 | * @var array |
||
| 147 | */ |
||
| 148 | protected $extlinks; |
||
| 149 | |||
| 150 | /** |
||
| 151 | * Interwiki links on the page |
||
| 152 | * (default value: null) |
||
| 153 | * @var array |
||
| 154 | */ |
||
| 155 | protected $iwlinks; |
||
| 156 | |||
| 157 | /** |
||
| 158 | * Time of script start. Must be set manually. |
||
| 159 | * (default null) |
||
| 160 | * @var mixed |
||
| 161 | */ |
||
| 162 | protected $starttimestamp; |
||
| 163 | |||
| 164 | /** |
||
| 165 | * Page ID of the talk page. |
||
| 166 | * (default null) |
||
| 167 | * @var int |
||
| 168 | */ |
||
| 169 | protected $talkid; |
||
| 170 | |||
| 171 | /** |
||
| 172 | * Whether the page is watched by the user |
||
| 173 | * (default null) |
||
| 174 | * @var bool |
||
| 175 | */ |
||
| 176 | protected $watched; |
||
| 177 | |||
| 178 | /** |
||
| 179 | * Number of watchers |
||
| 180 | * (default null) |
||
| 181 | * @var int |
||
| 182 | */ |
||
| 183 | protected $watchers; |
||
| 184 | |||
| 185 | /** |
||
| 186 | * Watchlist notification timestamp |
||
| 187 | * (default null) |
||
| 188 | * @var string |
||
| 189 | */ |
||
| 190 | protected $watchlisttimestamp; |
||
| 191 | |||
| 192 | /** |
||
| 193 | * Page ID of parent page |
||
| 194 | * (default null) |
||
| 195 | * @var int |
||
| 196 | */ |
||
| 197 | protected $subjectid; |
||
| 198 | |||
| 199 | /** |
||
| 200 | * Full urls of the page |
||
| 201 | * (default null) |
||
| 202 | * @var array |
||
| 203 | */ |
||
| 204 | protected $urls; |
||
| 205 | |||
| 206 | /** |
||
| 207 | * Whether the page can be read by a user |
||
| 208 | * (default null) |
||
| 209 | * @var bool |
||
| 210 | */ |
||
| 211 | protected $readable; |
||
| 212 | |||
| 213 | /** |
||
| 214 | * EditFormPreloadText |
||
| 215 | * (default null) |
||
| 216 | * @var string |
||
| 217 | */ |
||
| 218 | protected $preload; |
||
| 219 | |||
| 220 | /** |
||
| 221 | * Page's title formatting |
||
| 222 | * (default null) |
||
| 223 | * @var string |
||
| 224 | */ |
||
| 225 | protected $displaytitle; |
||
| 226 | |||
| 227 | /** |
||
| 228 | * Page properties |
||
| 229 | * (default null) |
||
| 230 | * @var array |
||
| 231 | */ |
||
| 232 | protected $properties; |
||
| 233 | |||
| 234 | /** |
||
| 235 | * Construction method for the Page class |
||
| 236 | * |
||
| 237 | * @param Wiki $wikiClass The Wiki class object |
||
| 238 | * @param mixed $title Title of the page (default: null) |
||
| 239 | * @param mixed $pageid ID of the page (default: null) |
||
| 240 | * @param bool $followRedir Should it follow a redirect when retrieving the page (default: true) |
||
| 241 | * @param bool $normalize Should the class automatically normalize the title (default: true) |
||
| 242 | * @param string|double|int $timestamp Set the start of a program or start reference to avoid edit conflicts. |
||
| 243 | * |
||
| 244 | * @throws InvalidArgumentException |
||
| 245 | * @throws NoTitle |
||
| 246 | * @return Page |
||
|
|
|||
| 247 | */ |
||
| 248 | 5 | public function __construct( Wiki $wikiClass, $title = null, $pageid = null, $followRedir = true, $normalize = true, $timestamp = null ) { |
|
| 307 | |||
| 308 | /** |
||
| 309 | * Returns page history. Can be specified to return content as well |
||
| 310 | * |
||
| 311 | * @param int $count Revisions to return (default: 1) |
||
| 312 | * @param string $dir Direction to return revisions (default: "older") |
||
| 313 | * @param bool $content Should content of that revision be returned as well (default: false) |
||
| 314 | * @param int $revid Revision ID to start from (default: null) |
||
| 315 | * @param bool $rollback_token Should a rollback token be returned (default: false) |
||
| 316 | * @param bool $recurse Used internally to provide more results than can be returned with a single API query |
||
| 317 | * @return array Revision data |
||
| 318 | */ |
||
| 319 | public function history( $count = 1, $dir = "older", $content = false, $revid = null, $rollback_token = false, $recurse = false ) { |
||
| 367 | |||
| 368 | /** |
||
| 369 | * Retrieves text from a page, or a cached copy unless $force is true |
||
| 370 | * |
||
| 371 | * @param bool $force Grab text from the API, don't use the cached copy (default: false) |
||
| 372 | * @param string|int $section Section title or ID to retrieve |
||
| 373 | * @return string|bool Page content |
||
| 374 | */ |
||
| 375 | public function get_text( $force = false, $section = null ) { |
||
| 455 | |||
| 456 | /** |
||
| 457 | * Returns the pageid of the page. |
||
| 458 | * |
||
| 459 | * @return int Pageid |
||
| 460 | */ |
||
| 461 | public function get_id() { |
||
| 464 | |||
| 465 | /** |
||
| 466 | * Returns if the page exists |
||
| 467 | * |
||
| 468 | * @return bool Exists |
||
| 469 | * @deprecated since 18 June 2013 |
||
| 470 | */ |
||
| 471 | public function exists() { |
||
| 475 | |||
| 476 | /** |
||
| 477 | * Returns if the page exists |
||
| 478 | * |
||
| 479 | * @return bool Exists |
||
| 480 | */ |
||
| 481 | public function get_exists() { |
||
| 484 | |||
| 485 | /** |
||
| 486 | * Returns links on the page. |
||
| 487 | * |
||
| 488 | * @link http://www.mediawiki.org/wiki/API:Query_-_Properties#links_.2F_pl |
||
| 489 | * @param bool $force Force use of API, won't use cached copy (default: false) |
||
| 490 | * @param array $namespace Show links in this namespace(s) only. Default array() |
||
| 491 | * @param array $titles Only list links to these titles. Default array() |
||
| 492 | * @return bool|array False on error, array of link titles |
||
| 493 | */ |
||
| 494 | public function get_links( $force = false, $namespace = array(), $titles = array() ) { |
||
| 525 | |||
| 526 | /** |
||
| 527 | * Returns templates on the page |
||
| 528 | * |
||
| 529 | * @link http://www.mediawiki.org/wiki/API:Query_-_Properties#templates_.2F_tl |
||
| 530 | * @param bool $force Force use of API, won't use cached copy (default: false) |
||
| 531 | * @param array $namespace Show templates in this namespace(s) only. Default array(). |
||
| 532 | * @param array $template Only list these templates. Default array() |
||
| 533 | * @return bool|array False on error, array of template titles |
||
| 534 | */ |
||
| 535 | public function get_templates( $force = false, $namespace = array(), $template = array() ) { |
||
| 536 | |||
| 537 | if( !$force && $this->templates !== null && empty( $namespace ) && empty( $template ) ) { |
||
| 538 | return $this->templates; |
||
| 539 | } |
||
| 540 | |||
| 541 | $this->templates = array(); |
||
| 542 | if( !$this->exists ) return array(); |
||
| 543 | |||
| 544 | $tArray = array( |
||
| 545 | 'prop' => 'templates', |
||
| 546 | 'titles' => $this->title, |
||
| 547 | '_code' => 'tl', |
||
| 548 | '_lhtitle' => 'templates' |
||
| 549 | ); |
||
| 550 | if( !empty( $namespace ) ) $tArray['tlnamespace'] = implode( '|', $namespace ); |
||
| 551 | if( !empty( $template ) ) $tArray['tltemplates'] = implode( '|', $template ); |
||
| 552 | |||
| 553 | pecho( "Getting templates transcluded on {$this->title}..\n\n", PECHO_NORMAL ); |
||
| 554 | |||
| 555 | $result = $this->wiki->listHandler( $tArray ); |
||
| 556 | |||
| 557 | if( count( $result ) > 0 ) { |
||
| 558 | foreach( $result as $template ){ |
||
| 559 | $this->templates[] = $template['title']; |
||
| 560 | } |
||
| 561 | } |
||
| 562 | |||
| 563 | return $this->templates; |
||
| 564 | } |
||
| 565 | |||
| 566 | /** |
||
| 567 | * Get various properties defined in the page content |
||
| 568 | * |
||
| 569 | * @link https://www.mediawiki.org/wiki/API:Properties#pageprops_.2F_pp |
||
| 570 | * @param bool $force Force use of API, won't use cached copy (default: false) |
||
| 571 | * @return bool|array False on error, array of template titles |
||
| 572 | */ |
||
| 573 | public function get_properties( $force = false ) { |
||
| 594 | |||
| 595 | /** |
||
| 596 | * Returns categories of page |
||
| 597 | * |
||
| 598 | * @link http://www.mediawiki.org/wiki/API:Query_-_Properties#categories_.2F_cl |
||
| 599 | * @param bool $force Force use of API, won't use cached copy (default: false) |
||
| 600 | * @param array|string $prop Which additional properties to get for each category. Default all |
||
| 601 | * @param bool $hidden Show hidden categories. Default false |
||
| 602 | * @return bool|array False on error, returns array of categories |
||
| 603 | */ |
||
| 604 | public function get_categories( $force = false, $prop = array( |
||
| 638 | |||
| 639 | /** |
||
| 640 | * Returns images used in the page |
||
| 641 | * |
||
| 642 | * @link http://www.mediawiki.org/wiki/API:Query_-_Properties#images_.2F_im |
||
| 643 | * @param bool $force Force use of API, won't use cached copy (default: false) |
||
| 644 | * @param string|array $images Only list these images. Default null. |
||
| 645 | * @return bool|array False on error, returns array of image titles |
||
| 646 | */ |
||
| 647 | public function get_images( $force = false, $images = null ) { |
||
| 683 | |||
| 684 | /** |
||
| 685 | * Returns external links used in the page |
||
| 686 | * |
||
| 687 | * @link http://www.mediawiki.org/wiki/API:Query_-_Properties#extlinks_.2F_el |
||
| 688 | * @param bool $force Force use of API, won't use cached copy (default: false) |
||
| 689 | * @return bool|array False on error, returns array of URLs |
||
| 690 | */ |
||
| 691 | public function get_extlinks( $force = false ) { |
||
| 719 | |||
| 720 | /** |
||
| 721 | * Returns interlanguage links on the page |
||
| 722 | * |
||
| 723 | * @link http://www.mediawiki.org/wiki/API:Query_-_Properties#langlinks_.2F_ll |
||
| 724 | * @param bool $force Force use of API, won't use cached copy (default: false) |
||
| 725 | * @param bool $fullurl Include a list of full of URLs. Output formatting changes. Requires force parameter to be true to return a different result. |
||
| 726 | * @param string $title Link to search for. Must be used with $lang. Default null |
||
| 727 | * @param string $lang Language code. Default null |
||
| 728 | * @return bool|array False on error, returns array of links in the form of lang:title |
||
| 729 | */ |
||
| 730 | public function get_langlinks( $force = false, $fullurl = false, $title = null, $lang = null ) { |
||
| 765 | |||
| 766 | /** |
||
| 767 | * Returns interwiki links on the page |
||
| 768 | * |
||
| 769 | * @link http://www.mediawiki.org/wiki/API:Query_-_Properties#langlinks_.2F_ll |
||
| 770 | * @param bool $force Force use of API, won't use cached copy (default: false) |
||
| 771 | * @param bool $fullurl Include a list of full of URLs. Output formatting changes. Requires force parameter to be true to return a different result. |
||
| 772 | * @param string $title Interwiki link to search for. Must be used with $prefix. Default null |
||
| 773 | * @param string $prefix Prefix for the interwiki. Default null |
||
| 774 | * @return bool|array False on error, returns array of links in the form of lang:title |
||
| 775 | */ |
||
| 776 | public function get_interwikilinks( $force = false, $fullurl = false, $title = null, $prefix = null ) { |
||
| 812 | |||
| 813 | /** |
||
| 814 | * Returns the protection level of the page |
||
| 815 | * |
||
| 816 | * @link http://www.mediawiki.org/wiki/API:Query_-_Properties#info_.2F_in |
||
| 817 | * @param bool $force Force use of API, won't use cached copy (default: false) |
||
| 818 | * @return bool|array False on error, returns array with protection levels |
||
| 819 | */ |
||
| 820 | public function get_protection( $force = false ) { |
||
| 844 | |||
| 845 | /** |
||
| 846 | * Returns the page ID of the talk page for each non-talk page |
||
| 847 | * |
||
| 848 | * @link http://www.mediawiki.org/wiki/API:Query_-_Properties#info_.2F_in |
||
| 849 | * @param bool $force Force use of API, won't use cached copy (default: false) |
||
| 850 | * @return int Null or empty if no id exists. |
||
| 851 | */ |
||
| 852 | public function get_talkID( $force = false ) { |
||
| 875 | |||
| 876 | /** |
||
| 877 | * Returns the watch status of the page |
||
| 878 | * |
||
| 879 | * @link http://www.mediawiki.org/wiki/API:Query_-_Properties#info_.2F_in |
||
| 880 | * @param bool $force Force use of API, won't use cached copy (default: false) |
||
| 881 | * @return bool |
||
| 882 | */ |
||
| 883 | public function is_watched( $force = false ) { |
||
| 906 | |||
| 907 | /** |
||
| 908 | * Returns the count for the number of watchers of a page. |
||
| 909 | * |
||
| 910 | * @link http://www.mediawiki.org/wiki/API:Query_-_Properties#info_.2F_in |
||
| 911 | * @param bool $force Force use of API, won't use cached copy (default: false) |
||
| 912 | * @return int |
||
| 913 | */ |
||
| 914 | public function get_watchcount( $force = false ) { |
||
| 937 | |||
| 938 | /** |
||
| 939 | * Returns the watchlist notification timestamp of each page. |
||
| 940 | * |
||
| 941 | * @link http://www.mediawiki.org/wiki/API:Query_-_Properties#info_.2F_in |
||
| 942 | * @param bool $force Force use of API, won't use cached copy (default: false) |
||
| 943 | * @return string |
||
| 944 | */ |
||
| 945 | public function get_notificationtimestamp( $force = false ) { |
||
| 968 | |||
| 969 | /** |
||
| 970 | * Returns the page ID of the parent page for each talk page. |
||
| 971 | * |
||
| 972 | * @link http://www.mediawiki.org/wiki/API:Query_-_Properties#info_.2F_in |
||
| 973 | * @param bool $force Force use of API, won't use cached copy (default: false) |
||
| 974 | * @return int Null if it doesn't exist. |
||
| 975 | */ |
||
| 976 | public function get_subjectid( $force = false ) { |
||
| 999 | |||
| 1000 | /** |
||
| 1001 | * Gives a full URL to the page, and also an edit URL. |
||
| 1002 | * |
||
| 1003 | * @link http://www.mediawiki.org/wiki/API:Query_-_Properties#info_.2F_in |
||
| 1004 | * @param bool $force Force use of API, won't use cached copy (default: false) |
||
| 1005 | * @return array |
||
| 1006 | */ |
||
| 1007 | public function get_urls( $force = false ) { |
||
| 1032 | |||
| 1033 | /** |
||
| 1034 | * Returns whether the user can read this page. |
||
| 1035 | * |
||
| 1036 | * @link http://www.mediawiki.org/wiki/API:Query_-_Properties#info_.2F_in |
||
| 1037 | * @param bool $force Force use of API, won't use cached copy (default: false) |
||
| 1038 | * @return boolean Null if it doesn't exist. |
||
| 1039 | */ |
||
| 1040 | public function get_readability( $force = false ) { |
||
| 1063 | |||
| 1064 | /** |
||
| 1065 | * Gives the text returned by EditFormPreloadText. |
||
| 1066 | * |
||
| 1067 | * @link http://www.mediawiki.org/wiki/API:Query_-_Properties#info_.2F_in |
||
| 1068 | * @param bool $force Force use of API, won't use cached copy (default: false) |
||
| 1069 | * @return string |
||
| 1070 | */ |
||
| 1071 | public function get_preload( $force = false ) { |
||
| 1094 | |||
| 1095 | /** |
||
| 1096 | * Gives the way the page title is actually displayed. |
||
| 1097 | * |
||
| 1098 | * @link http://www.mediawiki.org/wiki/API:Query_-_Properties#info_.2F_in |
||
| 1099 | * @param bool $force Force use of API, won't use cached copy (default: false) |
||
| 1100 | * @return string |
||
| 1101 | */ |
||
| 1102 | public function get_displaytitle( $force = false ) { |
||
| 1125 | |||
| 1126 | /** |
||
| 1127 | * Edits the page |
||
| 1128 | * |
||
| 1129 | * @link http://www.mediawiki.org/wiki/API:Edit_-_Create%26Edit_pages |
||
| 1130 | * @param string $text Text of the page that will be saved |
||
| 1131 | * @param string $summary Summary of the edit (default: "") |
||
| 1132 | * @param bool $minor Minor edit (default: false) |
||
| 1133 | * @param bool $bot Mark as bot edit (default: true) |
||
| 1134 | * @param bool $force Override nobots check (default: false) |
||
| 1135 | * @param string $pend Set to 'pre' or 'ap' to prepend or append, respectively (default: "") |
||
| 1136 | * @param bool $create Set to 'never', 'only', or 'recreate' to never create a new page, only create a new page, or override errors about the page having been deleted, respectively (default: false) |
||
| 1137 | * @param string $section Section number. 0 for the top section, 'new' for a new section. Default null. |
||
| 1138 | * @param string $sectiontitle The title for a new section. Default null. |
||
| 1139 | * @param string|bool $watch Unconditionally add or remove the page from your watchlist, use preferences or do not change watch. Default: go by user preference. |
||
| 1140 | * @return string|int|bool The revision id of the successful edit, false on failure. |
||
| 1141 | */ |
||
| 1142 | public function edit( |
||
| 1266 | |||
| 1267 | /** |
||
| 1268 | * Add text to the beginning of the page. Shortcut for Page::edit() |
||
| 1269 | * |
||
| 1270 | * @link http://www.mediawiki.org/wiki/API:Edit_-_Create%26Edit_pages |
||
| 1271 | * @param string $text Text of the page that will be saved |
||
| 1272 | * @param string $summary Summary of the edit (default: "") |
||
| 1273 | * @param bool $minor Minor edit (default: false) |
||
| 1274 | * @param bool $bot Mark as bot edit (default: true) |
||
| 1275 | * @param bool $force Override nobots check (default: false) |
||
| 1276 | * @param bool $create Set to 'never', 'only', or 'recreate' to never create a new page, only create a new page, or override errors about the page having been deleted, respectively (default: false) |
||
| 1277 | * @param string|bool $watch Unconditionally add or remove the page from your watchlist, use preferences or do not change watch. Default: go by user preference. |
||
| 1278 | * @return int|bool The revision id of the successful edit, false on failure. |
||
| 1279 | */ |
||
| 1280 | public function prepend( $text, $summary = "", $minor = false, $bot = true, $force = false, $create = false, $watch = null ) { |
||
| 1283 | |||
| 1284 | /** |
||
| 1285 | * Add text to the end of the page. Shortcut for Page::edit() |
||
| 1286 | * |
||
| 1287 | * @link http://www.mediawiki.org/wiki/API:Edit_-_Create%26Edit_pages |
||
| 1288 | * @param string $text Text of the page that will be saved |
||
| 1289 | * @param string $summary Summary of the edit (default: "") |
||
| 1290 | * @param bool $minor Minor edit (default: false) |
||
| 1291 | * @param bool $bot Mark as bot edit (default: true) |
||
| 1292 | * @param bool $force Override nobots check (default: false) |
||
| 1293 | * @param bool $create Set to 'never', 'only', or 'recreate' to never create a new page, only create a new page, or override errors about the page having been deleted, respectively (default: false) |
||
| 1294 | * @param string|bool $watch Unconditionally add or remove the page from your watchlist, use preferences or do not change watch. Default: go by user preference. |
||
| 1295 | * @return int|bool The revision id of the successful edit, false on failure. |
||
| 1296 | */ |
||
| 1297 | public function append( $text, $summary = "", $minor = false, $bot = true, $force = false, $create = false, $watch = null ) { |
||
| 1300 | |||
| 1301 | /** |
||
| 1302 | * Create a new section. Shortcut for Page::edit() |
||
| 1303 | * |
||
| 1304 | * @link http://www.mediawiki.org/wiki/API:Edit_-_Create%26Edit_pages |
||
| 1305 | * @param string $text Text of the page that will be saved |
||
| 1306 | * @param string $sectionTitle The title for a new section. Default null. |
||
| 1307 | * @param string $summary Summary of the edit (default: "") |
||
| 1308 | * @param bool $minor Minor edit (default: false) |
||
| 1309 | * @param bool $bot Mark as bot edit (default: true) |
||
| 1310 | * @param bool $force Override nobots check (default: false) |
||
| 1311 | * @param bool $create Set to 'never', 'only', or 'recreate' to never create a new page, |
||
| 1312 | * only create a new page, or override errors about the page having |
||
| 1313 | * been deleted, respectively (default: false) |
||
| 1314 | * @param string|bool $watch Unconditionally add or remove the page from your watchlist, use preferences |
||
| 1315 | * or do not change watch. Default: go by user preference. |
||
| 1316 | * @return int|bool The revision ID of the successful edit, false on failure. |
||
| 1317 | */ |
||
| 1318 | public function newsection( |
||
| 1334 | |||
| 1335 | /** |
||
| 1336 | * Undoes one or more edits. (Subject to standard editing restrictions.) |
||
| 1337 | * |
||
| 1338 | * @param string $summary Override the default edit summary (default null). |
||
| 1339 | * @param int $revisions The number of revisions to undo (default 1). |
||
| 1340 | * @param bool $force Force an undo, despite e.g. new messages (default false). |
||
| 1341 | * @param bool|string $watch Unconditionally add or remove the page from your watchlist, use preferences |
||
| 1342 | * or do not change watch. Default: goes by user preference. |
||
| 1343 | * @return bool|int The new revision id of the page edited. |
||
| 1344 | * @throws AssertFailure |
||
| 1345 | * @throws LoggedOut |
||
| 1346 | * @throws MWAPIError |
||
| 1347 | * @throws NoTitle |
||
| 1348 | */ |
||
| 1349 | public function undo($summary = null, $revisions = 1, $force = false, $watch = null ) { |
||
| 1424 | |||
| 1425 | /** |
||
| 1426 | * Returns a boolean depending on whether the page can have subpages or not. |
||
| 1427 | * |
||
| 1428 | * @return bool True if subpages allowed |
||
| 1429 | */ |
||
| 1430 | public function allow_subpages() { |
||
| 1434 | |||
| 1435 | /** |
||
| 1436 | * Returns a boolean depending on whether the page is a discussion (talk) page or not. |
||
| 1437 | * |
||
| 1438 | * @return bool True if discussion page, false if not |
||
| 1439 | */ |
||
| 1440 | public function is_discussion() { |
||
| 1443 | |||
| 1444 | /** |
||
| 1445 | * Returns the title of the discussion (talk) page associated with a page, if it exists. |
||
| 1446 | * |
||
| 1447 | * @return string Title of discussion page |
||
| 1448 | * @throws BadEntryError |
||
| 1449 | */ |
||
| 1450 | public function get_discussion() { |
||
| 1464 | |||
| 1465 | /** |
||
| 1466 | * Moves a page to a new location. |
||
| 1467 | * |
||
| 1468 | * @param string $newTitle The new title to which to move the page. |
||
| 1469 | * @param string $reason A descriptive reason for the move. |
||
| 1470 | * @param bool $movetalk Whether or not to move any associated talk (discussion) page. |
||
| 1471 | * @param bool $movesubpages Whether or not to move any subpages. |
||
| 1472 | * @param bool $noredirect Whether or not to suppress the leaving of a redirect to the new title at the old title. |
||
| 1473 | * @param string|bool $watch Unconditionally add or remove the page from your watchlist, use preferences or do not change watch. Default: go by user preference. |
||
| 1474 | * @param bool $nowarnings Ignore any warnings. Default false. |
||
| 1475 | * @return bool True on success |
||
| 1476 | */ |
||
| 1477 | public function move( $newTitle, $reason = '', $movetalk = true, $movesubpages = true, $noredirect = false, $watch = null, $nowarnings = false ) { |
||
| 1478 | global $pgNotag, $pgTag; |
||
| 1479 | $tokens = $this->wiki->get_tokens(); |
||
| 1480 | |||
| 1481 | if( $tokens['move'] == '+\\' ) { |
||
| 1482 | pecho( "User has logged out.\n\n", PECHO_FATAL ); |
||
| 1483 | return false; |
||
| 1484 | } elseif( $tokens['move'] == '' ) { |
||
| 1485 | pecho( "User is not allowed to move {$this->title}\n\n", PECHO_FATAL ); |
||
| 1486 | return false; |
||
| 1487 | } |
||
| 1488 | |||
| 1489 | if( mb_strlen( $reason, '8bit' ) > 255 ) { |
||
| 1490 | pecho( "Reason is over 255 bytes, the maximum allowed.\n\n", PECHO_FATAL ); |
||
| 1491 | return false; |
||
| 1492 | } |
||
| 1493 | |||
| 1494 | try{ |
||
| 1495 | $this->preEditChecks( "Move" ); |
||
| 1496 | } catch( EditError $e ){ |
||
| 1497 | pecho( "Error: $e\n\n", PECHO_FATAL ); |
||
| 1498 | return false; |
||
| 1499 | } |
||
| 1500 | |||
| 1501 | pecho( "Moving {$this->title} to $newTitle...\n\n", PECHO_NOTICE ); |
||
| 1502 | |||
| 1503 | $editarray = array( |
||
| 1504 | 'from' => $this->title, |
||
| 1505 | 'to' => $newTitle, |
||
| 1506 | 'action' => 'move', |
||
| 1507 | 'token' => $tokens['move'], |
||
| 1508 | ); |
||
| 1509 | |||
| 1510 | if( !is_null( $watch ) ) { |
||
| 1511 | if( $watch ) { |
||
| 1512 | $editarray['watchlist'] = 'watch'; |
||
| 1513 | } elseif( !$watch ) $editarray['watchlist'] = 'nochange'; |
||
| 1514 | elseif( in_array( |
||
| 1515 | $watch, array( |
||
| 1516 | 'watch', 'unwatch', 'preferences', 'nochange' |
||
| 1517 | ) |
||
| 1518 | ) ) { |
||
| 1519 | $editarray['watchlist'] = $watch; |
||
| 1520 | } else pecho( "Watch parameter set incorrectly. Omitting...\n\n", PECHO_WARN ); |
||
| 1521 | } |
||
| 1522 | |||
| 1523 | if( $nowarnings ) $editarray['ignorewarnings'] = ''; |
||
| 1524 | if( !$pgNotag ) $reason .= $pgTag; |
||
| 1525 | if( !empty( $reason ) ) $editarray['reason'] = $reason; |
||
| 1526 | |||
| 1527 | if( $movetalk ) $editarray['movetalk'] = ''; |
||
| 1528 | if( $movesubpages ) $editarray['movesubpages'] = ''; |
||
| 1529 | if( $noredirect ) $editarray['noredirect'] = ''; |
||
| 1530 | |||
| 1531 | if( $this->wiki->get_maxlag() ) { |
||
| 1532 | $editarray['maxlag'] = $this->wiki->get_maxlag(); |
||
| 1533 | |||
| 1534 | } |
||
| 1535 | |||
| 1536 | Hooks::runHook( 'StartMove', array( &$editarray ) ); |
||
| 1537 | |||
| 1538 | $result = $this->wiki->apiQuery( $editarray, true ); |
||
| 1539 | |||
| 1540 | if( isset( $result['move'] ) ) { |
||
| 1541 | if( isset( $result['move']['to'] ) ) { |
||
| 1542 | $this->__construct( $this->wiki, null, $this->pageid ); |
||
| 1543 | return true; |
||
| 1544 | } else { |
||
| 1545 | pecho( "Move error...\n\n" . print_r( $result['move'], true ) . "\n\n", PECHO_FATAL ); |
||
| 1546 | return false; |
||
| 1547 | } |
||
| 1548 | } else { |
||
| 1549 | pecho( "Move error...\n\n" . print_r( $result, true ), PECHO_FATAL ); |
||
| 1550 | return false; |
||
| 1551 | } |
||
| 1552 | } |
||
| 1553 | |||
| 1554 | /** |
||
| 1555 | * Protects the page. |
||
| 1556 | * |
||
| 1557 | * @param array $levels Array of protections levels. The key is the type, the value is the level. Default: array( 'edit' => 'sysop', 'move' => 'sysop' ) |
||
| 1558 | * @param string $reason Reason for protection. Default null |
||
| 1559 | * @param string $expiry Expiry time. Default 'indefinite' |
||
| 1560 | * @param bool $cascade Whether or not to enable cascade protection. Default false |
||
| 1561 | * @param string|bool $watch Unconditionally add or remove the page from your watchlist, use preferences or do not change watch. Default: go by user preference. |
||
| 1562 | * @return bool True on success |
||
| 1563 | */ |
||
| 1564 | public function protect( $levels = null, $reason = null, $expiry = 'indefinite', $cascade = false, $watch = null ) { |
||
| 1633 | |||
| 1634 | /** |
||
| 1635 | * Unprotects the page. |
||
| 1636 | * |
||
| 1637 | * @param string $reason A reason for the unprotection. Defaults to null (blank). |
||
| 1638 | * @param string|bool $watch Unconditionally add or remove the page from your watchlist, use preferences or do not change watch. Default: go by user preference. |
||
| 1639 | * @return bool True on success |
||
| 1640 | */ |
||
| 1641 | public function unprotect( $reason = null, $watch = null ) { |
||
| 1644 | |||
| 1645 | /** |
||
| 1646 | * Deletes the page. |
||
| 1647 | * |
||
| 1648 | * @param string $reason A reason for the deletion. Defaults to null (blank). |
||
| 1649 | * @param string|bool $watch Unconditionally add or remove the page from your watchlist, use preferences or do not change watch. Default: go by user preference. |
||
| 1650 | * @param string $oldimage The name of the old image to delete as provided by iiprop=archivename |
||
| 1651 | * @return bool True on success |
||
| 1652 | */ |
||
| 1653 | public function delete( $reason = null, $watch = null, $oldimage = null ) { |
||
| 1654 | global $pgNotag, $pgTag; |
||
| 1655 | |||
| 1656 | if( !in_array( 'delete', $this->wiki->get_userrights() ) ) { |
||
| 1657 | pecho( "User is not allowed to delete pages", PECHO_FATAL ); |
||
| 1658 | return false; |
||
| 1659 | } |
||
| 1660 | |||
| 1661 | $tokens = $this->wiki->get_tokens(); |
||
| 1662 | if( !$pgNotag ) $reason .= $pgTag; |
||
| 1663 | $editarray = array( |
||
| 1664 | 'action' => 'delete', |
||
| 1665 | 'title' => $this->title, |
||
| 1666 | 'token' => $tokens['delete'], |
||
| 1667 | 'reason' => $reason |
||
| 1668 | ); |
||
| 1669 | |||
| 1670 | if( !is_null( $watch ) ) { |
||
| 1671 | if( $watch ) { |
||
| 1672 | $editarray['watchlist'] = 'watch'; |
||
| 1673 | } elseif( !$watch ) $editarray['watchlist'] = 'nochange'; |
||
| 1674 | elseif( in_array( |
||
| 1675 | $watch, array( |
||
| 1676 | 'watch', 'unwatch', 'preferences', 'nochange' |
||
| 1677 | ) |
||
| 1678 | ) ) { |
||
| 1679 | $editarray['watchlist'] = $watch; |
||
| 1680 | } else pecho( "Watch parameter set incorrectly. Omitting...\n\n", PECHO_WARN ); |
||
| 1681 | } |
||
| 1682 | if( !is_null( $oldimage ) ) $editarray['oldimage'] = $oldimage; |
||
| 1683 | |||
| 1684 | Hooks::runHook( 'StartDelete', array( &$editarray ) ); |
||
| 1685 | |||
| 1686 | try{ |
||
| 1687 | $this->preEditChecks( "Delete" ); |
||
| 1688 | } catch( EditError $e ){ |
||
| 1689 | pecho( "Error: $e\n\n", PECHO_FATAL ); |
||
| 1690 | return false; |
||
| 1691 | } |
||
| 1692 | pecho( "Deleting {$this->title}...\n\n", PECHO_NOTICE ); |
||
| 1693 | |||
| 1694 | $result = $this->wiki->apiQuery( $editarray, true ); |
||
| 1695 | |||
| 1696 | if( isset( $result['delete'] ) ) { |
||
| 1697 | if( isset( $result['delete']['title'] ) ) { |
||
| 1698 | $this->__construct( $this->wiki, $this->title ); |
||
| 1699 | return true; |
||
| 1700 | } else { |
||
| 1701 | pecho( "Delete error...\n\n" . print_r( $result['delete'], true ) . "\n\n", PECHO_FATAL ); |
||
| 1702 | return false; |
||
| 1703 | } |
||
| 1704 | } else { |
||
| 1705 | pecho( "Delete error...\n\n" . print_r( $result, true ), PECHO_FATAL ); |
||
| 1706 | return false; |
||
| 1707 | } |
||
| 1708 | |||
| 1709 | } |
||
| 1710 | |||
| 1711 | /** |
||
| 1712 | * Undeletes the page |
||
| 1713 | * |
||
| 1714 | * @param string $reason Reason for undeletion |
||
| 1715 | * @param array $timestamps Array of timestamps to selectively restore |
||
| 1716 | * @param string|bool $watch Unconditionally add or remove the page from your watchlist, use preferences or do not change watch. Default: go by user preference. |
||
| 1717 | * @return bool |
||
| 1718 | */ |
||
| 1719 | public function undelete( $reason = null, $timestamps = null, $watch = null ) { |
||
| 1781 | |||
| 1782 | /** |
||
| 1783 | * List deleted revisions of the page |
||
| 1784 | * |
||
| 1785 | * @param bool $content Whether or not to retrieve the content of each revision, Default false |
||
| 1786 | * @param string $user Only list revisions by this user. Default null. |
||
| 1787 | * @param string $excludeuser Don't list revisions by this user. Default null |
||
| 1788 | * @param string $start Timestamp to start at. Default null |
||
| 1789 | * @param string $end Timestamp to end at. Default null |
||
| 1790 | * @param string $dir Direction to enumerate. Default 'older' |
||
| 1791 | * @param array $prop Properties to retrieve. Default array( 'revid', 'user', 'parsedcomment', 'minor', 'len', 'content', 'token' ) |
||
| 1792 | * @return array List of deleted revisions |
||
| 1793 | */ |
||
| 1794 | public function deletedrevs( $content = false, $user = null, $excludeuser = null, $start = null, $end = null, $dir = 'older', $prop = array( |
||
| 1821 | |||
| 1822 | /** |
||
| 1823 | * Alias of embeddedin |
||
| 1824 | * |
||
| 1825 | * @see Page::embeddedin() |
||
| 1826 | * @deprecated since 18 June 2013 |
||
| 1827 | * @param null $namespace |
||
| 1828 | * @param null $limit |
||
| 1829 | * @return array |
||
| 1830 | */ |
||
| 1831 | public function get_transclusions( $namespace = null, $limit = null ) { |
||
| 1835 | |||
| 1836 | /** |
||
| 1837 | * Adds the page to the logged in user's watchlist |
||
| 1838 | * |
||
| 1839 | * @param string $lang The code for the language to show any error message in (default: user preference) |
||
| 1840 | * @return bool True on success |
||
| 1841 | */ |
||
| 1842 | public function watch( $lang = null ) { |
||
| 1878 | |||
| 1879 | /** |
||
| 1880 | * Removes the page to the logged in user's watchlist |
||
| 1881 | * |
||
| 1882 | * @param string $lang The code for the language to show any error message in (default: user preference) |
||
| 1883 | * @return bool True on sucecess |
||
| 1884 | */ |
||
| 1885 | public function unwatch( $lang = null ) { |
||
| 1921 | |||
| 1922 | /** |
||
| 1923 | * Returns the page title |
||
| 1924 | * |
||
| 1925 | * @param bool $namespace Set to true to return the title with namespace, false to return it without the namespace. Default true. |
||
| 1926 | * @return string Page title |
||
| 1927 | */ |
||
| 1928 | public function get_title( $namespace = true ) { |
||
| 1934 | |||
| 1935 | /** |
||
| 1936 | * Returns whether or not a redirect was followed to get to the real page title |
||
| 1937 | * |
||
| 1938 | * @return bool |
||
| 1939 | */ |
||
| 1940 | public function redirectFollowed() { |
||
| 1943 | |||
| 1944 | /** |
||
| 1945 | * Returns whether or not the page is a special page |
||
| 1946 | * |
||
| 1947 | * @return bool |
||
| 1948 | */ |
||
| 1949 | public function get_special() { |
||
| 1952 | |||
| 1953 | /** |
||
| 1954 | * Gets ID or name of the namespace |
||
| 1955 | * |
||
| 1956 | * @param bool $id Set to true to get namespace ID, set to false to get namespace name. Default true |
||
| 1957 | * @return int|string |
||
| 1958 | */ |
||
| 1959 | public function get_namespace( $id = true ) { |
||
| 1968 | |||
| 1969 | /** |
||
| 1970 | * Returns the timestamp of the last edit |
||
| 1971 | * |
||
| 1972 | * @param bool $force Regenerate the cached value (default: false) |
||
| 1973 | * @return string |
||
| 1974 | */ |
||
| 1975 | public function get_lastedit( $force = false ) { |
||
| 1980 | |||
| 1981 | /** |
||
| 1982 | * Returns length of the page |
||
| 1983 | * |
||
| 1984 | * @param bool $force Regenerate the cached value (default: false) |
||
| 1985 | * @return int |
||
| 1986 | */ |
||
| 1987 | public function get_length( $force = false ) { |
||
| 1992 | |||
| 1993 | /** |
||
| 1994 | * Returns number of hits the page has received |
||
| 1995 | * |
||
| 1996 | * @param bool $force Regenerate the cached value (default: false) |
||
| 1997 | * @return int |
||
| 1998 | */ |
||
| 1999 | public function get_hits( $force = false ) { |
||
| 2004 | |||
| 2005 | /** |
||
| 2006 | * (Re)generates lastedit, length, and hits |
||
| 2007 | * |
||
| 2008 | * @param array $pageInfoArray2 Array of values to merge with defaults (default: null) |
||
| 2009 | * @throws BadTitle |
||
| 2010 | */ |
||
| 2011 | protected function get_metadata( $pageInfoArray2 = null ) { |
||
| 2080 | |||
| 2081 | /** |
||
| 2082 | * Returns all links to the page |
||
| 2083 | * |
||
| 2084 | * @param array $namespaces Namespaces to get. Default array( 0 ); |
||
| 2085 | * @param string $redirects How to handle redirects. 'all' = List all pages. 'redirects' = Only list redirects. 'nonredirects' = Don't list redirects. Default 'all' |
||
| 2086 | * @param bool $followredir List links through redirects to the page |
||
| 2087 | * @return array List of backlinks |
||
| 2088 | */ |
||
| 2089 | public function get_backlinks( $namespaces = array( 0 ), $redirects = 'all', $followredir = true ) { |
||
| 2106 | |||
| 2107 | /** |
||
| 2108 | * Rollbacks the latest edit(s) to a page. |
||
| 2109 | * |
||
| 2110 | * @see http://www.mediawiki.org/wiki/API:Edit_-_Rollback |
||
| 2111 | * @param bool $force Whether to force an (attempt at an) edit, regardless of news messages, etc. |
||
| 2112 | * @param string $summary Override the default edit summary for this rollback. Default null. |
||
| 2113 | * @param bool $markbot If set, both the rollback and the revisions being rolled back will be marked as bot edits. |
||
| 2114 | * @param string|bool $watch Unconditionally add or remove the page from your watchlist, use preferences or do not change watch. Default preferences. |
||
| 2115 | * @return array Details of the rollback perform. ['revid']: The revision ID of the rollback. ['old_revid']: The revision ID of the first (most recent) revision that was rolled back. ['last_revid']: The revision ID of the last (oldest) revision that was rolled back. |
||
| 2116 | */ |
||
| 2117 | public function rollback( $force = false, $summary = null, $markbot = false, $watch = null ) { |
||
| 2191 | |||
| 2192 | /** |
||
| 2193 | * Performs nobots checking, new message checking, etc |
||
| 2194 | * |
||
| 2195 | * @param string $action |
||
| 2196 | * @throws EditError |
||
| 2197 | */ |
||
| 2198 | protected function preEditChecks( $action = "Edit" ) { |
||
| 2201 | |||
| 2202 | /** |
||
| 2203 | * Returns array of pages that embed (transclude) the page given. |
||
| 2204 | * |
||
| 2205 | * @param array $namespace Which namespaces to search (default: null). |
||
| 2206 | * @param int $limit How many results to retrieve (default: null i.e. all). |
||
| 2207 | * @return array A list of pages the title is transcluded in. |
||
| 2208 | */ |
||
| 2209 | public function embeddedin( $namespace = null, $limit = null ) { |
||
| 2228 | |||
| 2229 | /** |
||
| 2230 | * Purges a list of pages. Shortcut for {@link Wiki::purge()} |
||
| 2231 | * |
||
| 2232 | * @see Wiki::purge() |
||
| 2233 | * @return boolean |
||
| 2234 | */ |
||
| 2235 | public function purge() { |
||
| 2238 | |||
| 2239 | /** |
||
| 2240 | * Parses wikitext and returns parser output. Shortcut for Wiki::parse |
||
| 2241 | * |
||
| 2242 | * @param string $summary Summary to parse. Default null. |
||
| 2243 | * @param string $id Parse the content of this revision |
||
| 2244 | * @param array $prop Properties to retrieve. array( 'text', 'langlinks', 'categories', 'links', 'templates', 'images', 'externallinks', 'sections', 'revid', 'displaytitle', 'headitems', 'headhtml', 'iwlinks', 'wikitext', 'properties' ) |
||
| 2245 | * @param string $uselang Code of the language to use in interface messages, etc. (where applicable). Default 'en'. |
||
| 2246 | * @param string $section Only retrieve the content of this section number. Default null. |
||
| 2247 | * @return array |
||
| 2248 | */ |
||
| 2249 | public function parse( $summary = null, $id = null, $prop = null, $uselang = 'en', $section = null ) { |
||
| 2260 | |||
| 2261 | } |
||
| 2262 |
Adding a
@returnannotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.