DynamicPageList3/includes/Article.php

408 lines
10 KiB
PHP
Raw Normal View History

2020-11-22 20:00:48 +00:00
<?php
2021-05-30 18:33:21 +00:00
namespace MediaWiki\Extension\DynamicPageList3;
2020-11-22 20:00:48 +00:00
2021-05-30 18:33:21 +00:00
use MediaWiki\MediaWikiServices;
2021-12-15 20:22:38 +00:00
use RequestContext;
use stdClass;
2021-10-01 22:52:30 +00:00
use Title;
2020-11-22 20:00:48 +00:00
class Article {
/**
* Title
*
2021-10-01 22:52:30 +00:00
* @var Title
2020-11-22 20:00:48 +00:00
*/
2021-10-01 22:52:30 +00:00
public $mTitle;
2020-11-22 20:00:48 +00:00
/**
* Namespace ID
*
2021-10-01 22:52:30 +00:00
* @var int
2020-11-22 20:00:48 +00:00
*/
public $mNamespace = -1;
/**
* Page ID
*
2021-02-22 23:48:01 +00:00
* @var int
2020-11-22 20:00:48 +00:00
*/
public $mID = 0;
/**
* Selected title of initial page.
*
2021-02-22 23:48:01 +00:00
* @var string
2020-11-22 20:00:48 +00:00
*/
public $mSelTitle = null;
/**
* Selected namespace ID of initial page.
*
2021-10-01 22:52:30 +00:00
* @var int
2020-11-22 20:00:48 +00:00
*/
public $mSelNamespace = -1;
/**
* Selected title of image.
*
2021-02-22 23:48:01 +00:00
* @var string
2020-11-22 20:00:48 +00:00
*/
public $mImageSelTitle = null;
/**
* HTML link to page.
*
2021-02-22 23:48:01 +00:00
* @var string
2020-11-22 20:00:48 +00:00
*/
public $mLink = '';
/**
* External link on the page.
*
2021-02-22 23:48:01 +00:00
* @var string
2020-11-22 20:00:48 +00:00
*/
public $mExternalLink = null;
/**
* First character of the page title.
*
2021-02-22 23:48:01 +00:00
* @var string
2020-11-22 20:00:48 +00:00
*/
public $mStartChar = null;
/**
* Heading (link to the associated page) that page belongs to in the list (default '' means no heading)
*
2021-02-22 23:48:01 +00:00
* @var string
2020-11-22 20:00:48 +00:00
*/
2021-12-15 20:22:38 +00:00
public $mParentHLink = '';
2020-11-22 20:00:48 +00:00
/**
* Category links on the page.
*
2021-02-22 23:48:01 +00:00
* @var array
2020-11-22 20:00:48 +00:00
*/
public $mCategoryLinks = [];
/**
* Category names (without link) in the page.
*
2021-02-22 23:48:01 +00:00
* @var array
2020-11-22 20:00:48 +00:00
*/
public $mCategoryTexts = [];
/**
* Number of times this page has been viewed.
*
2021-02-22 23:48:01 +00:00
* @var int
2020-11-22 20:00:48 +00:00
*/
public $mCounter = null;
/**
* Article length in bytes of wiki text
*
2021-02-22 23:48:01 +00:00
* @var int
2020-11-22 20:00:48 +00:00
*/
public $mSize = null;
/**
* Timestamp depending on the user's request (can be first/last edit, page_touched, ...)
*
2021-10-01 22:52:30 +00:00
* @var string|int
2020-11-22 20:00:48 +00:00
*/
public $mDate = null;
/**
* Timestamp depending on the user's request, based on user format definition.
*
2021-02-22 23:48:01 +00:00
* @var string
2020-11-22 20:00:48 +00:00
*/
public $myDate = null;
/**
* Revision ID
*
2021-02-22 23:48:01 +00:00
* @var int
2020-11-22 20:00:48 +00:00
*/
public $mRevision = null;
/**
* Link to editor (first/last, depending on user's request) 's page or contributions if not registered.
*
2021-02-22 23:48:01 +00:00
* @var string
2020-11-22 20:00:48 +00:00
*/
public $mUserLink = null;
/**
* Name of editor (first/last, depending on user's request) or contributions if not registered.
*
2021-10-06 18:44:11 +00:00
* @var string|null
2020-11-22 20:00:48 +00:00
*/
public $mUser = null;
/**
* Edit Summary(Revision Comment)
*
2021-02-22 23:48:01 +00:00
* @var string
2020-11-22 20:00:48 +00:00
*/
2021-10-06 18:44:11 +00:00
public $mComment = '';
2020-11-22 20:00:48 +00:00
/**
* Number of bytes changed.
*
2021-02-22 23:48:01 +00:00
* @var int
2020-11-22 20:00:48 +00:00
*/
public $mContribution = 0;
/**
* Short string indicating the size of a contribution.
*
2021-02-22 23:48:01 +00:00
* @var string
2020-11-22 20:00:48 +00:00
*/
public $mContrib = '';
/**
* User text of who made the changes.
*
2021-02-22 23:48:01 +00:00
* @var string
2020-11-22 20:00:48 +00:00
*/
public $mContributor = null;
/**
* Article Headings - Maps heading to count (# of pages under each heading).
*
2021-02-22 23:48:01 +00:00
* @var array
2020-11-22 20:00:48 +00:00
*/
2021-02-22 23:48:01 +00:00
private static $headings = [];
2020-11-22 20:00:48 +00:00
/**
2021-10-01 22:52:30 +00:00
* @param Title $title
* @param int $namespace
2020-11-22 20:00:48 +00:00
*/
2021-10-01 22:52:30 +00:00
public function __construct( Title $title, $namespace ) {
$this->mTitle = $title;
2020-11-22 20:00:48 +00:00
$this->mNamespace = $namespace;
}
/**
* Initialize a new instance from a database row.
*
* @param stdClass $row
2021-10-01 22:52:30 +00:00
* @param Parameters $parameters
* @param Title $title
2021-10-01 22:52:30 +00:00
* @param int $pageNamespace
* @param string $pageTitle
* @return self
2020-11-22 20:00:48 +00:00
*/
public static function newFromRow(
stdClass $row,
Parameters $parameters,
Title $title,
int $pageNamespace,
string $pageTitle
): self {
2021-10-06 18:44:11 +00:00
$services = MediaWikiServices::getInstance();
$contentLanguage = $services->getContentLanguage();
$userFactory = $services->getUserFactory();
2020-11-25 15:53:02 +00:00
$article = new self( $title, $pageNamespace );
2020-11-25 15:53:02 +00:00
2021-10-06 18:44:11 +00:00
$revActorName = null;
if ( isset( $row->rev_actor ) ) {
$revActorName = $userFactory->newFromActorId( $row->rev_actor )->getName();
2021-10-06 18:44:11 +00:00
}
2020-11-22 20:00:48 +00:00
$titleText = $title->getText();
2021-02-22 23:48:01 +00:00
if ( $parameters->getParameter( 'shownamespace' ) === true ) {
2020-11-22 20:00:48 +00:00
$titleText = $title->getPrefixedText();
}
2021-10-01 22:52:30 +00:00
2021-02-22 23:48:01 +00:00
$replaceInTitle = $parameters->getParameter( 'replaceintitle' );
if ( is_array( $replaceInTitle ) && count( $replaceInTitle ) === 2 ) {
$titleText = preg_replace( $replaceInTitle[0], $replaceInTitle[1], $titleText );
2020-11-22 20:00:48 +00:00
}
2021-10-01 22:52:30 +00:00
// Chop off title if longer than the 'titlemaxlen' parameter.
2021-02-22 23:48:01 +00:00
if ( $parameters->getParameter( 'titlemaxlen' ) !== null && strlen( $titleText ) > $parameters->getParameter( 'titlemaxlen' ) ) {
$titleText = substr( $titleText, 0, $parameters->getParameter( 'titlemaxlen' ) ) . '...';
2020-11-22 20:00:48 +00:00
}
2021-10-01 22:52:30 +00:00
if ( $parameters->getParameter( 'showcurid' ) === true && isset( $row->page_id ) ) {
$articleLink = '[' . $title->getLinkURL( [ 'curid' => $row->page_id ] ) . ' ' . htmlspecialchars( $titleText ) . ']';
2020-11-22 20:00:48 +00:00
} else {
2021-02-22 23:48:01 +00:00
$articleLink = '[[' . ( $parameters->getParameter( 'escapelinks' ) && ( $pageNamespace == NS_CATEGORY || $pageNamespace == NS_FILE ) ? ':' : '' ) . $title->getFullText() . '|' . htmlspecialchars( $titleText ) . ']]';
2020-11-22 20:00:48 +00:00
}
$article->mLink = $articleLink;
$languageConverter = $services->getLanguageConverterFactory()->getLanguageConverter();
2021-10-01 22:52:30 +00:00
// get first char used for category-style output
if ( isset( $row->sortkey ) ) {
$article->mStartChar = $languageConverter->convert( $contentLanguage->firstChar( $row->sortkey ) );
2020-11-22 20:00:48 +00:00
} else {
2021-10-01 22:52:30 +00:00
$article->mStartChar = $languageConverter->convert( $contentLanguage->firstChar( $pageTitle ) );
2020-11-22 20:00:48 +00:00
}
$article->mID = intval( $row->page_id );
2020-11-22 20:00:48 +00:00
2021-10-01 22:52:30 +00:00
// External link
if ( isset( $row->el_to ) ) {
$article->mExternalLink = $row->el_to;
2020-11-22 20:00:48 +00:00
}
2021-10-01 22:52:30 +00:00
// SHOW PAGE_COUNTER
if ( isset( $row->page_counter ) ) {
$article->mCounter = intval( $row->page_counter );
2020-11-22 20:00:48 +00:00
}
2021-10-01 22:52:30 +00:00
// SHOW PAGE_SIZE
if ( isset( $row->page_len ) ) {
$article->mSize = intval( $row->page_len );
2020-11-22 20:00:48 +00:00
}
2021-10-01 22:52:30 +00:00
// STORE initially selected PAGE
2021-02-22 23:48:01 +00:00
if ( is_array( $parameters->getParameter( 'linksto' ) ) && ( count( $parameters->getParameter( 'linksto' ) ) || count( $parameters->getParameter( 'linksfrom' ) ) ) ) {
if ( !isset( $row->sel_title ) ) {
2021-10-01 22:52:30 +00:00
$article->mSelTitle = 'unknown page';
2020-11-22 20:00:48 +00:00
$article->mSelNamespace = 0;
} else {
$article->mSelTitle = $row->sel_title;
$article->mSelNamespace = $row->sel_ns;
2020-11-22 20:00:48 +00:00
}
}
2021-10-01 22:52:30 +00:00
// STORE selected image
2021-02-22 23:48:01 +00:00
if ( is_array( $parameters->getParameter( 'imageused' ) ) && count( $parameters->getParameter( 'imageused' ) ) > 0 ) {
if ( !isset( $row->image_sel_title ) ) {
2020-11-22 20:00:48 +00:00
$article->mImageSelTitle = 'unknown image';
} else {
$article->mImageSelTitle = $row->image_sel_title;
2020-11-22 20:00:48 +00:00
}
}
2021-02-22 23:48:01 +00:00
if ( $parameters->getParameter( 'goal' ) != 'categories' ) {
2021-10-01 22:52:30 +00:00
// REVISION SPECIFIED
2021-02-22 23:48:01 +00:00
if ( $parameters->getParameter( 'lastrevisionbefore' ) || $parameters->getParameter( 'allrevisionsbefore' ) || $parameters->getParameter( 'firstrevisionsince' ) || $parameters->getParameter( 'allrevisionssince' ) ) {
$article->mRevision = $row->rev_id;
2021-10-06 18:44:11 +00:00
$article->mUser = $revActorName;
$article->mDate = $row->rev_timestamp;
2021-10-06 18:44:11 +00:00
// $article->mComment = $row->rev_comment;
2020-11-22 20:00:48 +00:00
}
2021-10-01 22:52:30 +00:00
// SHOW "PAGE_TOUCHED" DATE, "FIRSTCATEGORYDATE" OR (FIRST/LAST) EDIT DATE
2021-02-22 23:48:01 +00:00
if ( $parameters->getParameter( 'addpagetoucheddate' ) ) {
$article->mDate = $row->page_touched;
2021-02-22 23:48:01 +00:00
} elseif ( $parameters->getParameter( 'addfirstcategorydate' ) ) {
$article->mDate = $row->cl_timestamp;
} elseif ( $parameters->getParameter( 'addeditdate' ) && isset( $row->rev_timestamp ) ) {
$article->mDate = $row->rev_timestamp;
} elseif ( $parameters->getParameter( 'addeditdate' ) && isset( $row->page_touched ) ) {
$article->mDate = $row->page_touched;
2020-11-22 20:00:48 +00:00
}
2021-10-01 22:52:30 +00:00
// Time zone adjustment
2021-02-22 23:48:01 +00:00
if ( $article->mDate ) {
2021-12-15 20:22:38 +00:00
$lang = RequestContext::getMain()->getLanguage();
$article->mDate = $lang->userAdjust( $article->mDate );
2020-11-22 20:00:48 +00:00
}
2021-02-22 23:48:01 +00:00
if ( $article->mDate && $parameters->getParameter( 'userdateformat' ) ) {
2021-10-01 22:52:30 +00:00
// Apply the userdateformat
$article->myDate = gmdate( $parameters->getParameter( 'userdateformat' ), (int)wfTimestamp( TS_UNIX, $article->mDate ) );
2020-11-22 20:00:48 +00:00
}
2021-10-01 22:52:30 +00:00
2020-11-22 20:00:48 +00:00
// CONTRIBUTION, CONTRIBUTOR
2021-02-22 23:48:01 +00:00
if ( $parameters->getParameter( 'addcontribution' ) ) {
$article->mContribution = $row->contribution;
2021-10-06 18:44:11 +00:00
$article->mContributor = $userFactory->newFromActorId( $row->contributor )->getName();
2021-10-06 18:44:11 +00:00
$article->mContrib = substr( '*****************', 0, (int)round( log( $row->contribution ) ) );
2020-11-22 20:00:48 +00:00
}
2021-10-01 22:52:30 +00:00
// USER/AUTHOR(S)
2020-11-22 20:00:48 +00:00
// because we are going to do a recursive parse at the end of the output phase
// we have to generate wiki syntax for linking to a user´s homepage
2021-02-22 23:48:01 +00:00
if ( $parameters->getParameter( 'adduser' ) || $parameters->getParameter( 'addauthor' ) || $parameters->getParameter( 'addlasteditor' ) ) {
2021-10-06 18:44:11 +00:00
$article->mUserLink = '[[User:' . $revActorName . '|' . $revActorName . ']]';
$article->mUser = $revActorName;
2020-11-22 20:00:48 +00:00
}
2021-10-01 22:52:30 +00:00
// CATEGORY LINKS FROM CURRENT PAGE
if ( $parameters->getParameter( 'addcategories' ) && ( $row->cats ) ) {
$artCatNames = explode( ' | ', $row->cats );
2021-02-22 23:48:01 +00:00
foreach ( $artCatNames as $artCatName ) {
$article->mCategoryLinks[] = '[[:Category:' . $artCatName . '|' . str_replace( '_', ' ', $artCatName ) . ']]';
$article->mCategoryTexts[] = str_replace( '_', ' ', $artCatName );
2020-11-22 20:00:48 +00:00
}
}
2021-10-01 22:52:30 +00:00
2020-11-22 20:00:48 +00:00
// PARENT HEADING (category of the page, editor (user) of the page, etc. Depends on ordermethod param)
2021-02-22 23:48:01 +00:00
if ( $parameters->getParameter( 'headingmode' ) != 'none' ) {
switch ( $parameters->getParameter( 'ordermethod' )[0] ) {
2020-11-22 20:00:48 +00:00
case 'category':
2021-10-01 22:52:30 +00:00
// Count one more page in this heading
self::$headings[$row->cl_to] = ( isset( self::$headings[$row->cl_to] ) ? self::$headings[$row->cl_to] + 1 : 1 );
if ( $row->cl_to == '' ) {
2021-10-01 22:52:30 +00:00
// uncategorized page (used if ordermethod=category,...)
2021-02-22 23:48:01 +00:00
$article->mParentHLink = '[[:Special:Uncategorizedpages|' . wfMessage( 'uncategorizedpages' ) . ']]';
2020-11-22 20:00:48 +00:00
} else {
$article->mParentHLink = '[[:Category:' . $row->cl_to . '|' . str_replace( '_', ' ', $row->cl_to ) . ']]';
2020-11-22 20:00:48 +00:00
}
2021-10-06 18:44:11 +00:00
2020-11-22 20:00:48 +00:00
break;
case 'user':
2021-10-06 18:44:11 +00:00
if ( $revActorName ) {
self::$headings[$revActorName] = ( isset( self::$headings[$revActorName] ) ? self::$headings[$revActorName] + 1 : 1 );
$article->mParentHLink = '[[User:' . $revActorName . '|' . $revActorName . ']]';
}
2021-10-01 22:52:30 +00:00
2020-11-22 20:00:48 +00:00
break;
}
}
}
return $article;
}
/**
* Returns all heading information processed from all newly instantiated article objects.
*
2021-10-01 22:52:30 +00:00
* @return array
2020-11-22 20:00:48 +00:00
*/
public static function getHeadings() {
return self::$headings;
}
/**
* Reset the headings to their initial state.
* Ideally this Article class should not exist and be handled by the built in MediaWiki class.
*/
public static function resetHeadings() {
self::$headings = [];
}
/**
* Get the formatted date for this article if available.
*
2021-10-01 22:52:30 +00:00
* @return mixed Formatted string or null for none set.
2020-11-22 20:00:48 +00:00
*/
public function getDate() {
2021-02-22 23:48:01 +00:00
if ( $this->myDate !== null ) {
2020-11-22 20:00:48 +00:00
return $this->myDate;
2021-02-22 23:48:01 +00:00
} elseif ( $this->mDate !== null ) {
2021-12-15 20:22:38 +00:00
$lang = RequestContext::getMain()->getLanguage();
return $lang->timeanddate( $this->mDate, true );
2020-11-22 20:00:48 +00:00
}
2021-10-01 22:52:30 +00:00
2020-11-22 20:00:48 +00:00
return null;
}
2020-11-23 04:33:40 +00:00
}