Development/Tutorials/Libmediawiki

    From KDE TechBase
    Revision as of 14:46, 31 May 2019 by Jucato (talk | contribs) (Mark for archiving)
    (diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)


    This page has been archived
    The information on this page is outdated or no longer in use but is kept for historical purposes. Please see the Category:Archives for similar pages.

    How to edit a wiki with Libmediawiki

    Libmediawiki allows to edit a wiki easily. To reach its goal it uses the PHP API of Mediawiki. In this paper I will present how to edit a wiki with Libmediawiki. You can find the code in the edit sample stored in the library repository.

    https://projects.kde.org/projects/extragear/libs/libmediawiki

    Before being able to use the library functions you must create a MediaWiki object which will contain the information needed for the request to interact with the Wiki.

    MediaWiki mediawiki = new MediaWiki(QUrl(this->ui->mWikiEdit->text()));
    

    Load a page

    After the MediaWiki creation you can use the library features. First, we need to load the page which will be updated:

    QueryRevision* const queryrevision(new QueryRevision(*mediawiki));
    queryrevision->setPageName(this->ui->mPageEdit->text());
    queryrevision->setProperties(QueryRevision::Content);
    queryrevision->setExpandTemplates(true);
    queryrevision->setLimit(1);
    connect(queryrevision, SIGNAL(revision(const QList&)),this, SLOT(revisionHandle(const QList&)));
    connect(queryrevision, SIGNAL(result(KJob* )),this, SLOT(revisionError(KJob*)));
    queryrevision->start();
    

    We first create a request object, QueryRevision, which takes as parameter the previous MediaWiki object. With the setter, we define request options ( page name, content type... ). We connect signals and slots to handle request feedback which are page revision list and potentially an error message (WrongRevisionId, MultiPagesNotAllowed, TitleAccessDenied...). Finally we start the job which will process the asynchronous request.

    In revisionHandle slot we can find the following code which gets the revision content requested:

    this->ui->plainTextEdit->setPlainText(revisions[0].content().toUtf8());
    

    That's all we have to do to load a page!

    Login

    The second step is needed to update a wiki page.

    Login* login = new Login(*mediawiki,this->ui->mLoginEdit->text(),this->ui->mMdpEdit->text());
    connect(login, SIGNAL(result(KJob* )),this, SLOT(loginHandle(KJob*)));
    login->start();
    

    Has you can see the login constructor take tree parameters, one MediaWiki object, the user name and the password ( the other request in Libmediawiki take MediaWiki object in constructor and should be customized later on using setter ). We connect the job result signal to catch request feedback. Finaly, we start the job which will trigger the login asynchronously.

    Update a page

    For the last example, we will update the wiki page. The Edit request can be used to create a non existing page, or update an existing page. To apply our update, the process is the same has QueryRevision request ( and all other request in Libmediawiki ), we create a request object with a MediaWiki instance as parameter, we customize our request with the setters and we start the job.

    Edit* job = new Edit(*mediawiki,NULL);
    job->setPageName(this->ui->mPageEdit->text());
    job->setText(this->ui->plainTextEdit->toPlainText());
    connect(job, SIGNAL(result(KJob *)),this, SLOT(editError(KJob*)));
    job->start();
    

    Questions, comments and feedback are welcome.