EditPage: Refactor getCheckboxes() to allow changing the format
authorBartosz Dziewoński <matma.rex@gmail.com>
Sat, 25 Mar 2017 20:41:03 +0000 (21:41 +0100)
committerBartosz Dziewoński <matma.rex@gmail.com>
Wed, 29 Mar 2017 20:03:22 +0000 (22:03 +0200)
getCheckboxes() directly generated the HTML for the
"This is a minor edit" and "Watch this page" checkboxes,
and allowed extensions to add more HTML checkboxes (and
modify existing ones) using the 'EditPageBeforeEditChecks'
hook. This prevents us from ever changing the format of
the HTML (e.g. to use OOUI checkboxes).

Introduce new method getCheckboxesDefinition(), which
generates the checkboxes in a machine-readable format,
with a new hook 'EditPageGetCheckboxesDefinition'.
Rewrite getCheckboxes() in terms of that. The old hook
'EditPageBeforeEditChecks' is now deprecated.

Change-Id: I3dbe973dcac6cba0c3a1ac5d983cafcfb49d833c

RELEASE-NOTES-1.29
docs/hooks.txt
includes/EditPage.php

index ab75375..793769f 100644 (file)
@@ -272,6 +272,10 @@ changes to languages because of Phabricator reports.
 * Article::getLastPurgeTimestamp(), WikiPage::getLastPurgeTimestamp(), and the
   WikiPage::PURGE_* constants are deprecated, and the functions will always
   return false. They were a hack for an issue that has since been fixed.
+* Hook 'EditPageBeforeEditChecks' is now deprecated. Instead use the new hook
+  'EditPageGetCheckboxesDefinition', or 'EditPage::showStandardInputs:options'
+  if you don't actually care about checkboxes and just want to add some HTML
+  to the page.
 
 == Compatibility ==
 
index 3a57563..a38f9bb 100644 (file)
@@ -1404,10 +1404,12 @@ textarea in the edit form.
 &$buttons: Array of edit buttons "Save", "Preview", "Live", and "Diff"
 &$tabindex: HTML tabindex of the last edit check/button
 
-'EditPageBeforeEditChecks': Allows modifying the edit checks below the textarea
-in the edit form.
+'EditPageBeforeEditChecks': DEPRECATED! Use 'EditPageGetCheckboxesDefinition' instead,
+or 'EditPage::showStandardInputs:options' if you don't actually care about checkboxes
+and just want to add some HTML to the page.
+Allows modifying the edit checks below the textarea in the edit form.
 &$editpage: The current EditPage object
-&$checks: Array of edit checks like "watch this page"/"minor edit"
+&$checks: Array of the HTML for edit checks like "watch this page"/"minor edit"
 &$tabindex: HTML tabindex of the last edit check/button
 
 'EditPageBeforeEditToolbar': Allows modifying the edit toolbar above the
@@ -1420,6 +1422,12 @@ $title: title of page being edited
 &$msg: localization message name, overridable. Default is either
   'copyrightwarning' or 'copyrightwarning2'.
 
+'EditPageGetCheckboxesDefinition': Allows modifying the edit checkboxes
+below the textarea in the edit form.
+$editpage: The current EditPage object
+&$checkboxes: Array of checkbox definitions. See EditPage::getCheckboxesDefinition()
+for the format.
+
 'EditPageGetDiffContent': Allow modifying the wikitext that will be used in
 "Show changes". Note that it is preferable to implement diff handling for
 different data types using the ContentHandler facility.
index ac62e3f..7d19e94 100644 (file)
@@ -4005,70 +4005,114 @@ HTML
                return $toolbar;
        }
 
+       /**
+        * Return an array of checkbox definitions.
+        *
+        * Array keys correspond to the `<input>` 'name' attribute to use for each checkbox.
+        *
+        * Array values are associative arrays with the following keys:
+        *  - 'label-message' (required): message for label text
+        *  - 'id' (required): 'id' attribute for the `<input>`
+        *  - 'default' (required): default checkedness (true or false)
+        *  - 'title-message' (optional): used to generate 'title' attribute for the `<label>`
+        *  - 'tooltip' (optional): used to generate 'title' and 'accesskey' attributes
+        *    from messages like 'tooltip-foo', 'accesskey-foo'
+        *  - 'label-id' (optional): 'id' attribute for the `<label>`
+        *  - 'legacy-name' (optional): short name for backwards-compatibility
+        * @param array $checked Array of checkbox name (matching the 'legacy-name') => bool,
+        *   where bool indicates the checked status of the checkbox
+        * @return array
+        */
+       protected function getCheckboxesDefinition( $checked ) {
+               global $wgUser;
+               $checkboxes = [];
+
+               // don't show the minor edit checkbox if it's a new page or section
+               if ( !$this->isNew && $wgUser->isAllowed( 'minoredit' ) ) {
+                       $checkboxes['wpMinoredit'] = [
+                               'id' => 'wpMinoredit',
+                               'label-message' => 'minoredit',
+                               // Uses messages: tooltip-minoredit, accesskey-minoredit
+                               'tooltip' => 'minoredit',
+                               'label-id' => 'mw-editpage-minoredit',
+                               'legacy-name' => 'minor',
+                               'default' => $checked['minor'],
+                       ];
+               }
+
+               if ( $wgUser->isLoggedIn() ) {
+                       $checkboxes['wpWatchthis'] = [
+                               'id' => 'wpWatchthis',
+                               'label-message' => 'watchthis',
+                               // Uses messages: tooltip-watch, accesskey-watch
+                               'tooltip' => 'watch',
+                               'label-id' => 'mw-editpage-watch',
+                               'legacy-name' => 'watch',
+                               'default' => $checked['watch'],
+                       ];
+               }
+
+               $editPage = $this;
+               Hooks::run( 'EditPageGetCheckboxesDefinition', [ $editPage, &$checkboxes ] );
+
+               return $checkboxes;
+       }
+
        /**
         * Returns an array of html code of the following checkboxes:
         * minor and watch
         *
         * @param int $tabindex Current tabindex
-        * @param array $checked Array of checkbox => bool, where bool indicates the checked
-        *                 status of the checkbox
-        *
+        * @param array $checked See getCheckboxesDefinition()
         * @return array
         */
        public function getCheckboxes( &$tabindex, $checked ) {
-               global $wgUser, $wgUseMediaWikiUIEverywhere;
+               global $wgUseMediaWikiUIEverywhere;
 
                $checkboxes = [];
+               $checkboxesDef = $this->getCheckboxesDefinition( $checked );
 
-               // don't show the minor edit checkbox if it's a new page or section
+               // Backwards-compatibility for the EditPageBeforeEditChecks hook
                if ( !$this->isNew ) {
                        $checkboxes['minor'] = '';
-                       $minorLabel = $this->context->msg( 'minoredit' )->parse();
-                       if ( $wgUser->isAllowed( 'minoredit' ) ) {
-                               $attribs = [
-                                       'tabindex' => ++$tabindex,
-                                       'accesskey' => $this->context->msg( 'accesskey-minoredit' )->text(),
-                                       'id' => 'wpMinoredit',
-                               ];
-                               $minorEditHtml =
-                                       Xml::check( 'wpMinoredit', $checked['minor'], $attribs ) .
-                                       "&#160;<label for='wpMinoredit' id='mw-editpage-minoredit'" .
-                                       Xml::expandAttributes( [ 'title' => Linker::titleAttrib( 'minoredit', 'withaccess' ) ] ) .
-                                       ">{$minorLabel}</label>";
-
-                               if ( $wgUseMediaWikiUIEverywhere ) {
-                                       $checkboxes['minor'] =
-                                               Html::rawElement( 'div', [ 'class' => 'mw-ui-checkbox' ], $minorEditHtml );
-                               } else {
-                                       $checkboxes['minor'] = $minorEditHtml;
-                               }
-                       }
                }
-
-               $watchLabel = $this->context->msg( 'watchthis' )->parse();
                $checkboxes['watch'] = '';
-               if ( $wgUser->isLoggedIn() ) {
+
+               foreach ( $checkboxesDef as $name => $options ) {
+                       $legacyName = isset( $options['legacy-name'] ) ? $options['legacy-name'] : $name;
+                       $label = $this->context->msg( $options['label-message'] )->parse();
                        $attribs = [
                                'tabindex' => ++$tabindex,
-                               'accesskey' => $this->context->msg( 'accesskey-watch' )->text(),
-                               'id' => 'wpWatchthis',
+                               'id' => $options['id'],
+                       ];
+                       $labelAttribs = [
+                               'for' => $options['id'],
                        ];
-                       $watchThisHtml =
-                               Xml::check( 'wpWatchthis', $checked['watch'], $attribs ) .
-                               "&#160;<label for='wpWatchthis' id='mw-editpage-watch'" .
-                               Xml::expandAttributes( [ 'title' => Linker::titleAttrib( 'watch', 'withaccess' ) ] ) .
-                               ">{$watchLabel}</label>";
+                       if ( isset( $options['tooltip'] ) ) {
+                               $attribs['accesskey'] = $this->context->msg( "accesskey-{$options['tooltip']}" )->text();
+                               $labelAttribs['title'] = Linker::titleAttrib( $options['tooltip'], 'withaccess' );
+                       }
+                       if ( isset( $options['title-message'] ) ) {
+                               $labelAttribs['title'] = $this->context->msg( $options['title-message'] )->text();
+                       }
+                       if ( isset( $options['label-id'] ) ) {
+                               $labelAttribs['id'] = $options['label-id'];
+                       }
+                       $checkboxHtml =
+                               Xml::check( $name, $options['default'], $attribs ) .
+                               '&#160;' .
+                               Xml::tags( 'label', $labelAttribs, $label );
+
                        if ( $wgUseMediaWikiUIEverywhere ) {
-                               $checkboxes['watch'] =
-                                       Html::rawElement( 'div', [ 'class' => 'mw-ui-checkbox' ], $watchThisHtml );
-                       } else {
-                               $checkboxes['watch'] = $watchThisHtml;
+                               $checkboxHtml = Html::rawElement( 'div', [ 'class' => 'mw-ui-checkbox' ], $checkboxHtml );
                        }
+
+                       $checkboxes[ $legacyName ] = $checkboxHtml;
                }
 
                // Avoid PHP 7.1 warning of passing $this by reference
                $editPage = $this;
-               Hooks::run( 'EditPageBeforeEditChecks', [ &$editPage, &$checkboxes, &$tabindex ] );
+               Hooks::run( 'EditPageBeforeEditChecks', [ &$editPage, &$checkboxes, &$tabindex ], '1.29' );
                return $checkboxes;
        }