Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 54 |
|
0.00% |
0 / 8 |
CRAP | |
0.00% |
0 / 1 |
| ZObjectEditAction | |
0.00% |
0 / 54 |
|
0.00% |
0 / 8 |
182 | |
0.00% |
0 / 1 |
| getName | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getTargetZObjectWithLabels | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getTargetZObject | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| getZObjectEditPageHTMLTitle | |
0.00% |
0 / 12 |
|
0.00% |
0 / 1 |
12 | |||
| getZObjectEditTitle | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| show | |
0.00% |
0 / 32 |
|
0.00% |
0 / 1 |
12 | |||
| getRestriction | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| doesWrites | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * WikiLambda edit action for ZObjects |
| 4 | * |
| 5 | * @file |
| 6 | * @ingroup Extensions |
| 7 | * @copyright 2020– Abstract Wikipedia team; see AUTHORS.txt |
| 8 | * @license MIT |
| 9 | */ |
| 10 | |
| 11 | namespace MediaWiki\Extension\WikiLambda\ZObjectContent; |
| 12 | |
| 13 | use MediaWiki\Actions\Action; |
| 14 | use MediaWiki\Extension\WikiLambda\PageTitle\PageTitleBuilder; |
| 15 | use MediaWiki\Extension\WikiLambda\WikiLambdaServices; |
| 16 | use MediaWiki\Html\Html; |
| 17 | use MediaWiki\MainConfigNames; |
| 18 | use MediaWiki\Skin\Skin; |
| 19 | use MediaWiki\SpecialPage\SpecialPage; |
| 20 | |
| 21 | class ZObjectEditAction extends Action { |
| 22 | use ZObjectEditingPageTrait; |
| 23 | |
| 24 | /** |
| 25 | * @inheritDoc |
| 26 | */ |
| 27 | public function getName() { |
| 28 | return 'edit'; |
| 29 | } |
| 30 | |
| 31 | /** |
| 32 | * Get the type and language labels of the target zObject |
| 33 | * @return array |
| 34 | */ |
| 35 | public function getTargetZObjectWithLabels() { |
| 36 | return $this->getTargetZObject()->getTypeStringAndLanguage( $this->getLanguage() ); |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * Get the target zObject |
| 41 | * @return ZObjectContent|bool Found ZObject |
| 42 | */ |
| 43 | public function getTargetZObject() { |
| 44 | $zObjectStore = WikiLambdaServices::getZObjectStore(); |
| 45 | return $zObjectStore->fetchZObjectByTitle( $this->getTitle() ); |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * Get page title meta tag |
| 50 | * @return string |
| 51 | */ |
| 52 | protected function getZObjectEditPageHTMLTitle() { |
| 53 | // If the page doesn't exist (e.g. it's been deleted), return nothing. |
| 54 | $targetZObject = $this->getTargetZObject(); |
| 55 | if ( !$targetZObject ) { |
| 56 | return ''; |
| 57 | } |
| 58 | |
| 59 | $sitename = $this->getContext()->getConfig()->get( MainConfigNames::Sitename ); |
| 60 | |
| 61 | $zid = $targetZObject->getZid(); |
| 62 | $label = $targetZObject->getLabels() |
| 63 | ->buildStringForLanguage( $this->getLanguage() ) |
| 64 | ->fallbackWithEnglish() |
| 65 | ->getString(); |
| 66 | |
| 67 | return $this->msg( 'wikilambda-edit' )->text() . ' ' . |
| 68 | ( $label ?: $zid ) . ' - ' . |
| 69 | $sitename; |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Get page header message |
| 74 | * @return string |
| 75 | */ |
| 76 | protected function getZObjectEditTitle() { |
| 77 | // If the page doesn't exist (e.g. it's been deleted), return nothing. |
| 78 | $targetZObject = $this->getTargetZObject(); |
| 79 | if ( !$targetZObject ) { |
| 80 | return ''; |
| 81 | } |
| 82 | return PageTitleBuilder::createZObjectEditPageTitle( $targetZObject, $this->getLanguage(), $this ); |
| 83 | } |
| 84 | |
| 85 | public function show() { |
| 86 | $output = $this->getOutput(); |
| 87 | $output->addModules( [ 'ext.wikilambda.app' ] ); |
| 88 | |
| 89 | // (T347528) The action's Title object, not the Message getPageTitle() for the HTML page heading |
| 90 | $zId = $this->getTitle()->getBaseText(); |
| 91 | |
| 92 | $output->addModuleStyles( [ 'ext.wikilambda.editpage.styles' ] ); |
| 93 | |
| 94 | // (T328679) If the page doesn't exist yet, route the user to the ZObject creation system |
| 95 | // rather than running the code below that assumes the ZObject exists. |
| 96 | if ( !$this->getTitle()->exists() ) { |
| 97 | $specialTitle = SpecialPage::getTitleFor( 'CreateObject' ); |
| 98 | $this->getOutput()->redirect( $specialTitle->getFullURL() ); |
| 99 | return; |
| 100 | } |
| 101 | |
| 102 | // (T290217) Set page header |
| 103 | $pageTitle = $this->getZObjectEditTitle(); |
| 104 | $output->setPageTitle( $pageTitle ); |
| 105 | |
| 106 | // (T360169) Set page title meta tag |
| 107 | $htmlTitle = $this->getZObjectEditPageHTMLTitle(); |
| 108 | $output->setHTMLTitle( $htmlTitle ); |
| 109 | |
| 110 | $zObjectLabelsWithLang = $this->getTargetZObjectWithLabels(); |
| 111 | |
| 112 | if ( $zObjectLabelsWithLang[ 'title' ] === 'Function' ) { |
| 113 | $url = Skin::makeInternalOrExternalUrl( $this->msg( 'wikilambda-users-help-link' )->text() ); |
| 114 | $linkLabel = Html::element( |
| 115 | 'a', |
| 116 | [ |
| 117 | 'class' => 'ext-wikilambda-editpage-header__description--link', |
| 118 | 'href' => $url |
| 119 | ], |
| 120 | $this->msg( 'wikilambda-special-edit-function-definition-special-permission-link-label' )->text() |
| 121 | ); |
| 122 | |
| 123 | $output->addHtml( Html::rawElement( |
| 124 | 'div', [ 'class' => 'ext-wikilambda-editpage-header__description' ], |
| 125 | $this->msg( 'wikilambda-special-edit-function-definition-description' )->rawParams( $linkLabel ) |
| 126 | ->escaped() |
| 127 | ) ); |
| 128 | } |
| 129 | |
| 130 | $this->generateZObjectPayload( $this->getContext(), $output, [ |
| 131 | 'createNewPage' => false, |
| 132 | 'zId' => $zId, |
| 133 | ] ); |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * @inheritDoc |
| 138 | */ |
| 139 | public function getRestriction() { |
| 140 | // This is a very basic check; proper type-specific checking depends on the attemped |
| 141 | // edit/creation content, which isn't available yet |
| 142 | return 'wikilambda-create'; |
| 143 | } |
| 144 | |
| 145 | /** @inheritDoc */ |
| 146 | public function doesWrites() { |
| 147 | return true; |
| 148 | } |
| 149 | } |