| Conditions | 44 |
| Paths | > 20000 |
| Total Lines | 272 |
| Code Lines | 177 |
| Lines | 35 |
| Ratio | 12.87 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 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 | ], |
||
| 205 | ]; |
||
| 206 | if ( $config->get( 'ExportFromNamespaces' ) ) { |
||
| 207 | $formDescriptor += [ |
||
| 208 | 'nsindex' => [ |
||
| 209 | 'type' => 'namespaceselectwithbutton', |
||
| 210 | 'default' => $nsindex, |
||
| 211 | 'label-message' => 'export-addnstext', |
||
| 212 | 'horizontal-label' => true, |
||
| 213 | 'name' => 'nsindex', |
||
| 214 | 'id' => 'namespace', |
||
| 215 | 'cssclass' => 'namespaceselector', |
||
| 216 | 'buttontype' => 'submit', |
||
| 217 | 'buttonname' => 'addns', |
||
| 218 | 'buttondefault' => $this->msg( 'export-addns' )->text(), |
||
| 219 | ], |
||
| 220 | ]; |
||
| 221 | } |
||
| 222 | |||
| 223 | View Code Duplication | if ( $config->get( 'ExportAllowAll' ) ) { |
|
| 224 | $formDescriptor += [ |
||
| 225 | 'exportall' => [ |
||
| 226 | 'type' => 'check', |
||
| 227 | 'label-message' => 'exportall', |
||
| 228 | 'name' => 'exportall', |
||
| 229 | 'id' => 'exportall', |
||
| 230 | 'default' => $request->wasPosted() ? $request->getCheck( 'exportall' ) : false, |
||
| 231 | ], |
||
| 232 | ]; |
||
| 233 | } |
||
| 234 | |||
| 235 | $formDescriptor += [ |
||
| 236 | 'textarea' => [ |
||
| 237 | 'class' => 'HTMLTextAreaField', |
||
| 238 | 'name' => 'pages', |
||
| 239 | 'label-message' => 'export-manual', |
||
| 240 | 'nodata' => true, |
||
| 241 | 'rows' => 10, |
||
| 242 | 'default' => $page, |
||
| 243 | ], |
||
| 244 | ]; |
||
| 245 | |||
| 246 | View Code Duplication | if ( $config->get( 'ExportAllowHistory' ) ) { |
|
| 247 | $formDescriptor += [ |
||
| 248 | 'curonly' => [ |
||
| 249 | 'type' => 'check', |
||
| 250 | 'label-message' => 'exportcuronly', |
||
| 251 | 'name' => 'curonly', |
||
| 252 | 'id' => 'curonly', |
||
| 253 | 'default' => $request->wasPosted() ? $request->getCheck( 'curonly' ) : true, |
||
| 254 | ], |
||
| 255 | ]; |
||
| 256 | } else { |
||
| 257 | $out->addWikiMsg( 'exportnohistory' ); |
||
| 258 | } |
||
| 259 | |||
| 260 | $formDescriptor += [ |
||
| 261 | 'templates' => [ |
||
| 262 | 'type' => 'check', |
||
| 263 | 'label-message' => 'export-templates', |
||
| 264 | 'name' => 'templates', |
||
| 265 | 'id' => 'wpExportTemplates', |
||
| 266 | 'default' => $request->wasPosted() ? $request->getCheck( 'templates' ) : false, |
||
| 267 | ], |
||
| 268 | ]; |
||
| 269 | |||
| 270 | if ( $config->get( 'ExportMaxLinkDepth' ) || $this->userCanOverrideExportDepth() ) { |
||
| 271 | $formDescriptor += [ |
||
| 272 | 'pagelink-depth' => [ |
||
| 273 | 'type' => 'text', |
||
| 274 | 'name' => 'pagelink-depth', |
||
| 275 | 'id' => 'pagelink-depth', |
||
| 276 | 'label-message' => 'export-pagelinks', |
||
| 277 | 'default' => '0', |
||
| 278 | 'size' => 20, |
||
| 279 | ], |
||
| 280 | ]; |
||
| 281 | } |
||
| 282 | |||
| 283 | $formDescriptor += [ |
||
| 284 | 'wpDownload' => [ |
||
| 285 | 'type' => 'check', |
||
| 286 | 'name' =>'wpDownload', |
||
| 287 | 'id' => 'wpDownload', |
||
| 288 | 'default' => $request->wasPosted() ? $request->getCheck( 'wpDownload' ) : true, |
||
| 289 | 'label-message' => 'export-download', |
||
| 290 | ], |
||
| 291 | ]; |
||
| 292 | |||
| 293 | View Code Duplication | if ( $config->get( 'ExportAllowListContributors' ) ) { |
|
| 294 | $formDescriptor += [ |
||
| 295 | 'listauthors' => [ |
||
| 296 | 'type' => 'check', |
||
| 297 | 'label-message' => 'exportlistauthors', |
||
| 298 | 'default' => $request->wasPosted() ? $request->getCheck( 'listauthors' ) : false, |
||
| 299 | 'name' => 'listauthors', |
||
| 300 | 'id' => 'listauthors', |
||
| 301 | ], |
||
| 302 | ]; |
||
| 303 | } |
||
| 304 | |||
| 305 | $htmlForm = HTMLForm::factory( 'ooui', $formDescriptor, $this->getContext() ); |
||
| 306 | $htmlForm->setSubmitTextMsg( 'export-submit' ); |
||
| 307 | $htmlForm->prepareForm()->displayForm( false ); |
||
| 308 | $this->addHelpLink( 'Help:Export' ); |
||
| 309 | } |
||
| 310 | |||
| 592 |
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.