+
+ /**
+ * Fetched the current revision of the given title and return this.
+ * Will increment the expensive function count and
+ * add a template link to get the value refreshed on changes.
+ * For a given title, which is equal to the current parser title,
+ * the revision object from the parser is used, when that is the current one
+ *
+ * @param Parser $parser
+ * @param Title $title
+ * @return Revision
+ * @since 1.23
+ */
+ private static function getCachedRevisionObject( $parser, $title = null ) {
+ if ( is_null( $title ) ) {
+ return null;
+ }
+
+ // Use the revision from the parser itself, when param is the current page
+ // and the revision is the current one
+ if ( $title->equals( $parser->getTitle() ) ) {
+ $parserRev = $parser->getRevisionObject();
+ if ( $parserRev && $parserRev->isCurrent() ) {
+ // force reparse after edit with vary-revision flag
+ $parser->getOutput()->setFlag( 'vary-revision' );
+ wfDebug( __METHOD__ . ": use current revision from parser, setting vary-revision...\n" );
+ return $parserRev;
+ }
+ }
+
+ // Normalize name for cache
+ $page = $title->getPrefixedDBkey();
+
+ if ( !( $parser->currentRevisionCache && $parser->currentRevisionCache->has( $page ) )
+ && !$parser->incrementExpensiveFunctionCount() ) {
+ return null;
+ }
+ $rev = $parser->fetchCurrentRevisionOfTitle( $title );
+ $pageID = $rev ? $rev->getPage() : 0;
+ $revID = $rev ? $rev->getId() : 0;
+
+ // Register dependency in templatelinks
+ $parser->getOutput()->addTemplate( $title, $pageID, $revID );
+
+ return $rev;
+ }
+
+ /**
+ * Get the pageid of a specified page
+ * @param Parser $parser
+ * @param string $title Title to get the pageid from
+ * @return int|null|string
+ * @since 1.23
+ */
+ public static function pageid( $parser, $title = null ) {
+ $t = Title::newFromText( $title );
+ if ( is_null( $t ) ) {
+ return '';
+ }
+ // Use title from parser to have correct pageid after edit
+ if ( $t->equals( $parser->getTitle() ) ) {
+ $t = $parser->getTitle();
+ return $t->getArticleID();
+ }
+
+ // These can't have ids
+ if ( !$t->canExist() || $t->isExternal() ) {
+ return 0;
+ }
+
+ // Check the link cache, maybe something already looked it up.
+ $linkCache = LinkCache::singleton();
+ $pdbk = $t->getPrefixedDBkey();
+ $id = $linkCache->getGoodLinkID( $pdbk );
+ if ( $id != 0 ) {
+ $parser->mOutput->addLink( $t, $id );
+ return $id;
+ }
+ if ( $linkCache->isBadLink( $pdbk ) ) {
+ $parser->mOutput->addLink( $t, 0 );
+ return $id;
+ }
+
+ // We need to load it from the DB, so mark expensive
+ if ( $parser->incrementExpensiveFunctionCount() ) {
+ $id = $t->getArticleID();
+ $parser->mOutput->addLink( $t, $id );
+ return $id;
+ }
+ return null;
+ }
+
+ /**
+ * Get the id from the last revision of a specified page.
+ * @param Parser $parser
+ * @param string $title Title to get the id from
+ * @return int|null|string
+ * @since 1.23
+ */
+ public static function revisionid( $parser, $title = null ) {
+ $t = Title::newFromText( $title );
+ if ( is_null( $t ) ) {
+ return '';
+ }
+ // fetch revision from cache/database and return the value
+ $rev = self::getCachedRevisionObject( $parser, $t );
+ return $rev ? $rev->getId() : '';
+ }
+
+ /**
+ * Get the day from the last revision of a specified page.
+ * @param Parser $parser
+ * @param string $title Title to get the day from
+ * @return string
+ * @since 1.23
+ */
+ public static function revisionday( $parser, $title = null ) {
+ $t = Title::newFromText( $title );
+ if ( is_null( $t ) ) {
+ return '';
+ }
+ // fetch revision from cache/database and return the value
+ $rev = self::getCachedRevisionObject( $parser, $t );
+ return $rev ? MWTimestamp::getLocalInstance( $rev->getTimestamp() )->format( 'j' ) : '';
+ }
+
+ /**
+ * Get the day with leading zeros from the last revision of a specified page.
+ * @param Parser $parser
+ * @param string $title Title to get the day from
+ * @return string
+ * @since 1.23
+ */
+ public static function revisionday2( $parser, $title = null ) {
+ $t = Title::newFromText( $title );
+ if ( is_null( $t ) ) {
+ return '';
+ }
+ // fetch revision from cache/database and return the value
+ $rev = self::getCachedRevisionObject( $parser, $t );
+ return $rev ? MWTimestamp::getLocalInstance( $rev->getTimestamp() )->format( 'd' ) : '';
+ }
+
+ /**
+ * Get the month with leading zeros from the last revision of a specified page.
+ * @param Parser $parser
+ * @param string $title Title to get the month from
+ * @return string
+ * @since 1.23
+ */
+ public static function revisionmonth( $parser, $title = null ) {
+ $t = Title::newFromText( $title );
+ if ( is_null( $t ) ) {
+ return '';
+ }
+ // fetch revision from cache/database and return the value
+ $rev = self::getCachedRevisionObject( $parser, $t );
+ return $rev ? MWTimestamp::getLocalInstance( $rev->getTimestamp() )->format( 'm' ) : '';
+ }
+
+ /**
+ * Get the month from the last revision of a specified page.
+ * @param Parser $parser
+ * @param string $title Title to get the month from
+ * @return string
+ * @since 1.23
+ */
+ public static function revisionmonth1( $parser, $title = null ) {
+ $t = Title::newFromText( $title );
+ if ( is_null( $t ) ) {
+ return '';
+ }
+ // fetch revision from cache/database and return the value
+ $rev = self::getCachedRevisionObject( $parser, $t );
+ return $rev ? MWTimestamp::getLocalInstance( $rev->getTimestamp() )->format( 'n' ) : '';
+ }
+
+ /**
+ * Get the year from the last revision of a specified page.
+ * @param Parser $parser
+ * @param string $title Title to get the year from
+ * @return string
+ * @since 1.23
+ */
+ public static function revisionyear( $parser, $title = null ) {
+ $t = Title::newFromText( $title );
+ if ( is_null( $t ) ) {
+ return '';
+ }
+ // fetch revision from cache/database and return the value
+ $rev = self::getCachedRevisionObject( $parser, $t );
+ return $rev ? MWTimestamp::getLocalInstance( $rev->getTimestamp() )->format( 'Y' ) : '';
+ }
+
+ /**
+ * Get the timestamp from the last revision of a specified page.
+ * @param Parser $parser
+ * @param string $title Title to get the timestamp from
+ * @return string
+ * @since 1.23
+ */
+ public static function revisiontimestamp( $parser, $title = null ) {
+ $t = Title::newFromText( $title );
+ if ( is_null( $t ) ) {
+ return '';
+ }
+ // fetch revision from cache/database and return the value
+ $rev = self::getCachedRevisionObject( $parser, $t );
+ return $rev ? MWTimestamp::getLocalInstance( $rev->getTimestamp() )->format( 'YmdHis' ) : '';
+ }
+
+ /**
+ * Get the user from the last revision of a specified page.
+ * @param Parser $parser
+ * @param string $title Title to get the user from
+ * @return string
+ * @since 1.23
+ */
+ public static function revisionuser( $parser, $title = null ) {
+ $t = Title::newFromText( $title );
+ if ( is_null( $t ) ) {
+ return '';
+ }
+ // fetch revision from cache/database and return the value
+ $rev = self::getCachedRevisionObject( $parser, $t );
+ return $rev ? $rev->getUserText() : '';
+ }
+
+ /**
+ * Returns the sources of any cascading protection acting on a specified page.
+ * Pages will not return their own title unless they transclude themselves.
+ * This is an expensive parser function and can't be called too many times per page,
+ * unless cascading protection sources for the page have already been loaded.
+ *
+ * @param Parser $parser
+ * @param string $title
+ *
+ * @return string
+ * @since 1.23
+ */
+ public static function cascadingsources( $parser, $title = '' ) {
+ $titleObject = Title::newFromText( $title );
+ if ( !( $titleObject instanceof Title ) ) {
+ $titleObject = $parser->mTitle;
+ }
+ if ( $titleObject->areCascadeProtectionSourcesLoaded()
+ || $parser->incrementExpensiveFunctionCount()
+ ) {
+ $names = [];
+ $sources = $titleObject->getCascadeProtectionSources();
+ foreach ( $sources[0] as $sourceTitle ) {
+ $names[] = $sourceTitle->getPrefixedText();
+ }
+ return implode( $names, '|' );
+ }
+ return '';
+ }
+