Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 93 |
|
0.00% |
0 / 1 |
CRAP | |
0.00% |
0 / 1 |
| PFAutoEditRating | |
0.00% |
0 / 93 |
|
0.00% |
0 / 1 |
600 | |
0.00% |
0 / 1 |
| run | |
0.00% |
0 / 93 |
|
0.00% |
0 / 1 |
600 | |||
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * '#autoedit_rating' is called as: |
| 5 | * |
| 6 | * {{#autoedit_rating:form=|target=|rating field=|value=|star width=|num stars=|allow half stars=}} |
| 7 | * |
| 8 | */ |
| 9 | |
| 10 | use MediaWiki\Html\Html; |
| 11 | use MediaWiki\MediaWikiServices; |
| 12 | use MediaWiki\Title\Title; |
| 13 | |
| 14 | class PFAutoEditRating { |
| 15 | public static function run( Parser $parser ) { |
| 16 | global $wgPageFormsAutoeditNamespaces; |
| 17 | |
| 18 | $parser->getOutput()->addModules( [ 'ext.pageforms.autoeditrating' ] ); |
| 19 | |
| 20 | // Set defaults. |
| 21 | $formcontent = ''; |
| 22 | $summary = null; |
| 23 | $minorEdit = false; |
| 24 | $inQueryArr = []; |
| 25 | $editTime = null; |
| 26 | $latestRevId = null; |
| 27 | $confirmEdit = false; |
| 28 | $ratingAttrs = [ |
| 29 | 'class' => 'pfRating', |
| 30 | 'data-curvalue' => 0, |
| 31 | 'data-starwidth' => '24px', |
| 32 | 'data-numstars' => 5, |
| 33 | 'data-allows-half' => false |
| 34 | ]; |
| 35 | |
| 36 | // Parse parameters. |
| 37 | $params = func_get_args(); |
| 38 | array_shift( $params ); |
| 39 | |
| 40 | $wikiPageFactory = MediaWikiServices::getInstance()->getWikiPageFactory(); |
| 41 | |
| 42 | foreach ( $params as $param ) { |
| 43 | $elements = explode( '=', $param, 2 ); |
| 44 | |
| 45 | $key = trim( $elements[0] ); |
| 46 | $value = ( count( $elements ) > 1 ) ? trim( $elements[1] ) : ''; |
| 47 | |
| 48 | switch ( $key ) { |
| 49 | case 'rating field': |
| 50 | $inQueryArr = PFAutoEdit::convertQueryString( $value, $inQueryArr ); |
| 51 | break; |
| 52 | case 'target': |
| 53 | $value = $parser->recursiveTagParse( $value ); |
| 54 | $arr = [ $key => $value ]; |
| 55 | $inQueryArr = PFUtils::arrayMergeRecursiveDistinct( $inQueryArr, $arr ); |
| 56 | |
| 57 | $targetTitle = Title::newFromText( $value ); |
| 58 | |
| 59 | if ( $targetTitle !== null ) { |
| 60 | $allowedNamespaces = array_merge( |
| 61 | $wgPageFormsAutoeditNamespaces, |
| 62 | [ NS_CATEGORY ] |
| 63 | ); |
| 64 | if ( !in_array( $targetTitle->getNamespace(), $allowedNamespaces ) ) { |
| 65 | $errorMsg = wfMessage( 'pf-autoedit-invalidnamespace', $targetTitle->getNsText() )->parse(); |
| 66 | return Html::element( 'div', [ 'class' => 'error' ], $errorMsg ); |
| 67 | } |
| 68 | $targetWikiPage = $wikiPageFactory->newFromTitle( $targetTitle ); |
| 69 | $targetWikiPage->clear(); |
| 70 | $editTime = $targetWikiPage->getTimestamp(); |
| 71 | $latestRevId = $targetWikiPage->getLatest(); |
| 72 | } |
| 73 | break; |
| 74 | case 'value': |
| 75 | $ratingAttrs['data-curvalue'] = $parser->recursiveTagParse( $value ); |
| 76 | break; |
| 77 | case 'star width': |
| 78 | $ratingAttrs['data-starwidth'] = $parser->recursiveTagParse( $value ) . 'px'; |
| 79 | break; |
| 80 | case 'num stars': |
| 81 | $ratingAttrs['data-numstars'] = $parser->recursiveTagParse( $value ); |
| 82 | break; |
| 83 | case 'allow half stars': |
| 84 | $ratingAttrs['data-allows-half'] = true; |
| 85 | break; |
| 86 | case 'summary': |
| 87 | $summary = $parser->recursiveTagParse( $value ); |
| 88 | break; |
| 89 | case 'minor': |
| 90 | $minorEdit = true; |
| 91 | break; |
| 92 | case 'confirm': |
| 93 | $confirmEdit = true; |
| 94 | break; |
| 95 | default: |
| 96 | $value = $parser->recursiveTagParse( $value ); |
| 97 | $arr = [ $key => $value ]; |
| 98 | $inQueryArr = PFUtils::arrayMergeRecursiveDistinct( $inQueryArr, $arr ); |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | // query string has to be turned into hidden inputs. |
| 103 | if ( !empty( $inQueryArr ) ) { |
| 104 | $query_components = explode( '&', http_build_query( $inQueryArr, '', '&' ) ); |
| 105 | foreach ( $query_components as $query_component ) { |
| 106 | $var_and_val = explode( '=', $query_component, 2 ); |
| 107 | if ( count( $var_and_val ) == 2 ) { |
| 108 | $formcontent .= Html::hidden( urldecode( $var_and_val[0] ), urldecode( $var_and_val[1] ), $var_and_val[1] == '' ? [ 'id' => 'ratingInput' ] : [] ); |
| 109 | } |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | if ( $summary == null ) { |
| 114 | $summary = wfMessage( 'pf_autoedit_summary', "[[{$parser->getTitle()}]]" )->text(); |
| 115 | } |
| 116 | |
| 117 | $formcontent .= Html::hidden( 'wpSummary', $summary ); |
| 118 | |
| 119 | if ( $minorEdit ) { |
| 120 | $formcontent .= Html::hidden( 'wpMinoredit', true ); |
| 121 | } |
| 122 | |
| 123 | if ( $editTime !== null ) { |
| 124 | $formcontent .= Html::hidden( 'wpEdittime', $editTime ); |
| 125 | } |
| 126 | if ( $latestRevId !== null ) { |
| 127 | $formcontent .= Html::hidden( 'editRevId', $latestRevId ); |
| 128 | } |
| 129 | |
| 130 | if ( $confirmEdit ) { |
| 131 | $formAttrs = [ 'class' => [ 'autoedit-data', 'confirm-edit' ] ]; |
| 132 | } else { |
| 133 | $formAttrs = [ 'class' => 'autoedit-data' ]; |
| 134 | } |
| 135 | |
| 136 | $form = Html::rawElement( 'form', $formAttrs, $formcontent ); |
| 137 | |
| 138 | $text = Html::element( 'div', $ratingAttrs ); |
| 139 | $output = Html::rawElement( 'div', [ 'class' => 'autoedit' ], |
| 140 | $text . |
| 141 | Html::rawElement( 'span', [ 'class' => "autoedit-result" ], '' ) . |
| 142 | $form |
| 143 | ); |
| 144 | |
| 145 | return [ $output, 'noparse' => true, 'isHTML' => true ]; |
| 146 | } |
| 147 | } |