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 SpecialExport 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 SpecialExport, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 31 | class SpecialExport extends SpecialPage { |
||
| 32 | private $curonly, $doExport, $pageLinkDepth, $templates; |
||
|
|
|||
| 33 | |||
| 34 | public function __construct() { |
||
| 37 | |||
| 38 | public function execute( $par ) { |
||
| 39 | $this->setHeaders(); |
||
| 40 | $this->outputHeader(); |
||
| 41 | $config = $this->getConfig(); |
||
| 42 | |||
| 43 | // Set some variables |
||
| 44 | $this->curonly = true; |
||
| 45 | $this->doExport = false; |
||
| 46 | $request = $this->getRequest(); |
||
| 47 | $this->templates = $request->getCheck( 'templates' ); |
||
| 48 | $this->pageLinkDepth = $this->validateLinkDepth( |
||
| 49 | $request->getIntOrNull( 'pagelink-depth' ) |
||
| 50 | ); |
||
| 51 | $nsindex = ''; |
||
| 52 | $exportall = false; |
||
| 53 | |||
| 54 | if ( $request->getCheck( 'addcat' ) ) { |
||
| 55 | $page = $request->getText( 'pages' ); |
||
| 56 | $catname = $request->getText( 'catname' ); |
||
| 57 | |||
| 58 | if ( $catname !== '' && $catname !== null && $catname !== false ) { |
||
| 59 | $t = Title::makeTitleSafe( NS_MAIN, $catname ); |
||
| 60 | if ( $t ) { |
||
| 61 | /** |
||
| 62 | * @todo FIXME: This can lead to hitting memory limit for very large |
||
| 63 | * categories. Ideally we would do the lookup synchronously |
||
| 64 | * during the export in a single query. |
||
| 65 | */ |
||
| 66 | $catpages = $this->getPagesFromCategory( $t ); |
||
| 67 | if ( $catpages ) { |
||
| 68 | if ( $page !== '' ) { |
||
| 69 | $page .= "\n"; |
||
| 70 | } |
||
| 71 | $page .= implode( "\n", $catpages ); |
||
| 72 | } |
||
| 73 | } |
||
| 74 | } |
||
| 75 | } elseif ( $request->getCheck( 'addns' ) && $config->get( 'ExportFromNamespaces' ) ) { |
||
| 76 | $page = $request->getText( 'pages' ); |
||
| 77 | $nsindex = $request->getText( 'nsindex', '' ); |
||
| 78 | |||
| 79 | if ( strval( $nsindex ) !== '' ) { |
||
| 80 | /** |
||
| 81 | * Same implementation as above, so same @todo |
||
| 82 | */ |
||
| 83 | $nspages = $this->getPagesFromNamespace( $nsindex ); |
||
| 84 | if ( $nspages ) { |
||
| 85 | $page .= "\n" . implode( "\n", $nspages ); |
||
| 86 | } |
||
| 87 | } |
||
| 88 | } elseif ( $request->getCheck( 'exportall' ) && $config->get( 'ExportAllowAll' ) ) { |
||
| 89 | $this->doExport = true; |
||
| 90 | $exportall = true; |
||
| 91 | |||
| 92 | /* Although $page and $history are not used later on, we |
||
| 93 | nevertheless set them to avoid that PHP notices about using |
||
| 94 | undefined variables foul up our XML output (see call to |
||
| 95 | doExport(...) further down) */ |
||
| 96 | $page = ''; |
||
| 97 | $history = ''; |
||
| 98 | } elseif ( $request->wasPosted() && $par == '' ) { |
||
| 99 | $page = $request->getText( 'pages' ); |
||
| 100 | $this->curonly = $request->getCheck( 'curonly' ); |
||
| 101 | $rawOffset = $request->getVal( 'offset' ); |
||
| 102 | |||
| 103 | if ( $rawOffset ) { |
||
| 104 | $offset = wfTimestamp( TS_MW, $rawOffset ); |
||
| 105 | } else { |
||
| 106 | $offset = null; |
||
| 107 | } |
||
| 108 | |||
| 109 | $maxHistory = $config->get( 'ExportMaxHistory' ); |
||
| 110 | $limit = $request->getInt( 'limit' ); |
||
| 111 | $dir = $request->getVal( 'dir' ); |
||
| 112 | $history = [ |
||
| 113 | 'dir' => 'asc', |
||
| 114 | 'offset' => false, |
||
| 115 | 'limit' => $maxHistory, |
||
| 116 | ]; |
||
| 117 | $historyCheck = $request->getCheck( 'history' ); |
||
| 118 | |||
| 119 | if ( $this->curonly ) { |
||
| 120 | $history = WikiExporter::CURRENT; |
||
| 121 | } elseif ( !$historyCheck ) { |
||
| 122 | if ( $limit > 0 && ( $maxHistory == 0 || $limit < $maxHistory ) ) { |
||
| 123 | $history['limit'] = $limit; |
||
| 124 | } |
||
| 125 | |||
| 126 | if ( !is_null( $offset ) ) { |
||
| 127 | $history['offset'] = $offset; |
||
| 128 | } |
||
| 129 | |||
| 130 | if ( strtolower( $dir ) == 'desc' ) { |
||
| 131 | $history['dir'] = 'desc'; |
||
| 132 | } |
||
| 133 | } |
||
| 134 | |||
| 135 | if ( $page != '' ) { |
||
| 136 | $this->doExport = true; |
||
| 137 | } |
||
| 138 | } else { |
||
| 139 | // Default to current-only for GET requests. |
||
| 140 | $page = $request->getText( 'pages', $par ); |
||
| 141 | $historyCheck = $request->getCheck( 'history' ); |
||
| 142 | |||
| 143 | if ( $historyCheck ) { |
||
| 144 | $history = WikiExporter::FULL; |
||
| 145 | } else { |
||
| 146 | $history = WikiExporter::CURRENT; |
||
| 147 | } |
||
| 148 | |||
| 149 | if ( $page != '' ) { |
||
| 150 | $this->doExport = true; |
||
| 151 | } |
||
| 152 | } |
||
| 153 | |||
| 154 | if ( !$config->get( 'ExportAllowHistory' ) ) { |
||
| 155 | // Override |
||
| 156 | $history = WikiExporter::CURRENT; |
||
| 157 | } |
||
| 158 | |||
| 159 | $list_authors = $request->getCheck( 'listauthors' ); |
||
| 160 | if ( !$this->curonly || !$config->get( 'ExportAllowListContributors' ) ) { |
||
| 161 | $list_authors = false; |
||
| 162 | } |
||
| 163 | |||
| 164 | if ( $this->doExport ) { |
||
| 165 | $this->getOutput()->disable(); |
||
| 166 | |||
| 167 | // Cancel output buffering and gzipping if set |
||
| 168 | // This should provide safer streaming for pages with history |
||
| 169 | wfResetOutputBuffers(); |
||
| 170 | $request->response()->header( "Content-type: application/xml; charset=utf-8" ); |
||
| 171 | $request->response()->header( "X-Robots-Tag: noindex,nofollow" ); |
||
| 172 | |||
| 173 | if ( $request->getCheck( 'wpDownload' ) ) { |
||
| 174 | // Provide a sane filename suggestion |
||
| 175 | $filename = urlencode( $config->get( 'Sitename' ) . '-' . wfTimestampNow() . '.xml' ); |
||
| 176 | $request->response()->header( "Content-disposition: attachment;filename={$filename}" ); |
||
| 177 | } |
||
| 178 | |||
| 179 | $this->doExport( $page, $history, $list_authors, $exportall ); |
||
| 180 | |||
| 181 | return; |
||
| 182 | } |
||
| 183 | |||
| 184 | $out = $this->getOutput(); |
||
| 185 | $out->addWikiMsg( 'exporttext' ); |
||
| 186 | |||
| 187 | if ( $page == '' ) { |
||
| 188 | $categoryName = $request->getText( 'catname' ); |
||
| 189 | } else { |
||
| 190 | $categoryName = ''; |
||
| 191 | } |
||
| 192 | |||
| 193 | $formDescriptor = [ |
||
| 194 | 'catname' => [ |
||
| 195 | 'type' => 'textwithbutton', |
||
| 196 | 'name' => 'catname', |
||
| 197 | 'horizontal-label' => true, |
||
| 198 | 'label-message' => 'export-addcattext', |
||
| 199 | 'default' => $categoryName, |
||
| 200 | 'size' => 40, |
||
| 201 | 'buttontype' => 'submit', |
||
| 202 | 'buttonname' => 'addcat', |
||
| 203 | 'buttondefault' => $this->msg( 'export-addcat' )->text(), |
||
| 204 | 'hide-if' => [ '===', 'exportall', '1' ], |
||
| 205 | ], |
||
| 206 | ]; |
||
| 207 | if ( $config->get( 'ExportFromNamespaces' ) ) { |
||
| 208 | $formDescriptor += [ |
||
| 209 | 'nsindex' => [ |
||
| 210 | 'type' => 'namespaceselectwithbutton', |
||
| 211 | 'default' => $nsindex, |
||
| 212 | 'label-message' => 'export-addnstext', |
||
| 213 | 'horizontal-label' => true, |
||
| 214 | 'name' => 'nsindex', |
||
| 215 | 'id' => 'namespace', |
||
| 216 | 'cssclass' => 'namespaceselector', |
||
| 217 | 'buttontype' => 'submit', |
||
| 218 | 'buttonname' => 'addns', |
||
| 219 | 'buttondefault' => $this->msg( 'export-addns' )->text(), |
||
| 220 | 'hide-if' => [ '===', 'exportall', '1' ], |
||
| 221 | ], |
||
| 222 | ]; |
||
| 223 | } |
||
| 224 | |||
| 225 | View Code Duplication | if ( $config->get( 'ExportAllowAll' ) ) { |
|
| 226 | $formDescriptor += [ |
||
| 227 | 'exportall' => [ |
||
| 228 | 'type' => 'check', |
||
| 229 | 'label-message' => 'exportall', |
||
| 230 | 'name' => 'exportall', |
||
| 231 | 'id' => 'exportall', |
||
| 232 | 'default' => $request->wasPosted() ? $request->getCheck( 'exportall' ) : false, |
||
| 233 | ], |
||
| 234 | ]; |
||
| 235 | } |
||
| 236 | |||
| 237 | $formDescriptor += [ |
||
| 238 | 'textarea' => [ |
||
| 239 | 'class' => 'HTMLTextAreaField', |
||
| 240 | 'name' => 'pages', |
||
| 241 | 'label-message' => 'export-manual', |
||
| 242 | 'nodata' => true, |
||
| 243 | 'rows' => 10, |
||
| 244 | 'default' => $page, |
||
| 245 | 'hide-if' => [ '===', 'exportall', '1' ], |
||
| 246 | ], |
||
| 247 | ]; |
||
| 248 | |||
| 249 | View Code Duplication | if ( $config->get( 'ExportAllowHistory' ) ) { |
|
| 250 | $formDescriptor += [ |
||
| 251 | 'curonly' => [ |
||
| 252 | 'type' => 'check', |
||
| 253 | 'label-message' => 'exportcuronly', |
||
| 254 | 'name' => 'curonly', |
||
| 255 | 'id' => 'curonly', |
||
| 256 | 'default' => $request->wasPosted() ? $request->getCheck( 'curonly' ) : true, |
||
| 257 | ], |
||
| 258 | ]; |
||
| 259 | } else { |
||
| 260 | $out->addWikiMsg( 'exportnohistory' ); |
||
| 261 | } |
||
| 262 | |||
| 263 | $formDescriptor += [ |
||
| 264 | 'templates' => [ |
||
| 265 | 'type' => 'check', |
||
| 266 | 'label-message' => 'export-templates', |
||
| 267 | 'name' => 'templates', |
||
| 268 | 'id' => 'wpExportTemplates', |
||
| 269 | 'default' => $request->wasPosted() ? $request->getCheck( 'templates' ) : false, |
||
| 270 | ], |
||
| 271 | ]; |
||
| 272 | |||
| 273 | if ( $config->get( 'ExportMaxLinkDepth' ) || $this->userCanOverrideExportDepth() ) { |
||
| 274 | $formDescriptor += [ |
||
| 275 | 'pagelink-depth' => [ |
||
| 276 | 'type' => 'text', |
||
| 277 | 'name' => 'pagelink-depth', |
||
| 278 | 'id' => 'pagelink-depth', |
||
| 279 | 'label-message' => 'export-pagelinks', |
||
| 280 | 'default' => '0', |
||
| 281 | 'size' => 20, |
||
| 282 | ], |
||
| 283 | ]; |
||
| 284 | } |
||
| 285 | |||
| 286 | $formDescriptor += [ |
||
| 287 | 'wpDownload' => [ |
||
| 288 | 'type' => 'check', |
||
| 289 | 'name' =>'wpDownload', |
||
| 290 | 'id' => 'wpDownload', |
||
| 291 | 'default' => $request->wasPosted() ? $request->getCheck( 'wpDownload' ) : true, |
||
| 292 | 'label-message' => 'export-download', |
||
| 293 | ], |
||
| 294 | ]; |
||
| 295 | |||
| 296 | View Code Duplication | if ( $config->get( 'ExportAllowListContributors' ) ) { |
|
| 297 | $formDescriptor += [ |
||
| 298 | 'listauthors' => [ |
||
| 299 | 'type' => 'check', |
||
| 300 | 'label-message' => 'exportlistauthors', |
||
| 301 | 'default' => $request->wasPosted() ? $request->getCheck( 'listauthors' ) : false, |
||
| 302 | 'name' => 'listauthors', |
||
| 303 | 'id' => 'listauthors', |
||
| 304 | ], |
||
| 305 | ]; |
||
| 306 | } |
||
| 307 | |||
| 308 | $htmlForm = HTMLForm::factory( 'ooui', $formDescriptor, $this->getContext() ); |
||
| 309 | $htmlForm->setSubmitTextMsg( 'export-submit' ); |
||
| 310 | $htmlForm->prepareForm()->displayForm( false ); |
||
| 311 | $this->addHelpLink( 'Help:Export' ); |
||
| 312 | } |
||
| 313 | |||
| 314 | /** |
||
| 315 | * @return bool |
||
| 316 | */ |
||
| 317 | private function userCanOverrideExportDepth() { |
||
| 320 | |||
| 321 | /** |
||
| 322 | * Do the actual page exporting |
||
| 323 | * |
||
| 324 | * @param string $page User input on what page(s) to export |
||
| 325 | * @param int $history One of the WikiExporter history export constants |
||
| 326 | * @param bool $list_authors Whether to add distinct author list (when |
||
| 327 | * not returning full history) |
||
| 328 | * @param bool $exportall Whether to export everything |
||
| 329 | */ |
||
| 330 | private function doExport( $page, $history, $list_authors, $exportall ) { |
||
| 417 | |||
| 418 | /** |
||
| 419 | * @param Title $title |
||
| 420 | * @return array |
||
| 421 | */ |
||
| 422 | private function getPagesFromCategory( $title ) { |
||
| 452 | |||
| 453 | /** |
||
| 454 | * @param int $nsindex |
||
| 455 | * @return array |
||
| 456 | */ |
||
| 457 | private function getPagesFromNamespace( $nsindex ) { |
||
| 486 | |||
| 487 | /** |
||
| 488 | * Expand a list of pages to include templates used in those pages. |
||
| 489 | * @param array $inputPages List of titles to look up |
||
| 490 | * @param array $pageSet Associative array indexed by titles for output |
||
| 491 | * @return array Associative array index by titles |
||
| 492 | */ |
||
| 493 | private function getTemplates( $inputPages, $pageSet ) { |
||
| 500 | |||
| 501 | /** |
||
| 502 | * Validate link depth setting, if available. |
||
| 503 | * @param int $depth |
||
| 504 | * @return int |
||
| 505 | */ |
||
| 506 | private function validateLinkDepth( $depth ) { |
||
| 526 | |||
| 527 | /** |
||
| 528 | * Expand a list of pages to include pages linked to from that page. |
||
| 529 | * @param array $inputPages |
||
| 530 | * @param array $pageSet |
||
| 531 | * @param int $depth |
||
| 532 | * @return array |
||
| 533 | */ |
||
| 534 | private function getPageLinks( $inputPages, $pageSet, $depth ) { |
||
| 548 | |||
| 549 | /** |
||
| 550 | * Expand a list of pages to include items used in those pages. |
||
| 551 | * @param array $inputPages Array of page titles |
||
| 552 | * @param array $pageSet |
||
| 553 | * @param string $table |
||
| 554 | * @param array $fields Array of field names |
||
| 555 | * @param array $join |
||
| 556 | * @return array |
||
| 557 | */ |
||
| 558 | private function getLinks( $inputPages, $pageSet, $table, $fields, $join ) { |
||
| 590 | |||
| 591 | protected function getGroupName() { |
||
| 594 | } |
||
| 595 |
Only declaring a single property per statement allows you to later on add doc comments more easily.
It is also recommended by PSR2, so it is a common style that many people expect.