Umherirrender has uploaded a new change for review.

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


Change subject: Add expensive parser functions {{REVISION*:}}
......................................................................

Add expensive parser functions {{REVISION*:}}

The magic words REVISIONID, REVISIONUSER and REVISIONTIMESTAMP (with
friends) now exists as parser function to fetch revision information
from another page specified as param to the parser function.
Invalid title or non-existing title will return an empty string.
Requesting revision deleted information will always return an empty
string, because this is for public audience and goes into the parser
cache.
The parser function will register a template link to get updated with
refresh jobs on edit of the remote page. This is the same way, than
the parser function pagesize is working.
When the given param is equal to the current title of the parser, the
parser function is processed as variable. That requiered I58ed30c5

Bug: 6092
Change-Id: Ib15a4e54c65192ec3caef71fd5dcb93fb6fc444e
---
M RELEASE-NOTES-1.22
M includes/parser/CoreParserFunctions.php
2 files changed, 169 insertions(+), 15 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/core 
refs/changes/34/76534/1

diff --git a/RELEASE-NOTES-1.22 b/RELEASE-NOTES-1.22
index e67bb00..952ed32 100644
--- a/RELEASE-NOTES-1.22
+++ b/RELEASE-NOTES-1.22
@@ -167,6 +167,8 @@
 * (bug 30713) New mw.hook "wikipage.content".
 * (bug 40430) jquery.placeholder gets a new parameter to set the attribute 
value
   to be used.
+* (bug 6092) Add expensive parser functions {{REVISIONID:}}, {{REVISIONUSER:}}
+  and {{REVISIONTIMESTAMP:}} (with friends)
 
 === Bug fixes in 1.22 ===
 * Disable Special:PasswordReset when $wgEnableEmail is false. Previously one
diff --git a/includes/parser/CoreParserFunctions.php 
b/includes/parser/CoreParserFunctions.php
index 8246f71..f502d9e 100644
--- a/includes/parser/CoreParserFunctions.php
+++ b/includes/parser/CoreParserFunctions.php
@@ -100,6 +100,15 @@
                $parser->setFunctionHook( 'subjectpagenamee', array( __CLASS__, 
'subjectpagenamee' ), SFH_NO_HASH );
                $parser->setFunctionHook( 'tag',              array( __CLASS__, 
'tagObj'           ), SFH_OBJECT_ARGS );
                $parser->setFunctionHook( 'formatdate',       array( __CLASS__, 
'formatDate'       ) );
+               $parser->setFunctionHook( 'pageid',           array( __CLASS__, 
'pageid'           ), SFH_NO_HASH );
+               $parser->setFunctionHook( 'revisionid',       array( __CLASS__, 
'revisionid'       ), SFH_NO_HASH );
+               $parser->setFunctionHook( 'revisionday',      array( __CLASS__, 
'revisionday'      ), SFH_NO_HASH );
+               $parser->setFunctionHook( 'revisionday2',     array( __CLASS__, 
'revisionday2'     ), SFH_NO_HASH );
+               $parser->setFunctionHook( 'revisionmonth',    array( __CLASS__, 
'revisionmonth'    ), SFH_NO_HASH );
+               $parser->setFunctionHook( 'revisionmonth1',   array( __CLASS__, 
'revisionmonth1'   ), SFH_NO_HASH );
+               $parser->setFunctionHook( 'revisionyear',     array( __CLASS__, 
'revisionyear'     ), SFH_NO_HASH );
+               $parser->setFunctionHook( 'revisiontimestamp', array( 
__CLASS__, 'revisiontimestamp' ), SFH_NO_HASH );
+               $parser->setFunctionHook( 'revisionuser',     array( __CLASS__, 
'revisionuser'     ), SFH_NO_HASH );
 
                if ( $wgAllowDisplayTitle ) {
                        $parser->setFunctionHook( 'displaytitle', array( 
__CLASS__, 'displaytitle' ), SFH_NO_HASH );
@@ -705,16 +714,11 @@
         * @return string
         */
        static function pagesize( $parser, $page = '', $raw = null ) {
-               static $cache = array();
                $title = Title::newFromText( $page );
 
                if ( !is_object( $title ) ) {
-                       $cache[$page] = 0;
                        return self::formatRaw( 0, $raw );
                }
-
-               # Normalize name for cache
-               $page = $title->getPrefixedText();
 
                $length = 0;
                if ( $title->equals( $parser->getTitle() )
@@ -723,16 +727,9 @@
                        # We are on current page (and not in PST), so
                        # take length of input to parser.
                        $length = $parser->mInputSize;
-               } elseif ( isset( $cache[$page] ) ) {
-                       $length = $cache[$page];
-               } elseif ( $parser->incrementExpensiveFunctionCount() ) {
-                       $rev = Revision::newFromTitle( $title, false, 
Revision::READ_NORMAL );
-                       $pageID = $rev ? $rev->getPage() : 0;
-                       $revID = $rev ? $rev->getId() : 0;
-                       $length = $cache[$page] = $rev ? $rev->getSize() : 0;
-
-                       // Register dependency in templatelinks
-                       $parser->mOutput->addTemplate( $title, $pageID, $revID 
);
+               } else {
+                       $rev = self::getCachedRevisionObject( $parser, $title );
+                       $length = $rev ? $rev->getSize() : 0;
                }
                return self::formatRaw( $length, $raw );
        }
@@ -953,4 +950,159 @@
                );
                return $parser->extensionSubstitution( $params, $frame );
        }
+
+       /**
+        * Fetched the current revision of the given title and returned this.
+        * Will increment the expensive function count and
+        * add a template link to get the value refreshed on edit 
+        *
+        * @param $parser Parser
+        * @param $title Title
+        * @return Revision
+        */
+       private static function getCachedRevisionObject( $parser, $title = null 
) {
+               static $cache = array();
+
+               if ( is_null( $title ) ) {
+                       return null;
+               }
+
+               // Normalize name for cache
+               $page = $title->getPrefixedDBkey();
+
+               if ( array_key_exists( $page, $cache ) ) { // cache contains 
null values
+                       return $cache[$page];
+               }
+               if ( $parser->incrementExpensiveFunctionCount() ) {
+                       $rev = Revision::newFromTitle( $title, false, 
Revision::READ_NORMAL );
+                       $pageID = $rev ? $rev->getPage() : 0;
+                       $revID = $rev ? $rev->getId() : 0;
+                       $cache[$page] = $rev; // maybe null
+
+                       // Register dependency in templatelinks
+                       $parser->getOutput()->addTemplate( $title, $pageID, 
$revID );
+
+                       return $rev;
+               }
+               $cache[$page] = null;
+               return null;
+       }
+
+       static function pageid( $parser, $title = null ) {
+               $t = Title::newFromText( $title );
+               if ( is_null( $t ) ) {
+                       return '';
+               }
+               // when the param is equal to the current title, process as 
variable
+               if ( $t->equals( $parser->getTitle() ) ) {
+                       return CoreVariables::pageid( $parser );
+               }
+               // fetch pageid from cache/database and return the value
+               $pageid = $t->getArticleID();
+               return $pageid ? $pageid : '';
+       }
+       static function revisionid( $parser, $title = null ) {
+               $t = Title::newFromText( $title );
+               if ( is_null( $t ) ) {
+                       return '';
+               }
+               // when the param is equal to the current title, process as 
variable
+               if ( $t->equals( $parser->getTitle() ) ) {
+                       return CoreVariables::revisionid( $parser );
+               }
+               // fetch revision from cache/database and return the value
+               $rev = self::getCachedRevisionObject( $parser, $t );
+               return $rev ? $rev->getId() : '';
+       }
+       static function revisionday( $parser, $title = null ) {
+               $t = Title::newFromText( $title );
+               if ( is_null( $t ) ) {
+                       return '';
+               }
+               // when the param is equal to the current title, process as 
variable
+               if ( $t->equals( $parser->getTitle() ) ) {
+                       return CoreVariables::revisionday( $parser );
+               }
+               // fetch revision from cache/database and return the value
+               $rev = self::getCachedRevisionObject( $parser, $t );
+               return $rev ? intval( substr( $rev->getTimestamp(), 6, 2 ) ) : 
'';
+       }
+       static function revisionday2( $parser, $title = null ) {
+               $t = Title::newFromText( $title );
+               if ( is_null( $t ) ) {
+                       return '';
+               }
+               // when the param is equal to the current title, process as 
variable
+               if ( $t->equals( $parser->getTitle() ) ) {
+                       return CoreVariables::revisionday2( $parser );
+               }
+               // fetch revision from cache/database and return the value
+               $rev = self::getCachedRevisionObject( $parser, $t );
+               return $rev ? substr( $rev->getTimestamp(), 6, 2 ) : '';
+       }
+       static function revisionmonth( $parser, $title = null ) {
+               $t = Title::newFromText( $title );
+               if ( is_null( $t ) ) {
+                       return '';
+               }
+               // when the param is equal to the current title, process as 
variable
+               if ( $t->equals( $parser->getTitle() ) ) {
+                       return CoreVariables::revisionmonth( $parser );
+               }
+               // fetch revision from cache/database and return the value
+               $rev = self::getCachedRevisionObject( $parser, $t );
+               return $rev ? substr( $rev->getTimestamp(), 4, 2 ) : '';
+       }
+       static function revisionmonth1( $parser, $title = null ) {
+               $t = Title::newFromText( $title );
+               if ( is_null( $t ) ) {
+                       return '';
+               }
+               // when the param is equal to the current title, process as 
variable
+               if ( $t->equals( $parser->getTitle() ) ) {
+                       return CoreVariables::revisionmonth1( $parser );
+               }
+               // fetch revision from cache/database and return the value
+               $rev = self::getCachedRevisionObject( $parser, $t );
+               return $rev ? intval( substr( $rev->getTimestamp(), 4, 2 ) ) : 
'';
+       }
+       static function revisionyear( $parser, $title = null ) {
+               $t = Title::newFromText( $title );
+               if ( is_null( $t ) ) {
+                       return '';
+               }
+               // when the param is equal to the current title, process as 
variable
+               if ( $t->equals( $parser->getTitle() ) ) {
+                       return CoreVariables::revisionyear( $parser );
+               }
+               // fetch revision from cache/database and return the value
+               $rev = self::getCachedRevisionObject( $parser, $t );
+               return $rev ? substr( $rev->getTimestamp(), 0, 4 ) : '';
+       }
+       static function revisiontimestamp( $parser, $title = null ) {
+               $t = Title::newFromText( $title );
+               if ( is_null( $t ) ) {
+                       return '';
+               }
+               // when the param is equal to the current title, process as 
variable
+               if ( $t->equals( $parser->getTitle() ) ) {
+                       return CoreVariables::revisiontimestamp( $parser );
+               }
+               // fetch revision from cache/database and return the value
+               $rev = self::getCachedRevisionObject( $parser, $t );
+               return $rev ? $rev->getTimestamp() : '';
+       }
+       static function revisionuser( $parser, $title = null ) {
+               $t = Title::newFromText( $title );
+               if ( is_null( $t ) ) {
+                       return '';
+               }
+               // when the param is equal to the current title, process as 
variable
+               if ( $t->equals( $parser->getTitle() ) ) {
+                       return CoreVariables::revisionuser( $parser );
+               }
+               // fetch revision from cache/database and return the value
+               $rev = self::getCachedRevisionObject( $parser, $t );
+               return $rev ? $rev->getUserText() : '';
+       }
 }

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ib15a4e54c65192ec3caef71fd5dcb93fb6fc444e
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/core
Gerrit-Branch: master
Gerrit-Owner: Umherirrender <[email protected]>

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

Reply via email to