Pcoombe has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/142561

Change subject: Convert from tag to includable special page, revamp tracking
......................................................................

Convert from tag to includable special page, revamp tracking

As suggested on https://bugzilla.wikimedia.org/show_bug.cgi?id=65849,
converted the petition form from a tag to an includable special page.
This avoids misusing $wgRequest.

Petition can now be included in a page like this:
  {{Special:Petition/petitionname}}

Also removed the pagetitle and linksource tracking, as these needed
external context and weren't actually all that useful. Instead can now
include a tracking source parameter which will be recorded with signatures:
  {{Special:Petition/petitionname/source}}

Change-Id: If64de462aaca880df34d3eef81f46d4eb7bc1d01
---
M Petition.alias.php
M Petition.php
R SpecialPetition.php
M i18n/en.json
M i18n/qqq.json
M table.sql
6 files changed, 43 insertions(+), 43 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Petition 
refs/changes/61/142561/1

diff --git a/Petition.alias.php b/Petition.alias.php
index e7f9b72..fb33640 100755
--- a/Petition.alias.php
+++ b/Petition.alias.php
@@ -10,6 +10,7 @@
 
 /** English */
 $specialPageAliases['en'] = array(
+       'Petition' => array( 'Petition' ),
        'PetitionData' => array( 'PetitionData' )
 );
 
diff --git a/Petition.php b/Petition.php
index ad89c17..2b424e6 100755
--- a/Petition.php
+++ b/Petition.php
@@ -18,13 +18,13 @@
        'dependencies'  => 'mediawiki.ui'
 );
 
+## Petition form
+$wgAutoloadClasses['SpecialPetition'] = __DIR__ . "/SpecialPetition.php";
+$wgSpecialPages['Petition'] = 'SpecialPetition';
+
 ## Petition data download page
 $wgAutoloadClasses['SpecialPetitionData'] = __DIR__ . 
"/SpecialPetitionData.php";
 $wgSpecialPages['PetitionData'] = 'SpecialPetitionData';
-
-## Petition tag
-$wgAutoloadClasses['Petition'] = __DIR__ . '/TagPetition.php';
-$wgHooks['ParserFirstCallInit'][] = 'Petition::onParserInit';
 
 $wgMessagesDirs['Petition'] = __DIR__ . '/i18n';
 $wgExtensionMessagesFiles['PetitionDataAlias'] = __DIR__ . 
'/Petition.alias.php';
diff --git a/TagPetition.php b/SpecialPetition.php
similarity index 61%
rename from TagPetition.php
rename to SpecialPetition.php
index fe99adb..1058f7b 100755
--- a/TagPetition.php
+++ b/SpecialPetition.php
@@ -1,44 +1,48 @@
 <?php
-class Petition {
 
-       public static function onParserInit( Parser $parser ) {
-               $parser->setHook( 'petition', array( __CLASS__, 
'petitionRender' ) );
-               return true;
+class SpecialPetition extends IncludableSpecialPage {
+       function __construct() {
+               parent::__construct( 'Petition' );
        }
 
-       public static function petitionRender( $input, array $args, Parser 
$parser, PPFrame $frame ) {
-               global $wgRequest;
+       function execute($par) {
 
-               // Can have multiple named petitions using <petition 
name="foo"/>
-               $petitionName = isset($args['name']) ? $args['name'] : 
'default';
-               // Optional "linksource" URL parameter, to track how people 
arrived at petition
-               $linkSource = $wgRequest->getVal( 'linksource' );
+               $request = $this->getRequest();
+               $out = $this->getOutput();
 
-               $parser->disableCache(); // Need cache disabled for number of 
signatures to update, and for thank you message
+               // Can have multiple named petitions using 
{{Special:Petition/foo}}
+               // Can also specify am optional tracking parameter e.g. 
{{Special:Petition/foo/email}}
+               $arr = explode('/', $par);
+               $petitionName = isset($arr[0]) ? $arr[0] : 'default';
+               $source = isset($arr[1]) ? $arr[1] : '';
 
-               $parser->getOutput()->addModules( 'ext.Petition' );
+               $this->setHeaders();
+               $this->outputHeader();
 
-               $countries = Petition::getCountryArray( 
$parser->getOptions()->getUserLang() );
-               $form = Petition::defineForm( $petitionName, 
$parser->getTitle()->getPrefixedText(), $linkSource, $countries );
+               $out->addModules( 'ext.Petition' );
+
+               $countries = SpecialPetition::getCountryArray( 
$this->getLanguage()->getCode() );
+               $form = SpecialPetition::defineForm( $petitionName, $source, 
$countries );
 
                $form->prepareForm();
 
                $result = $form->tryAuthorizedSubmit();
+
                if ( $result === true || ( $result instanceof Status && 
$result->isGood() ) ) {
-                       return '<span class="petition-done">' . 
wfMessage('petition-done')->text() . '</span>';
+                       $htmlOut = '<span class="petition-done">' . 
wfMessage('petition-done')->text() . '</span>';
+               } else {
+                       $htmlOut = '<div class="petition-form">' . "\n";
+                       $numberOfSignatures = 
SpecialPetition::getNumberOfSignatures( $petitionName );
+                       $htmlOut .= '<div id="petition-num-signatures">';
+                       $htmlOut .= wfMessage('petition-num-signatures', 
$numberOfSignatures)->escaped();
+                       $htmlOut .= '</div>' . "\n";
+                       // Add the form, with any errors if there was an 
attempted submission
+                       $htmlOut .= $form->getHtml($result) . "\n";
+                       $htmlOut .= '</div>' . "\n";
                }
 
-               $htmlOut = '<div class="petition-form">' . "\n";
-               // Add the number of signatures first above the form.
-               $numberOfSignatures = Petition::getNumberOfSignatures( 
$petitionName );
-               $htmlOut .= '<div id="petition-num-signatures">';
-               $htmlOut .= wfMessage('petition-num-signatures', 
$numberOfSignatures)->escaped();
-               $htmlOut .= '</div>' . "\n";
-               // Add the form, with any errors if there was an attempted 
submission
-               $htmlOut .= $form->getHtml($result) . "\n";
-               $htmlOut .= '</div>' . "\n";
+               $out->addHtml($htmlOut);
 
-               return $htmlOut;
        }
 
        /**
@@ -53,8 +57,7 @@
                $dbw = wfGetDB( DB_MASTER, array(), $wgPetitionDatabase );
                $dbw->insert( 'petition_data', array(
                                'pt_petitionname' => $formData['petitionname'],
-                               'pt_pagetitle'    => $formData['pagetitle'],
-                               'pt_source'       => $formData['linksource'],
+                               'pt_source'       => $formData['source'],
                                'pt_name'         => $formData['name'],
                                'pt_email'        => $formData['email'],
                                'pt_country'      => $formData['country'],
@@ -84,7 +87,6 @@
 
        /**
         * Retrieve the list of countries in given language via CLDR
-        * If CLDR not available, use a fallback list in English
         *
         * @param string $language ISO code of required language
         * @return array Countries with names as keys and ISO codes as values
@@ -102,19 +104,15 @@
                return $countries;
        }
 
-       static function defineForm( $petitionName, $pageTitle, $linkSource, 
$countries ) {
+       static function defineForm( $petitionName, $source, $countries ) {
                $formDescriptor = array(
                        'petitionname' => array(
                                'type' => 'hidden',
                                'default' => $petitionName,
                        ),
-                       'pagetitle' => array(
+                       'source' => array(
                                'type' => 'hidden',
-                               'default' => $pageTitle,
-                       ),
-                       'linksource' => array(
-                               'type' => 'hidden',
-                               'default' => $linkSource,
+                               'default' => $source,
                        ),
                        'name' => array(
                                'type' => 'text',
@@ -153,9 +151,9 @@
                $form->setDisplayFormat( 'vform' );
                $form->setId( 'petition-form' );
                $form->setSubmitText( wfMessage( 'petition-form-submit' 
)->text() );
-               $form->setSubmitCallback( array( 'Petition', 'petitionSubmit' ) 
);
+               $form->setSubmitCallback( array( 'SpecialPetition', 
'petitionSubmit' ) );
 
                return $form;
        }
 
-}
\ No newline at end of file
+}
diff --git a/i18n/en.json b/i18n/en.json
index e772cd4..270ea6c 100755
--- a/i18n/en.json
+++ b/i18n/en.json
@@ -4,7 +4,8 @@
             "Pcoombe (WMF)"
         ]
     },
-    "petition-desc"           : "Adds a <nowiki><petition/></nowiki> tag to 
collect signatures, and a [[Special:PetitionData|special page]] for authorised 
users to download signatures as a csv file.",
+    "petition"                : "Sign petition",
+    "petition-desc"           : "Adds an includable page [[Special:Petition]] 
to collect signatures, and [[Special:PetitionData]] for authorised users to 
download signatures as a csv file.",
     "petition-form-name"      : "Name",
     "petition-form-email"     : "Email address",
     "petition-form-country"   : "Country",
diff --git a/i18n/qqq.json b/i18n/qqq.json
index 8183103..5b39516 100755
--- a/i18n/qqq.json
+++ b/i18n/qqq.json
@@ -4,6 +4,7 @@
             "Pcoombe (WMF)"
         ]
     },
+    "petition"                : "Title for Special:Petition page (not usually 
seen as it is transcluded on other pages)",
     "petition-desc"           : "Description of Extension:Petition",
     "petition-form-name"      : "Label for field to input name",
     "petition-form-email"     : "Label for field to input email address",
diff --git a/table.sql b/table.sql
index 6e919a7..d88fbf1 100755
--- a/table.sql
+++ b/table.sql
@@ -2,7 +2,6 @@
 CREATE TABLE /*_*/petition_data (
        pt_id int unsigned PRIMARY KEY auto_increment,
        pt_petitionname varchar(255),
-       pt_pagetitle varchar(255),
        pt_source varchar(255),
        pt_name varchar(255),
        pt_email varchar(255),

-- 
To view, visit https://gerrit.wikimedia.org/r/142561
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: If64de462aaca880df34d3eef81f46d4eb7bc1d01
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Petition
Gerrit-Branch: master
Gerrit-Owner: Pcoombe <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to