X-Git-Url: https://scripts.mit.edu/gitweb/autoinstallsdev/mediawiki.git/blobdiff_plain/19e297c21b10b1b8a3acad5e73fc71dcb35db44a..6932310fd58ebef145fa01eb76edf7150284d8ea:/includes/site/MediaWikiSite.php diff --git a/includes/site/MediaWikiSite.php b/includes/site/MediaWikiSite.php new file mode 100644 index 00000000..f31a77d3 --- /dev/null +++ b/includes/site/MediaWikiSite.php @@ -0,0 +1,213 @@ + + * @author Daniel Kinzler + * @author Jeroen De Dauw < jeroendedauw@gmail.com > + */ + +/** + * Class representing a MediaWiki site. + * + * @since 1.21 + * + * @ingroup Site + */ +class MediaWikiSite extends Site { + const PATH_FILE = 'file_path'; + const PATH_PAGE = 'page_path'; + + /** + * @since 1.21 + * + * @param string $type + */ + public function __construct( $type = self::TYPE_MEDIAWIKI ) { + parent::__construct( $type ); + } + + /** + * Returns the database form of the given title. + * + * @since 1.21 + * + * @param string $title The target page's title, in normalized form. + * + * @return string + */ + public function toDBKey( $title ) { + return str_replace( ' ', '_', $title ); + } + + /** + * Returns the normalized form of the given page title, using the + * normalization rules of the given site. If the given title is a redirect, + * the redirect weill be resolved and the redirect target is returned. + * + * @note This actually makes an API request to the remote site, so beware + * that this function is slow and depends on an external service. + * + * @note If MW_PHPUNIT_TEST is defined, the call to the external site is + * skipped, and the title is normalized using the local normalization + * rules as implemented by the Title class. + * + * @see Site::normalizePageName + * + * @since 1.21 + * + * @param string $pageName + * + * @return string + * @throws MWException + */ + public function normalizePageName( $pageName ) { + if ( defined( 'MW_PHPUNIT_TEST' ) ) { + // If the code is under test, don't call out to other sites, just + // normalize locally. + // Note: this may cause results to be inconsistent with the actual + // normalization used by the respective remote site! + + $t = Title::newFromText( $pageName ); + return $t->getPrefixedText(); + } else { + static $mediaWikiPageNameNormalizer = null; + + if ( $mediaWikiPageNameNormalizer === null ) { + $mediaWikiPageNameNormalizer = new MediaWikiPageNameNormalizer(); + } + + return $mediaWikiPageNameNormalizer->normalizePageName( + $pageName, + $this->getFileUrl( 'api.php' ) + ); + } + } + + /** + * @see Site::getLinkPathType + * Returns Site::PATH_PAGE + * + * @since 1.21 + * + * @return string + */ + public function getLinkPathType() { + return self::PATH_PAGE; + } + + /** + * Returns the relative page path. + * + * @since 1.21 + * + * @return string + */ + public function getRelativePagePath() { + return parse_url( $this->getPath( self::PATH_PAGE ), PHP_URL_PATH ); + } + + /** + * Returns the relative file path. + * + * @since 1.21 + * + * @return string + */ + public function getRelativeFilePath() { + return parse_url( $this->getPath( self::PATH_FILE ), PHP_URL_PATH ); + } + + /** + * Sets the relative page path. + * + * @since 1.21 + * + * @param string $path + */ + public function setPagePath( $path ) { + $this->setPath( self::PATH_PAGE, $path ); + } + + /** + * Sets the relative file path. + * + * @since 1.21 + * + * @param string $path + */ + public function setFilePath( $path ) { + $this->setPath( self::PATH_FILE, $path ); + } + + /** + * @see Site::getPageUrl + * + * This implementation returns a URL constructed using the path returned by getLinkPath(). + * In addition to the default behavior implemented by Site::getPageUrl(), this + * method converts the $pageName to DBKey-format by replacing spaces with underscores + * before using it in the URL. + * + * @since 1.21 + * + * @param string|bool $pageName Page name or false (default: false) + * + * @return string + */ + public function getPageUrl( $pageName = false ) { + $url = $this->getLinkPath(); + + if ( $url === false ) { + return false; + } + + if ( $pageName !== false ) { + $pageName = $this->toDBKey( trim( $pageName ) ); + $url = str_replace( '$1', wfUrlencode( $pageName ), $url ); + } + + return $url; + } + + /** + * Returns the full file path (ie site url + relative file path). + * The path should go at the $1 marker. If the $path + * argument is provided, the marker will be replaced by it's value. + * + * @since 1.21 + * + * @param string|bool $path + * + * @return string + */ + public function getFileUrl( $path = false ) { + $filePath = $this->getPath( self::PATH_FILE ); + + if ( $filePath !== false ) { + $filePath = str_replace( '$1', $path, $filePath ); + } + + return $filePath; + } +}