diff --git "a/class-mw\\Site.php" "b/class-mw\\Site.php" new file mode 100644 index 0000000..f9de59b --- /dev/null +++ "b/class-mw\\Site.php" @@ -0,0 +1,91 @@ +. + +# MediaWiki +namespace mw; + +class Site { + + private $api; + + /** + * @var Namespace[] + */ + private $namespaces = []; + + public function __construct( $url ) { + $this->setApi( $url ); + } + + public function getApi() { + return $this->api; + } + + public function setApi( $url ) { + $this->api = self::createApi( $url ); + return $this; + } + + public function fetch( $data ) { + return $this->getApi()->fetch( $data ); + } + + public function post( $data ) { + return $this->getApi()->post( $data ); + } + + public function hasNext() { + return $this->getApi()->hasNext(); + } + + public function fetchNext() { + return $this->getApi()->fetchNext(); + } + + public function setApiData( $data ) { + $this->getApi()->setData( $data ); + return $this; + } + + public function hasNamespace( $id ) { + return array_key_exists( $id, $this->namespaces ); + } + + public function getNamespace( $id ) { + if( ! $this->hasNamespace( $id ) ) { + throw new \Exception( sprintf( 'missing namespace %d', $id ) ); + } + return $this->namespaces[ $id ]; + } + + public function setNamespace( Ns $namespace ) { + $id = $namespace->getID(); + $this->namespaces[ $id ] = $namespace; + return $this; + } + + public function setNamespaces( $namespaces ) { + foreach( $namespaces as $namespace ) { + $this->setNamespace( $namespace ); + } + return $this; + } + + private static function createApi( $url ) { + return new APIRequest( $url ); + } +} diff --git "a/class-mw\\Wikitext.php" "b/class-mw\\Wikitext.php" new file mode 100644 index 0000000..92fbc32 --- /dev/null +++ "b/class-mw\\Wikitext.php" @@ -0,0 +1,114 @@ +. + +# MediaWiki +namespace mw; + +class Wikitext { + + /** + * MediaWiki site + */ + private $site; + + /** + * Plain text wikitext + * @var string + */ + private $wikitext; + + public function __construct( $site, $wikitext ) { + $this->setWikitext( $wikitext ); + $this->setSite( $site ); + } + + public function getSite() { + return $this->site; + } + + public function getWikitext() { + return $this->wikitext; + } + + public function setSite( $site ) { + $this->site = $site; + return $this; + } + + public function setWikitext( $wikitext ) { + $this->wikitext = $wikitext; + return $this; + } + + public function appendWikitext( $wikitext ) { + $this->wikitext = $this->wikitext . $wikitext; + return $this; + } + + public function prependWikitext( $wikitext ) { + $this->wikitext = $wikitext . $this->wikitext; + return $this; + } + + public function pregMatch( $pattern, & $matches = [], $flags = 0, $offset = 0 ) { + return preg_match( $pattern, $this->getWikitext(), $matches, $flags, $offset ); + } + + public function pregReplace( $pattern, $replacement, $limit = -1, &$count = 0 ) { + $this->setWikitext( preg_replace( $pattern, $replacement, $this->getWikitext(), $limit, $count ) ); + return $this; + } + + public function hasCategory( $category_name ) { + $category_name_regex = Title::factory( $category_name )->getRegexFirstCaseInsensitive(); + $category_name_regex = \regex\Generic::spaceBurger( $category_name_regex ); + + $category_ns_titleparts = $this->getSite()->getNamespace( 14 )->getAllTitlePartsCapitalized(); + $category_ns_regexes = []; + foreach( $category_ns_titleparts as $category_ns_titlepart ) { + $category_ns_regexes[] = $category_ns_titlepart->getRegexFirstCaseInsensitive(); + } + + $whatever_sortkey = '(\|.*?)?'; + foreach( $category_ns_regexes as $category_ns_regex ) { + $category_ns_regex = \regex\Generic::spaceBurger( $category_ns_regex ); + $pattern = sprintf( + '/\[\[%s\]\]/', + \regex\Generic::spaceBurger( $category_ns_regex . ':' . $category_name_regex . $whatever_sortkey ) + ); + if( 1 === $this->pregMatch( $pattern ) ) { + return true; + } + } + + return false; + } + + public function addCategory( $category_name, $sortkey = null ) { + if( $this->hasCategory( $category_name ) ) { + return false; + } + $category_namespace = $this->getSite()->getNamespace( 14 )->getName(); + $this->appendWikitext( sprintf( + "\n[[%s:%s%s]]", + $category_namespace, + $category_name, + $sortkey ? "|$sortkey" : '' + ) ); + return true; + } +}